コード例 #1
0
        /// <summary>
        /// Application plays 'Noughts and Crosses' aka Tic Tac Toe game.
        ///
        /// To create this application I have used 3 main classes:
        ///
        /// Board - which is board representation, it implements IBoard which give us possibility to create other boards
        /// with different rules and sizes and apply to application without making any changes in current code,
        ///
        /// BotRandomMovesForDefaultBoard - which is class representing default bot with random moves. This bot is created
        /// just for default 3x3 board. It implements IBot interface which give us possibility to create other bots
        /// with different behaviour for different boards witout making changes in current code,
        ///
        /// GameController - which is flow controller. This controller contains all objects and manage them to make progress
        /// in game.
        ///
        /// Thank you
        /// Jakub Wojciechowski
        ///
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Board board = new Board();
            BotRandomMovesForDefaultBoard bot1 = new BotRandomMovesForDefaultBoard(board, 'X');
            BotRandomMovesForDefaultBoard bot2 = new BotRandomMovesForDefaultBoard(board, '0');
            GameController gameController      = new GameController(board, bot1, bot2);
            bool           gameOver            = false;
            int            stepsCounter        = 1;

            do
            {
                Console.WriteLine("Step {0}", stepsCounter++);

                gameController.LetToMakeNextMove();

                Console.Write(gameController.PrintCurrentBoard());
                Console.WriteLine();

                if (gameController.IsWin(gameController.LastMoveBot.Signature))
                {
                    gameOver = true;
                    Console.WriteLine(String.Format("WE HAVE A WINNER: {0} Won", gameController.LastMoveBot.Signature));
                }
                if (gameController.HowManyMovesLeft() == 0 && !gameController.IsWin(gameController.LastMoveBot.Signature))
                {
                    gameOver = true;
                    Console.WriteLine("IT IS A DRAW !");
                }

                Thread.Sleep(1000);
            } while (!gameOver);

            Console.ReadKey();
        }
コード例 #2
0
        public void When_GameControllerIsCreated_Then_BoardShouldHas9MovesLeft()
        {
            Board board = new Board();
            BotRandomMovesForDefaultBoard botRandom = new BotRandomMovesForDefaultBoard(board, 'X');
            GameController gameController           = new GameController(board, botRandom, botRandom);

            Assert.AreEqual(9, gameController.HowManyMovesLeft());
        }
コード例 #3
0
        public void When_BotMakesFirstMove_Then_BoardShouldHas8MovesLeft()
        {
            Board board = new Board();
            BotRandomMovesForDefaultBoard botRandom = new BotRandomMovesForDefaultBoard(board, 'X');
            GameController gameController           = new GameController(board, botRandom, botRandom);

            botRandom.MakeMove();

            Assert.AreEqual(8, gameController.HowManyMovesLeft());
        }
コード例 #4
0
        public void When_GameControllerLetstoMake1Move_Then_BoardShouldHas8MovesLeft()
        {
            Board board = new Board();
            BotRandomMovesForDefaultBoard botRandom1 = new BotRandomMovesForDefaultBoard(board, 'X');
            GameController gameController            = new GameController(board, botRandom1, botRandom1);

            gameController.LetToMakeNextMove();

            Assert.AreEqual(8, gameController.HowManyMovesLeft());
        }
コード例 #5
0
        public void When_TwoBotsMake9Moves_Then_BoardShouldHas0MovesLeft()
        {
            Board board = new Board();
            BotRandomMovesForDefaultBoard botRandom1 = new BotRandomMovesForDefaultBoard(board, 'X');
            BotRandomMovesForDefaultBoard botRandom2 = new BotRandomMovesForDefaultBoard(board, '0');
            GameController gameController            = new GameController(board, botRandom1, botRandom2);

            botRandom1.MakeMove();
            botRandom2.MakeMove();

            Assert.AreEqual(7, gameController.HowManyMovesLeft());
        }
コード例 #6
0
        public void When_GameControllersLetsToMake4Moves_Then_BoardShouldHas4SignaturesAppropriateForBots()
        {
            Board board = new Board();
            BotRandomMovesForDefaultBoard botRandom1 = new BotRandomMovesForDefaultBoard(board, 'X');
            BotRandomMovesForDefaultBoard botRandom2 = new BotRandomMovesForDefaultBoard(board, '0');
            GameController gameController            = new GameController(board, botRandom1, botRandom2);

            gameController.LetToMakeNextMove();
            gameController.LetToMakeNextMove();
            gameController.LetToMakeNextMove();
            gameController.LetToMakeNextMove();

            Assert.AreEqual(5, gameController.HowManyMovesLeft());
            Assert.AreEqual(2, gameController.PrintCurrentBoard().Count(x => x == 'X'));
            Assert.AreEqual(2, gameController.PrintCurrentBoard().Count(x => x == '0'));
        }