Ejemplo n.º 1
0
        public void ConnectFour_SimpleAI_Test()
        {
            connect4 = new ConnectFour.ConnectFour(true);
            TurnResult result;

            for (int i = 0; i < 5; i++)
            {
                result = connect4.PlayTurn(i % 2);
                Assert.IsTrue(result == TurnResult.Next);
            }

            var column = ((SimpleAI)connect4.CurrentPlayer).ChooseColumn(connect4);

            Assert.IsTrue(column == 0);
            result = connect4.PlayTurn(column);
            Assert.IsTrue(result == TurnResult.Next);

            for (int i = 0; i < 5; i++)
            {
                result = connect4.PlayTurn(i % 2 + 2);
                Assert.IsTrue(result == TurnResult.Next);
            }

            column = ((SimpleAI)connect4.CurrentPlayer).ChooseColumn(connect4);
            Assert.IsTrue(column == 2);
        }
Ejemplo n.º 2
0
        public void ConnectFour_SimpleAI_Diagonal_Test()
        {
            connect4 = new ConnectFour.ConnectFour(true);
            TurnResult result;
            var        columns = new int[] { 0, 1, 1, 2, 2, 3, 3, 0, 2, 3, 0 };

            for (int i = 0; i < columns.Length; i++)
            {
                result = connect4.PlayTurn(columns[i]);
                Assert.IsTrue(result == TurnResult.Next);
            }

            var column = ((SimpleAI)connect4.CurrentPlayer).ChooseColumn(connect4);

            Assert.IsTrue(column == 3);
            result = connect4.PlayTurn(column);
            Assert.IsTrue(result == TurnResult.Next);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.Write($"Enter 0 to play humans. Enter 1 to play the CPU. ");
            var text    = Console.ReadLine();
            int boolInt = 0;

            int.TryParse(text, out boolInt);
            var game = new ConnectFour(boolInt == 1);

            var condition = TurnResult.Invalid;

            do
            {
                condition = TurnResult.Invalid;

                var column = 0;
                var isInt  = false;
                if (game.CurrentPlayer is SimpleAI)
                {
                    column = ((SimpleAI)game.CurrentPlayer).ChooseColumn(game) + 1;
                    isInt  = true;
                    Console.WriteLine($"Player {game.CurrentPlayer.Color}, chooses column {column}");
                }
                else
                {
                    Console.Write($"Player {game.CurrentPlayer.Color}, it's your turn. Choose a column (1-7)");
                    var columnText = Console.ReadLine();
                    isInt = int.TryParse(columnText, out column);
                }

                if (isInt)
                {
                    condition = game.PlayTurn(column - 1);
                }

                if (condition == TurnResult.Invalid)
                {
                    Console.WriteLine("That is an invalid column.");
                }
                else
                {
                    Console.WriteLine(game.ViewBoard());
                }
            } while (condition != TurnResult.Tie && condition != TurnResult.Victory);

            if (condition == TurnResult.Tie)
            {
                Console.WriteLine("The game has ended in a tie!");
            }
            else
            {
                Console.WriteLine($"Player {game.CurrentPlayer.Color} is the victor!");
            }

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        public void ConnectFour_PlayTurn_Valid_Test()
        {
            var result = connect4.PlayTurn(1);

            Assert.IsTrue(result == TurnResult.Next);
        }