Click here to Skip to main content
15,881,877 members
Articles / Programming Languages / Java

Bowling Score Board Game in Java

Rate me:
Please Sign up or sign in to vote.
2.60/5 (3 votes)
14 Nov 2012CPOL2 min read 61.8K   1.4K   9   4
A java Bowling Score Board Game.

Image 1

Introduction

The following Java console code is about viewing your scores after each sequence of Pins the player hits. The Java Console Bowling program is useful for keeping track of all your score and displaying all the scenarios possible each time you enter your bowling scores.

Also, this java code is useful as a plugin to incorporate it within your java program because it can populate data within a array structure and calculating the variables stored.   

Enjoy!  

Background

The  Java Game allows you to add your scores and keep track of the player's score as they hit each Pin in the Bowling game. Ball 1 score shows the number of pins knocked down with the first ball and Ball 2 score shows the number of pins knocked down with the second ball while assuming a second ball is used. The total score will display the total score up to and including that possible 10th frame extra ball.

Image 2

Using the code

To use the code, you will need a java editor such as TextPad and the Java JDK on your computer. Using Java classes can run on the major Operating Systems on the market with the JDK installed.

On completing the installation requirements, running the java code should be simple. However, if you like to create a bat or exe file to run your class file it can be done.  Enter the following code with your text editor and save it as Bowling.bat  

echo "Launching My Java Bowling game Score Board"
java -jar "location of java file (Bowling.java)"

Display the Console Menu Interface

The following java code below shows the menu of the Java game (refer to the comments): 

Java
//
//Main 
//Menu

int frme[] = new int[10]; // Init Frame in a 1 dimension Array
int allfrme[][] = new int[2][12]; // Init Frames with 2 Extra Pins in 2 dimension Array
String s; // Input String
 
 do { //A loop to get all input string from user and 
  System.out.println("\n\t  Welcome to Java Bowling Console 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: //Init the arrays

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

   while(!chk) { //Accepting string inputs and validating it
    System.out.printf("\n\t   FRAME %2d ", new Object[] {
	 Integer.valueOf(i + 1)	});

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

     if(Pins <= 10 && Pins >= 0) {
	  allfrme[0][i] = Pins;
	  chk = true;
	 }
   } 
   if(Pins == 10) { //Displaying to user that he made a Strike when equal to 10 pins
     System.out.println("\n\t\t\tExcelent, Strike!!");
	 continue;

   }
  chk = false;

Displaying your bowling score in two dimension

The code below display the Score after the user or player finish entering their Bowling scores. As the user enter each strings place it within its addresses, convert the string as an object int value and calculate each interval value as int as place in each array address. 

Java
//Calculating values
//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];

}

//Printing Values
System.out.print("\n\n\t  FRAME"); // Frame Heading
//Init object loops to get and add values to the right address
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("\n\n\tBall 1 ");

  for(int l = 0; l < 10; l++)
      System.out.printf("%4d", new Object[] {
	 Integer.valueOf(allfrme[0][l])
     });
 //Displaying the array addresses in each frame.
 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("\n\tBall 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("\n\n\t  SCORE");
  for(int j1 = 0; j1 < 10; j1++)
	System.out.printf("%4d", new Object[] {
		Integer.valueOf(frme[j1])
	});
  //Prompt to the user 
  System.out.print("\n\n\t\t\tPlay more (Y/N)? ");
  s = keyIn.next();
  String s1 = keyIn.nextLine();

  } while(s.toUpperCase().charAt(0) == 'Y');
}

Really simple, yet complicated java program because of getting the values to print in a two dimension pattern in Console. Run it yourself and discuss any issues, suggestions, and improvements towards creating a bowling game. 

Points of interest

Challenge yourself and see how you could use few set of for loops with if statements to achieve various programming issues. 

History

Created: Nov. 30 2011. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer sirdre.com
Canada Canada
I'm fascinated about new technology and innovations.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Dave Kreskowiak11-Sep-14 4:29
mveDave Kreskowiak11-Sep-14 4:29 
QuestionPoor Programming Pin
Mufim8-Oct-13 6:05
Mufim8-Oct-13 6:05 
BugWrong score... Pin
Stewart Roberts27-Apr-12 3:03
Stewart Roberts27-Apr-12 3:03 
SuggestionWhere's the beef? Pin
Slacker00727-Apr-12 1:07
professionalSlacker00727-Apr-12 1:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.