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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…