Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

java - Go method creating heap space issue in dice game

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

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

1 Reply

0 votes
by (71.8m points)

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();
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...