예제 #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);
            }
        }
예제 #2
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");
        }