I'm currently coding a Risk game in Java and I'm struggling on the attack part of the code.
For those of you who don't know how an attack turn works in a Risk game, I will explain briefly : Here we are in the case where the attacker uses 3 units to attack an opposing territory with 2 units to defend it. For every unit involved in the fight, a dice will be rolled. So this means that the attacker is going to roll 3 dices and the defender will roll 2 dices. Then, the goal is to store the 3 values of the attacker dices, order them in decreasing order, then do the same for the 2 values of the defender and compare the two lists to deduce who has won and who has lost the fight (for further information on how a risk attack is working, please check this website : https://www.wikihow.com/Play-Risk)
Now comes the issue. In my risk game, I have 3 different types of units, a rifleman (called carabinier in my code because I'm french), a paratrooper and a gunner. During a fight the only important difference between these 3 units is that the limits of the dice are different : - Rifleman : dice will roll between 1 and 6 included - Paratrooper : between 2 and 7 included - Gunner : between 4 and 9 included.
So that means that a gunner has a higher chance of winning a fight.
In my dice class, There are two important methods : the first one is the one rolling a dice and returning the result :
public int resultatdes (){
//borneinf = lower possible value of the dice
//bornesup = higher possible value of the dice
int resultat = 0;
Random rand = new Random();
resultat = rand.nextInt(this.bornesup) + this.borneinf ;
return resultat;
}
The second method is the one that will return the result of the fight (number of units lost for the attacker and defender). Here I'm just going to show you a case where the attacker attacks with 1 rifleman, 1 paratrooper and 1 gunner, because there are too many possibilities, it wouldn't be relevant to show them all :
Case where the attacker uses 1 rifleman, 1 paratrooper and 1 gunner :
public void resultat_combat(ArrayList<Integer> attaque, ArrayList<Integer> defense ){
//a1 = number of rifleman for the attacker
//a2 = number of paratrooper for the attacker
//a3 = number of gunner for the attacker
//d1, d2, d3 are the same but for the defender
attaque.addAll(Arrays.asList(a1, a2, a3));
defense.addAll(Arrays.asList(d1, d2, d3));
if (a1 + a2 + a3 == 3 && a1 == 1 && a2 ==1 && a3 == 1){
Des de1 = new Des(1,6); //creating dice for the rifleman
Des de2 = new Des(2,7); //creating dice for the paratrooper
Des de3 = new Des(4,9); //creating dice for the gunner
int a = de1.resultatdes(); //rolling the dices
int b = de2.resultatdes();
int c = de3.resultatdes();
//Here there should be a code to store the results in a list, order it in decreasing order and compare it to the defense list
}
Same but for the defender, where he is defending with two riflemans :
if (d1 + d2 + d3 == 2 && d1==2){
Des de4 = new Des(1,6);
Des de5 = new Des(1,6);
int d = de4.resultatdes();
int e = de5.resultatdes();
}
MY ISSUE : When storing the results of the dice in a list or in a table, I know that the first value of the list is the one of the rifleman, the second the one of the paratrooper and the third the one of the gunner. BUT when I sort the list in decreasing order, I am loosing track of which dice result is matching which unit. Therefore I am able to say who won between the attacker dice and the defender dice, but I can't know which type of unit has died because I can't link the value of a dice sorted in a list to a unit.
Do you know how to do this ?
Thank you in advance for taking your time to read this long question, I tried to be as clear as possible, even though english is not my native language.
As suggsted in the other answer, an OO program would have classes representing the different objects in the domain.
If you are unfamiliar with these concepts then, to address the immediate issue you could create a simple class to encapsulate the result of a roll and its associated type.
Firstly, we can create an Enum to represent the types:
public enum UnitType {
RIFLEMAN, PARATROOPER, GUNNER;
}
Then we can create a class to capture the result of a roll. This stores the result of the roll and its associated type and implements the java.lang.Comparable iterface so we can use Collections.sort() without having to specify a comparator;
public class DiceRollResult implements Comparable<DiceRollResult> {
private UnitType unitType;
private int result;
public DiceRollResult(UnitType unitType, int result) {
this.unitType = unitType;
this.result = result;
}
public UnitType getUnitType() {
return unitType;
}
public int getResult() {
return result;
}
@Override
public int compareTo(DiceRollResult other) {
return this.result - other.result;
}
}
A Test class:
public class DiceRollResultTest {
public static void main(String[] args) {
List<DiceRollResult> results = new ArrayList<>();
results.add(new DiceRollResult(UnitType.GUNNER, new
Random().nextInt(6)));
results.add(new DiceRollResult(UnitType.PARATROOPER, new
Random().nextInt(6)));
results.add(new DiceRollResult(UnitType.RIFLEMAN, new
Random().nextInt(6)));
Collections.sort(results);
for (int i = 0; i < results.size(); ++i) {
System.out.println("The result at position " + i + " is " +
results.get(i).getResult() + " for "
+ results.get(i).getUnitType());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With