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

Can some one please help me with this Java code! Elimination is a oneplayer game

ID: 3835576 • Letter: C

Question

Can some one please help me with this Java code!

Elimination is a oneplayer game. The board consists of a set of 12 tiles, numbered 1 through 12. The
player rolls a pair of dice and removes tiles based on the numbers shown on the dice. For each roll, the player can
remove either the two tiles corresponding to the numbers shown on the dice or a single tile corresponding to the sum of
the numbers on the dice. If the player rolls doubles (the same number on both dice), the player can remove only the tile
corresponding to the sum of the dice. Play continues until the player cannot make a legal move or all the tiles have been
removed. The sum of the remaining tiles is the player's score. The goal is to have a low score. Here is a sample game:
1 2 3 4 5 6 7 8 9 10 11 12
. . . . . . . . . . . .
Dice roll: 4 3
What is your move (V=values, S=sum, Q=quit)? v
1 2 3 4 5 6 7 8 9 10 11 12
. . X X . . . . . . . .
Dice roll: 2 5
What is your move (V=values, S=sum, Q=quit)? v
1 2 3 4 5 6 7 8 9 10 11 12
. X X X X . . . . . . .
Dice roll: 6 5
What is your move (V=values, S=sum, Q=quit)? V
Try again.
What is your move (V=values, S=sum, Q=quit)? s
1 2 3 4 5 6 7 8 9 10 11 12
. X X X X . . . . . X .
Dice roll: 4 5
What is your move (V=values, S=sum, Q=quit)? s
1 2 3 4 5 6 7 8 9 10 11 12
. X X X X . . . X . X .
Dice roll: 4 5
What is your move (V=values, S=sum, Q=quit)? s
Try again.
What is your move (V=values, S=sum, Q=quit)? v
Try again.
What is your move (V=values, S=sum, Q=quit)? q
1 2 3 4 5 6 7 8 9 10 11 12
. X X X X . . . X . X .
Score = 44
You need to complete the partially completed "Elimination" class.
You need to create the "Board" class with attributes and methods to represent the board. In addition to a constructor to
initialize the board and a toString to write the board out as shown above, you should create methods to allow the user
to make a "sum" single tile flip, a "value" two tile flip (if legal at that time). Also a "score" method to return the current
score of the board.
Also explain in detail what you should test.

11
  
import java.util.Scanner;
public class Elimination {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Board B = new Board();
int die1, die2;
boolean legalMove;
String choice=" ";
while (!choice.equalsIgnoreCase("Q")) {
System.out.println(" "+B);
die1 = (int)(1 + Math.random()*6);
die2 = (int)(1 + Math.random()*6);
legalMove=false;
System.out.println("Dice roll: " + die1 + " " + die2);
while (!legalMove) {
System.out.print("What is your move (V=values, S=sum, Q=quit)? ");
choice=in.next();
if (choice.equalsIgnoreCase("Q"))
legalMove=true;
else if (choice.equalsIgnoreCase("S") && B.removeTile(die1+die2))
legalMove=true;
else if (choice.equalsIgnoreCase("V") && B.removeTiles(die1, die2))
legalMove=true;
else if (choice.equalsIgnoreCase("S")) {
// ADD CODE HERE TO CHECK AND, IF VALID, MAKE THE “SUM” MOVE
}
else if (choice.equalsIgnoreCase("V")) {
// ADD CODE HERE TO CHECK AND, IF VALID, MAKE THE “VALUE” MOVE
}
else System.out.println("Try again.");
}
}
System.out.println(" "+B);
System.out.println("Score = " + B.score());
}
}

Explanation / Answer

PROGRAM CODE:

Elimination.java

package elimination;

import java.util.Scanner;
public class Elimination {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Board B = new Board();
int die1, die2;
boolean legalMove;
String choice=" ";
while (!choice.equalsIgnoreCase("Q")) {
System.out.println(" "+B);
die1 = (int)(1 + Math.random()*6);
die2 = (int)(1 + Math.random()*6);
legalMove=false;
System.out.println("Dice roll: " + die1 + " " + die2);
while (!legalMove) {
System.out.print("What is your move (V=values, S=sum, Q=quit)? ");
choice=in.next();
if (choice.equalsIgnoreCase("Q"))
legalMove=true;
else if (choice.equalsIgnoreCase("S") && B.removeTile(die1+die2))
legalMove=true;
else if (choice.equalsIgnoreCase("V") && B.removeTiles(die1, die2))
legalMove=true;
//else if (choice.equalsIgnoreCase("S")) {
// ADD CODE HERE TO CHECK AND, IF VALID, MAKE THE “SUM” MOVE
//}
//else if (choice.equalsIgnoreCase("V")) {
// ADD CODE HERE TO CHECK AND, IF VALID, MAKE THE “VALUE” MOVE
//}
else System.out.println("Try again.");
}
}
System.out.println(" "+B);
System.out.println("Score = " + B.score());
}
}

Board.java

package elimination;

public class Board {
  
   char board[];
  
   public Board() {
       board = new char[12];
       for(int i=0; i<12; i++)
           board[i] = '.';
   }
  
   public boolean removeTile(int index)
   {
       int newIndex = index-1;
       if(newIndex>=0 && newIndex <12)
       {
           if(board[newIndex] == 'X')
               return false;
           else{
               board[newIndex] = 'X';
               return true;
           }
       }
       return false;
   }
  
   public boolean removeTiles(int index1, int index2)
   {
       int newIndex1 = index1-1;
       int newIndex2 = index2-1;
       if(newIndex1>=0 && newIndex1 <12 && newIndex2>=0 && newIndex2<12)
       {
           if(board[newIndex1] == 'X' || board[newIndex2] == 'X')
               return false;
           else{
               board[newIndex1] = 'X';
               board[newIndex2] = 'X';
               return true;
           }
       }
       return false;
   }

   public int score() {
       int sum = 0;
       for(int i=0; i<board.length; i++)
       {
           if(board[i] != 'X')
               sum += i + 1;
       }
       return sum;
   }
  
   @Override
   public String toString() {
       String result = "1 2 3 4 5 6 7 8 9 10 11 12 ";
       for(int i=0; i<board.length; i++)
           result += board[i] + " ";
       return result;
   }
}

OUTPUT:


1 2 3 4 5 6 7 8 9 10 11 12
. . . . . . . . . . . .
Dice roll: 3 4
What is your move (V=values, S=sum, Q=quit)? v

1 2 3 4 5 6 7 8 9 10 11 12
. . X X . . . . . . . .
Dice roll: 5 5
What is your move (V=values, S=sum, Q=quit)? v

1 2 3 4 5 6 7 8 9 10 11 12
. . X X X . . . . . . .
Dice roll: 2 4
What is your move (V=values, S=sum, Q=quit)? v
Try again.
What is your move (V=values, S=sum, Q=quit)? s

1 2 3 4 5 6 7 8 9 10 11 12
. . X X X X . . . . . .
Dice roll: 4 3
What is your move (V=values, S=sum, Q=quit)? s

1 2 3 4 5 6 7 8 9 10 11 12
. . X X X X X . . . . .
Dice roll: 2 6
What is your move (V=values, S=sum, Q=quit)? s

1 2 3 4 5 6 7 8 9 10 11 12
. . X X X X X X . . . .
Dice roll: 3 3
What is your move (V=values, S=sum, Q=quit)? s
Try again.
What is your move (V=values, S=sum, Q=quit)? v
Try again.
What is your move (V=values, S=sum, Q=quit)? q

1 2 3 4 5 6 7 8 9 10 11 12
. . X X X X X X . . . .
Score = 45