/// <summary> /// Makes a list of all the moves player of code 'code' can make'. /// </summary> // XXX: TODO - optimize this. private static IEnumerable <Move> _findMoves(AIBoard board, int code) { int ocode = code == 1 ? 2 : 1; // Go through all possible moves of each piece of code 'code' on the board foreach (var gp in board.GetPieces(code)) { for (int i = 0; i < 16; i++) { int xto = gp.Item1 + positioncheck[i][0], yto = gp.Item2 + positioncheck[i][1]; if (xto >= Board.SIZE_X || xto < 0 || yto >= Board.SIZE_Y || yto < 0) { continue; } if (board[xto, yto] != 0) { continue; } // Count conversions int gain = board.Gain(xto, yto, ocode); if (i < 8) { gain += 1; } yield return(new Move(gp.Item1, gp.Item2, xto, yto, i >= 8, gain)); //yield return new Move(gp.x, gp.y, xto, yto, i >= 8, 0); // Conversion count is never used, so 0 is used as a placeholder } } }
/// <summary> /// Heuristic evaluation of a board state for 'goodness'. /// </summary> private static int _score(AIBoard b, int code, int othercode) { // If b is in a game over state, return a score that represents a win or loss if (b.GameOver) { if (b.Count(code) > b.Count(othercode)) { return(int.MaxValue - 10); } else { return(-(int.MaxValue - 10)); } } // There are 3 major components to a good board state: // - A small perimeter (not many spaces next to friendly pieces, which indicates a good structure) // - The number of pieces within capturing range of an enemy // - The friendly piece count int perimeter = 0, pieces = b.Count(code), vulnpieces = 0; // First find all friendly pieces foreach (var gp in b.GetPieces(code)) { // For each space next to this piece empty, add to the perimeter sum. perimeter += b.EmptyAdjacent(gp.Item1, gp.Item2); // If piece can be captured, add to vulnerable pieces. if (b.IsVulnerable(gp.Item1, gp.Item2, othercode)) { vulnpieces++; } } // Bring together the scores and apply math to generate the best representative score of the board return(25 * pieces - 8 * vulnpieces - 2 * perimeter); // Mess around with coefficients until ai works }