Skip to content

Instantly share code, notes, and snippets.

@jadonk
Created January 24, 2011 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadonk/793815 to your computer and use it in GitHub Desktop.
Save jadonk/793815 to your computer and use it in GitHub Desktop.
Explore techniques in JavaScript to solve a peg game
#!/usr/bin/env node
var sys = require("sys");
var console = {};
console.log = sys.print;
// c
// 3 4
// 9 0 a
// 8 2 1 5
// e 7 b 6 d
var jumps =
[
[ 0x2, 0x4 ], // 0x00
[ 0x0, 0x6 ], // 0x01
[ 0x1, 0x8 ], // 0x02
[ 0x9, 0xc ], // 0x03
[ 0xa, 0xc ], // 0x04
[ 0xa, 0xd ], // 0x05
[ 0xb, 0xd ], // 0x06
[ 0xb, 0xe ], // 0x07
[ 0x9, 0xe ], // 0x08
[ 0x3, 0x8 ], // 0x09
[ 0x4, 0x5 ], // 0x0a
[ 0x6, 0x7 ], // 0x0b
[ 0x2, 0x4 ], // 0x0c (0)
[ 0x2, 0x5 ], // 0x0d (1)
[ 0x1, 0x8 ], // 0x0e (2)
[ 0x9, 0xa ], // 0x0f (0)
[ 0xa, 0xb ], // 0x10 (1)
[ 0x9, 0xb ], // 0x11 (2)
];
var countPegs = function (pegStatus)
{
var count = 0;
for (i in pegStatus)
{
if (pegStatus[i])
count++;
}
//console.log(count + " pegs remain.\n");
return (count);
};
var showPegs = function (pegStatus)
{
// c
if (pegStatus[0xc])
console.log(" *\n");
else
console.log(" \n");
// 3 4
if (pegStatus[3])
console.log(" * ");
else
console.log(" ");
if (pegStatus[4])
console.log("*\n");
else
console.log(" \n");
// 9 0 a
if (pegStatus[9])
console.log(" * ");
else
console.log(" ");
if (pegStatus[0])
console.log("* ");
else
console.log(" ");
if (pegStatus[0xa])
console.log("*\n");
else
console.log(" \n");
// 8 2 1 5
if (pegStatus[8])
console.log(" * ");
else
console.log(" ");
if (pegStatus[2])
console.log("* ");
else
console.log(" ");
if (pegStatus[1])
console.log("* ");
else
console.log(" ");
if (pegStatus[5])
console.log("*\n");
else
console.log(" \n");
// e 7 b 6 d
if (pegStatus[0xe])
console.log(" * ");
else
console.log(" ");
if (pegStatus[7])
console.log("* ");
else
console.log(" ");
if (pegStatus[0xb])
console.log("* ");
else
console.log(" ");
if (pegStatus[6])
console.log("* ");
else
console.log(" ");
if (pegStatus[0xd])
console.log("*\n");
else
console.log(" \n");
}
var tryMoves = function (moves, pegStatus, showMoves)
{
for (var i in moves)
{
var move = moves[i];
//showPegs(pegStatus);
while(true)
{
var j = move;
var direction = true;
if (j > 0x11)
{
j = j % 0x12;
direction = false;
}
var peg = j;
if (peg > 0xb)
{
peg = peg % 3;
}
if (pegStatus[peg])
if
(
direction
&& pegStatus[jumps[j][0]]
&& !pegStatus[jumps[j][1]]
)
{
pegStatus[peg] = false;
pegStatus[jumps[j][0]] = false;
pegStatus[jumps[j][1]] = true;
if (showMoves)
console.log("Jumped from " + jumps[j][0] + " to " + jumps[j][1] + ".\n");
break;
}
else if
(
!direction
&& !pegStatus[jumps[j][0]]
&& pegStatus[jumps[j][1]]
)
{
pegStatus[peg] = false;
pegStatus[jumps[j][1]] = false;
pegStatus[jumps[j][0]] = true;
if (showMoves)
console.log("Jumped from " + jumps[j][1] + " to " + jumps[j][0] + ".\n");
break;
}
move++;
move %= 0x24;
if (move == moves[i])
break;
}
//countPegs(pegStatus);
}
};
var fixedMoves =
{
"moves": function ()
{
var moves = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ];
return (moves);
},
"pegStatus": function ()
{
var pegStatus =
[ false, true, true,
true, true, true,
true, true, true,
true, true, true,
true, true, true
];
return (pegStatus);
}
};
var randomMoves =
{
"moves": function ()
{
var moves = new Array();
for (var i = 0; i < 14; i++)
{
moves[i] = Math.floor(Math.random()*0x24);
}
return (moves);
},
"pegStatus": function ()
{
var pegStatus = new Array();
var missingPeg = Math.floor(Math.random()*15);
for (var i = 0; i < 15; i++)
pegStatus[i] = (i != missingPeg);
return (pegStatus);
}
};
var solve = function (solver, attempts)
{
var best_count = 14;
for(var i = 0; (i < attempts) || (attempts == 0); i++)
{
var count = 14;
var moves = solver.moves();
var startingPegStatus = solver.pegStatus();
var pegStatus = startingPegStatus.slice(0);
tryMoves(moves, pegStatus, false);
count = countPegs(pegStatus);
if (count < best_count)
{
console.log("Made " + (i+1) + " attempts.\n");
console.log("Previous best count was " + best_count +".\n");
var pegStatus = startingPegStatus.slice(0);
showPegs(pegStatus);
tryMoves(moves, pegStatus, true);
showPegs(pegStatus);
best_count = countPegs(pegStatus);
}
if (count == 1)
{
break;
}
}
console.log("Made " + (i+1) + " attempts.\n");
console.log("Best count was " + best_count +".\n");
};
solve(randomMoves, 10000);
Solution #1:
X
3 4
. 0 A
8 2 1 5
E 7 B 6 D
1) Jumped from C to 9. (missing peg in middle of side, go along side starting from top until stuck, *escaping* from left side when necessary)
.
. 4
9 0 A
X 2 1 5
E 7 B 6 D
2) Jumped from 8 to 3.
.
3 4
. 0 A
. 2 X 5
E 7 B 6 D
3) Jumped from 1 to 8.
.
3 4
. 0 A
8 . . 5
X 7 B 6 D
4) Jumped from E to 9.
.
X 4
9 0 A
. . . 5
. 7 B 6 D
5) Jumped from 3 to 8.
.
. X
. 0 A
8 . . 5
. 7 B 6 D
6) Jumped from 4 to 2. (*enter* from right and continue on right side until stuck)
.
. .
. . A
8 2 . X
. 7 B 6 D
7) Jumped from 5 to 4.
.
. 4
. . .
X 2 . .
. 7 B 6 D
8) Jumped from 8 to 1. (enter from left side)
.
. 4
. . .
. . 1 .
. 7 B X D
9) Jumped from 6 to 0. (enter from right bottom)
.
. 4
. 0 .
. . . .
. X B . D
10) Jumped from 7 to 6. (bottom and finish)
.
. X
. 0 .
. . . .
. . . 6 D
11) Jumped from 4 to 2.
.
. .
. . .
. 2 . .
. . . 6 X
12) Jumped from D to B.
.
. .
. . .
. 2 . .
. . X . .
13) Jumped from B to 9.
.
. .
9 . .
. . . .
. . . . .
Solution #2:
C
3 4
9 . A
8 2 1 5
E 7 B X D
1) Jumped from 6 to 0.
C
3 4
9 0 A
8 2 . 5
E X B . D
2) Jumped from 7 to 6.
C
3 4
X 0 A
8 2 . 5
E . . 6 D
3) Jumped from 9 to B.
C
3 4
. 0 A
8 . . 5
X . B 6 D
4) Jumped from E to 9.
C
X 4
9 0 A
. . . 5
. . B 6 D
5) Jumped from 3 to 8.
C
. 4
. 0 A
8 . . 5
. . B X D
6) Jumped from 6 to 7.
C
. X
. 0 A
8 . . 5
. 7 . . D
7) Jumped from 4 to 2.
C
. .
. . A
8 2 . X
. 7 . . D
8) Jumped from 5 to 4.
C
. 4
. . .
X 2 . .
. 7 . . D
9) Jumped from 8 to 1.
X
. 4
. . .
. . 1 .
. 7 . . D
10) Jumped from C to A.
.
. .
. . X
. . 1 .
. 7 . . D
11) Jumped from A to B.
.
. .
. . .
. . . .
. X B . D
12) Jumped from 7 to 6.
.
. .
. . .
. . . .
. . . 6 X
13) Jumped from D to B.
.
. .
. . .
. . . .
. . B . .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment