mercredi 14 mars 2018

Bowling Game Error in Java

I am having problems with my code. Any help would be appreciated.

I have made a bowling game in java, but my first bowls and second bowls in one frame sometimes add up to more than 10. Which should be impossible.

I think its an error in the bounds for the getTwo() method in the Pins Class but I am not sure. The bounds for getTwo have to be (10 pins - knockedPins1). knockedPins1 = the Random number knocked down in the first bowl.

This is my code:

Pins Class:

 public class Pins {
        private int pinsLeft;
        public int totalPins = 0;
        public int knockedPins1;
        public int knockedPins2;
        public int currentRoll = 0;
        static Random random = new Random();
        static Random rand = new Random();

    public int getKnockedPins1() {
        knockedPins1 = random.nextInt(10);
        return knockedPins1;
    }

    public int getTwo() {
        knockedPins2 = rand.nextInt(10 - knockedPins1);
        return knockedPins2;
    }

Frames Class:

public class Frames {

    public int firstThrow;
    public int secondThrow;
    private boolean thirdThrow;
    public int frameNumber;
    public Frames nextFrame;
    public int rollNumber;

    public Frames(int frameNumber){
        Pins pins = new Pins();
        this.firstThrow = pins.knockedPins1;
        this.secondThrow = pins.knockedPins2;
        this.thirdThrow = thirdThrow;
        this.frameNumber = frameNumber;
        this.rollNumber = pins.currentRoll;
    }

    public int bowlOne(){
        Pins pins = new Pins();
        frameNumber = currentFrameNumber();
        System.out.println(pins.getKnockedPins1());
        Game.pinsKnocked1 += pins.getKnockedPins1();

        return Game.pinsKnocked1;
    }

    public int bowlTwo(){
        Pins pins = new Pins();
        frameNumber += 1;
        System.out.println(pins.getTwo());
        Game.pinsKnocked2 += pins.getTwo();

        return Game.pinsKnocked2;
    }
}

Games Class:

public class Game {
    public static ArrayList <Players> players;
    public static ArrayList <Frames> frames;
    public static int pinsKnocked1 = 0;
    public static int pinsKnocked2 = 0;

    public Game() {
        this.players = new ArrayList <Players>(8);
        this.frames = new ArrayList <Frames>(10);
    }

    public void getBowlOne(){
        Pins pins = new Pins();
        Frames frames = new Frames(0);
        for (int x = 0; x < 10; x++) {
            frames.bowlOne();
        }
    }

    public void getBowlTwo(){
        Pins pins = new Pins();
        Frames frames = new Frames(0);
        for (int x = 0; x<10; x++){
            frames.bowlTwo();
        }
    }




Aucun commentaire:

Enregistrer un commentaire