コード例 #1
0
ファイル: Game.cs プロジェクト: alx2002/Tic-Tac-Toe-2
        public void DoGame(GameBoard.Player player,
                           Func <GameBoard, GameBoard.Player, GameHistory.GameMove> player1Move,
                           Func <GameBoard, GameBoard.Player, GameHistory.GameMove> player2Move)
        {
            currentBoard = GameBoard.StartGame;
            ConsoleWriteLine("Start New Game");
            currentBoard.DisplayBoard();
            while (!currentBoard.Winner(player) && !currentBoard.IsTie())
            {
                //reverse player
                player = (player == GameBoard.Player.O) ? GameBoard.Player.X : GameBoard.Player.O;

                GameHistory.GameMove move = player1Move(currentBoard, player);
                ConsoleWriteLine(String.Format("The First player places {0}.", move));
                gameHistory.MakeMove(currentBoard, move);
                currentBoard.DoMove(move);
                currentBoard.DisplayBoard();
                if (currentBoard.Winner(player))
                {
                    break;
                }

                player = (player == GameBoard.Player.O) ? GameBoard.Player.X : GameBoard.Player.O;
                //query game stats to get next move
                move = player2Move(currentBoard, player);
                ConsoleWriteLine(String.Format("The Second player places {0}.", move));
                //register move in game hidtory
                gameHistory.MakeMove(currentBoard, move);
                //update current board
                currentBoard.DoMove(move);
                currentBoard.DisplayBoard();
            }
            //by default all games start with X. Game win is relative to player X
            gameHistory.RegisterGameResults(currentBoard.Winner(GameBoard.Player.X));
        }
コード例 #2
0
ファイル: Game.cs プロジェクト: alx2002/Tic-Tac-Toe-2
        //Return Game Winner or if tie return Player.None
        public GameBoard.Player PlayGame()
        {
            //play starts with X so set player to O to force first move to X
            GameBoard.Player player = GameBoard.Player.O;

            try
            {
                DoGame(player, GetHumanPlayerMove, GetComputerPlayerMove);
            }
            catch (QuitGameException q)
            {
                ConsoleWriteLine(q.Message);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                ConsoleWriteLine("Error in game" + ex.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }

            //return winner
            if (currentBoard.IsTie())
            {
                player = GameBoard.Player.None;
            }
            return(player);
        }
コード例 #3
0
        /// <summary>
        /// CheckWin() calls methods in the gameboard class to check if there is horizontal, vertical or diagnol win
        /// </summary>
        private void CheckWin()
        {
            /// These methods will return true or false depending if there was a win or not
            /// IsDiagonalWin variable is set to true if there is a diagnol win
            IsDiagonalWin = gb.IsDiagonalWin();
            /// isVertWin variable is set to true if there is a vertical win
            IsVertWin = gb.IsVertWin();
            /// isHorzWin variable is set to true if there is a horizontal win
            IsHorzWin = gb.IsHorzWin();


            //HighlightWinningMove();
            ///Checks to see if the if the methods from the Gameboard class return true
            ///If it does return true it will display the Player that won in the tb_player_wins
            if (IsDiagonalWin)
            {
                ///Check if it is player 1 for win
                if (player1.isTurn)
                {
                    ///Highlight winning row, column, diag
                    HighlightWinningMove();
                    ///if so increment player 2 score and set the tb to player 2 wins
                    player2.newPlayerScore();
                    ///sets the score for lbl_Player_2_value.Content score
                    lbl_Player_2_value.Content = player2.score;
                    ///sets the text of the status to player 2 wins
                    tb_player_wins.Text = "Player 2 Wins!!!";
                    ///sets the bisgamestarted to false to end selection
                    bisGameStarted = false;
                }
                ///Check if it is player 2 for win
                else if (player2.isTurn)
                {
                    ///Highlight winning row, column, diag
                    HighlightWinningMove();
                    ///Because they offset if it changes on a winning move it is player 1 points
                    player1.newPlayerScore();
                    ///sets the score for lbl_Player_1_value.Content score
                    lbl_Player_1_value.Content = player1.score;
                    ///sets the text of the status to player 1 wins
                    tb_player_wins.Text = "Player 1 Wins!!!";
                    ///sets the bisgamestarted to false to end selection
                    bisGameStarted = false;
                }
            }
            ///Checks to see if it a VertWin
            else if (IsVertWin)
            {
                ///Check if it is player 1 turn
                if (player1.isTurn)
                {
                    ///Highlight winning row, column, diag
                    HighlightWinningMove();
                    ///if so increment player 2 score and set the tb to player 2 wins
                    player2.newPlayerScore();
                    ///sets the score for lbl_Player_2_value.Content score
                    lbl_Player_2_value.Content = player2.score;
                    ///sets the text of the status to player 2 wins
                    tb_player_wins.Text = "Player 2 Wins!!!";
                    ///sets the bisgamestarted to false to end selection
                    bisGameStarted = false;
                }
                else if (player2.isTurn)
                {
                    ///Highlight winning row, column, diag
                    HighlightWinningMove();
                    ///Because they offset if it changes on a winning move it is player 1 points
                    player1.newPlayerScore();
                    ///sets the score for lbl_Player_1_value.Content score
                    lbl_Player_1_value.Content = player1.score;
                    ///sets the text of the status to player 1 wins
                    tb_player_wins.Text = "Player 1 Wins!!!";
                    ///sets the bisgamestarted to false to end selection
                    bisGameStarted = false;
                }
            }
            ///Checks to see if it a VertWin
            else if (IsHorzWin)
            {
                ///Check if it is player 1 turn
                if (player1.isTurn)
                {
                    ///Highlight winning row, column, diag
                    HighlightWinningMove();
                    ///if so increment player 2 score and set the tb to player 2 wins
                    player2.newPlayerScore();
                    ///sets the score for lbl_Player_2_value.Content score
                    lbl_Player_2_value.Content = player2.score;
                    ///sets the text of the status to player 2 wins
                    tb_player_wins.Text = "Player 2 Wins!!!";
                    ///sets the bisgamestarted to false to end selection
                    bisGameStarted = false;
                }
                else if (player2.isTurn)
                {
                    ///Highlight winning row, column, diag
                    HighlightWinningMove();
                    ///Because they offset if it changes on a winning move it is player 1 points
                    player1.newPlayerScore();
                    ///sets the score for lbl_Player_1_value.Content score
                    lbl_Player_1_value.Content = player1.score;
                    ///sets the text of the status to player 1 wins
                    tb_player_wins.Text = "Player 1 Wins!!!";
                    ///sets the bisgamestarted to false to end selection
                    bisGameStarted = false;
                }
            }
            else if (gb.IsTie())
            {
                ///increment tie score and set it to the ties value, then display Tie!!! try again
                tie++;
                ///set content to lbl_ties_value to tie total
                lbl_ties_value.Content = tie.ToString();
                ///sets the text of the status to Tie!!! Try Again.
                tb_player_wins.Text = "Tie!!! Try Again.";
                ///sets the bisgamestarted to false to end selection
                bisGameStarted = false;
            }
        }