Ejemplo n.º 1
0
        static void RunGame(Game3x3 game)
        {
            Console.WriteLine();

            game.DrawBoardToOutput();
            Console.WriteLine("{0} goes first!", game.CurrentPlayer.PlayerName);

            while (!game.HasFinished())
            {
                System.Threading.Thread.Sleep(1000);
                game.MakeNextMove();
                game.DrawBoardToOutput();
                Console.WriteLine();

                if (game.WinDetected())
                    Console.WriteLine("{0}{1} has won the game!", Environment.NewLine, game.WinningPlayer.PlayerName);
                else if (game.HasFinished())
                    Console.WriteLine("Game finished. No winner!");
                else
                    Console.WriteLine("{0} plays next", game.CurrentPlayer.PlayerName);
            }

            Console.WriteLine("{0}Play again? y/n", Environment.NewLine);
            char key = Console.ReadKey().KeyChar;
            if (key == 'y' || key == 'Y')
            {
                game.ResetGame();
                RunGame(game);
            }
        }
Ejemplo n.º 2
0
        public void TestNextMoveGameFinished()
        {
            GenericGameState state = new GenericGameState();
            ComputerPlayer player1 = new ComputerPlayer("Jim", SpaceState.X);
            ComputerPlayer player2 = new ComputerPlayer("Denise", SpaceState.X);

            Game3x3 game = new Game3x3(state, null, player1, player2);
            finishGame(game);

            Assert.IsNull(player1.NextMove(game), "Should return null as there are no spaces");
        }
Ejemplo n.º 3
0
        public void TestGetNextMove()
        {
            GenericGameState state = new GenericGameState();
            ComputerPlayer player1 = new ComputerPlayer("Jim", SpaceState.X);
            ComputerPlayer player2 = new ComputerPlayer("Denise", SpaceState.X);

            Game3x3 game = new Game3x3(state, null, player1, player2);
            Space s = player1.NextMove(game);

            Assert.IsNotNull(s, "Should have returned an actual space");
        }
Ejemplo n.º 4
0
        public void TestDisplayBoard()
        {
            string expected = string.Format("---{0}---{0}---{0}", Environment.NewLine);

            GenericGameState state = new GenericGameState();
            ConsoleGameDrawer drawer = new ConsoleGameDrawer();

            Game3x3 game = new Game3x3(state, drawer, null, null);

            Assert.AreEqual(expected, game.GameDrawer.DrawBoardToString(game), "Expected board is different to actual");
        }
Ejemplo n.º 5
0
        public void TestGameHasStarted()
        {
            GenericGameState state = new GenericGameState();
            ConsoleGameDrawer drawer = new ConsoleGameDrawer();

            Game3x3 game = new Game3x3(state, drawer, null, null);
            Space s = game.SpaceAt(2, 0);

            Assert.IsFalse(game.HasStarted());
            s.State = SpaceState.O;
            Assert.IsTrue(game.HasStarted());
        }
Ejemplo n.º 6
0
        public void TestGameHasFinished()
        {
            GenericGameState state = new GenericGameState();
            ConsoleGameDrawer drawer = new ConsoleGameDrawer();

            Game3x3 game = new Game3x3(state, drawer, null, null);

            Assert.IsFalse(game.HasFinished());
            foreach(Space s in game.Spaces) {
                s.State = SpaceState.X;
            }

            Assert.IsTrue(game.HasFinished());
        }
Ejemplo n.º 7
0
        public void TestBoardUpdates()
        {
            string expected = string.Format("--X{0}-O-{0}X--{0}", Environment.NewLine);

            GenericGameState state = new GenericGameState();
            ConsoleGameDrawer drawer = new ConsoleGameDrawer();

            Game3x3 game = new Game3x3(state, drawer, null, null);

            Assert.AreNotEqual(expected, game.GameDrawer.DrawBoardToString(game), "Expected board is the same as actual, shouldn't be");
            game.SpaceAt(2, 0).State = SpaceState.X;
            game.SpaceAt(1, 1).State = SpaceState.O;
            game.SpaceAt(0, 2).State = SpaceState.X;
            Assert.AreEqual(expected, game.GameDrawer.DrawBoardToString(game), "Expected board is different to actual");
        }
Ejemplo n.º 8
0
        public void TestWinDetection()
        {
            string expected = string.Format("--X{0}-OX{0}O-X{0}", Environment.NewLine);

            GenericGameState state = new GenericGameState();
            ConsoleGameDrawer drawer = new ConsoleGameDrawer();

            Game3x3 game = new Game3x3(state, drawer, null, null);

            Assert.AreNotEqual(expected, game.GameDrawer.DrawBoardToString(game), "Expected board is the same as actual, shouldn't be");
            game.SpaceAt(2, 0).State = SpaceState.X;
            game.SpaceAt(1, 1).State = SpaceState.O;
            game.SpaceAt(0, 2).State = SpaceState.O;
            game.SpaceAt(2, 1).State = SpaceState.X;
            Assert.IsFalse(game.WinDetected(), "Should have not detected a winning row");
            game.SpaceAt(2, 2).State = SpaceState.X;
            Assert.IsTrue(game.WinDetected(), "Should have detected the winning row");
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Super-Awesome TicTacToe!!");
            Console.WriteLine();
            Console.Write("Any key to start");
            Console.ReadKey();

            // Create these objects and inject them into the game object, such
            // that the game has no idea about where it's drawing to, or what kind of players
            // are playing (Human / Computer etc)
            GenericGameState gameState = new GenericGameState();
            ConsoleGameDrawer drawer = new ConsoleGameDrawer();
            ComputerPlayer player1 = new ComputerPlayer("Jim", SpaceState.X);
            ComputerPlayer player2 = new ComputerPlayer("Susan", SpaceState.O);

            Game3x3 game = new Game3x3(gameState, drawer, player1, player2);

            RunGame(game);
        }