OOPDA Programming Assignment 2 For this assignment you will simulate the dice ga
ID: 3735427 • Letter: O
Question
OOPDA Programming Assignment 2 For this assignment you will simulate the dice game called "Shut the Box. Shut the box is played as follows: . The "board" consists of nine numbered (1-9) boxes that start open and can be closed . There can be any number of players. . The order of play will be randomly determined . On the players turn, the player rolls two dice. He then must close a combination of boxes that add up to the total rolled . As long as the player is able to find such a combination, the player's turn continues. . Once a player cannot find a combination of open boxes adding to the total rolled, the players turn is over When a player's turn ends, the players score is equal to the sum of the still open boxes Play then continues with the next player. When all players have completed their turn, the player with the lowest score is declared the . . winner If any player is able to close all the boxes during their turn, he has "shut the box" and is immediately declared the winner . Notes Think though this problem in an object oniented fashion. Go through the nouns and verbs in the description (as demonstrated in cless and chapter 15) to try to determine what classes you think you wll need . Be very systematic in planning and coding. Do it "piece by plece . You willneed to implement an option to have a computer player * The computer player can use one of two strategies. Always picks the highest available box(es) that works for the combination o a Picks a combination of baxes that works at random You should have a ShutTheox class that has a start method. This class will be responsible fo guiding game play Use inheritarce where appropriate . . You can use the Die classes providedExplanation / Answer
import java.util.Random;
import java.util.Scanner;
class Box {
boolean openFlag;
public Box () {
this.openFlag=true;
}
}
public class ShutTheBox {
public static void main(String args[]) {
Box[] boxArray =new Box[9];
int i;
for (i=0;i<9;i++) {
boxArray[i]=new Box();
}
boolean winFlag=true;
char choice='N';
if (choice=='N') {
System.out.println("Please enter number of Players..");
int numPlayers;
Scanner choicei = new Scanner(System.in);
numPlayers = choicei.nextInt();
int[] pointsList=new int[numPlayers];
int[] playedList=new int[numPlayers];
for (i=0;i<numPlayers;i++) {
Random rand = new Random();
int temp;
temp=rand.nextInt(numPlayers)+1;
boolean flag=false;
int j;
for (j=0;j<numPlayers;j++) {
if (playedList[j]==temp) {
flag=true;
break;
}
}
if (flag==false) {
playedList[i]=temp;
}
if (flag==true) {
i--;
}
}
int k;
for (k=0;k<playedList.length;k++) {
System.out.println("Its player number ");
System.out.println(playedList[k]);
System.out.println(" turn");
boolean flag=true;
while(flag==true) {
int d1;
int d2;
Random rand1 = new Random();
d1=rand1.nextInt(6)+1;
Random rand2 = new Random();
d2=rand2.nextInt(6)+1;
int sum;
sum=d1+d2;
if (sum<9) {
if (boxArray[sum].openFlag==true) {
boxArray[sum].openFlag=false;
pointsList[playedList[k]-1]=sum;
}else{
flag=false;
}
}else{
flag=false;
}
int m;
for (m=0;m<9;m++) {
if (boxArray[m].openFlag==true) {
winFlag=false;
}
if (winFlag==true) {
System.out.println("Shut the box");
System.out.println("Player number ");
System.out.println(playedList[k]);
System.out.println(" is the winner");
}
}
}
}
int min;
int l;
min=pointsList[0];
int win=0;
for (l=0;l<pointsList.length;l++) {
if (min>pointsList[l]) {
min=pointsList[l];
win=l;
}
}
if (winFlag==false) {
System.out.println("Player number ");
System.out.println(win+1);
System.out.println(" is the winner");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.