Results 1 to 5 of 5
  1. #1
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default Help with my Bowling Program

    I have to make a bowling program for my school and I can't really see what's wrong, I've tried to have my friends and teacher to help me but they can't.

    Here's the source code:


    import java.util.*;

    public class TestBowling{

    //************************************************** ****************************
    public static void HitPins(int b) {

    System.out.println("You hit " + b + " pin(s)");


    }
    //************************************************** ****************************
    public static void Strike(int s){

    System.out.println("You got a strike");
    System.out.println("You got a perfect 20 points");
    System.out.println("Starting new frame");
    System.out.println(" ");

    }
    //************************************************** ****************************
    public static void Gutter(int g){
    System.out.println ("You got a gutter ball");
    System.out.println("You got 0 points");
    }

    //************************************************** ****************************
    public static void Score(int h, int t){
    if (h<10&&t<2){

    System.out.println("You have " + h + " points");

    }
    else if (t>=2){

    System.out.println("You have a total of " + h + " points");
    System.out.println("Starting new frame");
    System.out.println(" ");

    }

    }

    //************************************************** **********
    public static void main (String[]args){
    Scanner keyboard=new Scanner(System.in);

    int chance;
    int start=1;
    int tries=0;
    int points=0;
    int counter=0;


    System.out.println("\t\t\tBowling Alley");
    System.out.println("\t\t\t=============");

    for (int i = 0;i<10; i++){

    do{
    System.out.print("Press the 1 button to bowl: ");
    chance=keyboard.nextInt();



    if (chance==start){

    int bowlBall=0;


    Random r= new Random();

    for (int pins=1; pins<=1;pins++){
    bowlBall=r.nextInt(10-0+1)+0;

    if (bowlBall<10){

    HitPins(bowlBall);
    counter=counter+bowlBall;
    tries++;
    Score(counter,tries);

    }


    else if (bowlBall==10){

    Strike(bowlBall);
    tries=2;
    }



    }




    }





    } while(tries<=2);

    }
    }
    }


    I'm not really good at explaining things, so I'll try the best I can to explain my problem.

    The one major problem that I have with this program is that I have is that every frame is supposed to consist of two throws unless a strike is throw. My program with correct execute this the first time(two throws per frame unless strike occurs), but it won't the other 9 times. It usually makes it one throw per frame after the first try. I appreciate any who tries to help me. I've been working on this for a week and I'm falling behind so I'll really want to finish this now.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    i didn't read through your code much, but i'm pretty sure you aren't resetting a value that indicates whether you are bowling once or twice.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Needs some work

    The more I tried to list what I found, the more complicated things got. In general, I think the idea is OK, but the logic needs to be reviewed and corrected. Some simple things I found are:
    • What is this?

    Java Code:
    bowlBall=r.nextInt(10-0+1)+0;
    Why are you subtracting and adding zero? Wouldn't the following be more appropiate?:
    Java Code:
    bowlBall=r.nextInt(11);
    • Shouldn't the strike method be returning 20 points ?


    • Get rid of the "for" loop that does nothing with the pins:

    Java Code:
    for (int pins=1; pins<=1;pins++)
    This "for" does nothing. Leave the random pin part, but remove the "for" loop.

    • The tries problem you're seeing is becasue you're not resetting the tries variable.


    For the rest , you need to sit down and figure it out on paper. I suggest that you do the program in small pieces:
    first the 2 tries/10 turns logic
    then add the points accounting
    etc...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Unknown is offline Member
    Join Date
    Dec 2008
    Posts
    15
    Rep Power
    0

    Default

    Okay, I got the points done but in a different way. I updated my source code so here it is:


    import java.util.*;

    public class TestBowling{

    //************************************************** ****************************
    public static void HitPins(int b) {

    System.out.println("You hit " + b + " pin(s)");


    }
    //************************************************** ****************************
    public static void Strike(){

    System.out.println("You got a strike");
    System.out.println("You got a perfect 20 points");
    System.out.println("Starting new frame");
    System.out.println(" ");


    }
    //************************************************** ****************************
    public static void Gutter(){

    System.out.println ("You got a gutter ball");
    System.out.println("You got 0 points");

    }

    //************************************************** ****************************
    //public static void Score(int h, int t){

    // if (h<10&&t<2){

    // System.out.println("You have " + h + " points");

    // }




    // }

    public static void Total(int p){
    System.out.println("You have a total of " + p + " points");
    System.out.println("Starting new frame");
    System.out.println(" ");

    }

    //************************************************** ****************************
    public static void main (String[]args){
    Scanner keyboard=new Scanner(System.in);
    //************************************************** ***************************
    int chance;
    int start=1;
    int tries=0;
    int points=0;
    int points2=0;
    int points3=0;
    int total=0;
    int bowlBall2=0;
    Random r= new Random();
    //************************************************** ****************************
    System.out.println("\t\t\tBowling Alley");
    System.out.println("\t\t\t=============");

    for (int frame = 0;frame<10; frame++){
    tries=0;

    do{
    System.out.print("Press the 1 button to bowl: ");
    chance=keyboard.nextInt();


    //************************************************** ****************************
    if (chance==start){

    int bowlBall=0;

    //************************************************** ****************************



    bowlBall=r.nextInt(11);

    if (tries>=1){
    bowlBall=r.nextInt(11-bowlBall);
    if(bowlBall>10-bowlBall){
    System.out.println("You got a spare");
    points3=points3+15;
    }
    else if(bowlBall<10-bowlBall){
    System.out.println("You got "+ bowlBall + " points");
    }
    }




    //************************************************** ***************************
    if (bowlBall<10&&bowlBall>0){

    HitPins(bowlBall);
    points=points+bowlBall;
    tries++;
    // Score(points,tries);
    }

    //************************************************** ****************************
    else if (bowlBall==10){
    bowlBall=20;
    points2=points2+bowlBall;
    tries=2;
    Strike();


    }

    else if (bowlBall==0){
    tries++;
    Gutter();
    // Score(bowlBall,tries);
    }

    //************************************************** ****************************






    }





    } while(tries<2);

    total=points+points2+points3;
    Total(total);

    }
    }
    }


    The last thing I have to do is to program a spare which is supposed to give you 15 points, but I don't know how to do that. Can someone help?

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    without having looked at the code the best I can give is the overall idea.
    Java Code:
    if((tries == 2) && (allPinsKnockedDown = true)){
    total += 15
    }
    granted if you're not using a boolean variable you can set it with 10 additional conditions that determine if each pin is knocked down.

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  3. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •