attacking function in GameManager works now. Also tested in Test.java. And wrote some comments.

This commit is contained in:
LarsNiessen 2023-10-06 14:17:02 +02:00
parent 54235c3e75
commit 2eadb4059e
4 changed files with 71 additions and 34 deletions

View File

@ -1,5 +1,11 @@
package manager.attackinformations; package manager.attackinformations;
/**
* AttackInformations is just for the Informations of which field threw which
* Number with the current Dice, if the Attack was succesfull or not and
* if the Attack was allowed.
* **/
public class AttackInformations{ public class AttackInformations{
private int attack_field_dice; private int attack_field_dice;
private int defense_field_dice; private int defense_field_dice;

View File

@ -2,6 +2,13 @@ package manager.field;
import java.util.ArrayList; import java.util.ArrayList;
/**
* Each Field has a number of dices on it, neighbours which they can attack
* and a owner who can be changed while the Game.
* The Neighbours can be set at the beginning of the game, so the class field
* must have an option to add a neighbour as a field.
* **/
public class Field { public class Field {
private int dice_number; private int dice_number;
private ArrayList<Field> neighbours; private ArrayList<Field> neighbours;

View File

@ -6,27 +6,9 @@ import manager.field.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
class Max{
private int max;
public Max(int num1, int num2){
if(num1 < num2){
max = num1;
}
else{
max = num2;
}
}
public int get_max(){
return max;
}
}
public class GameManager { public class GameManager {
private ArrayList<Field> fields; private ArrayList<Field> fields;
private ArrayList<Player> players; private ArrayList<Player> players;
private Player current_att_pl;
private Random rand_num_gen; private Random rand_num_gen;
public GameManager(){ public GameManager(){
@ -47,6 +29,10 @@ public class GameManager {
} }
public AttackInformations attack_field(Field att_field, Field def_field){ public AttackInformations attack_field(Field att_field, Field def_field){
// In the following in Lines we check if the attack is allowed or not
// For this we have to check, that the field exist in our ArrayList
// Fields and that they are Neighbours.
boolean attack_allowed= false; boolean attack_allowed= false;
boolean att_field_allowed = false; boolean att_field_allowed = false;
boolean def_field_allowed = false; boolean def_field_allowed = false;
@ -60,37 +46,51 @@ public class GameManager {
} }
} }
if(att_field.get_dice_number()<= 1){
att_field_allowed = false;
}
for(Field neigh:att_field.get_neighbours()){ for(Field neigh:att_field.get_neighbours()){
if(neigh == def_field){ if(neigh == def_field){
attack_allowed = true; attack_allowed = true;
break;
} }
} }
attack_allowed = att_field_allowed && def_field_allowed && attack_allowed; attack_allowed = att_field_allowed && def_field_allowed && attack_allowed && (att_field.get_owner() != def_field.get_owner());
AttackInformations infor; AttackInformations infor;
if(attack_allowed){ if(attack_allowed){
// Now we know, that the Attack is allowed.
// Now we simulate the threw of the Dices.
int att_field_sum = 0; int att_field_sum = 0;
int def_field_sum = 0; int def_field_sum = 0;
// The sum of all dice-threws of the attacking Field get counted here:
for(int i = 0; i < att_field.get_dice_number();i++){ for(int i = 0; i < att_field.get_dice_number();i++){
att_field_sum += 1 + rand_num_gen.nextInt(6); att_field_sum += 1 + rand_num_gen.nextInt(6);
} }
// Analof the of all dice-threws of the defending Field get counted here:
for(int i = 0; i < def_field.get_dice_number();i++){ for(int i = 0; i < def_field.get_dice_number();i++){
def_field_sum += 1 + rand_num_gen.nextInt(6); def_field_sum += 1 + rand_num_gen.nextInt(6);
} }
// We save the Attackinformations here, so they can be given out at the end:
infor = new AttackInformations(att_field_sum, def_field_sum,true); infor = new AttackInformations(att_field_sum, def_field_sum,true);
// We update the Status of the different Fields in this part:
if(infor.get_attack_success()){ if(infor.get_attack_success()){
Max M = new Max(att_field.get_dice_number()-1,1); def_field.set_dice_number(java.lang.Math.max(att_field.get_dice_number()-1,1));
def_field.set_dice_number(M.get_max());
def_field.set_owner(att_field.get_owner()); def_field.set_owner(att_field.get_owner());
} }
else{
att_field.set_dice_number(1); att_field.set_dice_number(1);
} }
}
else{ else{
infor = new AttackInformations(-1,-1,false); infor = new AttackInformations(-1,-1,false);
} }
@ -102,15 +102,7 @@ public class GameManager {
return fields; return fields;
} }
public void new_game(){ public void new_game(ArrayList<Field> inp){
fields.clear(); fields = inp;
int num_fields = 20 + rand_num_gen.nextInt(10);
int[] player_num_fields = new int[players.size()];
for(int x: player_num_fields){
x = 0;
}
} }
} }

View File

@ -1,11 +1,15 @@
package manager; package manager;
import manager.GameManager; import manager.GameManager;
import manager.field.Field;
import manager.field.Player; import manager.field.Player;
import java.util.ArrayList;
public class Test { public class Test {
public static void main(String[] args){ public static void main(String[] args){
GameManager gm = new GameManager(); GameManager gm = new GameManager();
// Test to add Players to the Gamemanager and print them:
gm.add_player("Sebastian"); gm.add_player("Sebastian");
gm.add_player("Lars"); gm.add_player("Lars");
gm.add_player("Nils"); gm.add_player("Nils");
@ -13,5 +17,33 @@ public class Test {
for(Player current:gm.get_players()){ for(Player current:gm.get_players()){
System.out.println(current.get_name()); System.out.println(current.get_name());
} }
// Test to add a Field-List to the Gamemanager:
ArrayList<Field> fields = new ArrayList<Field>();
Field f1 = new Field(4,gm.get_players().get(0));
Field f2 = new Field(3,gm.get_players().get(1));
Field f3 = new Field(5,gm.get_players().get(2));
f1.add_neighbour(f2);
f2.add_neighbour(f1);
fields.add(f1);
fields.add(f2);
fields.add(f3);
gm.new_game(fields);
// Test to attack a neighboured fields:
System.out.println("\n The Owner of the First Field is:");
System.out.println(gm.get_fields().get(0).get_owner().get_name());
System.out.println("The Number of Dices of the First Field is:");
System.out.println(gm.get_fields().get(0).get_dice_number());
System.out.println("\n");
gm.attack_field(gm.get_fields().get(1),gm.get_fields().get(0));
System.out.println("\n Attack Field 1 with Field 2: \n");
System.out.println("\n The Owner of the First Field is:");
System.out.println(gm.get_fields().get(0).get_owner().get_name());
System.out.println("The Number of Dices of the First Field is:");
System.out.println(gm.get_fields().get(0).get_dice_number());
System.out.println("\n");
} }
} }