public void Simulate(int targetGames, ITicTacToeAgent xPlayer, ITicTacToeAgent oPlayer) { TargetGames = targetGames; while (GamesPlayed < TargetGames) { simulation = new Game(); CryptoRandom rand = new CryptoRandom(); var randomPlayer = rand.Next(0, 2); ITicTacToeAgent firstPlayer; ITicTacToeAgent secondPlayer; if (randomPlayer == 0) { firstPlayer = xPlayer; secondPlayer = oPlayer; } else { firstPlayer = oPlayer; secondPlayer = xPlayer; } do { firstPlayer.Play(simulation); if (!simulation.GameOver()) secondPlayer.Play(simulation); } while (!simulation.GameOver()); if (simulation.Winner == Token.X) { XWins++; } else if (simulation.Winner == Token.O) { OWins++; } xPlayer.GameOver(simulation.Winner); oPlayer.GameOver(simulation.Winner); GamesPlayed++; } }
public void Play(Game simulation) { if (!simulation.GameOver()) { CryptoRandom rand = new CryptoRandom(); var square = rand.Next(0, 9); while (!simulation.PlaceToken(PlayerToken, square)) { square = rand.Next(0, 9); } } else { throw new InvalidOperationException("No token can be placed because the game is over."); } }