jeudi 31 mars 2016

(Java) Bowling object storing incorectly

Im and trying to make a program to calculate a bowling score base on a string. I have created a class that stores each frame as a seperate object, which stores 3 intergers for each roll, and two boolean variables that store wethere or not the frame is a strike or a spare. I am having trouble getting the spare to store the rolls correctly. Below is the code I am using to create the objects and sgtore the data.

        String gameTemp = "1 0 1 / 2 2 X 3 3 X 1 / 3 / X 1 2";
    game = gameTemp.replace(" ", "");

    temp = game.charAt(i);

    if( temp == 'X'){
        roll1 = 10;
        roll2 = 0;
        strike = true;
        i++;
    }
    else{
        if( temp == '0'){
            roll1 = 0;
            i++;
        }
        else{
            roll1 = Character.getNumericValue( temp);
            i++;
        }
        temp = game.charAt(i);
        if( temp == '/'){
            roll2 = 10 - roll1;
            spare = true;
            i++;
        }
        else if( temp == '0'){
            roll2 = 0;
            i++;
        }
        else{
            roll2 = Character.getNumericValue( temp);
            i++;
        }
    }
    bowlingObj f1 = new bowlingObj( roll1, roll2, strike, spare);
    strike = false;
    spare = false;
    roll1 = 0;
    roll2 = 0;

    if( temp == 'X'){
        roll1 = 10;
        roll2 = 0;
        strike = true;
        i++;
    }
    else{
        if( temp == '0'){
            roll1 = 0;
            i++;
        }
        else{
            roll1 = Character.getNumericValue( temp);
            System.out.println(roll1);
            i++;
        }
        temp = game.charAt(i);
        if( temp == '/'){
            roll2 = 10 - roll1;
            spare = true;
            i++;
        }
        else if( temp == '0'){
            roll2 = 0;
            i++;
        }
        else{
            roll2 = Character.getNumericValue( temp);
            i++;
        }
    }
    bowlingObj f2 = new bowlingObj( roll1, roll2, strike, spare);

    System.out.println( f1);
    System.out.println( f2);
  }

This is the output. A number represents one roll:

[ 1 0]
[ 0 10]

This is the input:

1 0 1 /

it should store:

   [ 1 0]
   [ 1 9]

Aucun commentaire:

Enregistrer un commentaire