/// <summary> /// Play <paramref name="game"/> randomly to the end /// Return positive if the player to make the first move won /// Return negative if the player to make the first move lost /// Return 0 if the game ends in a tie /// </summary> /// <param name="game"></param> /// <returns></returns> int Simulate(GlobalGame game) { Player activePlayer = game.ActivePlayer(); GlobalGame copy = CopyGlobalGame(game); while (!copy.GameOver()) { List <Spot> moves = copy.AvailableSpots; bool moveFound = false; // If there's a game-winning move, play it // This shortens simulations and makes them more realistic foreach (Spot spot in moves) { copy.Play(spot, false, true); if (copy.Winner != null) { moveFound = true; break; } copy.UndoLastMove(true); } // Otherwise play a random move if (!moveFound) { Spot randomSpot = moves[UnityEngine.Random.Range(0, moves.Count)]; copy.Play(randomSpot, false, true); } } if (copy.Winner == activePlayer) { return(1); } if (copy.Winner == null) { return(0); } return(-1); }
void Update() { if (game.ActivePlayer() is AI && !game.GameOver()) { if (game.HasNextMove) { if (confirmTimer <= 0) { Game.Confirm(); confirmTimer = confirmTime; return; } else { confirmTimer -= Time.deltaTime; return; } } else { if (previewTimer <= 0) { Game.Preview(((AI)game.ActivePlayer()).BestMove()); previewTimer = previewTime; return; } else { previewTimer -= Time.deltaTime; return; } } } else { confirmTimer = confirmTime; previewTimer = previewTime; } }
public void HandleGameStateChanged(object o, GameEventArgs e) { Text text = GetComponent <Text>(); if (game == null) { text.text = ""; return; } if (game.GameOver()) { if (game.Winner != null) { text.text = game.Winner.Name + " wins!"; return; } text.text = "Tie game"; return; } text.text = game.ActivePlayer().Name + "'s turn"; }