Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericojordan committed Jan 28, 2017
1 parent 80027c1 commit bd64b37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 43 deletions.
61 changes: 19 additions & 42 deletions src/fast-chess.c
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ Node quiescenceSearch(Position * position) {
return (Node) { .move = bestMove, .score = bestScore };
}

Node alphaBeta(Position * position, char depth, int alpha, int beta, BOOL verbose) {
Node alphaBeta(Position * position, char depth, int alpha, int beta) {
if (hasGameEnded(position))
return (Node) { .score = endNodeEvaluation(position) };

Expand All @@ -1948,48 +1948,29 @@ Node alphaBeta(Position * position, char depth, int alpha, int beta, BOOL verbos
Move bestMove = 0;

Move moves[MAX_BRANCHING_FACTOR];
// int moveCount = legalMoves(moves, position, position->toMove);
int moveCount = staticOrderLegalMoves(moves, position, position->toMove);

Position newPosition;
int i;
for (i=0; i<moveCount; i++) {
updatePosition(&newPosition, position, moves[i]);

if (verbose) {
printf("(%d/%d) evaluating move: ", i+1, moveCount);
printMove(moves[i]);
printf(" = ");
fflush(stdout);
}

int score = alphaBeta(&newPosition, depth-1, alpha, beta, FALSE).score;

if (verbose) {
printf("%.2f\n", score/100.0);
fflush(stdout);
}
int score = alphaBeta(&newPosition, depth-1, alpha, beta).score;

if (score == winScore(position->toMove)) {
return (Node) { .move = moves[i], .score = score };
}

if (position->toMove == WHITE) {
if (score > alpha) {
alpha = score;
bestMove = moves[i];
if (alpha > beta) {
break;
}
}
} else if (position->toMove == BLACK) {
if (score < beta) {
beta = score;
bestMove = moves[i];
if (alpha > beta) {
break;
}
}
if (position->toMove == WHITE && score > alpha) {
alpha = score;
bestMove = moves[i];
} else if (position->toMove == BLACK && score < beta) {
beta = score;
bestMove = moves[i];
}

if (alpha > beta) {
break;
}
}

Expand All @@ -2007,7 +1988,7 @@ int alphaBetaNodes(Node * sortedNodes, Position * position, char depth) {
updatePosition(&newPosition, position, moves[i]);

nodes[i].move = moves[i];
nodes[i].score = depth>1?alphaBeta(&newPosition, depth-1, INT32_MIN, INT32_MAX, FALSE).score:staticEvaluation(&newPosition);
nodes[i].score = depth>1?alphaBeta(&newPosition, depth-1, INT32_MIN, INT32_MAX).score:staticEvaluation(&newPosition);
}

sortNodes(sortedNodes, nodes, moveCount, position->toMove);
Expand Down Expand Up @@ -2108,16 +2089,12 @@ Node pIDAB(Position * position, char depth, int * p_alpha, int * p_beta) {
return (Node) { .move = nodes[i].move, .score = score };
}

if (position->toMove == WHITE) {
if (score > alpha) {
alpha = score;
bestMove = nodes[i].move;
}
} else if (position->toMove == BLACK) {
if (score < beta) {
beta = score;
bestMove = nodes[i].move;
}
if (position->toMove == WHITE && score > alpha) {
alpha = score;
bestMove = nodes[i].move;
} else if (position->toMove == BLACK && score < beta) {
beta = score;
bestMove = nodes[i].move;
}

if (alpha > beta || alpha > *p_beta || *p_alpha > beta) {
Expand Down
2 changes: 1 addition & 1 deletion src/fast-chess.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ int quiescenceEvaluation(Position * position);

Node staticSearch(Position * position);
Node quiescenceSearch(Position * position);
Node alphaBeta(Position * position, char depth, int alpha, int beta, BOOL verbose);
Node alphaBeta(Position * position, char depth, int alpha, int beta);
int alphaBetaNodes(Node * nodes, Position * position, char depth);
Node iterativeDeepeningAlphaBeta(Position * position, char depth, int alpha, int beta, BOOL verbose);
Node pIDAB(Position * position, char depth, int * p_alpha, int * p_beta);
Expand Down

0 comments on commit bd64b37

Please sign in to comment.