/* Constructor */ public Node(Player p, State s, float alpha_in, float beta_in, bool root_in) { player = p; state = s; alpha = alpha_in; beta = beta_in; root = root_in; }
public static float evade(State state) { if (state.ourMove) { if (state.isTerminal()) { return float.MinValue; } else { return state.AIPlayer.possibleMoves.Count; } } else { if (state.isTerminal()) { return float.MaxValue; } else { return state.AIPlayer.possibleMoves.Count; } } }
public static float attack(State state) { if (state.opponent.possibleMoves.Count == 0) { return float.MaxValue; } return 1.0f / state.opponent.possibleMoves.Count; }
public static float ratio(State state) { if (state.opponent.possibleMoves.Count == 0) { return float.MaxValue; } return (float)state.AIPlayer.possibleMoves.Count / (float)state.opponent.possibleMoves.Count; }
public static float mixedOverTime(State state) { return 7; }
public static float mixed(State state) { if (state.opponent.possibleMoves.Count == 0) { return float.MaxValue; } return (float)(state.AIPlayer.possibleMoves.Count - state.opponent.possibleMoves.Count) / (float)(state.AIPlayer.possibleMoves.Count + state.opponent.possibleMoves.Count); }
/* * Iterative deepening of AlphaBeta */ public static Move maxNextMoveID(State currState, float timeout) { /* reset timerOn variable */ timerOn = true; /* # height changes by 2 on every iteration */ int height = 2; /* Timer that expires when the specified number of seconds is passed */ System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += new ElapsedEventHandler(TimerExpiredEvent); timer.Interval = timeout * 1000; // timeout = # seconds timer.Enabled = true; // turn timer on! /* Create initial node */ Game.Node max_init = new Game.Node(currState.AIPlayer, currState, float.MinValue, float.MaxValue, true); /* reset best move */ best_move_value = new Tuple<Game.Node, float>(null, float.MinValue); /* Loop executes until timer expires */ while (timerOn && height_reached) //while (timerOn) { //Console.WriteLine(height); /* keep track of AB reaching the allowed height */ height_reached = false; /* calculate next best move given new depth */ alphabetaID(max_init, height); /* increase plys level - plys = height/2 */ height = height * 2; } /* turn timer off */ timer.Enabled = false; /* reset height */ height_reached = true; /* this line will break of there was not enough time to find a state, * which should not happen given the speed of AI */ return best_move_value.Item1.state.generatorMove; }
/* * ---------------------------------------------------------------------------- * ALPHA BETA * ---------------------------------------------------------------------------- */ /* * Entry point to alphabeta */ public static Move maxNextMoveAB(State currState, int height) { /* Create initial node */ Game.Node max_init = new Game.Node(currState.AIPlayer, currState, int.MinValue, int.MaxValue, true); // find next move alphabeta(max_init, height); return best_move.state.generatorMove; }
public static void update() { counter++; if (counter <= maxCount) { return; } else { counter = 0; } if (currentPlayer.type == PlayerType.AI) { //Make the current Game State currentState = new State(currentPlayer, Core.getNotCurrentPlayer(), drawCube.cube, Core.getNotCurrentPlayer().currentPosition, true); //Pass it on to the Computer currentPlayer.makeMove(currentState); //Console.ReadLine(); switchPlayers(); } }
private Move pickRandomMove(State currentState) { if (possibleMoves.Count == 0) { MainMethod.die(" Error : Player.pickRandomMove : the number of possible moves was zero. The game should have ended before this."); return null; } return possibleMoves[Core.numberGenerator.Next(possibleMoves.Count)]; }
private Move alphaBeta(State currentState) { /*if (currentPosition == null) { return pickRandomMove(currentState); } else*/ //{ switch(playerNumber) { case 1: return AlphaBeta.maxNextMoveID(currentState, Config.convertSettingToFloat("ai", "player_one_time")); //return AlphaBeta.maxNextMoveAB(currentState, Config.convertSettingToInt("ai", "player_one_depth")); break; case 2: return AlphaBeta.maxNextMoveID(currentState, Config.convertSettingToFloat("ai", "player_two_time")); //return AlphaBeta.maxNextMoveAB(currentState, Config.convertSettingToInt("ai", "player_two_depth")); break; default: MainMethod.die("Player.alphaBeta : player number is not 1 or 2. It is: " + playerNumber); break; } //} return null; }
public void makeMove(State currentState) { if (type != PlayerType.AI) { MainMethod.die("Player.makeMove : makeMove called on a human player."); } Move move = aiFunc(currentState); if (move == null) { return; } //Log.writeSpecial("Move sent back by AI is: " + move.ToString()); Core.drawCube.cube[move.row, move.col, move.distance] = (byte)playerNumber; Move.addShadows(Core.drawCube.cube, currentPosition, move); currentPosition = move; }