10 Replies - 11587 Views - Last Post: 24 August 2007 - 07:58 AM Rate Topic: -----

#1 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Help with bowling scoring project!

Post icon  Posted 22 August 2007 - 10:35 PM

In this project, you are asked to implement class BowlingLine that maintains the score for one game of bowling for one person. There main goal is to maintain the total score that would appear to the right of the bowling scoring system (0 at the beginning of a new game). Here is one version of the scoring rules:

http://oncampus.rich...th/bowling.html



The unit tests must be named BowlingLineTest.java pass. Here are some suggestions for a test plan that were derived together in lecture: BowlingLineTestPlan.txt. The class must be named BowlingLine with the following two methods (please excuse me for giving you the design, but I do this so I can run my unit test):


/**

 * React to the number of pins just knocked don by the bowler.

 * Precondition: 0 <= theRoll <= 10 and the game is not over.

 * @param theRoll

 *		 The number of pins detected as knocked down on one roll in a

 *		 bowling lane

 */

public void pinsDowned(int theRoll) 

 

/**

 * The score that is supposed to appear to the right of a bowling line.

 * @return The score of the total game if the bowler rolls gutter balls on

 *		 each and every possible successive roll.

 */

public int scoreSoFar() {



I am really lost on how to count the scores if a strike happens. I know the rules of strike, however I am confused on how to keep track on the next pin down after the strike with using these two methods

Is This A Good Question/Topic? 0
  • +

Replies To: Help with bowling scoring project!

#2 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Help with bowling scoring project!

Posted 22 August 2007 - 11:32 PM

It would be recommended to keep track of the rolls by storing them in an array. This array contains 20 ints (2 for each of the 10 frames). Having this data, it becomes relatively easy to keep track of the scores. At start, all elements of this array should be -1, meaning we haven't rolled this one. We'll use these guidelines for each element of the array:

-1: The ball hasn't been thrown yet.
0: The ball didn't hit anything or fell to the side of the alley.
1-9: The ball knocked a certain number of pins.
10: The ball knocked all pins down. It can be either a strike or a spare, the actual roll with figure it out.

Here's what your classes should look like:




Code 1.0: BowlingLineText.java
public class BowlingLineTest{
	public static void main (String[] args){
		BowlingLine bl = new BowlingLine(); //Create an object
		//Now we can call the various methods of that class. Up to you to put that code up.
	}
}




Code 1.1: BowlingLine.java
public class BowlingLine{
	int[] rolls = new int[20]; //The array I've been talking about
	int turn = 1; //The actual roll (1 to 20, because there are 2 rolls in a frame)

	public BowlingLine(){ //Constructor
		for (int i=0; i<20; i++){
			rolls[i] = -1; //Set all element to -1 (not rolled)
		}
	}

	public void pinsDowned(int pins){ //We downed a certain number of pins
		rolls[turn-1] = pins; //Record the roll
		if (pins == 10 && turn%2 == 1){ //We made a strike
			rolls[turn] = 0;
			turn += 1; //End the frame, no need to roll second ball
		}
		turn += 1; //Switch to next roll
	}

	public int strikeValueAt(int tu){ //Used internally to get the value of a strike at a certain roll
		int val = 0; //Holds the return value
		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		int t = rolls[tu+3]; //The value of the second next roll (the second one of the next frame)
		if (s == 10){ //Strike
			val = strikeValueAt(t+1);
		}
		else if (s+t == 10){ //Spare
			val = spareValueAt(t+1);
		}
		else if (s != -1 && t != -1){ //Regular rolls
			val = s+t+10;
		}
		return val; //Return the found value
	}

	public int spareValutAt(int tu){ //Used internally to get the value of a spare at a certain roll
		int val = 0; //Holds the return value
		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		if (s == 10){ //Strike
			val = strikeValueAt(t+2);
		}
		else if (s != -1){ //Regular roll
			val = s+10;
		}
		return val; //Return the found value
	}

	public int scoreSoFar(){ //We want to get the score
		int score = 0; //The score will increment
		int inc = 0; //Used to count strike/spare scores
		for (int i=0; i<20; i+=2){ //Through all our frames
			int s = rolls[i]; //We will work with that
			int t = rolls[i+1]; //We will work with that
			if (s == 10){ //Strike
				inc = strikeValueAt(i);
			}
			else if (s+t == 10){ //Spare
				inc = spareValueAt(i);
			}
			else{ //Regular roll
				if (s != -1){
					inc += s; //Add the number of pins knocked down
				}
				if (t != -1){
					inc += t; //Add the pins
				}
			}
			score += inc; //Add to score
		}
		return score; //Return the total score
	}
}


Please note that the code was not tested, I just helped you to get on the right track although the work seems almost achieved. Remember one thing: sometimes a function needs to be recursive, meaning it calls itself when running. This is used for the strikeValueAt(int) method. This is a simple thing in Java but at some places (file browser, this project...) it is mendatory. Any more help needed just reply :)
Was This Post Helpful? 0
  • +
  • -

#3 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Help with bowling scoring project!

Posted 23 August 2007 - 11:26 AM

View Postalpha02, on 22 Aug, 2007 - 11:32 PM, said:

It would be recommended to keep track of the rolls by storing them in an array. This array contains 20 ints (2 for each of the 10 frames). Having this data, it becomes relatively easy to keep track of the scores. At start, all elements of this array should be -1, meaning we haven't rolled this one. We'll use these guidelines for each element of the array:

-1: The ball hasn't been thrown yet.
0: The ball didn't hit anything or fell to the side of the alley.
1-9: The ball knocked a certain number of pins.
10: The ball knocked all pins down. It can be either a strike or a spare, the actual roll with figure it out.

Here's what your classes should look like:




Code 1.0: BowlingLineText.java
public class BowlingLineTest{
	public static void main (String[] args){
		BowlingLine bl = new BowlingLine(); //Create an object
		//Now we can call the various methods of that class. Up to you to put that code up.
	}
}




Code 1.1: BowlingLine.java
public class BowlingLine{
	int[] rolls = new int[20]; //The array I've been talking about
	int turn = 1; //The actual roll (1 to 20, because there are 2 rolls in a frame)

	public BowlingLine(){ //Constructor
		for (int i=0; i<20; i++){
			rolls[i] = -1; //Set all element to -1 (not rolled)
		}
	}

	public void pinsDowned(int pins){ //We downed a certain number of pins
		rolls[turn-1] = pins; //Record the roll
		if (pins == 10 && turn%2 == 1){ //We made a strike
			rolls[turn] = 0;
			turn += 1; //End the frame, no need to roll second ball
		}
		turn += 1; //Switch to next roll
	}

	public int strikeValueAt(int tu){ //Used internally to get the value of a strike at a certain roll
		int val = 0; //Holds the return value
		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		int t = rolls[tu+3]; //The value of the second next roll (the second one of the next frame)
		if (s == 10){ //Strike
			val = strikeValueAt(t+1);
		}
		else if (s+t == 10){ //Spare
			val = spareValueAt(t+1);
		}
		else if (s != -1 && t != -1){ //Regular rolls
			val = s+t+10;
		}
		return val; //Return the found value
	}

	public int spareValutAt(int tu){ //Used internally to get the value of a spare at a certain roll
		int val = 0; //Holds the return value
		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		if (s == 10){ //Strike
			val = strikeValueAt(t+2);
		}
		else if (s != -1){ //Regular roll
			val = s+10;
		}
		return val; //Return the found value
	}

	public int scoreSoFar(){ //We want to get the score
		int score = 0; //The score will increment
		int inc = 0; //Used to count strike/spare scores
		for (int i=0; i<20; i+=2){ //Through all our frames
			int s = rolls[i]; //We will work with that
			int t = rolls[i+1]; //We will work with that
			if (s == 10){ //Strike
				inc = strikeValueAt(i);
			}
			else if (s+t == 10){ //Spare
				inc = spareValueAt(i);
			}
			else{ //Regular roll
				if (s != -1){
					inc += s; //Add the number of pins knocked down
				}
				if (t != -1){
					inc += t; //Add the pins
				}
			}
			score += inc; //Add to score
		}
		return score; //Return the total score
	}
}


Please note that the code was not tested, I just helped you to get on the right track although the work seems almost achieved. Remember one thing: sometimes a function needs to be recursive, meaning it calls itself when running. This is used for the strikeValueAt(int) method. This is a simple thing in Java but at some places (file browser, this project...) it is mendatory. Any more help needed just reply :)



First of all I would like to thank you for helping me out. It clears a lot of what the project was asking. However, you mentioned that I need to do recursion here at strikeValueAt(), however your style at this method does not resemble any kind of base case that is used if you are going to use recursive? How is this possible?

I also noticed that there are error's in your code:

   val = strikeValueAt(t+2); 


in this method they can't find t because t is located on the other method, are you trying to say tu here??

and one more thing that sometimes people forget, on the last frame (frame 10), we have a chance to make 3 hit, how's that going to be

This post has been edited by EquinoX: 23 August 2007 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Help with bowling scoring project!

Posted 23 August 2007 - 03:17 PM

View PostEquinoX, on 23 Aug, 2007 - 02:26 PM, said:

in this method they can't find t because t is located on the other method, are you trying to say tu here?


Yes, I changed the variable names many times and I forgot to do it here, that's why I am telling you that the code wasn't tested.

View PostEquinoX, on 23 Aug, 2007 - 02:26 PM, said:

and one more thing that sometimes people forget, on the last frame (frame 10), we have a chance to make 3 hit, how's that going to be


I will lead you on the right path: resize the variable which holds all the rolls to 21 elements. If you really can't figure out how to do this, I can help you out.
Was This Post Helpful? 0
  • +
  • -

#5 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Help with bowling scoring project!

Posted 23 August 2007 - 05:19 PM

thank you.. I am working on the 21 elements now. One more thing, I tested the program for two strikes at a row and it doesn't work. I think the problem is at:

if (s == 10){ //Strike
			val = strikeValueAt(t+1);
		}


should I add 10 to the val

This post has been edited by EquinoX: 23 August 2007 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Help with bowling scoring project!

Posted 23 August 2007 - 07:33 PM

View PostEquinoX, on 23 Aug, 2007 - 08:19 PM, said:

thank you.. I am working on the 21 elements now. One more thing, I tested the program for two strikes at a row and it doesn't work. I think the problem is at:

if (s == 10){ //Strike
			val = strikeValueAt(t+1);
		}


should I add 10 to the val


Yes, you must add 10 to the value of the next strike :)

if (s == 10){ //Strike
			val = strikeValueAt(t+1)+10;
		}


Was This Post Helpful? 0
  • +
  • -

#7 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Help with bowling scoring project!

Posted 23 August 2007 - 08:56 PM

View Postalpha02, on 23 Aug, 2007 - 07:33 PM, said:

View PostEquinoX, on 23 Aug, 2007 - 08:19 PM, said:

thank you.. I am working on the 21 elements now. One more thing, I tested the program for two strikes at a row and it doesn't work. I think the problem is at:

if (s == 10){ //Strike
			val = strikeValueAt(t+1);
		}


should I add 10 to the val


Yes, you must add 10 to the value of the next strike :)

if (s == 10){ //Strike
			val = strikeValueAt(t+1)+10;
		}



one thing that doesn't work is on the test below:
BowlingLine bowl = new BowlingLine();
		bowl.pinsDowned(3);
		bowl.pinsDowned(5);
		bowl.pinsDowned(10);
		bowl.pinsDowned(5);
		bowl.pinsDowned(2);
		bowl.pinsDowned(9);
		bowl.pinsDowned(1);
		bowl.pinsDowned(10);
		bowl.pinsDowned(7);
		bowl.pinsDowned(3);

assertEquals(72, bowl.scoreSoFar());



this test always fails and I am still searching for it why
Was This Post Helpful? 0
  • +
  • -

#8 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Help with bowling scoring project!

Posted 23 August 2007 - 08:59 PM

You ended your list of rolls by a spare. The spare and all strikes have a value of zero until you knocked other pins!
Was This Post Helpful? 0
  • +
  • -

#9 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Help with bowling scoring project!

Posted 23 August 2007 - 09:12 PM

View Postalpha02, on 23 Aug, 2007 - 08:59 PM, said:

You ended your list of rolls by a spare. The spare and all strikes have a value of zero until you knocked other pins!


so am is 72 the wrong score here or is the code??
Was This Post Helpful? 0
  • +
  • -

#10 alpha02  Icon User is offline

  • Sexy DIC God
  • member icon

Reputation: 47
  • View blog
  • Posts: 803
  • Joined: 20-May 06

Re: Help with bowling scoring project!

Posted 23 August 2007 - 11:27 PM

Fixed via IM, here's the full code:

public class BowlingLine{
	int[] rolls = new int[21]; //The array I've been talking about
	int turn = 1; //The actual roll (1 to 21, because there are 2 rolls in a frame except for the 10th)

	public BowlingLine(){ //Constructor
		for (int i=0; i<21; i++){
			rolls[i] = -1; //Set all element to -1 (not rolled)
		}
	}

	public void pinsDowned(int pins){ //We downed a certain number of pins
		rolls[turn-1] = pins; //Record the roll
		if (pins == 10 && turn%2 == 1 && turn < 19){ //We made a strike
			rolls[turn] = 0;
			turn += 1; //End the frame, no need to roll second ball
		}
		turn += 1; //Switch to next roll
	}

	public int strikeValueAt(int tu){ //Used internally to get the value of a strike at a certain roll
		int val = 0; //Holds the return value

		if (tu == 18){ //Strike on the 10th frame
			return rolls[19]+rolls[20]+10;
		}

		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		int t = rolls[tu+3]; //The value of the second next roll (the second one of the next frame)
		int u = rolls[tu+4); //In case we have 2 consecutive strikes

		if ((s == 10 && u == -1) || (s < 10 && t == -1)){return 0;} //Cannot determine for now
		val += s;
	if (s == 10){val += u;} //We have a strike in the next frame, so add the other frame's score
		else{val += t;} //Normal add
		return val+10; //Return the found value
	}

	public int spareValueAt(int tu){ //Used internally to get the value of a spare at a certain roll
		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		if (s == -1){return 0;} //Cannot determine for now
		return 10+s;
	}

	public int scoreSoFar(){ //We want to get the score
		int score = 0; //The score will increment
		int inc = 0; //Used to count strike/spare scores

		for (int i=0; i<20; i+=2){ //Through all our frames
			int s = rolls[i]; //We will work with that
			int t = rolls[i+1]; //We will work with that
			int u = rolls[i+2]; //Used only on 10th frame

			if (s == 10){ //Strike
				inc = strikeValueAt(i);
			}
			else if (s+t == 10){ //Spare
				inc = spareValueAt(i);
			}
			else{ //Regular roll
				if (s != -1){
					inc += s; //Add the number of pins knocked down
				}
				if (t != -1){
					inc += t; //Add the pins
				}
			}
			score += inc; //Add to score
			inc = 0;
		}
		return score; //Return the total score
	}
}


Hope this can help anyone!
Was This Post Helpful? 0
  • +
  • -

#11 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Help with bowling scoring project!

Posted 24 August 2007 - 07:58 AM

View Postalpha02, on 23 Aug, 2007 - 11:27 PM, said:

Fixed via IM, here's the full code:

public class BowlingLine{
	int[] rolls = new int[21]; //The array I've been talking about
	int turn = 1; //The actual roll (1 to 21, because there are 2 rolls in a frame except for the 10th)

	public BowlingLine(){ //Constructor
		for (int i=0; i<21; i++){
			rolls[i] = -1; //Set all element to -1 (not rolled)
		}
	}

	public void pinsDowned(int pins){ //We downed a certain number of pins
		rolls[turn-1] = pins; //Record the roll
		if (pins == 10 && turn%2 == 1 && turn < 19){ //We made a strike
			rolls[turn] = 0;
			turn += 1; //End the frame, no need to roll second ball
		}
		turn += 1; //Switch to next roll
	}

	public int strikeValueAt(int tu){ //Used internally to get the value of a strike at a certain roll
		int val = 0; //Holds the return value

		if (tu == 18){ //Strike on the 10th frame
			return rolls[19]+rolls[20]+10;
		}

		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		int t = rolls[tu+3]; //The value of the second next roll (the second one of the next frame)
		int u = rolls[tu+4); //In case we have 2 consecutive strikes

		if ((s == 10 && u == -1) || (s < 10 && t == -1)){return 0;} //Cannot determine for now
		val += s;
	if (s == 10){val += u;} //We have a strike in the next frame, so add the other frame's score
		else{val += t;} //Normal add
		return val+10; //Return the found value
	}

	public int spareValueAt(int tu){ //Used internally to get the value of a spare at a certain roll
		int s = rolls[tu+2]; //The value of the next roll (the first one of the next frame)
		if (s == -1){return 0;} //Cannot determine for now
		return 10+s;
	}

	public int scoreSoFar(){ //We want to get the score
		int score = 0; //The score will increment
		int inc = 0; //Used to count strike/spare scores

		for (int i=0; i<20; i+=2){ //Through all our frames
			int s = rolls[i]; //We will work with that
			int t = rolls[i+1]; //We will work with that
			int u = rolls[i+2]; //Used only on 10th frame

			if (s == 10){ //Strike
				inc = strikeValueAt(i);
			}
			else if (s+t == 10){ //Spare
				inc = spareValueAt(i);
			}
			else{ //Regular roll
				if (s != -1){
					inc += s; //Add the number of pins knocked down
				}
				if (t != -1){
					inc += t; //Add the pins
				}
			}
			score += inc; //Add to score
			inc = 0;
		}
		return score; //Return the total score
	}
}


Hope this can help anyone!



the code however still fails for this test:

@Test
public void testSpare() {
		BowlingLine bowl = new BowlingLine();
		bowl.pinsDowned(5);
		bowl.pinsDowned(5);
		bowl.pinsDowned(3);
		bowl.pinsDowned(7);
		bowl.pinsDowned(4);
		bowl.pinsDowned(3);
		assertEquals(34, bowl.scoreSoFar());
		bowl.pinsDowned(10);
		bowl.pinsDowned(6);
		bowl.pinsDowned(2);
		assertEquals(60, bowl.scoreSoFar());
		bowl.pinsDowned(10);
		bowl.pinsDowned(10);
		assertEquals(90, bowl.scoreSoFar());
	}

EDIT: PLEASE USE CODE TAGS

it fails on asserting 90 on the last part because the result of what the program says is 70. I just found out the error was at this part:
 if ((s == 10 && u == -1) || (s < 10 && t == -1)){return 0;} 


which should be :
 if ((s == 10 && u == -1) || (s < 10 && t == -1)){return 10 + s;}

This post has been edited by EquinoX: 24 August 2007 - 08:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1