I have tried my best looking in the debugger and testing, but i cannot wrap my mind around this problem.
In this method it skips going inside the for loop. But in the roll() method, the arraylist gets values?
public int lol(int a) {
num = 0;
for (int i = 0; i < objectDie.getDices().size(); i++) {
if (objectDie.getDices().get(i) == a) {
num += a;
}
}
return num;
}
I will post all my classes to create a better image of the code, the troublemaker is at the bottom of the last class I have linked.
Main
package dicegame;
public class DiceGame {
public static void main(String[] args) {
Game test = new Game();
test.numberDices();
Player spil = new Human();
spil.takeTurn(test);
}
}
Game
package dicegame;
import java.util.Scanner;
public class Game {
Die DieObjekt = new Die();
Scanner sc = new Scanner(System.in);
public void numberDices() {
System.out.println("How many dices would you like to play with?");
int numberOfDices = sc.nextInt();
DieObjekt.setNumber(numberOfDices);
}
public void play() {
DieObjekt.roll();
DieObjekt.printRoll();
}
}
Die
package dicegame;
import java.util.ArrayList;
import java.util.Random;
public class Die {
ArrayList<Integer> dices = new ArrayList<>();
Random ran = new Random();
int number;
public ArrayList<Integer> getDices() {
return dices;
}
public void setNumber(int number) {
this.number = number;
}
public void roll() {
for (int i = 0; i < number; i++) {
dices.add(ran.nextInt(6) + 1);
}
}
public void printRoll() {
System.out.println("You got ");
for (int i = 0; i < dices.size(); i++) {
System.out.print(dices.get(i) + ", ");
}
}
}
Human - the last method in this class is the troublemaker
package dicegame;
import java.util.Scanner;
public class Human implements Player {
int one, two, three, four, five, six, num;
Scanner sc = new Scanner(System.in);
Die objectDie = new Die();
Game object = new Game();
@Override
public void takeTurn(Game object) {
object.play();
System.out.println("If you want to stop, press enter, if not press"
+"the number on the die that you would like to save");
int valg = sc.nextInt();
switch (valg) {
case 1:
one = lol(1);
System.out.println("You got " + one + " points in the"
+"ones");
break;
case 2:
two = lol(2);
System.out.println("You got " + two + " points in the"
+"twos");
break;
case 3:
three = lol(3);
System.out.println("You got " + three + " points in the"
+"threes");
break;
case 4:
four = lol(4);
System.out.println("You got " + four + " points in the"
+"fours");
break;
case 5:
five = lol(5);
System.out.println("You got " + five + " points in the"
+"fives");
break;
case 6:
six = lol(6);
System.out.println("You got" + six + " points in the"
+"sixes");
break;
default:
System.out.println("You have stopped, and that is OK");
break;
}
}
public int lol(int a) {
num = 0;
for (int i = 0; i < objectDie.getDices().size(); i++) {
if (objectDie.getDices().get(i) == a) {
num += a;
}
}
return num;
}
}
In class game, you finished the process to create dice list (set the total number and add the valuue with roll) => array list has values
But you forgot to do it with human class
====================
EDIT
Here is some suggestion for you to fix your game
/**
* Don't try to create dice and game in human class, you can take it from previous game which you were created in the static main
*/
//Die objectDie = new Die();
//Game object = new Game();
Just do it like this
Game object;
And then store the game object when you have it from takeTurn method
public void takeTurn(Game object) {
this.object = object;
object.play();
.....
}
And then in your lol method, try to loop through all the dice of game which you just have from takeTurn method
public int lol(int a) {
num = 0;
for (int i = 0; i < object.getDices().size(); i++) {
if (object.getDices().get(i) == a) {
num += a;
}
}
return num;
}
Finaly, remember to create getDices method for Game class :)
public class Game {
Die DieObjekt = new Die();
public List<Integer> getDices() {
return DieObjekt.getDices();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.