...

View Full Version : Checkers game, alert when no more moves possible?



eddiegilbert
01-20-2005, 04:40 AM
Here is the game: http://www.patrickriley.com/games/checkers.html
It is for one player vs. the "computer"

I modified the original script so that once one player's checkers were all removed, a pop-up message box alert would appear and say who won. I added the redcount and whitecount variables to the removeChecker function to accomplish that. However, I was unable to make it so that a similar alert would appear once one player was trapped and could no longer make a legal move

You can see the full code in the source of the third frame of the page. I posted most of it here since I don't know which function to edit or if it is somehow possible. Or maybe a new function needs to be created

I posted on the old freewarejava.com board about this, a couple years back, but couldn't solve it then
Can anyone help this time? :confused: Thanx

<script language="JavaScript1.1"><!--
var numSquares = 64;
var blackSquare = "blacksquare.gif";
var greenSquare = "greensquare.gif";
var redChecker = "redchecker.gif";
var whiteChecker = "whitechecker.gif";
var redKing = "redking.gif";
var whiteKing = "whiteking.gif";
var point1 = "";
var point2 = "";
var turn = 1;
var changeTurns = 0;
var movedChecker = 1;
var alternate = 0;
var timeout = null;
var redcount = 12;
var whitecount = 12;

function startFromTheTop() { window.open("checkers.html", "_top"); }

function makeArray(n) {
this.length = n;

for (var i = 1; i <= n; i++) { this[i] = 0; }
return this;
}

function fillArraySeq(arrayName, String, elements) {

for (var i = 1; i <= elements; i++) { arrayName[i] = String + i; }
}

function fillArray(arrayName) {

for (var i = 1; i < fillArray.arguments.length; i++) {
arrayName[i] = fillArray.arguments[i];
}
}

function findChecker(name) {
var i;

for (i = 1; i <= checkers.length; i++) {

if (name == checkers[i]) { break }
}
return(i);
}

function removeChecker(point1) {
var pos1 = findChecker(point1.name);
if(checkerBoard[pos1]==redChecker || checkerBoard[pos1]==redKing){
redcount--;}
else if(checkerBoard[pos1]==whiteChecker || checkerBoard[pos1]==whiteKing){
whitecount--;}
checkerBoard[pos1] = greenSquare;
point1.src = greenSquare;
if(redcount==0)
{ alert("Game over. You win!!!");
redcount=12;
whitecount=12;
}
if(whitecount==0)
{ alert("Game over. You lose");
redcount=12;
whitecount=12;
}
}

function kingChecker(pos2, point2) {

if (checkerBoard[pos2] == redChecker) {
checkerBoard[pos2] = redKing;
point2.src = redKing;
}
else {

if (checkerBoard[pos2] == whiteChecker) {
checkerBoard[pos2] = whiteKing;
point2.src = whiteKing;
}
}
}

function moveChecker(point1, point2) {
var moved = 1;
var pos1 = findChecker(point1.name);
var pos2 = findChecker(point2.name);
var diff = pos2 - pos1;
var absDiff;
with (Math) { absDiff = abs(diff); }

movedChecker = pos2;
temp = checkerBoard[pos1];
checkerBoard[pos1] = checkerBoard[pos2];
checkerBoard[pos2] = temp;
temp = point1.src;
point1.src = point2.src;
point2.src = temp;
// have we jumped
if (absDiff > 9) {
removeChecker(checkerSrc[pos1 + diff/2]);
moved = 2;
}
// have we reached kings row
if ( ( (pos2 < 9) && (checkerBoard[pos2] == whiteChecker) ) ||
( (pos2 > 56) && (checkerBoard[pos2] == redChecker) ) ) {
kingChecker(pos2, point2);
moved = 3;
}

return(moved);
}

function legalMove(point1, point2) {
var flag = 0; // 0 means illegal
var pos1 = findChecker(point1.name);
var pos2 = findChecker(point2.name);
// are we moving to a green square
if (checkerBoard[pos2] == greenSquare) {
var diff = pos2 - pos1;
var absDiff;
with (Math) { absDiff = abs(diff); }
var sign = diff / absDiff;
// reds turn
if (turn == 0) {
// are we moving a red checker to a green square ?
if ( (checkerBoard[pos1] == redKing) ||
( (checkerBoard[pos1] == redChecker) && (sign == 1) ) ) {
// are we jumping a red checker over a white checker ?
if (absDiff > 9) {

if ( (checkerBoard[pos1 + (diff / 2)] == whiteChecker) ||
(checkerBoard[pos1 + (diff / 2)] == whiteKing) ) {
// is this a continuation jump
if ( (movedChecker == pos1) ||
(checkerBoard[movedChecker] == whiteChecker) ||
(checkerBoard[movedChecker] == whiteKing) ) { flag = 1; }
}
}
else { flag = 1; }
}
}
else {
// are we moving a white checker to a green square ?
if ( (checkerBoard[pos1] == whiteKing) ||
( (checkerBoard[pos1] == whiteChecker) && (sign == -1) ) ) {
// are we jumping a white checker over a red checker ?
if (absDiff > 9) {

if ( (checkerBoard[pos1 + (diff / 2)] == redChecker) ||
(checkerBoard[pos1 + (diff / 2)] == redKing) ) {
// is this a continuation jump
if ( (movedChecker == pos1) ||
(checkerBoard[movedChecker] == redChecker) ||
(checkerBoard[movedChecker] == redKing) ) { flag = 1; }
}
}
else { flag = 1; }
}
}
}
return(flag);
}

// Move object
function Move(from, to, weight) {
this.from = from;
this.to = to;
this.weight = weight;
}

function discoverMoves(moves) {
var j = 0;
var start = 1;
var end = checkerBoard.length + 1;
var incr = 1;

if (turn == 0) {

if (alternate == 1) {
start = checkerBoard.length;
end = 0;
incr = -1;
}
with (Math) { alternate = abs(alternate - 1); }
}

for (var i = start; i != end; i += incr) {

if ( (checkerBoard[i] != greenSquare) && (checkerBoard[i] != blackSquare) ) {

if ( (i > 7) && (legalMove(checkerSrc[i], checkerSrc[i - 7]) == 1) ) {
moves[++j] = new Move(i, i - 7, 1);
}

if ( (i > 9) && (legalMove(checkerSrc[i], checkerSrc[i - 9]) == 1) ) {
moves[++j] = new Move(i, i - 9, 1);
}

if ( (i < 58) && (legalMove(checkerSrc[i], checkerSrc[i + 7]) == 1) ) {
moves[++j] = new Move(i, i + 7, 1);
}

if ( (i < 56) && (legalMove(checkerSrc[i], checkerSrc[i + 9]) == 1) ) {
moves[++j] = new Move(i, i + 9, 1);
}
}
}
// null out the rest
for (var i = ++j; i <= moves.length; i++) { moves[i] = new Move(0, 0, 1);}
}

function discoverJumps(moves) {
var j = 0;
var start = 1;
var end = checkerBoard.length + 1;
var incr = 1;

if (turn == 0) {

if (alternate == 1) {
start = checkerBoard.length;
end = 0;
incr = -1;
}
}

for (var i = start; i != end; i += incr) {

if ( (checkerBoard[i] != greenSquare) && (checkerBoard[i] != blackSquare) ) {

if ( (i > 14) && (legalMove(checkerSrc[i], checkerSrc[i - 14]) == 1) ) {
moves[++j] = new Move(i, i - 14, 1);
}

if ( (i > 18) && (legalMove(checkerSrc[i], checkerSrc[i - 18]) == 1) ) {
moves[++j] = new Move(i, i - 18, 1);
}

if ( (i < 51) && (legalMove(checkerSrc[i], checkerSrc[i + 14]) == 1) ) {
moves[++j] = new Move(i, i + 14, 1);
}

if ( (i < 47) && (legalMove(checkerSrc[i], checkerSrc[i + 18]) == 1) ) {
moves[++j] = new Move(i, i + 18, 1);
}
}
}
// null out the rest
for (var i = ++j; i <= moves.length; i++) { moves[i] = new Move(0, 0, 1);}
}

function weighMoves(moves) {
for (var i = 1; ( (i <= moves.length) && (moves[i].from != 0) ); i++) {
var from = moves[i].from;
var to = moves[i].to;
var diff = to - from;
var absDiff;
with (Math) { absDiff = abs(diff); }
var sign = diff / absDiff;
var cb_to_diff_p_2_sign = checkerBoard[to + diff + (2 * sign)];
var cb_to_diff_m_2_sign = checkerBoard[to + diff - (2 * sign)];
var cb_from_p_2_sign = checkerBoard[from + (2 * sign)];
var cb_from_m_2_sign = checkerBoard[from - (2 * sign)];
var cb_from_m_2_diff = checkerBoard[from - (2 * diff)];

// moving directly towards opponent
if ( (checkerBoard[to + diff] == whiteChecker) ||
(checkerBoard[to + diff] == whiteKing) ) {
moves[i].weight++;
// we can capture opponent on next move
if ( ( (checkerBoard[from - diff] == redChecker) ||
(checkerBoard[from - diff] == redKing) ) &&
( (cb_from_m_2_diff == redChecker) ||
(cb_from_m_2_diff == redKing) ) ) { moves[i].weight--; }
}

// moving to right (7 - forward, -7 - backward)
if (absDiff == 7) {

if ( (cb_to_diff_p_2_sign == whiteChecker) ||
(cb_to_diff_p_2_sign == whiteKing) ) {
moves[i].weight++;

if ( (cb_from_m_2_sign == redChecker) ||
(cb_from_m_2_sign == redKing) ||
(cb_from_m_2_sign == whiteChecker) ||
(cb_from_m_2_sign == whiteKing) ) {
moves[i].weight--;
}
}

if ( ( (cb_from_m_2_sign == whiteChecker) && (sign == -1) ) ||
(cb_from_m_2_sign == whiteKing) ) {
moves[i].weight++;

if ( (cb_to_diff_p_2_sign == redChecker) ||
(cb_to_diff_p_2_sign == redKing) ||
(cb_to_diff_p_2_sign == whiteChecker) ||
(cb_to_diff_p_2_sign == whiteKing) ) {
moves[i].weight--;
}
}
}
else {
// moving to left (9 - forward, -9 - backward)
if ( (cb_to_diff_m_2_sign == whiteChecker) ||
(cb_to_diff_m_2_sign == whiteKing) ) {
moves[i].weight++;

if ( (cb_from_p_2_sign == redChecker) ||
(cb_from_p_2_sign == redKing) ||
(cb_from_p_2_sign == whiteChecker) ||
(cb_from_p_2_sign == whiteKing) ) {
moves[i].weight--;
}
}

if ( ( (cb_from_p_2_sign == whiteChecker) && (sign == -1) ) ||
(cb_from_p_2_sign == whiteKing) ) {
moves[i].weight++;

if ( (cb_to_diff_m_2_sign == redChecker) ||
(cb_to_diff_m_2_sign == redKing) ||
(cb_to_diff_m_2_sign == whiteChecker) ||
(cb_to_diff_m_2_sign == whiteKing) ) {
moves[i].weight--;
}
}
}
}
}

function makeMove(moves) {
var moved = 0;
var curweight = 0;

while ((moved == 0) && (curweight < 5)) {

for (var i = 1; i <= moves.length; i++) {

if (moves[i].from == 0) { break; }

if (moves[i].weight == curweight) {
moved = moveChecker(checkerSrc[moves[i].from], checkerSrc[moves[i].to]);
break;
}
}
curweight++;
}
return(moved);
}

function autoMove() {
turn = 1;
discoverJumps(blackMoves); // check for jumps
turn = 0;
discoverMoves(redMoves);
weighMoves(redMoves);

for (var i = 1; i <= blackMoves.length; i++) {

if (blackMoves[i].from == 0) { break; }

var diff = blackMoves[i].to - blackMoves[i].from;
var absDiff;
with (Math) { absDiff = abs(diff); }
var sign = diff / absDiff;

for (var j = 1; j <= redMoves.length; j++) {
if (redMoves[j].from == 0) { break; }

if ( (redMoves[j].to == blackMoves[i].to) ||
(redMoves[j].from == blackMoves[i].from + (diff / 2) ) &&
(redMoves[j].weight != 0) ) {
redMoves[j].weight--;
}
}
}
makeMove(redMoves);
}

function autoJump(moves) {
var moved = 0;
clearTimeout(timeout);

discoverJumps(moves);
// weighJumps(moves);
moved = makeMove(moves);

// reached kings row
if (moved == 3) { movePause(0); }
else {

if (moved != 0) { movePause(1); }
else {

if (changeTurns == 1) { movePause(0); }
else {

if (turn == 0) {
autoMove();
movePause(0);
}
}
}
}
}

function movePause(change) {

if (change == 0) {
with (Math) { turn = abs(turn - 1); }

var status = parent.ga3.document;
if (turn == 0) { status.forms[0].toplay.value = "red"; }
else { status.forms[0].toplay.value = "white"; }
}

changeTurns = change;

if (turn == 0) {
timeout = setTimeout('autoJump(redMoves)', 700);
}
// else {
// timeout = setTimeout('autoJump(blackMoves)', 700);
// }
}

function chooseMove(name) {
var moved = 0;

if (point1 == "") { point1 = name; }
else {
point2 = name;

if (legalMove(point1, point2) == 1) {
discoverJumps(blackMoves);

if (blackMoves[1].from == 0) { moved = moveChecker(point1, point2); }
else {
var pos1 = findChecker(point1.name);
var pos2 = findChecker(point2.name);

for (var i = 1; i <= blackMoves.length; i++) {

if ( (blackMoves[i].from == pos1) && (blackMoves[i].to == pos2) ) {
moved = moveChecker(point1, point2);
break;
}
}

if ( (moved != 2) && (moved != 3) ) { alert("You must make a jump."); }
}

if (moved == 2) {
discoverJumps(blackMoves);

if (blackMoves[1].from == 0) { movePause(0); }
}
else {
if (moved != 0) { movePause(0); }
}
}
else { alert("Illegal or cancelled move, try again."); }
point1 = "";
point2 = "";
}
}

eddiegilbert
01-22-2005, 11:47 PM
Found another problem. With my addition of the redcount and whitecount variables, the function startFromTheTop, does not seem to reset the count. So if someone clicks the new game button, the counts remain the same. If you then remove enough checkers for 12 total, the alert will pop up even if the current game is not finished, since it keeps the count from the previous unfinished game

I tried to add var redcount= 12; and var whitecount = 12; to the function:

function startFromTheTop() { window.open("checkers.html", "_top");}

but it did not work

Any help with this one? Anyone working on it? Just let me know.
I would like to know if anyone is trying or has come up with any ideas I could try to do myself :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum