Dicewars-Java-Approach/server/game-logic/Manager/GameManager.java

108 lines
3.2 KiB
Java

package manager;
import manager.attackinformations.AttackInformations;
import manager.field.Field;
import manager.field.Player;
import java.util.ArrayList;
import java.util.Random;
public class GameManager {
private ArrayList<Field> fields;
private ArrayList<Player> players;
private Random rand_num_gen;
public GameManager(){
fields = new ArrayList<Field>();
players = new ArrayList<Player>();
rand_num_gen = new Random();
}
public ArrayList<Player> get_players(){
return players;
}
public void add_player(String input){
if(players.size() < 8){
Player pl = new Player(input,players.size());
players.add(pl);
}
}
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 att_field_allowed = false;
boolean def_field_allowed = false;
for(Field field:fields){
if(field == att_field){
att_field_allowed = true;
}
if(field == def_field){
def_field_allowed = true;
}
}
if(att_field.get_dice_number()<= 1){
att_field_allowed = false;
}
for(Field neigh:att_field.get_neighbours()){
if(neigh == def_field){
attack_allowed = true;
break;
}
}
attack_allowed = att_field_allowed && def_field_allowed && attack_allowed && (att_field.get_owner() != def_field.get_owner());
AttackInformations infor;
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 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++){
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++){
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);
// We update the Status of the different Fields in this part:
if(infor.get_attack_success()){
def_field.set_dice_number(java.lang.Math.max(att_field.get_dice_number()-1,1));
def_field.set_owner(att_field.get_owner());
}
else{
att_field.set_dice_number(1);
}
}
else{
infor = new AttackInformations(-1,-1,false);
}
return infor;
}
public ArrayList<Field> get_fields(){
return fields;
}
public void new_game(ArrayList<Field> inp){
fields = inp;
}
}