6 for opening the red pawn 5 for moving forward then it gets stuck at 3我使用了ludo的示例代码,并对其进行了修改以使其自动化。它适用于手动运动,但输出异常,即自动运动时错误。在给定的代码中,第一个功能是可以自动移动,而第二个功能是用户必须决定移动哪个棋子。
function randomNum() {
var cleanText = document.getElementById('badtext');
cleanText.innerText = "";
currcolortext = document.getElementById('player');
currcolor = currcolortext.innerText;
var Colorr = currcolor;
if (!clicked) {
num = Math.floor((Math.random() * 6) + 1);
var dice = document.getElementById('dice');
dice.style.backgroundImage = "url(photos/" + num + ".jpg)";
clicked = true;
}
if (DontHaveOtherFree()) {
currpawn = currcolor + "pawn" + 1;
}
else if (onboard[currcolor + "pawn" + 1] === 0) {
currpawn = currcolor + "pawn" + 2;
}
else {
currpawn = currcolor + "pawn" + 1;
}
currPos = positions[currpawn];
if (num != 6&&DontHaveOtherFree()) {
var bad = document.getElementById('badtext');
bad.innerText = "Unfortunately you stuck";
window.setTimeout(changePlayer, 1000);
clicked = false;
}
else if ((num === 6 && DontHaveOtherFree()) || (OnlyOneIsFree() && num != 6) || (pawnOut(currcolor) == 1)) {
if (num + currPos > 28){
stuck();
}
else {
if (clicked) {
var position = currPos;
{
if (onboard[currpawn] === 1 || num === 6) {
if (onboard[currpawn] === 0) {
var doc = document.getElementById(currpawn);
var curr = Number(doc.style.left.replace(/[a-z]/g, ''));
switch (Colorr) {
case "red":
doc.style.left = 327 + 'px';
doc.style.top = 109 + "px";
break;
case "blue":
doc.style.left = 866 + 'px';
doc.style.top = 648 + "px";
break;
}
onboard[currpawn] = 1;
positions[currpawn] = currPos;
var victim = HaveHover();
if (victim != false) {
ResetPawn(victim);
}
var bad = document.getElementById('badtext');
bad.innerText = "";
window.setTimeout(dice_img, 1000);
}
else {
switch (Colorr) {
case "red":
for (i = currPos; i < position + num; i++) {
stepsRed[i]();
}
break;
case "blue":
for (i = currPos; i < position + num; i++) {
stepsBlue[i]();
}
break;
}
positions[currpawn] = currPos;
var victim = HaveHover();
if (victim != false) {
ResetPawn(victim);
}
if (currPos == 28) { pawnOut[currcolor]++; onboard[currpawn] = 0; positions[currpawn] = 0; document.getElementById(currpawn).style.visibility = "hidden"; }
CheckForWinner();
var bad = document.getElementById('badtext');
bad.innerText = "";
window.setTimeout(changePlayer, 1000);
}
num = 0;
clicked = false;
}
else {
Stuck();
}
}
}
}
}
/*if (num + currPos > 28) {
Stuck();
}
else {
if (clicked) {
var position = currPos;
if (currcolortext.innerText == currcolor) {
if (positions[currcolor + "pawn" + 1] == positions[currcolor + "pawn" + 2]) {
switch (currcolor) {
case "red":
for (i = currPos; i < position + num; i++) {
stepsRed[i]();
}
break;
case "blue":
for (i = currPos; i < position + num; i++) {
stepsBlue[i]();
}
break;
}
positions[currpawn] = currPos;
var victim = HaveHover();
if (victim != false) {
ResetPawn(victim);
}
if (currPos == 28) { pawnOut[currcolor]++; onboard[currpawn] = 0; positions[currpawn] = 0; document.getElementById(currpawn).style.visibility = "hidden"; };
CheckForWinner();
window.setTimeout(changePlayer, 1000);
num = 0;
}
}
}
}*/
}
function randomMove(Color, paw) {
var text = document.getElementById('player');
NumOfPaw = paw;
currcolor = Color;
currpawn = currcolor + "pawn" + NumOfPaw;
currPos = positions[currpawn];
if (num + currPos > 28) {
Stuck();
}
else {
if (clicked) {
var position = currPos;
if (text.innerText == currcolor) {
if ((num === 6 && OnlyOneIsFree()) || (!OnlyOneIsFree() && !DontHaveOtherFree()) ) {
if (onboard[currpawn] === 1 || num === 6) {
if (onboard[currpawn] === 0) {
var doc = document.getElementById(currpawn);
var curr = Number(doc.style.left.replace(/[a-z]/g, ''));
switch (Color) {
case "red":
doc.style.left = 327 + 'px';
doc.style.top = 109 + "px";
break;
case "blue":
doc.style.left = 866 + 'px';
doc.style.top = 648 + "px";
break;
}
onboard[currpawn] = 1;
positions[currpawn] = currPos;
var victim = HaveHover();
if (victim != false) {
ResetPawn(victim);
}
}
else {
switch (Color) {
case "red":
for (i = currPos; i < position + num; i++) {
stepsRed[i]();
}
break;
case "blue":
for (i = currPos; i < position + num; i++) {
stepsBlue[i]();
}
break;
}
positions[currpawn] = currPos;
var victim = HaveHover();
if (victim != false) {
ResetPawn(victim);
}
if (currPos == 28) { pawnOut[currcolor]++; onboard[currpawn] = 0; positions[currpawn] = 0; document.getElementById(currpawn).style.visibility = "hidden"; }
CheckForWinner();
changePlayer();
}
num = 0;
clicked = false;
var dice = document.getElementById('dice');
dice.style.backgroundImage = "url(photos/dice.gif)";
}
else Stuck();
}
}
}
}
}
当棋子必须改变方向时会出现问题。例如,如果骰子显示5,而我们必须向前走2步,然后向右走3步,那么在经过两步后它将停止。