Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on code where a user rolls 5 customized dice and for each time they roll a 5 or 10 that number gets added to their total score until they reach a total score of 500 and then the game ends. I have all of the methods that I will need to construct my go method but I can't quite figure it out without it causing me to run out of heap space.

My current go method gives me an array of the rolls that the player got but then just says the updated score is 0 no matter what they actually roll. I then clear so that I can re-roll but that requires a loop and when I do that I get the heap space issue. I can't figure out how to create the proper game without so type of heap space issue. Any help on figuring it out would be appreciated!

something possibly like:

while (checkIfWinner() != true){
     System.out.println(dice.rollDice());
     dice.addFivesOrTens();
     System.out.println("After that roll your updated score is " + player.getTotalScore());
     dice.rollDice().clear();  
}

but this creates a heap space issue

NOTE: The go method is in the game class

Dice class:

import java.util.Random;
import java.util.*;

public class inc1_dice{
   private int die1;
   private int die2;
   private int die3;
   private int die4;
   private int die5;
   Random r = new Random();
   List<Integer> dietype1 = Arrays.asList(2, 3, 4, 5, 6, 10);
   List<Integer> dietype2 = Arrays.asList(1, 2, 4, 5, 6, 10);
   ArrayList<Integer> diesrolled = new ArrayList<Integer>();

   public ArrayList<Integer> rollDice(){
      die1 = (dietype1.get(r.nextInt(dietype1.size())));
      die2 = (dietype1.get(r.nextInt(dietype1.size())));
      die3 = (dietype1.get(r.nextInt(dietype1.size())));
      die4 = (dietype1.get(r.nextInt(dietype1.size())));
      die5 = (dietype2.get(r.nextInt(dietype2.size())));
      diesrolled.add(die1);
      diesrolled.add(die2);
      diesrolled.add(die3);
      diesrolled.add(die4);
      diesrolled.add(die5);
      return diesrolled;
   }
 }

Player class:

public class inc1_player{
   private int totalScore;

   public int getTotalScore(){
      return totalScore;
   }

   public void setTotalScore(int score){
      totalScore += score;
   }
}

Game class:

import java.io.*;
import java.util.*;
import java.util.Scanner;

public class inc1{
   private inc1_player player;
   private inc1_dice dice;

   public inc1(inc1_dice dice, inc1_player player){
      this.dice = dice;
      this.player = player;
   }

   public void go(){
         System.out.println(dice.rollDice());
         System.out.println("After that roll your updated score is " + player.getTotalScore());
         dice.rollDice().clear();            
   }

   public void addFivesOrTens(){
      int score = 0;

      for (int i = 0; i < dice.rollDice().size(); i++) {         
         if (dice.rollDice().get(i) == 5)
            score = score + 5;
            player.setTotalScore(score);              
         if (dice.rollDice().get(i) == 10)
            score = score + 10;
            player.setTotalScore(score);
      }                    
   }

   public boolean checkIfWinner(){
      if (player.getTotalScore() >= 500)
         return true;
      else
         return false;
   }  
}

Main class:

public class inc1_main
{
    public static void main(String[] args){
        inc1 inc = new inc1(new inc1_dice(), new inc1_player());
    inc.go();
}
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
120 views
Welcome To Ask or Share your Answers For Others

1 Answer

Problem is in this part of code:

  for (int i = 0; i < dice.rollDice().size(); i++) {         
     if (dice.rollDice().get(i) == 5)
        score = score + 5;
        player.setTotalScore(score);              
     if (dice.rollDice().get(i) == 10)
        score = score + 10;
        player.setTotalScore(score);
  }  

You should call dice.rollDice() only once and store its result in a variable. The way you wrote, ArrayList<Integer> diesrolled keeps growing and your for loop never ends because you call dice.rollDice() in every iteration of for loop and condition i = dice.rollDice().size() is never met because size() grows faster than i - until you finally get OutOfMemoryException.

Solution:

  • move ArrayList<Integer> diesrolled = new ArrayList<Integer>(); to rollDice() method

  • rewrite addFivesOrTens() method as:

    public void addFivesOrTens() {
      ArrayList<Integer> a = dice.rollDice();
      for (int i = 0; i < a.size(); i++) {         
         if (a.get(i) == 5)
            player.setTotalScore(5);              
         else if (a.get(i) == 10)
            player.setTotalScore(10);
      }                    
    }
    
  • rewrite go() method as:

    while (!checkIfWinner()) {
        dice.addFivesOrTens();
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...