internal static Board getBoard() { const bool v_InvalidBoard = true; int boardWidth; int boardHeight; while (v_InvalidBoard) { //// Get board's Width and Height from user MessageDisplayer.DisplayMessage(MessageDisplayer.EnterBoardWidth); if (!int.TryParse(Console.ReadLine(), out boardWidth)) { MessageDisplayer.DisplayMessage(MessageDisplayer.NotANumber); continue; } MessageDisplayer.DisplayMessage(MessageDisplayer.EnterBoardHeight); if (!int.TryParse(Console.ReadLine(), out boardHeight)) { MessageDisplayer.DisplayMessage(MessageDisplayer.NotANumber); continue; } //// Validate Board if (validateBoard(boardWidth, boardHeight)) { break; } } return(new Board(boardWidth, boardHeight)); }
private void announceWinner(Player i_FirstPlayer, Player i_SecondPlayer) { string winnerPlayer = string.Empty; if (i_FirstPlayer.Score < i_SecondPlayer.Score) { winnerPlayer = i_SecondPlayer.Name; } else if (i_FirstPlayer.Score > i_SecondPlayer.Score) { winnerPlayer = i_FirstPlayer.Name; } if (i_FirstPlayer.Score == i_SecondPlayer.Score) { MessageDisplayer.DisplayMessage(MessageDisplayer.Draw); } else { string msg = string.Format( @"{0} {1} Final score: {2} : {3} Points {4} : {5} Points {6}", MessageDisplayer.TheWinnerIs, winnerPlayer, i_FirstPlayer.Name, i_FirstPlayer.Score, i_SecondPlayer.Name, i_SecondPlayer.Score, MessageDisplayer.CongratulationsToWinner); MessageDisplayer.DisplayMessage(msg); } }
private bool validateMove(string i_MoveInput, Board i_Board) { bool validMove = i_MoveInput.Length == 2 && char.IsUpper(i_MoveInput[0]) && char.IsDigit(i_MoveInput[1]); if (validMove) // valid syntax { int lineNum = i_MoveInput[1] - '1'; int colNum = i_MoveInput[0] - 'A'; validMove = lineNum >= 0 && lineNum < i_Board.Height && colNum >= 0 && colNum < i_Board.Width; if (!validMove) // Out of range { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidMoveOutOfRange); } else { GameCell inputCell = i_Board.BoardCells[lineNum, colNum]; validMove = !inputCell.IsRevealed; // this should be false for a valid move! if (!validMove) // Cell is Revealed { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidMoveCellRevealed); } } } else // invalid syntax { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidMoveSyntaxError); } return(validMove); }
public static void Main() { GameUI memoryGame = new GameUI(); memoryGame.RunGames(); MessageDisplayer.DisplayMessage(MessageDisplayer.GoodBye); Thread.Sleep(2500); }
private static bool validatePlayerType(string i_TypeNum) { bool validOponnent = i_TypeNum == "1" || i_TypeNum == "2"; if (!validOponnent) { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidOpponent); } return(validOponnent); }
private static bool validateYesNo(string i_Decision) { i_Decision = i_Decision.ToLower(); bool validChoice = i_Decision.Equals("yes") || i_Decision.Equals("no"); if (!validChoice) { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidPlayAnotherGame); } return(validChoice); }
private bool getPlayerHalfMove(Player i_Player, Board i_Board, ref GameCell io_SelectedCell) { MessageDisplayer.DisplayMessage(i_Player.Name + MessageDisplayer.Turn); if (i_Player.Type == ePlayerType.Human) { MessageDisplayer.DisplayMessage(MessageDisplayer.EnterMove); } io_SelectedCell = i_Player.PlayerMove(i_Board); bool wantsToPlay = io_SelectedCell != null; return(wantsToPlay); }
internal void RunGames() { MessageDisplayer.DisplayMessage(MessageDisplayer.Welcome); Player firstPlayer = MainMenu.getHumanPlayer(); Player secondPlayer = MainMenu.getSecondPlayer(); bool playAnotherGame; do { resetGame(firstPlayer, secondPlayer); Board board = MainMenu.getBoard(); playAnotherGame = runSingleGame(firstPlayer, secondPlayer, board); }while(playAnotherGame); }
private static bool validatePlayerName(string i_PlayerName) { bool validName = true; if (i_PlayerName.Length > 20) { MessageDisplayer.DisplayMessage(MessageDisplayer.NameTooLarge); validName = false; } if (i_PlayerName.Contains(" ")) { MessageDisplayer.DisplayMessage(MessageDisplayer.NameContainsSpaces); validName = false; } return(validName); }
internal static Player getHumanPlayer() { string playerName; const bool v_InvalidName = true; //// Get Player #1 name from user while (v_InvalidName) { MessageDisplayer.DisplayMessage(MessageDisplayer.EnterPlayer); playerName = Console.ReadLine(); if (validatePlayerName(playerName)) { break; } } return(new Player(playerName, ePlayerType.Human)); }
private bool stillWantToPlay() { bool anotherGame; const bool v_NotYetAnswered = true; MessageDisplayer.DisplayMessage(MessageDisplayer.PlayAnotherGame); while (v_NotYetAnswered) { // Get player's decision string yesOrNoInput = Console.ReadLine().ToLower(); if (validateYesNo(yesOrNoInput)) { anotherGame = yesOrNoInput.Equals("yes"); break; } } return(anotherGame); }
private static bool validateBoard(int i_boardWidth, int i_boardHeight) { bool validBoardWidth = i_boardWidth >= 4 && i_boardWidth <= 6; bool validBoardHeight = i_boardHeight >= 4 && i_boardHeight <= 6; bool validBoardSize = (i_boardHeight * i_boardWidth) % 2 == 0; if (!validBoardWidth) { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidWidth); } if (!validBoardHeight) { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidHeight); } if (!validBoardSize) { MessageDisplayer.DisplayMessage(MessageDisplayer.InvalidSize); } return(validBoardWidth && validBoardHeight && validBoardSize); }
internal static Player getSecondPlayer() { Player secondPlayer = null; const bool v_InvalidType = true; string playerTypeString; //// Get input type from user: 1.Human / 2.Computer while (v_InvalidType) { MessageDisplayer.DisplayMessage(MessageDisplayer.ChooseOpponentType); playerTypeString = Console.ReadLine(); if (validatePlayerType(playerTypeString)) { break; } } //// Convert input to ePlayerType int playerTypeNum; int.TryParse(playerTypeString, out playerTypeNum); ePlayerType playerType = (ePlayerType)playerTypeNum; // Create and return Player #2 switch (playerType) { case ePlayerType.Computer: secondPlayer = new Player("COMPUTER", ePlayerType.Computer); break; case ePlayerType.Human: secondPlayer = getHumanPlayer(); break; } return(secondPlayer); }