Go method creating heap space issue in dice game

馋奶兔 提交于 2019-12-12 07:03:31

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/48602043/go-method-creating-heap-space-issue-in-dice-game

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!