I am creating a Domineering java program to prepare for a Data Structures class
ID: 668104 • Letter: I
Question
I am creating a Domineering java program to prepare for a Data Structures class I'm taking in the next semester. There are a couple of requested modifications I have to make in order for the program to be consider fully functional. I am somewhat stumped as for how or what I should do. Here is the list:
1. Use a try-catch block around the input of the row and column in the play() method to handle InputMismatchExceptions. Write classes DominoOffBoardException and DominoOnTopOfDominoException extending RunTimeException to check if dominos are being placed off the board or on top of other dominos.
2. In the play() method, use the statement System.out.print(“Row Column: “); instead of the two statements System.out.print(“Row: “); and System.out.print(“Column: “);
3. Prompt the user to enter nr and nc (number of rows and number of columns) 1<=nr, nc <=10 to play Domineering on the board with rows 0, 1, …, nr-1 and columns 0, 1, …, nc-1.
4. Prompt the user to see who goes first, Horizontal or Vertical. Use the question: Who goes first? (h/v)
Here is my code thus far:
/** 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() {
int row = 0;
int column = 0;
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: ");
row = INPUT.nextInt();
System.out.print("Column: ");
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
Modified Code:
import java.util.InputMismatchException;
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() {
int row;
int column;
boolean player;// = HORIZONTAL;
while (true) {
System.out.println("Enter number of rows");
row = INPUT.nextInt();
System.out.println("Enter number of columns");
column = INPUT.nextInt();
System.out.println("Whose goes first, Horizontal or vertical(h/v):");
player = INPUT.hasNext();
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 Column: ");
try
{
row = INPUT.nextInt();
column = INPUT.nextInt();
playAt(row, column, player);
player = !player;
}
catch(InputMismatchException e)
{
System.out.println("hi");
}
catch(Exception e)
{
System.out.println("hello");
}
}
}
/**
* 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();
}
}
//DominoOffBoardException extending RunTimeException
public class DominoOffBoardException extends RuntimeException
{
public DominoOffBoardException()
{
super();
}
public DominoOffBoardException(String msg)
{
super(msg);
}
}
//DominoOnTopOfDominoException extending RunTimeException
public class DominoOnTopOfDominoException extends RuntimeException
{
public DominoOnTopOfDominoException()
{
super();
}
public DominoOnTopOfDominoException(String test)
{
super(test);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.