Question: 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 ;
private static final int DOWN ;
private static final int RIGHT ;
private static final int LEFT ;
Initializes the board at its starting position
XXX
XXX
XXXXXXX
XXXOXXX
XXXXXXX
XXX
XXX
public PegSolitaireBoard
this.pegBoard new char;
for int i ; i ; i
for int j ; j ; j
if i && j
this.pegBoardij;
else if i && j
this.pegBoardij;
else if i && j
this.pegBoardij;
else if i && j
this.pegBoardij;
else if i && j
this.pegBoardijO;
else
this.pegBoardijX;
private boolean moveForwardint x int y int direction
Implement moveForward here
int newX getNewXx direction;
int newY getNewYy direction;
if checkMovex y newX, newY && pegBoardnewXnewYX && pegBoardxyO
System.out.println ADD ;
System.out.printlnfrom newX newY to x y ;
addPegx y;
removePegnewX xnewY y;
removePegnewX newY;
print;
System.out.println ADD END
;
return true;
return false;
moves the Xs and Os back to their initial position before the jump
occurred
private void moveBackwardint x int y int direction
Implement moveBackward here
int newX getNewXx direction;
int newY getNewYy direction;
int midX newX x;
int midY newY y;
if newX newX newY newY
System.out.println UNDO ;
System.out.printlnundo from newX newY to x y ;
print;
addPegnewX newY;
addPegmidX midY;
removePegx y;
print;
System.out.println UNDO END
;
private void addPegint x int y
pegBoardxyX;
private void removePegint x int y
pegBoardxyO;
private boolean checkMoveint x int y int newX, int newY
Implement checkMove here
checks if the result of the jump will be out of bounds
if newX newX newY newY
return false;
checks that the X performing the jump has one more X in between itself
int midX x newX;
int midY y newY;
if pegBoardmidXmidYX pegBoardnewXnewY
return false;
return true;
private boolean isValidMoveint x int y int direction
int newX getNewXx direction;
int newY getNewYy direction;
return checkMovex y newX, newY && pegBoardnewXnewYO && pegBoardxyX;
private int getNewXint x int direction
int newX x;
switch direction
case RIGHT:
newX newX ;
break;
case LEFT:
newX newX ;
return newX;
private int getNewYint y int direction
int newY y;
switch direction
case UP:
newY newY ;
break;
case DOWN:
newY newY ;
return newY;
private void print
for char row : pegBoard
for char cell : row
System.out.printcell ;
System.out.println;
System.out.println;
public boolean findSolutionint move
if move && pegBoardX
System.out.printlnDone;
print;
return true;
else
for int horizontal ; horizontal pegBoard.length; horizontal
for int vertical ; vertical pegBoard.length; vertical
int x vertical;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
