public override void Move(Board b) { { //Create a new game state for the root of the state space GameState gameState = new GameState(GameState.State.initial, b, this, null, cell, true); //Set the state as the root MiniMaxTree m = new MiniMaxTree(gameState); //Find children states if they exist m.GenerateStates(gameState, 4, true); //The value returned by the recursive minimax function int value = m.MiniMax(gameState, 4, true); cell = null; foreach(GameState child in gameState.GetChildren()) { if (child.GetHeuristicValue() == value) { cell = child.GetCell(); } } //Set the cell on the playing board b.getCell(cell.getRow(), cell.getColumn()).setState((int)cell.getState()); //if a row exists above this cell, make it playable if (cell.getRow() != 0) { b.getCell(cell.getRow() - 1, cell.getColumn()).isPlayable(true); } //Test if the game is over if(MiniMaxTree.TerminalTest(cell)) { b.printBoard(); Console.WriteLine("GameOver: " + this.getColorToString() + " wins"); b.isGameOver(true); } moveCount++; } }
public int MiniMax(GameState gs, int depth, Boolean maxPlayer) { //base case if (depth == 0 || gs.GetState().Equals(GameState.State.terminal)) { return FindHeuristicValue(gs); } if(maxPlayer) { int bestValue = MIN_VALUE; foreach(GameState child in gs.GetChildren()) { int value = MiniMax(child, depth - 1, false); if (value.CompareTo(bestValue) > 0) { gs.SetHeuristicValue(value); bestValue = value; } } return bestValue; } else { int bestValue = MAX_VALUE; foreach (GameState child in gs.GetChildren()) { int value = MiniMax(child, depth - 1, true); if(value.CompareTo(bestValue) < 0) { gs.SetHeuristicValue(value); bestValue = value; } } return bestValue; } }
public void GenerateStates(GameState gs, int depth, Boolean maxPlayer) { if(depth == 0 || gs.GetState().Equals(GameState.State.terminal)) { return; } gs.FindChildrenStates(maxPlayer); foreach(GameState child in gs.GetChildren()) { if (maxPlayer) { GenerateStates(child, depth - 1, false); } else GenerateStates(child, depth - 1, true); } }