Exemplo n.º 1
0
        private int RunGame()
        {
            int winner = 0;

            while (!isWinner)
            {
                if (currentTurn == 9)
                {
                    break;
                }

                UpdateBoard();
                ChangeTurns();
                DrawGame();

                isWinner = WinChecker.CheckForWinner(board);
            }

            if (isWinner)
            {
                ChangeTurns();
                winner = CurrentTurnPlayer;
            }


            return(winner);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Board      board      = new Board();
            WinChecker winChecker = new WinChecker();
            Renderer   renderer   = new Renderer();
            Player     player1    = new Player();
            Player     player2    = new Player();

            while (!winChecker.IsDraw(board) && winChecker.Check(board) == State.Undecided)
            {
                renderer.Render(board);

                Position nextMove;
                if (board.NextTurn == State.X)
                {
                    nextMove = player1.GetPosition(board);
                }
                else
                {
                    nextMove = player2.GetPosition(board);
                }

                if (!board.SetState(nextMove, board.NextTurn))
                {
                    Console.WriteLine("That is not a legal move.");
                }
            }

            renderer.Render(board);
            renderer.RenderResults(winChecker.Check(board));

            Console.ReadKey();
        }
Exemplo n.º 3
0
 public Game()
 {
     _board      = new Board();
     _winChecker = new WinChecker(_board);
     _renderer   = new Renderer(_board);
     _x          = new Player();
     _o          = new Player();
 }
Exemplo n.º 4
0
 public TicTacToeGame(BasePlayer player1, BasePlayer player2)
 {
     _player1      = player1;
     _player2      = player2;
     _currentBoard = Enumerable.Repeat(PlayerGlyph.Empty, 9).ToArray();
     _player1Turn  = true;
     _winChecker   = new WinChecker();
 }
Exemplo n.º 5
0
 static void Main(string[] args)
 {
     Console.WindowWidth  = 75;
     Console.WindowHeight = 35;
     playingBoard         = new Board();
     while (WinChecker.Win == false)
     {
         Turn();
         renderGame = new Renderer();
         winCheck   = new WinChecker();
     }
 }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var    field   = new Field();
            Player player1 = new HumanPlayer('X');
            Player player2 = new CPUPlayer('O');

            Console.WriteLine(field);

            var players = new[] { player1, player2 };
            var player  = 0;

            while (true)
            {
                var currentPlayer = players[player];
                // "1 2" --> row = 1 a column = 2
                int row, column;
                currentPlayer.GetPlayerInput(out row, out column);

                while (row < 0 || row > 2 ||
                       column < 0 || column > 2 ||
                       field.GetField(row, column) != ' ')
                {
                    currentPlayer.GetPlayerInput(out row, out column);
                }

                field.SetField(row, column, currentPlayer.C);

                Console.WriteLine(field);

                player = player == 0 ? 1 : 0;

                var winner = WinChecker.Check(field.ToCharTable());
                if (winner != -1)
                {
                    Console.WriteLine();
                    Console.WriteLine($"The winner is: {(winner == 0 ? player1.C : player2.C)}");
                    Console.WriteLine("Congratulations!!!");
                    Console.WriteLine("Do you wish to play again? (y - yes, n - no)");
                    var input = Console.ReadLine();
                    if (input != "y")
                    {
                        break;
                    }

                    field.Reset();
                    Console.WriteLine(field);
                    player = 0;
                }
            }
        }