Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 1dchess/chess.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h1> 1D-Chess </h1>
Play as white against the AI. You might initally find it more difficult than expected, but assming optimal play, is there a forced win for white?
<br>
<br>
Mouse over to reveal answer: <span class="spoiler-box">Try this line: N4 N5, N6 K7, R4 K6, R2 K7, R5++</span>
Mouse over to reveal answer: <span class="spoiler-box">Try this line: N4 N5, N6 K7, R4 N3, K2 N5, N8 K8, R5++</span>
</p>

<h2> Rules </h2>
Expand Down
24 changes: 8 additions & 16 deletions 1dchess/js/chess-ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ function minimax(gameState, a=-1, b=1, depth=0) {
switch (gameState["gameResult"]["winner"]){
case "white":
//console.log("white win");
return 1;
return 1 - depth * 0.001;
case "black":
//console.log("black win");
return -1
return -1 + depth * 0.001;
case "draw":
return 0;
}
}
if (canClaimDraw(gameState["pieceList"]) && gameState["bestMove"] != "None") {
if (canClaimDraw(gameState["pieceList"]) && depth > 0) {
// console.log("1 knight draw");
return 0;
}
Expand Down Expand Up @@ -121,12 +121,8 @@ function minimax(gameState, a=-1, b=1, depth=0) {
childGameState["depth"] = gameState["depth"] + 1;

let childValue = minimax(childGameState, a, b, depth+1);
if(childValue == 1) {
gameState["bestMove"] = movePair;
return childValue
}
value = Math.max(value, childValue);
if (value == childValue) {
if (childValue > value) {
value = childValue;
gameState["bestMove"] = movePair;
}
a = Math.max(a, value);
Expand Down Expand Up @@ -173,15 +169,11 @@ function minimax(gameState, a=-1, b=1, depth=0) {
childGameState["turn"] = otherColor(childGameState["turn"]);

let childValue = minimax(childGameState, a, b, depth+1);
if(childValue == -1) {
gameState["bestMove"] = movePair;
return childValue
}
value = Math.min(value, childValue);
if (value == childValue) {
if (childValue < value) {
value = childValue;
gameState["bestMove"] = movePair;
}
b = Math.max(b, value);
b = Math.min(b, value);
if (b <= a){
break;
}
Expand Down