Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

* * * * * * * * * * * * * * * * * * * * * * * * *

**************************************
//***You must use all the given methods without changing their signature.**
//***However, you may add new methods using the given ones if necessary.***
//*************************************************************************
public class PegSolitaireBoardProfVersion {
protected char[][] pegBoard;
private static final int UP =1;
private static final int DOWN =2;
private static final int LEFT =3;
private static final int RIGHT =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 PegSolitaireBoardProfVersion(){
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';
}
}
}
}
// moves the 'X's and 'O's to the corresponding positions after the jump happens
private boolean moveForward(int x, int y, int direction){
// Implement moveForward here
//*********The error will go away once you implement and returns a boolean
// value*********
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
}
private void addPeg(int x, int y){
pegBoard[x][y]='X';
}
private void removePeg(int x, int y){
pegBoard[x][y]='O';
}
// check if the jump is valid
// checks if the result of the jump will be out of bounds
// checks that the 'X' performing the jump has one more 'X' in between itself
// and the 'O'
private boolean checkMove(int x, int y, int newX, int newY){
// Implement checkMove here
//*********The error will go away once you implement and returns a boolean
// value*********
return false;
}
// getNewX and getNewY get the x and y coordinates of the position where the 'X'
// will occupy after the jump
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(){
// Implement print here
}
// recursive and backtracking algorithm to compute all the possible moves
// loops through the rows and columns of the matrix and tests if a jump in each
// of the 4 directions is a valid move
// if it is a valid move it will perform the move and update the board and store
// it into the array at the corresponding move count
// base case occurs if the move count is equal to 31 AND the centre position is
// still filled with an 'X', otherwise it calls the recursive method and moves
// on to the next move
// if the base case is not reached, than the backtracking method is called and
// tries a new jump direction
public boolean findSolution(int move){
// Implement findSolution here
//*********The error will go away once you implement and returns a boolean
// value*********
return false;
}
}
considering this code, help me implement the peg solitaire so the solution can look like this:-
/['','','0','0','0','',''],
//['','','0','0','0','',''],
//['0','0','0','0','0','0','0'],
//['0','0','0','X','0','0','0'],
//['0','0','0','0','0','0','0'],
//['','','0','0','0','',''],
//['','','0','0','0','','']
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions