public int fitness(PieceSide max) { int fitness = 0; int[] blackPieces = { 0, 0, 0, 0, 0, 0 }; int[] whitePieces = { 0, 0, 0, 0, 0, 0 }; int blackMoves = 0; int whiteMoves = 0; // sum up the number of moves and pieces foreach (Position pos in Pieces[PieceSide.Black]) { blackMoves += LegalMoveSet.getLegalMove(this, pos).Count; blackPieces[(int)Grid[pos.number][pos.letter].piece]++; } // sum up the number of moves and pieces foreach (Position pos in Pieces[PieceSide.White]) { whiteMoves += LegalMoveSet.getLegalMove(this, pos).Count; whitePieces[(int)Grid[pos.number][pos.letter].piece]++; } // if viewing from black side if (max == PieceSide.Black) { // apply weighting to piece counts for (int i = 0; i < 6; i++) { fitness += pieceWeights[i] * (blackPieces[i] - whitePieces[i]); } // apply move value fitness += (int)(0.5 * (blackMoves - whiteMoves)); } else { // apply weighting to piece counts for (int i = 0; i < 6; i++) { fitness += pieceWeights[i] * (whitePieces[i] - blackPieces[i]); } // apply move value fitness += (int)(0.5 * (whiteMoves - blackMoves)); } return(fitness); }
/// <summary> /// Get all legal moves for the player on the current board. /// </summary> /// <param name="b">The state of the game.</param> /// <param name="player">The player whose moves you want.</param> /// <returns>A 1-to-many dictionary of moves from one position to many</returns> public static Dictionary <Position, List <Position> > getPlayerMoves(BoardHelper b, PieceSide player) { Dictionary <Position, List <Position> > moves = new Dictionary <Position, List <Position> >(); foreach (Position pos in b.Pieces[player]) { if (b.Grid[pos.number][pos.letter].piece != PieceType.None) { if (!moves.ContainsKey(pos)) { moves[pos] = new List <Position>(); } moves[pos].AddRange(LegalMoveSet.getLegalMove(b, pos)); } } return(moves); }
private int mimaab(BoardHelper board, PieceSide turn, int depth, int alpha, int beta) { // base case, at maximum depth return board fitness if (depth >= DEPTH) { return(board.fitness(MAX)); } else { List <BoardHelper> boards = new List <BoardHelper>(); // get available moves / board states from moves for the current player foreach (Position pos in board.Pieces[turn]) { if (STOP) { return(-1); // interupts } List <Position> moves = LegalMoveSet.getLegalMove(board, pos); foreach (Position move in moves) { if (STOP) { return(-1); // interupts } BoardHelper b2 = LegalMoveSet.move(board, new Move(pos, move)); boards.Add(b2); } } int a = alpha, b = beta; if (turn != MAX) // minimize { foreach (BoardHelper b2 in boards) { if (STOP) { return(-1); // interupt } b = Math.Min(b, mimaab(b2, (turn == PieceSide.White) ? PieceSide.Black : PieceSide.White, depth + 1, a, b)); if (a >= b) { return(a); } } return(b); } else // maximize { foreach (BoardHelper b2 in boards) { if (STOP) { return(-1); // interupt } a = Math.Max(a, mimaab(b2, (turn == PieceSide.White) ? PieceSide.Black : PieceSide.White, depth + 1, a, b)); if (a >= b) { return(b); } } return(a); } } }