-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattle.java
More file actions
executable file
·159 lines (140 loc) · 4.72 KB
/
Battle.java
File metadata and controls
executable file
·159 lines (140 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.Math.*;
import java.util.Random;
public class Battle {
private Random rand;
private Nodes[]buildings;
private GameBoard g;
private Nodes attack;
private Nodes defend;
private int attackLost = 0;
private int defendLost = 0;
private Boolean result = false;
public Battle(Nodes attacks, Nodes defense, GameBoard gb, Nodes[]build){
attack = attacks;
defend = defense;
rand = new Random();
buildings = build;
g = gb;
}
public int[] rollDice(){
int[] rollList = new int[5];
int highAttack = 0;
int highDefense = 0;
int lowAttack = 100;
int lowDefense = 100;
SoundClip d = new SoundClip("dicerollsound.wav");
for (int i = 0; i < 3; i++){
int roll = dice();
rollList[i] = roll;
if (roll > highAttack){
highAttack = roll;
}
if (roll > lowAttack && roll != highAttack ){
lowAttack = roll;
}
}
if (lowAttack == 100){
lowAttack = highAttack;
}
rollList[3] = dice();
rollList[4] = dice();
if(rollList[3]>rollList[4])
{
highDefense = rollList[3];
lowDefense = rollList[4];
}
else
{
highDefense = rollList[4];
lowDefense = rollList[3];
}
double attackValue = 0;
double defendValue = 0;
if(defend.getUnits().size()>0 && attack.getUnits().size()>0)
{
for (int i = 0; i < attack.getUnits().size(); i++){
attackValue += attack.getUnits().get(i).getAttack();
}
attackValue = attackValue/attack.getUnits().size();
for (int i = 0; i < defend.getUnits().size();i++){
defendValue += defend.getUnits().get(i).getDefense();
}
double newHighAttack = 0.0;
double newHighDefense = 0.0;
defendValue = defendValue/defend.getUnits().size();
if (highAttack == 6){newHighAttack = 1.5;}if (highAttack == 5){newHighAttack = 1.25;}if (highAttack == 4){newHighAttack = 1;}
if (highAttack == 3){newHighAttack = 1;}if (highAttack == 2){newHighAttack = .75;}if (highAttack == 1){newHighAttack = .5;}
if (highDefense == 6){newHighDefense = 1.5;}if (highDefense == 5){newHighDefense = 1.25;}if (highDefense == 4){newHighDefense = 1;}
if (highDefense == 3){newHighDefense = 1;}if (highDefense == 2){newHighDefense = .75;}if (highDefense == 1){newHighDefense = .5;}
if (newHighAttack * attackValue > newHighDefense * defendValue){
defend.removeUnit(defend.getUnits().get(0));
defendLost ++;
}
if (newHighAttack * attackValue < newHighDefense * defendValue){
attack.removeUnit(attack.getUnits().get(0));
attackLost++;
}
if (newHighAttack * attackValue == newHighDefense * defendValue){
attack.removeUnit(attack.getUnits().get(0));
attackLost++;
}
}
if(defend.getUnits().size()>0 && attack.getUnits().size()>0)
{
double newLowAttack = 0.0;
double newLowDefense = 0.0;
if (lowAttack == 6){newLowAttack = 1.5;}if (lowAttack == 5){newLowAttack = 1.25;}if (lowAttack == 4){newLowAttack = 1;}
if (lowAttack == 3){newLowAttack = 1;}if (lowAttack == 2){newLowAttack = .75;}if (lowAttack == 1){newLowAttack = .5;}
if (lowDefense == 6){newLowDefense = 1.5;}if (lowDefense == 5){newLowDefense = 1.25;}if (lowDefense == 4){newLowDefense = 1;}
if (lowDefense == 3){newLowDefense = 1;}if (lowDefense == 2){newLowDefense = .75;}if (lowDefense == 1){newLowDefense = .5;}
if (newLowAttack * attackValue > newLowDefense * defendValue){
defend.removeUnit(defend.getUnits().get(0));
defendLost ++;
}
if (newLowAttack * attackValue < newLowDefense * defendValue){
attack.removeUnit(attack.getUnits().get(0));
attackLost++;
}
if (newLowAttack * attackValue == newLowDefense * defendValue){
attack.removeUnit(attack.getUnits().get(0));
attackLost++;
}
}
if (defend.getUnits().size() == 0 ){
defend.setOwner(attack.getOwner());
result = true;
}
g.updateSidePanel(defend, buildings);
return rollList;
}
public int dice(){
return rand.nextInt(6)+1;
}
public Boolean getResult(){
return result;
}
public int getAttackLost(){
return attackLost;
}
public int getDefendLost(){
return defendLost;
}
public Nodes getAttacker()
{
return attack;
}
public Nodes getDefender()
{
return defend;
}
public Nodes[] getBuildingList()
{
return buildings;
}
public GameBoard getGameBoard()
{
return g;
}
}