Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class PegSolitaireBoard { protected char [ ] [ ] pegBoard; private static final int UP = 1 ; private static final int DOWN =

public class PegSolitaireBoard {
protected char[][] pegBoard;
private static final int UP =1;
private static final int DOWN =2;
private static final int RIGHT =3;
private static final int LEFT =4;
// Initializes the board at its starting position
//['','','X','X','X','',''],
//['','','X','X','X','',''],
//['X','X','X','X','X','X','X'],
//['X','X','X','O','X','X','X'],
//['X','X','X','X','X','X','X'],
//['','','X','X','X','',''],
//['','','X','X','X','','']
public PegSolitaireBoard(){
this.pegBoard = new char[7][7];
for (int i =0; i <7; i++){
for (int j =0; j <7; j++){
if (i <2 && j <2){
this.pegBoard[i][j]='';
} else if (i <2 && j >4){
this.pegBoard[i][j]='';
} else if (i >4 && j <2){
this.pegBoard[i][j]='';
} else if (i >4 && j >4){
this.pegBoard[i][j]='';
} else if (i ==3 && j ==3){
this.pegBoard[i][j]='O';
} else {
this.pegBoard[i][j]='X';
}
}
}
}
private boolean moveForward(int x, int y, int direction){
// Implement moveForward here
int newX = getNewX(x, direction);
int newY = getNewY(y, direction);
if (checkMove(x, y, newX, newY) && pegBoard[newX][newY]=='X' && pegBoard[x][y]=='O'){
System.out.println("*********** ADD ***********");
System.out.println("from ("+ newX +","+ newY +") to ("+ x +","+ y +")");
addPeg(x, y);
removePeg((newX + x)/2,(newY + y)/2);
removePeg(newX, newY);
print();
System.out.println("*********** ADD END ***********
");
return true;
}
return false;
}
// moves the 'X's and 'O's back to their initial position before the jump
// occurred
private void moveBackward(int x, int y, int direction){
// Implement moveBackward here
int newX = getNewX(x, direction);
int newY = getNewY(y, direction);
int midX =(newX + x)/2;
int midY =(newY + y)/2;
// if (newX <0|| newX >=7|| newY <0|| newY >=7){
//}
System.out.println("*********** UNDO ***********");
System.out.println("undo from ("+ newX +","+ newY +") to ("+ x +","+ y +")");
print();
addPeg(newX, newY);
addPeg(midX, midY);
removePeg(x, y);
print();
System.out.println("*********** UNDO END ***********
");
}
private void addPeg(int x, int y){
pegBoard[x][y]='X';
}
private void removePeg(int x, int y){
pegBoard[x][y]='O';
}
private boolean checkMove(int x, int y, int newX, int newY){
// Implement checkMove here
// checks if the result of the jump will be out of bounds
if (newX <0|| newX >=7|| newY <0|| newY >=7){
return false;
}
// checks that the 'X' performing the jump has one more 'X' in between itself
int midX =(x + newX)/2;
int midY =(y + newY)/2;
if (pegBoard[midX][midY]!='X'|| pegBoard[newX][newY]==''){
return false;
}
return true;
}
private boolean isValidMove(int x, int y, int direction){
int newX = getNewX(x, direction);
int newY = getNewY(y, direction);
return checkMove(x, y, newX, newY) && pegBoard[newX][newY]=='O' && pegBoard[x][y]=='X';
}
private int getNewX(int x, int direction){
int newX = x;
switch (direction){
case RIGHT:
newX = newX +2;
break;
case LEFT:
newX = newX -2;
}
return newX;
}
private int getNewY(int y, int direction){
int newY = y;
switch (direction){
case UP:
newY = newY +2;
break;
case DOWN:
newY = newY -2;
}
return newY;
}
private void print(){
for (char[] row : pegBoard){
for (char cell : row){
System.out.print(cell +"");
}
System.out.println();
}
System.out.println();
}
public boolean findSolution(int move){
if (move ==32 && pegBoard[3][3]=='X'){
System.out.println("Done!");
print();
return true;
} else {
for (int horizontal =0; horizontal < pegBoard.length; horizontal++){
for (int vertical =0; vertical < pegBoard.length; vertical++){
int x = vertical;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions

Question

understand the restrictions of top-down job redesign approaches;

Answered: 1 week ago