Example #1
0
        public static TicTacToeGame CreateWithRandomPlayers(object lockObject)
        {
            var           random       = new Random();
            Func <Player> createPlayer = () => new Player(MovePickerFactory.CreateRandom(random.Next));

            var player1 = createPlayer();
            var player2 = createPlayer();
            var game    = new TicTacToeGame("         ", player1, player2, () => DateTimeOffset.UtcNow);

            Task.Run(() =>
            {
                Func <bool> performUpdate = () =>
                {
                    lock (lockObject)
                    {
                        player1.TryToMakeMove(game);
                        player2.TryToMakeMove(game);
                        return(game.IsInProgress);
                    }
                };

                while (performUpdate())
                {
                    Task.Delay(100).Wait();
                }
            });
            return(game);
        }
        public void RandomPlayerMakesMovesAtRandom()
        {
            //Given several available moves for the next MovePicker
            var moves = new List <IMove> {
                new FakeMove(), new FakeMove(), new FakeMove()
            };

            //And an automated game MovePicker with a fixed random
            var fixedRandomValue = 2;
            var pickAMove        = MovePickerFactory.CreateRandom(limit => 2);

            //When asked to make a move in a game
            var selectedMove = pickAMove(moves.AsReadOnly());

            //Then a move should be have selected using the random function
            Assert.AreEqual(selectedMove, moves[fixedRandomValue]);
        }