我对编程和使用CS 143还是很陌生。我正在从事一个名为Peg Solitaire的课堂项目。钉纸牌有两种方法,都是并且需要是采用布尔2d数组的String方法。第二种方法更多地是根据指令的“帮助”方法。我相信我拥有所需的大多数代码,以获得13 33 /的期望输出。但是,我缺少一些无法启动我的移动回溯器的东西。由于我没有任何解决办法。大概是某个时候,我是,但这是错误的解决方案。我的教授使用一个测试程序来测试我们代码的功能。因此,测试仪程序将使用不同类型的电路板进行测试,并测试钉子上下,左右移动的情况。因此,这些确实是我需要确保程序能够执行的唯一动作。我完全被困住了。将这段代码花了两周时间后,我无法执行任何操作。教授从我那里得到了一些帮助,帮助了我到达了现在的位置,但是我现在无法找出任何其他方法可以返回解决方案。任何帮助或建议,不胜感激!感谢您抽出宝贵的时间来查看我的代码。
尝试使用递归方法,但最终不得不使用for循环。
public class PegSolitaire {
public static String solve(boolean[][] pegs) {
// For find/replace go to edit and ctl + f
// Checks that peg does not/did not jump/is not out of bounds area
if (pegs[0][0]==true||pegs[1][0]==true||pegs[0][1]==true||pegs[1][1]==true||pegs[5][0]==true||
pegs[5][1]==true|| pegs[6][0]==true||pegs[6][1]==true||pegs[0][5]==true||pegs[0][6]==true||
pegs[1][6]==true||pegs[1][5]==true||pegs[5][5]==true||pegs[6][5]==true||pegs[5][6]==true||
pegs[6][6]== true) {
throw new RuntimeException("This should never happen.");
}
// External solution ?
//return solve(pegs, 1,3,2,3,3,3);
return solve(pegs, -1,0,0,0,0,0); // "kick things off"
//return solve(pegs, 0,0,0,0,3,3);
//return solve(pegs, 1,3,0,0,3,3);
}
/**
*
* @param pegs Uses [x][y] indexing
* @param startX pegs starting location on x axis
* @param startY pegs starting location on y axis
* @param jumpX peg skips over one index on x axis
* @param jumpY peg skips over one index on y axis
* @param endX location where peg lands on x axis
* @param endY location of where peg lands on y axis
* @remPegs keeps track of pegs removed from the board.
* @return false if peg makes a bad move or true if peg makes legal move
*/
private static String solve(boolean[][] pegs, int startX,int startY,int jumpX,int jumpY, int endX,int endY) {
int remPegs = 0;
// Check to make sure that proposed move is legal.
// // Make sure the endX, endY coordinate is inside the board.
//
// if (endX < 0 || endY < 0 || endX > pegs.length || endY > pegs[0].length) {
// return null;
// }
if(pegs[endX][endY]==false) {
return null; // Bail out: can't land here!!!
}
// If we're not in the "Kick things off" mode, check and make the move.
String makeMove = " ";
if (remPegs==0) {
// Make the proposed move.
makeMove = " "+startX+" "+startY+" "+endX+" "+endY+" ";
pegs[startX][startY] = false;
pegs[jumpX][jumpY] = false;
pegs[endX][endY] = true;
return makeMove;
// } else {
}
// Look for all next moves.
String solution = " "; // solution is null since there is no known solutions assigned.
// Look at each peg in the board and try to move it in all four directions.
for (int x = 0; x<pegs.length;x++) {
for (int y=0; y<pegs[0].length;y++) {
// use the x y coordinate from the loop to move the peg in four directions.
if (pegs[x][y] == true) {
remPegs = 1;
solution = solve(pegs, x, y, x+1, y, x+2, y); // move to the right
if (remPegs != 0) return solution+makeMove;
solution = solve(pegs, x, y, x-1, y, x-2, y);// move to the left
if (remPegs != 0) return solution+makeMove;
solution = solve(pegs, x, y, x, y+1, x, y+2); // move up
if (remPegs != 0) return solution+makeMove;
solution = solve(pegs, x, y, x, y-1, x, y-2); // move down
if (remPegs != 0)return makeMove+solution; // All possible moves/solutions have been assigned to the solution variable
}
}
}
// Check to see if puzzle is solved (how???)
// Count the pegs and make sure there is only one left
boolean onlyOnePeg = true; // FIX????
// in addition, it's in the middle.
if(onlyOnePeg && pegs[3][3] == false) {
makeMove = " "+startX+" "+startY+" "+endX+" "+endY+" ";
return makeMove+onlyOnePeg; // If solved, game is won!
}
// Resets move
pegs[startX][startY] = true;
pegs[jumpX][jumpY] = true;
pegs[endX][endY] = false;
return null;
}
}
//" "+startX+" "+startY+" "+endX+" "+endY+" "
//if(pegs[startX][startY]==true||pegs[jumpX][jumpY]==true||pegs[endX][endY]==false) {
//return ""+startX+""+startY+""+endX+""+endY+""; // returns null if move is not legal
//return null;
//startX+=2;
//startY+=3;
//endX+=3;
//endY+=3;
//
//makeMove = " "+startX+" "+startY+" "+endX+" "+endY+" ";
//return makeMove;
//Backtracking recursive peg move which moves peg left, right, up, or down
//if ((solution = solve(pegs, startX, startY, jumpX+1, jumpY, endX+2, endY)) != null ||
//(solution = solve(pegs, startX, startY, jumpX-1, jumpY, endX-2, endY)) != null ||
//(solution = solve(pegs, startX, startY, jumpX, jumpY+1, endX, endY+2)) != null ||
//(solution = solve(pegs, startX, startY, jumpX, jumpY-1, endX, endY-2)) != null) {
需要最终返回的解决方案看起来像“ 1 3 3 3”。