Esempio n. 1
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();
        }
Esempio n. 2
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;
                }
            }
        }
Esempio n. 3
0
        private void genericButton_Click(object sender)
        {
            String buttonsender = ((Button)sender).Text;

            if (buttonsender == "")
            {
                if (board.NextTurn == State.X || richTextBox1.Text.Equals(RichTextHeader + "Player 1 Starts"))
                {
                    ((Button)sender).Text = "X";
                    richTextBox1.Text     = RichTextHeader + "Player 2, its your turn !";
                    board.SwitchNextTurn();
                }
                else
                {
                    ((Button)sender).Text = "O";
                    richTextBox1.Text     = RichTextHeader + "Player 1, its your turn !";
                    board.SwitchNextTurn();
                }
            }
            State currentState = winChecker.Check(board);

            if (currentState == State.X)
            {
                richTextBox1.Text = RichTextHeader + "Player 1 Won ! Congratulations";
                enableButtons(false);
            }
            else if (currentState == State.O)
            {
                richTextBox1.Text = RichTextHeader + "Player 2 Won ! Congratulations";
                enableButtons(false);
            }
            else if (winChecker.IsDraw(board))
            {
                richTextBox1.Text = RichTextHeader + "Its a draw, press Restart for another game !";
                enableButtons(false);
            }
        }