-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfantasticKingdom.java
More file actions
260 lines (252 loc) · 5.52 KB
/
fantasticKingdom.java
File metadata and controls
260 lines (252 loc) · 5.52 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import java.util.*;
import java.io.*;
import java.util.concurrent.*;
abstract class citizen{
int wealth, min_wealth, favour, rank, id, age = 0;
static int population = 0;
boolean isAlive;
citizen overlord;
void die(){
isAlive = false;
System.out.println("Another one bites the dust !!");
overlord.wealth += wealth;
wealth = 0;
}
void revolt(){
System.out.println(id+" is revolting !!");
buyFavour();
if (favour > overlord.favour)
overlord.die();
else
die();
overlord.favour -= rank;
}
//Underlying Function is Added
//Royal was abstract class earlier
void addUnderling(citizen new_underling)
{
}
abstract void buyFavour();
abstract void earn();
abstract int giveWealth(citizen c, int w);
}
class royal extends citizen{
citizen [] underling;
int no_of_underling ;
//Royal changed to citizen
royal(citizen overlord){
id = ++population;
isAlive = true;
wealth = 100;
min_wealth = 100;
this.overlord = overlord;
no_of_underling = 0;
}
void addUnderling(citizen new_underling){
if (no_of_underling++ == 0){
underling = new citizen[1];
}else{
underling = Arrays.copyOf(underling, no_of_underling);
}
underling[no_of_underling-1] = new_underling;
}
int giveWealth(citizen c, int w){
if (wealth >= w){
favour += c.rank;
wealth -=w;
c.wealth += w;
}else{
favour -= c.rank;
}
return w;
}
void buyFavour(){
overlord.wealth = wealth;
favour += wealth;
wealth = 0;
}
void earn(){
for(int i =0; i < no_of_underling;i++){
wealth += underling[i].giveWealth(this, underling[i].rank);
}
}
}
//King data type is changed to Citizen Data type
class prince extends royal{
prince(citizen k){
super(k);
rank = 5;
}
void complainToKingAbout(citizen c){
c.favour -=10/c.rank;
favour -=1;
}
private king inheritsThrone(citizen underling[]){
king k = new king();
k.underling = underling;
die();
k.wealth = wealth;
return k;
}
}
class king extends prince{
king(){
super(null);
rank = 10;
}
void buyFavour(){
favour += wealth;
wealth = 0;
}
public void earn(){
super.earn();
buyFavour();
}
void complainToKingAbout(citizen c, king k){
k.favour +=10;
super.complainToKingAbout(c);
}
void revolt(){
System.out.println("The KING is revolting !!");
wealth +=100;
}
void wageWarAgaint(king k){
if (favour > k.favour)
k.die();
else
die();
}
void die(){
System.out.println("Kings wealth :"+ wealth);
System.out.println("The king is dead. Long Live the king!!!");
isAlive = false;
for(int i=0; i<no_of_underling;i++){
underling[i].wealth += wealth/no_of_underling;
}
wealth = 0;
}
}
class peasant extends citizen{
peasant(citizen r){
id = ++population;
isAlive = true;
overlord = r;
wealth = 0;
min_wealth = 0;
rank = 1;
}
int giveWealth(citizen c, int w){
wealth -= w;
return w;
}
void earn(){
wealth++;
}
void buyFavour(){
overlord.wealth = wealth;
favour += wealth/2;
wealth = 0;
}
}
class kingdom{
citizen citizen_of_country[];
boolean living[];
int no_of_princes, no_of_citizen, living_citizen_count;
kingdom(int no_of_citizen, int no_of_princes){
this.no_of_princes = no_of_princes;
this.no_of_citizen = no_of_citizen;
citizen_of_country = new citizen[no_of_citizen];
living=new boolean[no_of_citizen];
citizen_of_country[0] = new king();
living[0] = true;
int i=1;
for(;i<no_of_princes+1;i++){
citizen_of_country[i] = new prince(citizen_of_country[0]);
citizen_of_country[0].addUnderling(citizen_of_country[i]);
living[i] = true;
}
Random r=new Random();
for(;i<no_of_citizen;i++){
int overlord_id = r.nextInt(no_of_princes-1) + 1;
citizen_of_country[i] = new peasant(citizen_of_country[overlord_id]);
citizen_of_country[overlord_id].addUnderling(citizen_of_country[i]);
living[i] = true;
}
}
//Return type was void earlier
//Rank variable is passed
String rank(int rank){
if (rank==10){
return "king";
}else if(rank==5){
return "prince";
}else{
return "peasant";
}
}
void status(citizen c){
System.out.println(c.id+"\t a "+rank(c.rank)+" \t("+c.age+")yrs \t"+c.wealth+"$, \t"+c.favour+" favour "+c.isAlive);
}
}
class citizenThread implements Runnable{
citizen c;
citizenThread(citizen c){
this.c = c;
}
public void run(){
Random r = new Random();
while(c.isAlive){
try{
Thread.sleep(r.nextInt(5000)+5000);
c.earn();
if (c.wealth < c.min_wealth){
c.revolt();
}
c.age++;
if(c.age >= 10){
c.die();
}
}catch(Exception e){
System.err.println(c.id+ ": "+e.getMessage());
}
}
}
}
class kingdomThread implements Runnable {
kingdom k ;
kingdomThread(kingdom k){
this.k = k;
}
public void run(){
while(k.living_citizen_count >0){
for(int i=0;i<k.no_of_citizen;i++){
citizen c = k.citizen_of_country[i];
if(k.living[i] && !c.isAlive){
k.living_citizen_count--;
k.living[i] = false;
}
k.status(c);
}
System.out.println("==================================================================================");
//Thread.sleep() was not put in try catch block earlier
try
{
Thread.sleep(10000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
class fantasticKingdom{
public static void main(String args[]) throws InterruptedException{
kingdom fantasticKingdom = new kingdom(21,5);
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new kingdomThread(fantasticKingdom));
for(int i=0;i<fantasticKingdom.no_of_citizen;i++){
exec.execute(new citizenThread(fantasticKingdom.citizen_of_country[i]));
}
}
}