I need to have the following modifications to the listed Domineering game. Plsea
ID: 3542082 • Letter: I
Question
I need to have the following modifications to the listed Domineering game. Plsease Help.
1. A method isMoveValid(int, int, boolean), which returns a boolean telling whether or not the move by player boolean at position row and column is legal.
2. The number of rows, r, and the number of columns, c, is entered on the command line, e.g
java Domineering 4 3
You may assume that the number of rows and columns is less than or equal to 10.
3. In the play() method, use the statement System.out.print("Row Column: ")
4. Use a try-catch block around the input of the row and column in the play() method to be able to handle InputMismatchExceptions.
/** The game of Domineering. */
public class Domineering {
/** For reading from the console. */
public static final java.util.Scanner INPUT
= new java.util.Scanner(System.in);
/** The player who plays their dominoes horizontally. */
public static final boolean HORIZONTAL = false;
/** The player who plays their dominoes vertically. */
public static final boolean VERTICAL = true;
/** Array of board squares, true if occupied. */
private boolean[][] squares;
/** The board is initially empty. */
public Domineering() {
squares = new boolean[8][8];
// Java initializes all array elements to false
}
/**
* Return true if there is a legal move for the specified player.
*/
public boolean hasLegalMoveFor(boolean player) {
int rowOffset = 0;
int columnOffset = 0;
if (player == HORIZONTAL) {
columnOffset = 1;
} else {
rowOffset = 1;
}
for (int row = 0; row < (8 - rowOffset); row++) {
for (int column = 0; column < (8 - columnOffset); column++) {
if (!(squares[row][column]
|| squares[row + rowOffset][column + columnOffset])) {
return true;
}
}
}
return false;
}
/** Play until someone wins. */
public void play() {
boolean player = HORIZONTAL;
while (true) {
System.out.println(" " + this);
if (player == HORIZONTAL) {
System.out.println("Horizontal to play");
} else {
System.out.println("Vertical to play");
}
if (!(hasLegalMoveFor(player))) {
System.out.println("No legal moves -- you lose!");
return;
}
System.out.print("Row: ");
int row = INPUT.nextInt();
System.out.print("Column: ");
int column = INPUT.nextInt();
playAt(row, column, player);
player = !player;
}
}
/**
* Play a domino with its upper left corner at row, column.
*/
public void playAt(int row, int column, boolean player) {
squares[row][column] = true;
if (player == HORIZONTAL) {
squares[row][column + 1] = true;
} else {
squares[row + 1][column] = true;
}
}
public String toString() {
String result = " 0 1 2 3 4 5 6 7";
for (int row = 0; row < 8; row++) {
result += " " + row;
for (int column = 0; column < 8; column++) {
if (squares[row][column]) {
result += " #";
} else {
result += " .";
}
}
}
return result;
}
/** Create and play the game. */
public static void main(String[] args) {
System.out.println("Welcome to Domineering.");
Domineering game = new Domineering();
game.play();
}
}
Explanation / Answer
answer link in comments