简体   繁体   English

在骰子游戏中创建堆空间问题的Go方法

[英]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. 我正在编写代码,其中用户掷出5个自定义骰子,并且每次掷出5或10,该数字就会加到他们的总得分中,直到总得分达到500,然后游戏结束。 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. 我拥有构造go方法所需的所有方法,但是如果没有它导致我耗尽堆空间,我就无法弄清楚。

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. 我目前的go方法为我提供了玩家获得的掷骰的数组,但无论他们实际掷出什么,都只是说更新的分数为0。 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 注意: go方法在游戏类中

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

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. 您应该只调用dice.rollDice()一次,并将其结果存储在变量中。 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 . 您编写的方式ArrayList<Integer> diesrolled保持增长,并且for循环永远不会结束,因为您在for循环和条件的每次迭代中都调用dice.rollDice() ,因为size()不会满足i = dice.rollDice().size() size()增长速度比i快,直到最终获得OutOfMemoryException为止。

Solution: 解:

  • move ArrayList<Integer> diesrolled = new ArrayList<Integer>(); 移动ArrayList<Integer> diesrolled = new ArrayList<Integer>(); to rollDice() method rollDice()方法

  • rewrite addFivesOrTens() method as: addFivesOrTens()方法重写为:

     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: go()方法重写为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM