public void Play(Game game) { if (game.IsBoardFull) { throw new InvalidOperationException("Board is full"); } GameDecisionNode decision = new GameDecisionNode(game); decision.BuildTree(maxDepth: 3); var move = decision.Children.OrderByDescending(c => c.CalculateValue(game.CurrentPlayer)).FirstOrDefault(); if (move != null) { //var value = move.Value; //move = decision.Children.Where(v => v.Value == value).Random(); game.Play(move.Cell); } else { PickRandomMove(game); } }
public void PlayGame(int player1Index, int player2Index) { List <TournamentPlayer> players = new List <TournamentPlayer>(); TournamentPlayer player1 = this.Players[player1Index]; TournamentPlayer player2 = this.Players[player2Index]; players.Add(this.Players[player1Index]); players.Add(this.Players[player2Index]); Game game = this.referee.NewGame(players); if (player1Index == 0 || player2Index == 0) { game.ShouldPrint = true; } game.Play(); if (game.GetWinner() != null) { TournamentPlayer winner = game.GetWinner().SourcePlayer; int winnerIndex, loserIndex; TournamentPlayer loser = null; if (winner == player1) { winnerIndex = player1Index; loserIndex = player2Index; } else { if (winner == player2) { winnerIndex = player2Index; loserIndex = player1Index; } else { return; } } loser = this.Players[loserIndex]; winner.NumWins++; loser.NumLosses++; if (winnerIndex > 0) { // improve winner's rank by 1 this.Players.RemoveAt(winnerIndex); winnerIndex--; this.Players.Insert(winnerIndex, winner); if (winnerIndex == loserIndex) { return; } } // worsen losers's rank by 1 if (loserIndex < players.Count - 1) { this.Players.RemoveAt(loserIndex); loserIndex++; this.Players.Insert(loserIndex, loser); } } }
private void PickRandomMove(Game game) { var rnd = new Random(); var cell = game.Cells().Where(c => game[c] == BlockState.Empty).Random(); game.Play(cell); }
public IGame PlayInCopy(Cell cell) { Game result = new Game(this); result.Play(cell); return result; }