Пасьянс с привязкой не всегда может найти путь

#java

Вопрос:

Я разрабатываю программное обеспечение, которое должно решать некоторые типы пасьянсов с привязкой, и печатаю шаги для их решения таким образом: [3,3,»^»],[4,5,»:

 XXXXXXXXX  XXXXXXXXX  XX     XX  XX     XX  XXXXXXXXX  XXXXXXXXX  

программа не может найти ответ, я пробую какую-то базовую плату и эту работу, но после 3 или 4 шагов программа не может найти путь. Я пытаюсь отключить код, но не могу понять, почему он не может найти путь

мой колышек:

 package com.company;   public class PegSolver {  public int peg=0;  String answer[];  public boolean superLevel=false;  public void start(){   boolean makefrance=false;  boolean lastLevel = false;   //france  String end,start;  //the starting board  start="OOOOOOOOO", "OOOOOOOOO", "OO OO", "OO O.", "OO OO", "OOOOOOOOO", "OOOOOOOOO"";  //the wanted board  end = " OOOOOOOOO", "OOOOOOOO.", "OO OO", "OO O.", "OO OO", "OOOOOOOO.", "OOOOOOOO."] ";   //clean the Strings, let only 'O' or '.'  start = removeUnwanted(start);  end = removeUnwanted(end);  //adapt the board to normal, france, or other by the number of char in the string after remove the unwanted chars  if(start.length()==33)  {  makefrance=false;  lastLevel=false;  }  else if(start.length()==37)  {  makefrance = true;  lastLevel = false;  }  else if(start.length()==30){  makefrance=false;  lastLevel=true;  }  else if(start.length()==48){  superLevel=true;  }   int startDots=0;  int endDots=0;  Board board;  Board destinationBoard;  if(lastLevel)  {  board = new Board(start,true,true,superLevel);//The start of my board  destinationBoard = new Board(end,true,true,superLevel);//the wanted destination board, '.' for no peg and O for peg.  }  else{  board = new Board(start,makefrance,lastLevel,superLevel);//The start of my board  destinationBoard = new Board(end,makefrance,lastLevel,superLevel);//the wanted destination board, '.' for no peg and O for peg.  }    //count the pegs from the begining and ending board  for(int i=0;ilt;start.length()-1;i  ) {  if (start.charAt(i) == '.') {  startDots  ;  }  }  for(int i=0;ilt;end.length()-1;i  ) {  if (end.charAt(i) == '.') {  endDots  ;  }  }  //calculate the number of turns to the destination, every turn only one peg is disapear so finally there is:  //pegs at the end - pegs at the begining = number of turns  peg=endDots-startDots;  //create a string for save the steps.  answer = new String[peg];   //if we have a result, return the answer and print a text  if(Solve(board,destinationBoard,0))  {  System.out.println("The best day of my life");  for(int i=0;ilt;answer.length;i  )  {  System.out.println(answer[i]);  }   }  //if don't find path, print WTF  else{  System.out.println("WTFFFFF");  }  }  //recurse method that solve the peg solitaire  public boolean Solve(Board board, Board destination, int turn)  {  int rows=0;  //if we are on the end of the game  if(turn==peg)  {  //check if the current board is like the destination board.  if(board.isFinished(destination))  {  System.out.println("found the path");  return true;//this board is the same as the desitnation  }  else  {  return false;//this board is not the same as the desitnation  }  }  else {   if (superLevel)//if its a bigger board, need to check the board on more rows and lines  {  rows=9;  }  else{  rows=7;  }  for(int i=0;ilt;rows;i  ){  for(int j=0;jlt;rows;j  )  {  if(board.isLegal(j,i))  {  if(board.canTurn(j,i,"right"))//test if it's possible to turn right  {  if(Solve(board.turn(board,j,i,"right"),destination,turn 1))//create a new board after changment of direction and recurse  {  answer[turn]= "[" j "," i ","gt;"],";//add this step to the answer  return true;  }  }  if(board.canTurn(j,i,"up"))//test if it's possible to turn up  {  if(Solve(board.turn(board,j,i,"up"),destination,turn 1))//create a new board after changment of direction and recurse  {  answer[turn]= "[" j "," i ","^"],";//add this step to the answer  return true;  }  }   if(board.canTurn(j,i,"down"))//test if it's possible to turn down  {  if(Solve(board.turn(board,j,i,"down"),destination,turn 1))//create a new board after changment of direction and recurse  {  answer[turn]= "[" j "," i ","v"],";//add this step to the answer  return true;  }  }  if(board.canTurn(j,i,"left"))//test if it's possible to turn left  {  if(Solve(board.turn(board,j,i,"left"),destination,turn 1))//create a new board after changment of direction and recurse  {  answer[turn]= "[" j "," i ","lt;"],";//add this step to the answer  return true;  }  }  }  }  }  return false;//if don't find any path return.  }   }  public String removeUnwanted(String boardText)  {  String returnBoard="";  for(int i=0; ilt;boardText.length();i  )  {  if(boardText.charAt(i) == '.' || boardText.charAt(i)== 'O')//remove all the char that are not '.' or 'O'  {  returnBoard  = boardText.charAt(i);  }  }  System.out.println(returnBoard);  return returnBoard;  } }  

и мой класс по правлению:

 package com.company;   import java.security.PublicKey;   public class Board {  //the array that storage the board data  private int[][] board = new int[9][9];;  public boolean superLvl=false;  int pegs = 0;   public Board(String place, boolean makefrance,boolean lastLevel,boolean superlevel) {  int boardx,boardy;  //add 2 to make the board compatible to the string  if(superlevel) {  superLvl=true;  boardx = 9;  boardy = 9;  int[][] temp = new int[][]{{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,2,2,2,2,2,0,0},{0,0,2,2,2,2,2,0,0},{0,0,2,2,2,2,2,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{2,2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2,2}};  for(int i=0;ilt;9;i  )  {  for(int j=0;jlt;9;j  )  {  board[j][i]=temp[i][j];  }  }  }  else {   if (!lastLevel) {  boardx = 7;  boardy = 7;  board[0][0] = 2;  board[1][0] = 2;  board[5][0] = 2;  board[6][0] = 2;  board[0][1] = 2;  board[1][1] = 2;  board[5][1] = 2;  board[6][1] = 2;  board[0][5] = 2;  board[1][5] = 2;  board[5][5] = 2;  board[6][5] = 2;  board[0][6] = 2;  board[1][6] = 2;  board[5][6] = 2;  board[6][6] = 2;  if (makefrance) {  board[1][1] = 1;  board[1][5] = 1;  board[5][1] = 1;  board[5][5] = 1;  }  } else {  boardx = 6;  boardy = 6;  board[0][6] = 2;  board[1][6] = 2;  board[2][6] = 2;  board[3][6] = 2;  board[4][6] = 2;  board[5][6] = 2;  board[6][6] = 2;  board[0][5] = 2;  board[1][5] = 2;  board[2][5] = 2;  board[3][5] = 2;  board[4][5] = 2;  board[5][5] = 2;  board[6][5] = 2;  board[6][0] = 2;  board[6][1] = 2;  board[6][2] = 2;  board[6][3] = 2;  board[6][4] = 2;  }  }       int loca = 0;//location on the string of place  //add 0 or 1 to the table  for (int i = 0; i lt; boardy; i  ) {  for (int j = 0; j lt; boardx; j  ) {  if (board[j][i] != 2) {  if (place.charAt(loca) == 'O') {  loca  ;  board[j][i] = 1;  pegs  ;  } else if (place.charAt(loca) == '.') {  loca  ;  board[j][i] = 0;  }  }  }  }   System.out.println();  this.printBoard();   }  public Board(Board newone)  {  int placement=7;  if(superLvl)  {  placement=9;  }  for(int i=0;ilt;placement;i  )  {  for(int j=0;jlt;placement;j  )  {  board[i][j]= newone.getValue(i,j);  }  }   }   //test if this board is like the other board at the end of the game  public boolean isFinished(Board destination) {  int placement=7;  if(superLvl)  {  placement=9;  }  for (int i = 0; i lt; placement; i  ) {  for (int j = 0; j lt; placement; j  ) {  if (this.getValue(j,i)!=destination.getValue(j,i))  {  return false;  }  }  }  return true;  }  //get the value of this coordinate.  public int getValue(int x, int y)  {  return board[x][y];  }  //test if this cooardiante is a place on the board(not 2)  public boolean isLegal(int x,int y)  {  if(board[x][y]!=2)  {  return true;  }  else{  return false;  }  }  //test if can turn- if this place is 1 and the next is 1 and the second next is 0  //also protect from stack overflow  public boolean canTurn(int x,int y,String direction)  {  if(direction.equals("right"))  {  if(xlt;5){   if(this.getValue(x,y)==1 amp;amp; this.getValue(x 1,y)==1 amp;amp; this.getValue(x 2,y)==0)  {  return true;  }  else {  return false;  }  }  else  {  return false;  }  }  else if(direction.equals("left"))  {  if(xgt;1){  if(this.getValue(x,y)==1 amp;amp; this.getValue(x-1,y)==1 amp;amp; this.getValue(x-2,y)==0)  {  return true;  }  else {  return false;  }  }  else  {  return false;  }  }  else if(direction.equals("up"))  {  if(ygt;1){   if(this.getValue(x,y)==1 amp;amp; this.getValue(x,y-1)==1 amp;amp; this.getValue(x,y-2)==0)  {  return true;  }  else {  return false;  }  }  else  {  return false;  }  }  else if(direction.equals("down"))  {  if(ylt;5){  if(this.getValue(x,y)==1 amp;amp; this.getValue(x,y 1)==1 amp;amp; this.getValue(x,y 2)==0)  {  return true;  }  else {  return false;  }  }  else  {  return false;  }  }  else{  System.out.println("method canTurn on board get a wrong direction string");  return false;  }  }  //make the move and return a "new" board for saving the originial one.  public Board turn(Board oldBoard,int x, int y ,String direction)  {  Board board = new Board(oldBoard);  if(direction.equals("right"))  {  board.setValue(x,y,0);  board.setValue(x 1,y,0);  board.setValue(x 2,y,1);  return board;  }  else if(direction.equals("left"))  {  board.setValue(x,y,0);  board.setValue(x-1,y,0);  board.setValue(x-2,y,1);  return board;  }  else if(direction.equals("up"))  {  board.setValue(x,y,0);  board.setValue(x,y-1,0);  board.setValue(x,y-2,1);  return board;  }  else if(direction.equals("down"))  {  board.setValue(x,y,0);  board.setValue(x,y 1,0);  board.setValue(x,y 2,1);  return board;  }  else{  System.out.println("method Turn on board get a wrong direction string");  return null;  }  }  //change the value of the wanted board.  public void setValue(int x,int y,int value)  {  board[x][y]=value;  }  //print the board, made for tests.  public void printBoard()  {  int placement=7;  if(superLvl)  {  placement=9;  }  for(int i=0;ilt;placement;i  )  {  for(int j=0;jlt;placement;j  )  {  System.err.print(board[j][i]);  }  System.err.println();  }  System.err.println("------------------");  }  }  

Комментарии:

1. Я нахожу некоторые ошибки , поэтому меняю свой код, но все равно в большинстве случаев он не работает.

2. найдите решение, это происходит из-за моего кода в классе платы в методе can turn, я пытаюсь защитить себя от переполнения стека и забываю изменить размер моей платы там, где if(xlt;5){ на if(x