public override int getMove(Flower[] board) { const int INFINITY = 10000; int result = -INFINITY; Flower.FlowerType[] types = LogicUtils.getFlowerTypes(board); // figure out if this is our first move bool firstMove = true; for (int i = 0; i < types.Length; i++) { if (types[i] != Flower.FlowerType.None) { firstMove = false; break; } } if (firstMove) { result = base.generateMove(board); } else { int move; int bestMove = -INFINITY; int alpha = -INFINITY; int beta = INFINITY; Flower.FlowerType[] cloned; Winner winner; for (int i = 0; i < types.Length; i++) { if (types[i] == Flower.FlowerType.None) // only process empty nodes { cloned = LogicUtils.cloneFlowerTypes(types); // clone our board cloned[i] = LogicUtils.COMPUTERS_TYPE; // change this piece to the computers piece if (LogicUtils.isGameOver(cloned, out winner)) // check if this move puts the game into game over state { if (winner.winningType == LogicUtils.COMPUTERS_TYPE) // check if the computer wins { result = i; break; // we found a win so no need to continue looping } } move = miniMax(cloned, LogicUtils.PLAYERS_TYPE, alpha, beta); //run the algorithm if (move > bestMove) // interpret the results { result = i; bestMove = move; } } } } return(result); }
//Alpha: a score the computer knows with certanty it can achieve //Beta: a score the human knows with certanty it can achieve //If beta becomes <= alpha, further investigation is useless private int miniMax(Flower.FlowerType[] types, Flower.FlowerType turn, int alpha, int beta) { int bestMove; int move; Winner winner; if (LogicUtils.isGameOver(types, out winner)) { if (winner.winningType == LogicUtils.COMPUTERS_TYPE) { return(1); } else if (winner.winningType == LogicUtils.PLAYERS_TYPE) { return(-1); } else { return(0); } } else { if (LogicUtils.COMPUTERS_TYPE.Equals(turn)) { bestMove = alpha; } else { bestMove = beta; } Flower.FlowerType[] cloned = null; Flower.FlowerType inversedTurn = EnumUtils.inverseValue <Flower.FlowerType>(turn); for (int i = 0; i < types.Length; i++) { if (types[i] == Flower.FlowerType.None) // get valid moves { cloned = LogicUtils.cloneFlowerTypes(types); // get our result cloned[i] = turn; move = miniMax(cloned, inversedTurn, alpha, beta); // interpret our result if (LogicUtils.COMPUTERS_TYPE == turn) { if (move > alpha) { alpha = move; } if (alpha >= beta) { return(alpha); } } else { if (move < beta) { beta = move; } if (alpha >= beta) { return(beta); } } } } } if (turn == LogicUtils.COMPUTERS_TYPE) { return(alpha); } else { return(beta); } }