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

(Java Assignment) Write a program that simulates and scores a game of bowling. A

ID: 3887594 • Letter: #

Question

(Java Assignment)

Write a program that simulates and scores a game of bowling. As background information, here is an explanation of terminology and scoring.

Your game will need to keep score for at least one player, multiple players are allowed. 10 frames for each player, and if the final frame is a strike or spare they roll the 11th frame.

(Instructions)

1.No BlueJ, use Netbeans, Eclipse or another approved IDE.

2.Create a class named Bowling that will manage the game....my public static void main was in my bowling, I set things up in constructor then called play() and that method handled the 10 frames, including calling methods to do things such as print the formatted output. .

3.Create a Bowling class such that when the main method of the Driver class is run, this (link coming soon) is produced. Note: match the output format exactly!

4.Your bowling game should knock down pins randomly, use a random number generator.......but just like in real bowling (no I do not bowl) higher numbers should be weighted more so you roll more 7s than 2s for instance. 0, 1, 2, 3, 4, 5 should all have a weight of 1. 6 should have a weight of 2. 7 and 10 should have a weight of 3, 8 and 9 should have a weight of 4 for first roll only. Second roll should be weighted all equal but you should only knock down how many of the 10 pins are left after the first roll. Weights make the game more fun to play.

5.Any spare or strike (anytime you get all 10 pins down on roll1 or roll2) you should do the scoring properly adding to the previous frame.

6.10th frame should be done properly. Handling spares and strikes in the 10th frame properly.

7.You should set your game up to hold on to all frames first and second ball scores and each frame score.....it will make it easier in the long run.

8.Feel free to add two player games. Put in some skill by a player instead of just randomness. Still needs to be able to do random numbers also.

9.import java.util.random and look up the Random class to use random number generation.

10.Test without random numbers, force certain scores to happen with a test data set to make sure your game is working properly. All strikes equals a perfect game of 300....test that.

(Hints and Assumptions)

1.Design your solution out on paper before you start coding.

2.You may assume that the constructor of the Bowling class is called with valid bowling scores and the correct number of frames in the game.

3.Design first. Translate into a programming language (Java) second.

4.When the final frame contains a strike or spare, the frame needs to be printed differently. Please study the output transcript above carefully to see how.

Explanation / Answer

import java.io.*;
import java.util.*;
import java.math.*;

public class Bowling
{

static Scanner keyIn = new Scanner(System.in);

public Bowling()
{
}

public static void main(String args[])
{

int frme[] = new int[10];
int allfrme[][] = new int[2][12];
String s;

do {
System.out.println(" Java Bowling Gaming Score Board ");
   for(int l1 = 0; l1 < 10; l1++)
      frme[l1] = 0;
     for(int l2 = 0; l2 < 12; l2++)
       allfrme[0][l2] = allfrme[1][l2] = 0;
   nxtfrme:

for(int i = 0; i < 10; i++) {
    boolean chk = false;
    int Pins = 0;

   while(!chk) {
    System.out.printf("    FRAME ", new Object[] {
   Integer.valueOf(i + 1)   });

    System.out.printf(" Ball 1: ");
   Pins = keyIn.nextInt();

     if(Pins <= 10 && Pins >= 0) {
      allfrme[0][i] = Pins;
      chk = true;
   }
   }
   if(Pins == 10) {
     System.out.println(" Excelent, Strike!!");
   continue;

   }
chk = false;

do {
int Pins2 = 0;

if(chk)
    continue nxtfrme;
    System.out.print(" Ball 2: ");
    Pins2 = keyIn.nextInt();

   if(Pins2 <= 10 && Pins2 >= 0 && Pins2 + allfrme[0][i] < 11) {
   allfrme[1][i] = Pins2;

   if(Pins2 + allfrme[0][i] == 10)
        System.out.println(" Nice, Spare!!");
   else if(allfrme[0][i] < 4 || Pins2 < 4)
        System.out.println(" Do better next time");

   chk = true;
    }

} while(true);

}

if(allfrme[0][9] == 10) {
   boolean chk1 = false;

   int Pins3 = 0;

while(!chk1) {
    System.out.print(" XTR1 Ball 1: ");
   Pins3 = keyIn.nextInt();

   if(Pins3 < 11 && Pins3 >= 0) {
     allfrme[0][10] = Pins3;
   chk1 = true;
   }
}

if(allfrme[0][10] == 10) {
   boolean chk2 = false;

   int Pins4 = 0;

   System.out.println(" Excelent, Strike...");

   if(allfrme[0][11] == 10)
    System.out.print(" Excelent, Strike...");

while(!chk2) {
    System.out.print(" XTR2 Ball 2: ");
   Pins4 = keyIn.nextInt();

   if(Pins4 < 11 && Pins4 > 0) {
   allfrme[0][11] = Pins4;
   chk2 = true;
   }
}

}else {
   boolean chk3 = false;

   int Pins5 = 0;

   while(!chk3) {
    System.out.print(" XTR2 Ball 2: ");
   Pins5 = keyIn.nextInt();

   if(Pins5 < 11 && Pins5 >= 0 && Pins5 + allfrme[0][10] < 11)
   {
       allfrme[1][10] = Pins5;
       chk3 = true;
   }

   }
}

}else if(allfrme[0][9] + allfrme[1][9] == 10){
boolean chk4 = false;

int Pins6 = 0;

while(!chk4){
    System.out.print(" XTR2 Ball 2: ");
        Pins6 = keyIn.nextInt();

   if(Pins6 <= 10 && Pins6 >= 0){
     allfrme[0][10] = Pins6;
     chk4 = true;
   }
}

}

//start
if(allfrme[0][0] + allfrme[0][1] == 10)
   frme[0] = 10 + allfrme[0][1];
else
   frme[0] = allfrme[0][0] + allfrme[1][0];

//middle
if(allfrme[0][0] == 10) {
if(allfrme[0][1] == 10)
   frme[0] = 20 + allfrme[0][2];
   else
   frme[0] = 10 + allfrme[0][1] + allfrme[1][1];
}

//end
for(int j = 1; j < 10; j++) {

if(allfrme[0][j] == 10){
if(allfrme[0][j + 1] == 10)
   frme[j] = frme[j - 1] + 20 + allfrme[0][j + 2];
else
   frme[j] = frme[j - 1] + 10 + allfrme[0][j + 1] + allfrme[1][j + 1];
    continue;
}

if(allfrme[0][j] + allfrme[1][j] == 10)
   frme[j] = frme[j - 1] + 10 + allfrme[0][j + 1];
else
   frme[j] = frme[j - 1] + allfrme[0][j] + allfrme[1][j];

}

//Print
System.out.print(" FRAME");
for(int k = 1; k < 11; k++)
   System.out.printf("%4d", new Object[] {
       Integer.valueOf(k) });

   if(allfrme[0][9] == 10) {
   if(allfrme[0][10] == 10)
       System.out.print(" XTR1 XTR2");
   else
       System.out.print(" EXTRA");

   }else if(allfrme[0][9] + allfrme[1][9] == 10)
   System.out.print(" XTRA");
   System.out.print(" Ball 1 ");

for(int l = 0; l < 10; l++)
       System.out.printf("%4d", new Object[] {
           Integer.valueOf(allfrme[0][l])
       });

if(allfrme[0][9] == 10)   {
if(allfrme[0][10] == 10)
    System.out.printf("%4d%4d", new Object[] {
     Integer.valueOf(allfrme[0][10]), Integer.valueOf(allfrme[0][11]) });
   else
      System.out.printf("%4d", new Object[] {
       Integer.valueOf(allfrme[0][10]) });

} else
if(allfrme[0][9] + allfrme[1][9] == 10)
   System.out.printf("%4d", new Object[] {
       Integer.valueOf(allfrme[0][10])
   });
    System.out.print(" Ball 2 ");
for(int i1 = 0; i1 < 10; i1++)
   System.out.printf("%4d", new Object[] {
       Integer.valueOf(allfrme[1][i1])
   });

if(allfrme[0][9] == 10 && allfrme[0][10] != 10)
   System.out.printf("%4d", new Object[] {
       Integer.valueOf(allfrme[1][10])
   });

    System.out.print(" SCORE");
for(int j1 = 0; j1 < 10; j1++)
   System.out.printf("%4d", new Object[] {
       Integer.valueOf(frme[j1])
   });

System.out.print(" Play more (Y/N)? ");
s = keyIn.next();
String s1 = keyIn.nextLine();

   }
}


}