blue piece on (release) { if (_root.turn == 'b' && _root.dragPiece._x < _root.BOARD_LEFT) { _root.pressed = 1; _root.srcClick = Number(this._name); gotoAndStop(5); Mouse.hide(); _root.dragPiece.gotoAndStop(1); startDrag(_root.dragPiece, true, _root.BOARD_LEFT, _root.BOARD_TOP, _root.BOARD_RIGHT, _root.BOARD_BOTTOM); } } green piece on (release) { if (_root.turn == 'g' && _root.dragPiece._x < _root.BOARD_LEFT) { _root.pressed = 2; _root.srcClick = Number(this._name); gotoAndStop(5); Mouse.hide(); _root.dragPiece.gotoAndStop(2); startDrag(_root.dragPiece, true, _root.BOARD_LEFT, _root.BOARD_TOP, _root.BOARD_RIGHT, _root.BOARD_BOTTOM); } } blue king on (release) { if (_root.turn == 'b' && _root.dragPiece._x < _root.BOARD_LEFT) { _root.pressed = 3; _root.srcClick = Number(this._name); gotoAndStop(5); Mouse.hide(); _root.dragPiece.gotoAndStop(3); startDrag(_root.dragPiece, true, _root.BOARD_LEFT, _root.BOARD_TOP, _root.BOARD_RIGHT, _root.BOARD_BOTTOM); } } green king on (release) { if (_root.turn == 'g' && _root.dragPiece._x < _root.BOARD_LEFT) { _root.pressed = 4; _root.srcClick = Number(this._name); gotoAndStop(5); Mouse.hide(); _root.dragPiece.gotoAndStop(4); startDrag(_root.dragPiece, true, _root.BOARD_LEFT, _root.BOARD_TOP, _root.BOARD_RIGHT, _root.BOARD_BOTTOM); } } space position on (release) { _root.pieceNumber = this._name; if (_root.pressed>0 && this._currentframe == 5 && _root.validMove(Number(this._name))) { stopDrag(); Mouse.show(); // move the drag piece off the screen _root.dragPiece._x = -1000; gotoAndStop(_root.pressed); /* Change turn */ _root.pressed = 0; } } //code to start the ai onClipEvent (load) { var halismoving = 0; } onClipEvent (enterFrame) { if (_root.turn == 'b' && !halismoving) { halismoving=1; _root.hal(); halismoving=0; } } //Main section, with functions // The frame that the clicked piece was from var pressed:Number = 0; // The square position the clicked piece was from var srcClick:Number = 0; // The square position the clicked piece was from var halmoving:Number = 0; // The current piece turn var turn:String = 'g'; // Top of the game board var BOARD_TOP:Number = 27; // Bottom of the game board var BOARD_BOTTOM:Number = 405; // Left side of the game board var BOARD_LEFT:Number = 27; // Right side of the game board var BOARD_RIGHT:Number = 405; // When a person has made a jump, make sure their next move is a jump from the same piece var alreadyJumped = 0; /** * Validate that a move is valid * @param dest The square position of the destination fo the piece */ function changeTurn(dest:Number) { // change the piece to a king if (_root.turn == 'g' && (dest>=1 && dest<=4)) { _root.pressed = 4; _level[dest].gotoAndStop(4); } else if (_root.turn == 'b' && (dest>=29 && dest<=32)) { _root.pressed = 3; _level[dest].gotoAndStop(3); } if (_root.turn == 'b') { nowinner = 0; for (var i = 1; i<=32; i++) { if (_level[i]._currentframe == '2' || _level[i]._currentframe == '4') { nowinner = 1; } } if (nowinner != 1 && !halmoving) { winner.gotoAndStop(2); } _root.turn = 'g'; } else { nowinner = 0; for (var i = 1; i<=32; i++) { if (_level[i]._currentframe == '1' || _level[i]._currentframe == '3') { nowinner = 1; } } if (nowinner != 1 && !halmoving) { winner.gotoAndStop(3); } _root.turn = 'b'; } alreadyJumped = 0; } function validMove(dest:Number):Boolean { var theBoard = new Array(33); for (var i = 1; i<=32; i++) { theBoard[i] = _level[i]._currentframe; } thismovescore(dest, theBoard); var valid:Boolean = false; if (_root.srcClick == dest) { return true; } //Check if player has already made a jump move if ((alreadyJumped != 0) && (srcClick != alreadyJumped)) { return false; } switch (pressed) { case 0 : // not used case 1 : // blue piece if (alreadyJumped == 0) { valid = moveDown(dest, false); } if (!valid) { valid = jumpDown(dest, false, false); } break; case 2 : // green piece if (alreadyJumped == 0) { valid = moveUp(dest, false); } if (!valid) { valid = jumpUp(dest, false, false); } break; case 3 : // king blue piece if (alreadyJumped == 0) { if (moveDown(dest, false) || moveUp(dest, false)) { valid = true; } } if (!valid) { if (jumpUp(dest, false, true) || jumpDown(dest, false, true)) { valid = true; } } break; case 4 : // king green piece if (alreadyJumped == 0) { if (moveDown(dest, false) || moveUp(dest, false)) { valid = true; } } if (!valid) { if (jumpUp(dest, false) || jumpDown(dest, false)) { valid = true; } } } return valid; } function moveDown(dest:Number, checking:Boolean):Boolean { if (dest>32 || mustjump() || _level[dest]._currentframe != 5) { return false; } switch (srcClick%8) { case 0 : case 1 : if (dest == srcClick+4) { if (checking == false) { changeTurn(dest); } return true; } break; case 2 : case 3 : case 4 : if ((dest == srcClick+4) || (dest == srcClick+3)) { if (checking == false) { changeTurn(dest); } return true; } break; case 5 : case 6 : case 7 : //move right or left if ((dest == srcClick+5) || (dest == srcClick+4)) { if (checking == false) { changeTurn(dest); } return true; } break; } return false; } function jumpDown(dest:Number, checking:Boolean, isKing:Boolean):Boolean { if (pressed == 2) { return false; } if (dest == srcClick+7 && _level[srcClick+7]._currentframe != 5) { return false; } if (dest == srcClick+9 && _level[srcClick+9]._currentframe != 5) { return false; } if (dest == srcClick-7 && _level[srcClick-7]._currentframe != 5) { return false; } if (dest == srcClick-9 && _level[srcClick-9]._currentframe != 5) { return false; } var srcPos:Number = srcClick%8; var jumpFrame:Number = 0; var jumpFrame:Number = 0; var right:Number = 0; var left:Number = 0; if (dest>32) { return false; } switch (srcPos) { case 0 : //jumps down left jumpFrame = _level[srcClick+4]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick+7]._currentframe; if ((dest == srcClick+7) && (destFrame == 5) && ((turn == 'b' && jumpFrame == 2) || (turn == 'g' && jumpFrame == 1)) && srcClick<25) { if (!checking) { //remove jumped piece _level[srcClick+4].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpDown(dest+7, true, isKing) && !jumpDown(dest+9, true, isKing)) { if (isKing) { if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; case 1 : //jump down right jumpFrame = _level[srcClick+4]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick+9]._currentframe; if ((dest == srcClick+9) && (destFrame == 5) && ((turn == 'b' && jumpFrame == 2) || (turn == 'g' && jumpFrame == 1)) && srcClick<25) { if (!checking) { //remove jumped piece _level[srcClick+4].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpDown(dest+7, true, isKing) && !jumpDown(dest+9, true, isKing)) { if (isKing) { if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; case 2 : case 3 : case 4 : //jump down right right = srcClick+4; jumpFrame = _level[right]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick+9]._currentframe; if ((srcPos != 4) && (destFrame == 5) && (dest == right+5) && ((turn == 'b' && jumpFrame == 2) || (turn == 'g' && jumpFrame == 1)) && srcClick<25) { if (!checking) { //remove jumped piece _level[right].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpDown(dest+7, true, isKing) && !jumpDown(dest+9, true, isKing)) { if (isKing) { if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } //jump down left left = srcClick+3; jumpFrame = _level[left]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick+7]._currentframe; if ((dest == left+4) && (destFrame == 5) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick<25) { if (!checking) { //remove jumped piece _level[left].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpDown(dest+7, true, isKing) && !jumpDown(dest+9, true, isKing)) { if (isKing) { if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; case 5 : case 6 : case 7 : //jumps down left left = srcClick+4; jumpFrame = _level[left]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick+7]._currentframe; if ((srcPos != 5) && (destFrame == 5) && (dest == left+3) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick<25) { if (!checking) { //remove jumped piece _level[left].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpDown(dest+7, true) && !jumpDown(dest+9, true)) { if (isKing) { if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } //jumps down right right = srcClick+5; jumpFrame = _level[right]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick+9]._currentframe; if ((dest == right+4) && (destFrame == 5) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick<25) { if (!checking) { //remove jumped piece _level[right].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpDown(dest+7, true, isKing) && !jumpDown(dest+9, true, isKing)) { if (isKing) { if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; } return false; } function moveUp(dest:Number, checking:Boolean):Boolean { if (dest<1 || mustjump() || _level[dest]._currentframe != 5) { return false; } switch (srcClick%8) { case 0 : case 1 : //moves right or left if (dest == srcClick-4) { if (checking == false) { changeTurn(dest); } return true; } break; case 2 : case 3 : case 4 : //moves right or left up if ((dest == srcClick-4) || (dest == srcClick-5)) { if (checking == false) { changeTurn(dest); } return true; } break; case 5 : case 6 : case 7 : //moves left or right up if ((dest == srcClick-3) || (dest == srcClick-4)) { if (checking == false) { changeTurn(dest); } return true; } break; } return false; } function jumpUp(dest:Number, checking:Boolean, isKing:Boolean):Boolean { if (pressed == 1) { return false; } var srcPos:Number = srcClick%8; var jumpFrame:Number = 0; var right:Number = 0; var left:Number = 0; if (dest<1) { return false; } switch (srcPos) { case 0 : //jump up left jumpFrame = _level[srcClick-4]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick-9]._currentframe; if ((dest == Number(srcClick)-9) && (destFrame == 5) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick>8) { if (!checking) { //remove jumped piece _level[srcClick-4].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { if (isKing) { if (!jumpDown(dest-7, true, isKing) && !jumpDown(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; case 1 : //jump up right jumpFrame = _level[srcClick-4]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick-7]._currentframe; if ((dest == Number(srcClick)-7) && (destFrame == 5) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick>8) { if (!checking) { _level[srcClick-4].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { if (isKing) { if (!jumpDown(dest-7, true, isKing) && !jumpDown(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; case 2 : case 3 : case 4 : //jump up left left = srcClick-5; jumpFrame = _level[left]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick-9]._currentframe; if ((dest == srcClick-9) && (destFrame == 5) && ((turn == 'b' && jumpFrame == 2) || (turn == 'g' && jumpFrame == 1)) && srcClick>8) { if (!checking) { //remove jumped piece _level[left].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { if (isKing) { if (!jumpDown(dest-7, true, isKing) && !jumpDown(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } //jumps up right right = srcClick-4; jumpFrame = _level[right]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick-7]._currentframe; if ((srcPos != 4) && (dest == right-3) && (destFrame == 5) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick>8) { if (!checking) { //remove jumped piece _level[right].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { if (isKing) { if (!jumpDown(dest-7, true, isKing) && !jumpDown(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; case 5 : case 6 : case 7 : //jump up right right = srcClick-3; jumpFrame = _level[right]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick-7]._currentframe; if ((dest == right-4) && (destFrame == 5) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick>8) { if (!checking) { //remove jumped piece _level[right].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { if (isKing) { if (!jumpDown(dest-7, true, isKing) && !jumpDown(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } //jumps up left left = srcClick-4; jumpFrame = _level[left]._currentframe; if ((jumpFrame-2)>0) { jumpFrame = jumpFrame-2; } destFrame = _level[srcClick-9]._currentframe; if ((srcPos != 5) && (destFrame == 5) && (dest == left-5) && ((turn == 'b') && (jumpFrame == 2) || (turn == 'g') && (jumpFrame == 1)) && srcClick>8) { if (!checking) { //remove jumped piece _level[left].gotoAndStop(5); srcClick = Number(dest); alreadyJumped = Number(dest); if (!jumpUp(dest-7, true, isKing) && !jumpUp(dest-9, true, isKing)) { if (isKing) { if (!jumpDown(dest-7, true, isKing) && !jumpDown(dest-9, true, isKing)) { changeTurn(dest); } } else { changeTurn(dest); } } } return true; } break; } return false; } function mustjump() { var forcejump = 0; var tempsource = srcClick; for (var i = 0; i<=32; i++) { srcClick = i; if (_level[srcClick]._currentframe == 1 && _root.turn == 'b') { if (jumpDown(srcClick+7, true, false) && srcClick%8 != 1 && srcClick%8 != 5) { forcejump = 1; } if (jumpDown(srcClick+9, true, false) && srcClick%8 != 0 && srcClick%8 != 4) { forcejump = 1; } } if (_level[srcClick]._currentframe == 2 && _root.turn == 'g') { if (jumpUp(srcClick-7, true, false) && srcClick%8 != 1 && srcClick%8 != 5) { forcejump = 1; } if (jumpUp(srcClick-9, true, false) && srcClick%8 != 0 && srcClick%8 != 4) { forcejump = 1; } } else if ((_level[srcClick]._currentframe == 3 && _root.turn == 'b') || (_level[srcClick]._currentframe == 4 && _root.turn == 'g')) { if (jumpDown(srcClick+7, true, true) && srcClick%8 != 1 && srcClick%8 != 5) { forcejump = 1; } if (jumpDown(srcClick+9, true, true) && srcClick%8 != 0 && srcClick%8 != 4) { forcejump = 1; } if (jumpUp(srcClick-7, true, true) && srcClick%8 != 1 && srcClick%8 != 5) { forcejump = 1; } if (jumpUp(srcClick-9, true, true) && srcClick%8 != 0 && srcClick%8 != 4) { forcejump = 1; } } } srcClick = tempsource; _root.test3++; return forcejump; } function thismovescore(moveto:Number, theBoard):Number { //uses srcClick and the arguement from the function call to give a score to the current move var score:Number = 0; //100-move if ((moveto == srcClick+3) || (moveto == srcClick+4) || (moveto == srcClick+5) || (moveto == srcClick-3) || (moveto == srcClick-4) || (moveto == srcClick-5)) { score = 100; } //700-jump single piece if ((moveto == srcClick+7) || (moveto == srcClick-7) || (moveto == srcClick+9) || (moveto == srcClick-9)) { score = 700; } //+50-move to corner if ((srcClick%8 == 5 && moveto%8 == 1) || (srcClick%8 == 4 && moveto%8 == 0)) { score += 50; } //-50-move to noncorner from corner if ((srcClick%8 == 1 && moveto%8 == 5) || (srcClick%8 == 0 && moveto%8 == 4)) { score -= 50; } //+50-jump to corner if ((srcClick%8 == 2 && moveto%8 == 1) || (srcClick%8 == 7 && moveto%8 == 0)) { score += 50; } //-50-jump to noncorner if ((srcClick%8 == 1 && moveto%8 == 2) || (srcClick%8 == 0 && moveto%8 == 7)) { score -= 50; } //-100-no longer top if (srcClick == 1 || srcClick == 2 || srcClick == 3 || srcClick == 4 || srcClick == 29 || srcClick == 30 || srcClick == 31 || srcClick == 32) { score -= 100; } //+200-jump becoming king if ((dragPiece._currentframe == 1 || dragPiece._currentframe == 2) && ((moveto<=4 && moveto>=1) || (moveto<=32 && moveto>=29))) { score += 200; } //+300-jump over king //row starting with red square if ((srcClick%8 == 2 && moveto%8 == 1) || (srcClick%8 == 3 && moveto%8 == 2) || (srcClick%8 == 4 && moveto%8 == 3)) { //jumping up left from positions 2 3 4 if ((_level[srcClick-5]._currentframe == 3 || _level[srcClick-5]._currentframe == 4) && srcClick>moveto) { score += 300; } //jumping down left from positions 2 3 4 if ((_level[srcClick+3]._currentframe == 3 || _level[srcClick+3]._currentframe == 4) && srcClickmoveto) { score += 300; } //jumping down right from positions 1 2 3 if ((_level[srcClick+4]._currentframe == 3 || _level[srcClick+4]._currentframe == 4) && srcClickmoveto) { score += 300; } //jumping down left from positions 6 7 8 if ((_level[srcClick+4]._currentframe == 3 || _level[srcClick+4]._currentframe == 4) && srcClickmoveto) { score += 300; } //jumping down right from positions 5 6 7 if ((_level[srcClick+5]._currentframe == 3 || _level[srcClick+5]._currentframe == 4) && srcClickscore) { score = testscore; bestpiece = i; bestposition = i+7; } } if (jumpDown(i+9, true, false)) { testscore = thismovescore(i+9, tempBoard)+bestmove(depth, i+9); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i+9; } } if (moveDown(i+3, true) && !mustjump()) { testscore = thismovescore(i+3, tempBoard)+bestmove(depth, i+3); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i+3; } } if (moveDown(i+4, true) && !mustjump()) { testscore = thismovescore(i+4, tempBoard)+bestmove(depth, i+4); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i+4; } } if (moveDown(i+5, true) && !mustjump()) { testscore = thismovescore(i+5, tempBoard)+bestmove(depth, i+5); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i+5; } } if (jumpUp(i-7, true, false) && pressed != 1) { testscore = thismovescore(i-7, tempBoard)+bestmove(depth, i-7); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i-7; } } if (jumpUp(i-9, true, false) && pressed != 1) { testscore = thismovescore(i-9, tempBoard)+bestmove(depth, i-9); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i-9; } } if (moveUp(i-3, true) && !mustjump() && pressed != 1) { testscore = thismovescore(i-3, tempBoard)+bestmove(depth, i-3); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i-3; } } if (moveUp(i-4, true) && !mustjump() && pressed != 1) { testscore = thismovescore(i-4, tempBoard)+bestmove(depth, i-4); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i-4; } } if (moveUp(i-5, true) && !mustjump() && pressed != 1) { testscore = thismovescore(i-5, tempBoard)+bestmove(depth, i-5); if (testscore>score) { score = testscore; bestpiece = i; bestposition = i-5; } } } _level[i].gotoAndStop(pressed); } //best move has now been determined. move the piece. pieceNumber = bestposition; srcClick = bestpiece; pressed = _level[srcClick]._currentframe; _level[bestpiece].gotoAndStop(5); _level[bestposition].gotoAndStop(pressed); var jumpflag = 0; if ((srcClick+7 == bestposition) || (srcClick+9 == bestposition) || (srcClick-7 == bestposition) || (srcClick-9 == bestposition)) { jumpflag = 1; } //jump down left from positions 2 3 4 if (srcClick%8<=4 && srcClick%8>=2 && (bestposition == srcClick+7)) { _level[srcClick+3].gotoAndStop(5); } else if ((srcClick%8<4 && srcClick%8 != 0 && (bestposition == srcClick+9)) || ((srcClick%8>=6 || srcClick%8 == 0) && (bestposition == srcClick+7))) { //jump down right from 1 2 3 and jump down left from 6 7 0 _level[srcClick+4].gotoAndStop(5); } else if ((srcClick%8>=5) && (bestposition == srcClick+9)) { //jump down right from postition 5 6 7 _level[srcClick+5].gotoAndStop(5); } else if (srcClick%8<=4 && srcClick%8>=2 && (bestposition == srcClick-9)) { //jump up left from positions 2 3 4 _level[srcClick-5].gotoAndStop(5); } else if ((srcClick%8<=3 && srcClick%8 != 0 && (bestposition == srcClick-7)) || ((srcClick%8>=6 || srcClick%8 == 0) && (bestposition == srcClick-9))) { //jump up right from 1 2 3 and jump up left from 6 7 0 _level[srcClick-4].gotoAndStop(5); } else if ((srcClick%8>=5) && (bestposition == srcClick-7)) { //jump up right from postition 5 6 7 _level[srcClick-3].gotoAndStop(5); } srcClick = bestposition; if (((!jumpDown(bestposition+7, true, false) && !jumpDown(bestposition+9, true, false)) && (!jumpUp(bestposition-7, true, false) && !jumpUp(bestposition-9, true, false))) || (jumpflag == 0)) { changeTurn(bestposition); } _root.test = score; _root.halmoving = 0; nowinner = 0; for (var t = 1; t<=32; t++) { if (_level[t]._currentframe == '1' || _level[t]._currentframe == '3') { nowinner = 1; } } if (bestposition == 0 && nowinner != 1 || score == -999999) { winner.gotoAndStop(3); } } function bestmove(depth:Number, moveto:Number) { if (depth<=0) { return 0; } //store original board var inturn:String = turn; var insrcClick:Number = srcClick; var inpressed:Number = pressed; var bestmoveBoard = new Array(33); for (var r = 1; r<=32; r++) { bestmoveBoard[r] = _level[r]._currentframe; } _level[moveto].gotoAndStop(pressed); var jumpflag = 0; if ((srcClick+7 == moveto) || (srcClick+9 == moveto) || (srcClick-7 == moveto) || (srcClick-9 == moveto)) { jumpflag = 1; } //jump down left from positions 2 3 4 if (srcClick%8<=4 && srcClick%8>=2 && (moveto == srcClick+7)) { _level[srcClick+3].gotoAndStop(5); } else if ((srcClick%8<4 && srcClick%8 != 0 && (moveto == srcClick+9)) || ((srcClick%8>=6 || srcClick%8 == 0) && (moveto == srcClick+7))) { //jump down right from 1 2 3 and jump down left from 6 7 0 _level[srcClick+4].gotoAndStop(5); } else if ((srcClick%8>=5) && (moveto == srcClick+9)) { //jump down right from postition 5 6 7 _level[srcClick+5].gotoAndStop(5); } else if (srcClick%8<=4 && srcClick%8>=2 && (moveto == srcClick-9)) { //jump up left from positions 2 3 4 _level[srcClick-5].gotoAndStop(5); } else if ((srcClick%8<=3 && srcClick%8 != 0 && (moveto == srcClick-7)) || ((srcClick%8>=6 || srcClick%8 == 0) && (moveto == srcClick-9))) { //jump up right from 1 2 3 and jump up left from 6 7 0 _level[srcClick-4].gotoAndStop(5); } else if ((srcClick%8>=5) && (moveto == srcClick-7)) { //jump up right from postition 5 6 7 _level[srcClick-3].gotoAndStop(5); } srcClick = moveto; if (((!jumpDown(moveto+7, true, false) && !jumpDown(moveto+9, true, false)) && (!jumpUp(moveto-7, true, false) && !jumpUp(moveto-9, true, false))) || (jumpflag == 0)) { changeTurn(moveto); } else { //since turn doesn't change depth++; } //record the board var tempBoard = new Array(33); for (var s = 1; s<=32; s++) { tempBoard[s] = _level[s]._currentframe; } //board has now been moved and moves are now about to be tested var score = -999999; //initialize to 0 var bestpiece = 0; //initialize to 0 var bestposition = 0; //go through the pieces for (i=1; i<=32; i++) { //variable to flag whether a piece can be moved var moved = 0; //if blue //save the current piece's frame for later pressed = _level[i]._currentframe; var isKing = false; if (pressed == 3 || pressed == 4) { isKing = true; } srcClick = Number(i); //pick up piece ///////// //_level[i].gotoAndStop(5); if ((turn == 'b' && (pressed == 1 || pressed == 3)) || (turn == 'g' && (pressed == 2 || pressed == 4))) { var jumpscore = 999999; if (jumpDown(i+7, true, isKing)) { var testscore = thismovescore(i+7, tempBoard); var recursescore = bestmove(depth-1, i+7); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i+7; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i+7; } } } if (jumpDown(i+9, true, isKing)) { testscore = thismovescore(i+9, tempBoard); var recursescore = bestmove(depth-1, i+9); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i+9; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i+9; } } } if (moveDown(i+3, true) && !mustjump()) { testscore = thismovescore(i+3, tempBoard); var recursescore = bestmove(depth-1, i+3); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i+3; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i+3; } } } if (moveDown(i+4, true) && !mustjump()) { testscore = thismovescore(i+4, tempBoard); var recursescore = bestmove(depth-1, i+4); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i+4; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i+4; } } } if (moveDown(i+5, true) && !mustjump()) { testscore = thismovescore(i+5, tempBoard); var recursescore = bestmove(depth-1, i+5); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i+5; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i+5; } } } if (jumpUp(i-7, true, isKing) && pressed != 1) { testscore = thismovescore(i-7, tempBoard); var recursescore = bestmove(depth-1, i-7); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i-7; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i-7; } } } if (jumpUp(i-9, true, isKing) && pressed != 1) { testscore = thismovescore(i-9, tempBoard); var recursescore = bestmove(depth-1, i-9); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i-9; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i-9; } } } if (moveUp(i-3, true) && !mustjump() && pressed != 1) { testscore = thismovescore(i-3, tempBoard); var recursescore = bestmove(depth-1, i-3); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i-3; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i-3; } } } if (moveUp(i-4, true) && !mustjump() && pressed != 1) { testscore = thismovescore(i-4, tempBoard); var recursescore = bestmove(depth-1, i-4); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i-4; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i-4; } } } if (moveUp(i-5, true) && !mustjump() && pressed != 1) { testscore = thismovescore(i-5, tempBoard); var recursescore = bestmove(depth-1, i-5); if (turn == 'g') { if (jumpscore == 999999 || testscore>jumpscore) { jumpscore = testscore; testscore = testscore*(-1)+recursescore; score = testscore; bestpiece = i; bestposition = i-5; } } else if (turn == 'b') { if (jumpscore == 999999 || (testscore+recursescore)>jumpscore) { jumpscore = recursescore+testscore; score = testscore+recursescore; bestpiece = i; bestposition = i-5; } } } _level[i].gotoAndStop(pressed); } } //reset turn = inturn; srcClick = insrcClick; pressed = inpressed; for (var q = 1; q<=32; q++) { _level[q].gotoAndStop(bestmoveBoard[q]); } return score; }