Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with moveAmphibian. Posted below is my code for the class FrogGame a

ID: 3767957 • Letter: I

Question

I need help with moveAmphibian. Posted below is my code for the class FrogGame along with the classes Pad and Pond

FrogGame

/**
* The FrogGame class keeps track of the current state
* of a Frogs and Toads game (which consists of a Pond
* object and where the vacant pad is). It also has
* methods for handling the game rules, such as
* validMove, moveStillPossible, and moveAmphibian.
*
* @author
* @date 2015.11.19 created
*
*/

public class FrogGame
{
private Pond gameBoard; // the playing surface of our game
private int size; // the number of Pads in a row or column of the gameBoard
/**
* The FrogGame class keeps track of the current state of a
* Frogs and Toads game (which consists of a Pond object and
* where the vacant pad is). It also has methods for handling
* the game rules, such as validMove, moveStillPossible, and
* moveAmphibian.
*/
public FrogGame(int size) throws Exception
{   
gameBoard = new Pond(size);
this.size = size;
}

/**
* Indicates whether or not the Pond gameBoard is solved
*
* @return true if the puzzle is solved and false otherwise.
*/
public boolean isSolved()
{
return gameBoard.isSolved(); // use the isSolved for the Pond class
}

/**
* Returns the pond object associated with this FrogGame object.
*
*
*/
public Pond getPond()
{
return gameBoard;   
}

/**
* This method returns the status of the pad at the given coordinates
* (either FROG, TOAD, VACANT, OR INVALID). If the given row or column
* is out of bounds, this method returns INVALID.
*
* @param row The first row is row 0
* @param column the first coulmn is column 0
* @return status of Pad located at [row][coulmn]
*/
public int getStatusOfPad(int row, int column)
{
Pad p = gameBoard.getPad(row, column);
int status = p.getStatus();
return status;
}

/**
* Returns true if the given coordinates represent a Pad that has
* an amphibian that can make a legal move (hint: you may want to
* use the statusOfPad method to make your job easier). In order
* to be legal, the jump to the vacant pad must be in one of the
* two correct directions for that type of amphibian, and the vacant
* pad must either be adjacent or two pads away with an amphibian of
* the opposite type in between. Note that row/column pairs that
* represent locations outside the boundaries of the game are never
* a legal move.
*
* @param row
* @param column
* @return True if the given row and column constitute a legal move and false otherwise.
*
*/
public boolean validMove(int row, int column)
{
if (getStatusOfPad(row,column) == Pad.FROG)
{
// FROG can move to right one
if (getStatusOfPad(row,column+1) == Pad.VACANT)
return true;
// FROG can jump over TOAD to right
if (getStatusOfPad(row,column+1) == Pad.TOAD && getStatusOfPad(row,column+2) == Pad.VACANT)
return true;
// FROG can move up one
if (getStatusOfPad(row-1,column) == Pad.VACANT)
return true;
// FROG can jump over TOAD moving up
if (getStatusOfPad(row-1,column) == Pad.TOAD && getStatusOfPad(row-2,column) == Pad.VACANT)
return true;
}
else if (getStatusOfPad(row,column) == Pad.TOAD)
{
// FROG can move to left one
if (getStatusOfPad(row,column-1) == Pad.VACANT)
return true;
// FROG can jump over TOAD to left
if (getStatusOfPad(row,column-1) == Pad.TOAD && getStatusOfPad(row,column-2) == Pad.VACANT)
return true;
// FROG can move up down
if (getStatusOfPad(row+1,column) == Pad.VACANT)
return true;
// FROG can jump over TOAD moving down
if (getStatusOfPad(row+1,column) == Pad.TOAD && getStatusOfPad(row+2,column) == Pad.VACANT)
return true;
}
else
{
// (row, column) is not FROG or TOAD
// return false;
}

// if the selected Pad is a FROG
// if the Pad to the right is Vacant, or the Pad above is Vacant
// then the move is valid.
// else if the Pad to the right is a TOAD and
// and the Pad to the right of the TOAD is vacant the move is valid
// else if the Pad above is a TOAD and
// the Pad above that is vacant the move is valid
// else the move is invalid
// else if the selected Pad is a TOAD
// if the Pad to the left is vacant, or the Pad below is Vacant,
// then the move is valid.
// else if the Pad to the left is a FROG and
// the Pad to the left of the FROG is vacant the move is valid
// else if the Pad below is a FROG and
// the Pad below that is vacant the move is valid
// else the move is invalid
// else the move is not valid
return false;
}

/**
* Moves the amphibian at given location to the vacant lily pad.
* This method may assume as a precondition that the move provided
* is actually a legal move.
* (Hint: 1. Find the VACANT Pad.
* 2. The VACANT Pad becomes either FROG or TOAD.
* 3. The Pad at [row][col] now becomes VACANT.)
*/
public void moveAmphibian(int row, int column)
{

}

// first check that the move is a valid move.
// find the VACANT Pad, the VACANT Pad is changed to FROG or TOAD
// The specified Pad is changed to VACANT.

/**
* Indicates whether or not a legal move is still possible
* (Hint: you may want to utilize the validMove method).
* @return True
*/
public boolean moveStillPossible()
{
for(int row=0; row {
for(int col=0; col {
if(validMove(row, col) == true);
return true;
}
}
return false;
}

public void print(){
System.out.println(gameBoard);
}

}

Pad

/**
* The Pad class is used to represent individual lily pads in the
* pond. Each pad keeps track of its starting status and it's
* status as the the game goes on (VACANT, FROG, TOAD, or INVALID
* - these are public symbolic constantsa that will also be used in
* other classes). Among other things, each Pad object needs to be
* capable of determining whether or not it is in the solved-puzzle
* configuration (ignoring all the other lily pads).
*/

public class Pad
{
// add the remaining 3 constants WITH JAVADOCS descriptors
/** Symbolic constant representing a lilypad with a frog */
public static final int FROG = 1;
/** Symbolic constant representing a lilypad with a toad */
public static final int TOAD = 2;
/** Symbolic constant representing a lilypad with a vacant lilypad */
public static final int VACANT = 3;
/** Symbolic constant representing a lilypad with a invalid lilypad */
public static final int INVALID = 4;

// INSTANCE VARIABLES
private int currentState; // the current state of lilypad
private int startState; // The original state of lilypad

/**
* Creates a new lily pad with a given initial state (the state
* when the game begins).
* @param startState - the starting state of the lilypad (FROG, TOAD, VACANT or INVALID)
*/
public Pad(int startState)
{
// initialise instance variables
this.currentState = startState;
this.startState = startState;
}

/**
* Returns the current state of the lily pad; either VACANT, TOAD, FROG, or INVALID.
*
* @return the currentState
*/
public int getStatus()
{
return currentState;
}

/**
* Returns true if this lily pad is in the correct state for the solved puzzle and
* false otherwise.
*/
public boolean isSolved()
{ // use the following algorithm:
// if the startState is FROG and the current state is TOAD return true
// else if the startState is TOAD and the current state is FROG return true
// else if the startState is VACANT and the current state is VACANT return true
// otherwise ...
if (startState == FROG && currentState == TOAD)
{
return true;
}
if (startState == TOAD && currentState == FROG)
{
return true;
}
if (startState == VACANT && currentState == VACANT)
{
return true;
}
return false;
}

/**
* This method restores the lily pad to the state it was in when originally constructed.
*/
public void reset()
{
currentState = startState;
}

/**
* Changes the current state of the lily pad. If the status is invalid (i.e., not VACANT, TOAD, or FROG), then setStatus sets the status of the lily pad to INVALID.
*/
public void setStatus(int status)
{
if (status == TOAD || status == FROG || status == VACANT)
{
currentState = status;
}
else
{
currentState = INVALID;
}
}

/**
* Returns the string representation of the lily pad.
*/
public String toString()
{
if (currentState == FROG)
return "F";
// add symbols for the other possibilities
else if (currentState==TOAD) return "T";
else if (currentState==VACANT) return " ";
else // must be INVALID
return "#";
}
}

Pond


/**
* This class represents a grid of lily pads in a pond
* for the purpose of playing Frogs and Toads.
*
* do not begin working on this until the Pad class is fully tested and debugged
* @author
* @version 2015.11.17
*/
public class Pond
{

private Pad [][] grid; // the 2D array of Pad objects that make up our pond
int size; // the number of Pads in a row or column

/**
* Creates a grid of lily pads with height and width both equal
* to the given size; each lily pad is constructed with the appropriate
* status (frog, toad, or a vacant spot) for starting a game of
* Frogs and Toads.
*   
* The starting configuration consists of a vacant
* lily pad in the center and the remaining lily pads being half
* occupied by frogs and half occupied by toads. Each (vertical)
* column with a higher index than the middle column starts with
* all toads, and each column with an index lower than the middle
* column starts with all frogs. The center column has frogs in
* the high row indices, toads in the low row indices, and a vacant
* spot in the middle.
*   
* Internally, an instance of this class contains "size * size"
* instances of the Pad class (stored in a 2D array). The legal
* range of size is odd numbers from 3 up to 9, which is assumed
* by the constructor as a precondition.
*   
* @param size - the height and width of the grid (assumed to be
* an odd number at least 3 and at most 9 as a precondition)
*/
public Pond(int size) throws Exception
{   
// check that size is an odd number between 3 and 9
// HINT size%2 computes the remainder of size/2.
// if (size%2 == 1) that means size is odd
// so there are three conditions on size: must be >=3, <=9 AND odd
// if this is NOT true, print an error message and return. Otherwise...
if(size%2==0 || size <3 || size > 9){
throw new Exception ("Invalid Pond size " + size);

}
this.size=size;
grid = new Pad[size][size]; // declare the 2D array of Pad

// now fill each cell with a new Pad(Pad.XXXX)
for(int row=0; row<size; row++)
{
for (int col=0; col<size; col++)
{
if(col < size/2) // leftside
grid[row][col] = new Pad(Pad.FROG );
else if (col > size/2)
grid[row][col] = new Pad(Pad.TOAD);
else {

if (row < size/2)
grid[row][col] = new Pad(Pad.TOAD);
else if (row > size/2)
grid[row][col] = new Pad(Pad.FROG);
else
grid[row][col] = new Pad(Pad.VACANT);

}
}
}
}

/**
* Indicates whether or not all lily pads in the pond are in the solved state.
*
* @return true if the puzzle is solved and false otherwise.
*/
public boolean isSolved()
{ // use the following algorithm:
// create an int variable to count numSolved and set it to 0
// use a nested for loop to visit each cell in grid.
// If the cell at row, col is solved, then add one to numSolved
//
// after the loop, if numSolved == size*size then it's solved (return true)
// otherwise it's not (false)
for(int row=0; row<size; row++)
{
for (int col=0; col<size; col++)
{
Pad p = grid[row][col];
if (p.isSolved() == false)
return false;
}
}
return true;
}  

/**
* Return a reference to the Pad object
* at the specific now and column of the pond.
* Return an INVALID Pad if row or column index is invalid
* @param row
* @param column
* @return Pad located at row, column or INVALID Pad
*/

public Pad getPad(int row, int col) {
if (row >= 0 && row < size && col >=0 && col <size)
return grid[row][col];
else
return new Pad(Pad.INVALID);

}

/**
* Resets the pond to the state it
* was in when originally created
*/
public int getSize()
{
return size;
}

/**
* Resets the pond to the state it
* was in when originally created
*/
public void reset()
{
for(int row=0; row<size; row++)
{
for (int col=0; col<size; col++)
{
Pad p = grid[row][col];
p.reset();
}
}
}

/**
* Returns the current state of the pond as a String (with the VACANT, FROG,
* TOAD, and VACANT Pads represented as defined by the toString() method of
* the Pad class).
*/
public String toString(){
String result="";
for (int row=0; row<size; row++){
for (int col=0; col<size; col++){
result = result + grid[row][col].toString(); // add the next grid Pad in a row
}
result = result + " "; // go to a new line after each row
}
return result;
}

}

Explanation / Answer

Answer:

The required code is highlighted in the bold letters and also a tester class FrogGameTester.java is provide. To show the working of the program sample output is also provided along with the code. The game keep on prompting to select the amphibian position to move and then prints the board. The game is ended if there are no more possible moves are present.

Program code to copy:

/**

* The Pad class is used to represent individual lily pads in the pond. Each pad

* keeps track of its starting status and it's status as the the game goes on

* (VACANT, FROG, TOAD, or INVALID - these are public symbolic constantsa that

* will also be used in other classes). Among other things, each Pad object

* needs to be capable of determining whether or not it is in the solved-puzzle

* configuration (ignoring all the other lily pads).

*/

public class Pad

{

     // add the remaining 3 constants WITH JAVADOCS descriptors

     /** Symbolic constant representing a lilypad with a frog */

     public static final int FROG = 1;

     /** Symbolic constant representing a lilypad with a toad */

     public static final int TOAD = 2;

     /** Symbolic constant representing a lilypad with a vacant lilypad */

     public static final int VACANT = 3;

     /** Symbolic constant representing a lilypad with a invalid lilypad */

     public static final int INVALID = 4;

     // INSTANCE VARIABLES

     private int currentState; // the current state of lilypad

     private int startState; // The original state of lilypad

     /**

     * Creates a new lily pad with a given initial state (the state when the

     * game begins).

     *

     * @param startState

     *             - the starting state of the lilypad (FROG, TOAD, VACANT or

     *             INVALID)

     */

     public Pad(int startState)

     {

          // initialize instance variables

          this.currentState = startState;

          this.startState = startState;

     }

     /**

     * Returns the current state of the lily pad; either VACANT, TOAD, FROG, or

     * INVALID.

     *

     * @return the currentState

     */

     public int getStatus()

     {

          return currentState;

     }

     /**

     * Returns true if this lily pad is in the correct state for the solved

     * puzzle and false otherwise.

     */

     public boolean isSolved()

     { // use the following algorithm:

          // if the startState is FROG and the current state is TOAD return true

          // else if the startState is TOAD and the current state is FROG return

          // true

          // else if the startState is VACANT and the current state is VACANT

          // return true

          // otherwise ...

          if (startState == FROG && currentState == TOAD)

          {

              return true;

          }

          if (startState == TOAD && currentState == FROG)

          {

              return true;

          }

          if (startState == VACANT && currentState == VACANT)

          {

              return true;

          }

          return false;

     }

     /**

     * This method restores the lily pad to the state it was in when originally

     * constructed.

     */

     public void reset()

     {

          currentState = startState;

     }

     /**

     * Changes the current state of the lily pad. If the status is invalid

     * (i.e., not VACANT, TOAD, or FROG), then setStatus sets the status of the

     * lily pad to INVALID.

     */

     public void setStatus(int status)

     {

          if (status == TOAD || status == FROG || status == VACANT)

          {

              currentState = status;

          }

          else

          {

              currentState = INVALID;

          }

     }

     /**

     * Returns the string representation of the lily pad.

     */

     public String toString()

     {

          if (currentState == FROG)

              return "F";

          // add symbols for the other possibilities

          else if (currentState == TOAD)

              return "T";

          else if (currentState == VACANT)

              return " ";

          else

              // must be INVALID

              return "#";

     }

}

/**

* This class represents a grid of lily pads in a pond for the purpose of

* playing Frogs and Toads.

*

* do not begin working on this until the Pad class is fully tested and debugged

*

* @author

* @version 2015.11.17

*/

public class Pond

{

     private Pad[][] grid; // the 2D array of Pad objects that make up our pond

     int size; // the number of Pads in a row or column

     /**

     * Creates a grid of lily pads with height and width both equal to the

     * given size; each lily pad is constructed with the appropriate status

     * (frog, toad, or a vacant spot) for starting a game of Frogs and Toads.

     *

     * The starting configuration consists of a vacant lily pad in the center

     * and the remaining lily pads being half occupied by frogs and half

     * occupied by toads. Each (vertical) column with a higher index than the

     * middle column starts with all toads, and each column with an index lower

     * than the middle column starts with all frogs. The center column has

     * frogs in the high row indices, toads in the low row indices, and a

     * vacant spot in the middle.

     *

     * Internally, an instance of this class contains "size * size" instances

     * of the Pad class (stored in a 2D array). The legal range of size is odd

     * numbers from 3 up to 9, which is assumed by the constructor as a

     * precondition.

     *

     * @param size

     *             - the height and width of the grid (assumed to be an odd

     *             number at least 3 and at most 9 as a precondition)

     */

     public Pond(int size) throws Exception

     {

          // check that size is an odd number between 3 and 9

          // HINT size%2 computes the remainder of size/2.

          // if (size%2 == 1) that means size is odd

          // so there are three conditions on size: must be >=3, <=9 AND odd

          // if this is NOT true, print an error message and return.

          // Otherwise...

          if (size % 2 == 0 || size < 3 || size > 9)

          {

              throw new Exception("Invalid Pond size " + size);

          }

          this.size = size;

          grid = new Pad[size][size]; // declare the 2D array of Pad

          // now fill each cell with a new Pad(Pad.XXXX)

          for (int row = 0; row < size; row++)

          {

              for (int col = 0; col < size; col++)

              {

                   if (col < size / 2) // leftside

                        grid[row][col] = new Pad(Pad.FROG);

                   else if (col > size / 2)

                        grid[row][col] = new Pad(Pad.TOAD);

                   else

                   {

                        if (row < size / 2)

                             grid[row][col] = new Pad(Pad.TOAD);

                        else if (row > size / 2)

                             grid[row][col] = new Pad(Pad.FROG);

                        else

                             grid[row][col] = new Pad(Pad.VACANT);

                   }

              }

          }

     }

     /**

     * Indicates whether or not all lily pads in the pond are in the solved

     * state.

     *

     * @return true if the puzzle is solved and false otherwise.

     */

     public boolean isSolved()

     { // use the following algorithm:

          // create an int variable to count numSolved and set it to 0

          // use a nested for loop to visit each cell in grid.

          // If the cell at row, col is solved, then add one to numSolved

          //

          // after the loop, if numSolved == size*size then it's solved (return

          // true)

          // otherwise it's not (false)

          for (int row = 0; row < size; row++)

          {

              for (int col = 0; col < size; col++)

              {

                   Pad p = grid[row][col];

                   if (p.isSolved() == false)

                        return false;

              }

          }

          return true;

     }

     /**

     * Return a reference to the Pad object at the specific now and column of

     * the pond. Return an INVALID Pad if row or column index is invalid

     *

     * @param row

     * @param column

     * @return Pad located at row, column or INVALID Pad

     */

     public Pad getPad(int row, int col)

     {

          if (row >= 0 && row < size && col >= 0 && col < size)

              return grid[row][col];

          else

              return new Pad(Pad.INVALID);

     }

     /**

     * Resets the pond to the state it was in when originally created

     */

     public int getSize()

     {

          return size;

     }

     /**

     * Resets the pond to the state it was in when originally created

     */

     public void reset()

     {

          for (int row = 0; row < size; row++)

          {

              for (int col = 0; col < size; col++)

              {

                   Pad p = grid[row][col];

                   p.reset();

              }

          }

     }

     /**

     * Returns the current state of the pond as a String (with the VACANT,

     * FROG, TOAD, and VACANT Pads represented as defined by the toString()

     * method of the Pad class).

     */

     public String toString()

     {

          String result = "";

          for (int row = 0; row < size; row++)

          {

              for (int col = 0; col < size; col++)

              {

                   result = result + grid[row][col].toString(); // add the next

                   // grid Pad in

                   // a row

              }

              result = result + " "; // go to a new line after each row

          }

          return result;

     }

}

/**

* The FrogGame class keeps track of the current state of a Frogs and Toads game

* (which consists of a Pond object and where the vacant pad is). It also has

* methods for handling the game rules, such as validMove, moveStillPossible,

* and moveAmphibian.

*

* @author

* @date 2015.11.19 created

*

*/

public class FrogGame

{

     private Pond gameBoard; // the playing surface of our game

     private int size; // the number of Pads in a row or column of the gameBoard

     /**

     * The FrogGame class keeps track of the current state of a Frogs and Toads

     * game (which consists of a Pond object and where the vacant pad is). It

     * also has methods for handling the game rules, such as validMove,

     * moveStillPossible, and moveAmphibian.

     */

     public FrogGame(int size) throws Exception

     {

          gameBoard = new Pond(size);

          this.size = size;

     }

     /**

     * Indicates whether or not the Pond gameBoard is solved

     *

     * @return true if the puzzle is solved and false otherwise.

     */

     public boolean isSolved()

     {

          return gameBoard.isSolved(); // use the isSolved for the Pond class

     }

     /**

     * Returns the pond object associated with this FrogGame object.

     *

     *

     */

     public Pond getPond()

     {

          return gameBoard;

     }

     /**

     * This method returns the status of the pad at the given coordinates

     * (either FROG, TOAD, VACANT, OR INVALID). If the given row or column is

     * out of bounds, this method returns INVALID.

     *

     * @param row

     *             The first row is row 0

     * @param column

     *             the first column is column 0

     * @return status of Pad located at [row][column]

     */

     public int getStatusOfPad(int row, int column)

     {

          Pad p = gameBoard.getPad(row, column);

          int status = p.getStatus();

          return status;

     }

     /**

     * Returns true if the given coordinates represent a Pad that has an

     * amphibian that can make a legal move (hint: you may want to use the

     * statusOfPad method to make your job easier). In order to be legal, the

     * jump to the vacant pad must be in one of the two correct directions for

     * that type of amphibian, and the vacant pad must either be adjacent or

     * two pads away with an amphibian of the opposite type in between. Note

     * that row/column pairs that represent locations outside the boundaries of

     * the game are never a legal move.

     *

     * @param row

     * @param column

     * @return True if the given row and column constitute a legal move and

     *         false otherwise.

     *

     */

     public boolean validMove(int row, int column)

     {

          if (getStatusOfPad(row, column) == Pad.FROG)

          {

              // FROG can move to right one

              if (getStatusOfPad(row, column + 1) == Pad.VACANT)

                   return true;

              // FROG can jump over TOAD to right

              if (getStatusOfPad(row, column + 1) == Pad.TOAD

                        && getStatusOfPad(row, column + 2) == Pad.VACANT)

                   return true;

              // FROG can move up one

              if (getStatusOfPad(row - 1, column) == Pad.VACANT)

                   return true;

              // FROG can jump over TOAD moving up

              if (getStatusOfPad(row - 1, column) == Pad.TOAD

                        && getStatusOfPad(row - 2, column) == Pad.VACANT)

                   return true;

          }

          else if (getStatusOfPad(row, column) == Pad.TOAD)

          {

              // FROG can move to left one

              if (getStatusOfPad(row, column - 1) == Pad.VACANT)

                   return true;

              // FROG can jump over TOAD to left

              if (getStatusOfPad(row, column - 1) == Pad.FROG

                        && getStatusOfPad(row, column - 2) == Pad.VACANT)

                   return true;

              // FROG can move up down

              if (getStatusOfPad(row + 1, column) == Pad.VACANT)

                   return true;

              // FROG can jump over TOAD moving down

              if (getStatusOfPad(row + 1, column) == Pad.FROG

                        && getStatusOfPad(row + 2, column) == Pad.VACANT)

                   return true;

          }

          else

          {

              // (row, column) is not FROG or TOAD

              return false;

          }

          // if the selected Pad is a FROG

          // if the Pad to the right is Vacant, or the Pad above is Vacant

          // then the move is valid.

          // else if the Pad to the right is a TOAD and

          // and the Pad to the right of the TOAD is vacant the move is valid

          // else if the Pad above is a TOAD and

          // the Pad above that is vacant the move is valid

          // else the move is invalid

          // else if the selected Pad is a TOAD

          // if the Pad to the left is vacant, or the Pad below is Vacant,

          // then the move is valid.

          // else if the Pad to the left is a FROG and

          // the Pad to the left of the FROG is vacant the move is valid

          // else if the Pad below is a FROG and

          // the Pad below that is vacant the move is valid

          // else the move is invalid

          // else the move is not valid

          return false;

     }

     /**

     * Moves the amphibian at given location to the vacant lily pad. This

     * method may assume as a precondition that the move provided is actually a

     * legal move. (Hint: 1. Find the VACANT Pad. 2. The VACANT Pad becomes

     * either FROG or TOAD. 3. The Pad at [row][col] now becomes VACANT.)

     */

     public void moveAmphibian(int row, int column)

     {

          int isVacant = getStatusOfPad(row, column);

          boolean moved = false;

          Pad p;

          if (Pad.FROG == isVacant)

          {

              if (validMove(row, column))

              {

                   for (int i = row; i < size; i++)

                   {

                        for (int j = column; j < size; j++)

                        {

                             if (getStatusOfPad(i, j) == Pad.VACANT)

                             {

                                  p = gameBoard.getPad(i, j);

                                  p.setStatus(Pad.FROG);

                                  moved = true;

                                  break;

                             }

                             if (moved)

                                  break;

                        }

                   }

                   for (int i = row; i >= 0; i--)

                   {

                        for (int j = column; j >= 0; j--)

                        {

                             if (getStatusOfPad(i, j) == Pad.VACANT)

                             {

                                  p = gameBoard.getPad(i, j);

                                  p.setStatus(Pad.FROG);

                                  moved = true;

                                  break;

                             }

                             if (moved)

                                  break;

                        }

                   }

              }

          }

          else if (Pad.TOAD == isVacant)

          {

              if (validMove(row, column))

              {

                   for (int i = row; i < size; i++)

                   {

                        for (int j = column; j >= 0; j--)

                        {

                             if (getStatusOfPad(i, j) == Pad.VACANT)

                             {

                                  p = gameBoard.getPad(i, j);

                                  p.setStatus(Pad.TOAD);

                                  moved = true;

                                  break;

                             }

                             if (moved)

                                  break;

                        }

                   }

              }

          }

          if (moved)

          {

              p = gameBoard.getPad(row, column);

              p.setStatus(Pad.VACANT);

          }

          else

          {

              System.out.println("Unable to move");

          }

     }

     // first check that the move is a valid move.

     // find the VACANT Pad, the VACANT Pad is changed to FROG or TOAD

     // The specified Pad is changed to VACANT.

     /**

     * Indicates whether or not a legal move is still possible (Hint: you may

     * want to utilize the validMove method).

     *

     * @return True

     */

     public boolean moveStillPossible()

     {

          for (int row = 0; row < size; row++)

          {

              for (int col = 0; col < size; col++)

              {

                   if (validMove(row, col) == true)

                        return true;

              }

          }

          return false;

     }

     public void print()

     {

          System.out.println(gameBoard);

     }

}

//FrogGameTester.java

import java.util.Scanner;

public class FrogGameTester

{

     public static void main(String args[]) throws Exception

     {

          FrogGame fg = new FrogGame(3);

          fg.print();

          int x, y;

          Scanner in = new Scanner(System.in);      

          do

          {

              System.out.println("Select the amphibian you want to move: ");

              System.out.println("Enter x-value: ");

              x = in.nextInt();

              System.out.println("Enter y-value: ");

              y = in.nextInt();

              fg.moveAmphibian(x, y);

              fg.print();

          } while (fg.moveStillPossible());

     }

}

Sample output:

FTT

F T

FFT

Select the amphibian you want to move:

Enter x-value:

2

Enter y-value:

1

FTT

FFT

F T

Select the amphibian you want to move:

Enter x-value:

2

Enter y-value:

2

FTT

FFT

FT

Select the amphibian you want to move:

Enter x-value:

2

Enter y-value:

0

FTT

FFT

TF

Select the amphibian you want to move:

Enter x-value:

2

Enter y-value:

1

FTT

FFT

T F

Select the amphibian you want to move:

Enter x-value:

0

Enter y-value:

1

F T

FFT

TTF

Select the amphibian you want to move:

Enter x-value:

0

Enter y-value:

0

FT

FFT

TTF

Select the amphibian you want to move:

Enter x-value:

0

Enter y-value:

2

TF

FFT

TTF

Select the amphibian you want to move:

Enter x-value:

0

Enter y-value:

1

T F

FFT

TTF

Select the amphibian you want to move:

Enter x-value:

1

Enter y-value:

1

TFF

F T

TTF

Select the amphibian you want to move:

Enter x-value:

1

Enter y-value:

0

TFF

FT

TTF

Select the amphibian you want to move:

Enter x-value:

1

Enter y-value:

2

TFF

TF

TTF

Select the amphibian you want to move:

Enter x-value:

1

Enter y-value:

1

TFF

T F

TTF

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote