Esempio n. 1
0
        static void Main(string[] args)
        {
            while (true)
            {
                Grid grid = new Grid(6, 7);
                GameService gameService = new GameService();

                grid.CounterAdded += gameService.OnCounterAdded;

                while (gameService.GameStatus == GameStatus.NoWinner)
                {
                    if (!grid.IsComputersTurn)
                    {
                        WriteGridToConsole(grid);

                        string column = Console.ReadLine();

                        if (!grid.IsValidUserInput(column))
                        {
                            Console.WriteLine("Please enter a valid column number.");

                            continue;
                        }

                        if (!grid.IsColumnFull(Convert.ToInt32(column)))
                        {
                            grid.AddCounter(new Counter
                            {
                                Column = Convert.ToInt32(column),
                                PlayerType = PlayerType.Human
                            });

                            grid.IsComputersTurn = true;
                        }
                        else
                        {
                            Console.WriteLine("That column is full please select another.");
                        }
                    }
                    else
                    {
                        grid.TakeComputersTurn();

                        grid.IsComputersTurn = false;
                    }
                }

                WriteGridToConsole(grid, true);

                WriteGameResultToConsole(gameService);
            }
        }
        public void Human_Can_Not_Win_Game_Horizontally_With_Three_Connecting_Counters()
        {
            //Arrange
            Grid grid = new Grid(6, 7);
            GameService gameService = new GameService();

            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 2 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 3 && c.Row == 1).PlayerType = PlayerType.Human;

            //Act
            gameService.Horizontal(grid);

            //Assert
            Assert.AreEqual(GameStatus.NoWinner, gameService.GameStatus);
        }
        public void Human_Can_Not_Win_Game_Horizontally_If_There_Is_Gap_Inbetween_Four_Counters()
        {
            //Arrange
            Grid grid = new Grid(6, 7);
            GameService gameService = new GameService();

            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 2 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 4 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 5 && c.Row == 1).PlayerType = PlayerType.Human;

            //Act
            gameService.Horizontal(grid);

            //Assert
            Assert.AreEqual(GameStatus.NoWinner, gameService.GameStatus);
        }
        public void Human_Can_Win_Game_Horizontally_In_The_Middle_Of_The_Grid()
        {
            //Arrange
            Grid grid = new Grid(6, 7);
            GameService gameService = new GameService();

            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 2 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 3 && c.Row == 1).PlayerType = PlayerType.Computer;
            grid.Counters.SingleOrDefault(c => c.Column == 4 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 5 && c.Row == 1).PlayerType = PlayerType.Human;

            //Winning moves
            grid.Counters.SingleOrDefault(c => c.Column == 2 && c.Row == 2).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 3 && c.Row == 2).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 4 && c.Row == 2).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 5 && c.Row == 2).PlayerType = PlayerType.Human;

            //Act
            gameService.Horizontal(grid);

            //Assert
            Assert.AreEqual(GameStatus.HumanWon, gameService.GameStatus);
        }
        public void Human_Can_Win_Game_Vertically()
        {
            //Arrange
            Grid grid = new Grid(6, 7);
            GameService gameService = new GameService();

            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 2).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 3).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 4).PlayerType = PlayerType.Human;

            //Act
            gameService.Vertical(grid);

            //Assert
            Assert.AreEqual(GameStatus.HumanWon, gameService.GameStatus);
        }
        public void Human_Can_Not_Win_Game_Diagonally_With_Computer_Counter_Inbetween_Four_Connecting_Counters()
        {
            //Arrange
            Grid grid = new Grid(6, 7);
            GameService gameService = new GameService();

            //Act
            grid.Counters.SingleOrDefault(c => c.Column == 1 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 2 && c.Row == 2).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 3 && c.Row == 3).PlayerType = PlayerType.Computer;
            grid.Counters.SingleOrDefault(c => c.Column == 4 && c.Row == 4).PlayerType = PlayerType.Human;

            gameService.Diagonal(grid);

            //Assert
            Assert.AreEqual(GameStatus.NoWinner, gameService.GameStatus);
        }
        public void Human_Can_Win_Game_Diagonally_To_The_Left()
        {
            //Arrange
            Grid grid = new Grid(6, 7);
            GameService gameService = new GameService();

            //Act
            grid.Counters.SingleOrDefault(c => c.Column == 6 && c.Row == 1).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 5 && c.Row == 2).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 4 && c.Row == 3).PlayerType = PlayerType.Human;
            grid.Counters.SingleOrDefault(c => c.Column == 3 && c.Row == 4).PlayerType = PlayerType.Human;

            gameService.Diagonal(grid);

            //Assert
            Assert.AreEqual(GameStatus.HumanWon, gameService.GameStatus);
        }
Esempio n. 8
0
        private static void WriteGameResultToConsole(GameService gameService)
        {
            switch (gameService.GameStatus)
            {
                case GameStatus.HumanWon:
                    Console.WriteLine("Congratulations, you won! Press any key to start another game.");
                    break;
                case GameStatus.ComputerWon:
                    Console.WriteLine("Oh no the computer won. Press any key to start another game.");
                    break;
                case GameStatus.NoWinnerGridFull:
                    Console.WriteLine("The grid is full and no one won. Press any key to start another game.");
                    break;
            }

            Console.ReadKey();
        }