-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayGame.java
More file actions
executable file
·126 lines (111 loc) · 4.21 KB
/
PlayGame.java
File metadata and controls
executable file
·126 lines (111 loc) · 4.21 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
package Project01;
import Project01.Nation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.*;
/**
* PlayGame.java is where the nations start attacking each other.
*/
public class PlayGame
{
ArrayList<Nation> allLivingNations = new ArrayList<>();
ArrayList<Project01.People> worldLivingPopulation = new ArrayList<>();
Random generator;
/**
* This class will start the game and determine which nation will in the game.
*/
public PlayGame()
{
Date seed = new Date();
generator = new Random(seed.getTime());
}
/**
* Adding the total population of the world by counting each nation's population.
* @param nations An Arraylist of nations in the world
*/
public void getWorldLivingPopulation(ArrayList<Nation> nations)
{
// add all living people to world list
worldLivingPopulation.clear();
//System.out.println(allLivingNations);
for(int nation = 0; nation < nations.size(); nation++)
//System.out.println(nations.get(nation));
worldLivingPopulation.addAll(nations.get(nation).getNationPopulation());
//System.out.println(worldLivingPopulation);
}
/**
* Adding nations in the allLivingNations ArrayList if they are alive.
* @param nations ArrayList of nations in the world.
*/
public void getAllLivingNations(ArrayList<Nation> nations)
{
getWorldLivingPopulation(nations);
allLivingNations.clear();
for(int nation = 0; nation < nations.size(); nation++)
{
if(nations.get(nation).isNationAlive())
{
allLivingNations.add(nations.get(nation));
}
}
//System.out.print(allLivingNations);
}
/**
* This method takes a parameter of two people and checks if the two people are not from the same
* nation. Then the two people fight each other and both loses damage. It prints out a statement
* of the life points of each other.
* @param p1 A person from the People class
* @param p2 A person from the People class
*/
public void encounter(Project01.People p1, Project01.People p2)
{
// need to fix this to take strategies into account.
if(p1.getNation() != p2.getNation())
{
System.out.print(p1 + " encounters " + p2);
int p1Damage = (int) (generator.nextFloat() * generator.nextInt(20));
int p2Damage = (int) (generator.nextFloat() * generator.nextInt(20));
p1.reduceLifePoints(p1Damage);
p2.reduceLifePoints(p2Damage);
System.out.println("\t\tp1 damage is " + p1Damage + ". p2 damage is " + p2Damage + ".");
}
}
/**
* This method calls getAllLivingNations with parameter and use a while loop to check if the arraylist
* is greater than 1 and index is less the size of worldLivingPopulation Arraylist
* @param nations An Arraylist of nations
* @return Boolean
*/
public Boolean playOneRound(ArrayList<Nation> nations)
{
getAllLivingNations(nations);
int index = 0;
while((allLivingNations.size() > 1) && (index < worldLivingPopulation.size()))
{
//encounter(worldLivingPopulation.get(pointers.get(index)), worldLivingPopulation.get(pointers.get(index+1)));
//System.out.println((worldLivingPopulation.size()-1) + "\t" + limit + "\t" + index + "\t" + (index+1));
int p1Index = generator.nextInt(worldLivingPopulation.size());
int p2Index;
do
p2Index = generator.nextInt(worldLivingPopulation.size());
while(p1Index == p2Index);
encounter(worldLivingPopulation.get(p1Index), worldLivingPopulation.get(p2Index));
getAllLivingNations(nations);
if(allLivingNations.size() < 2)
break;
index = index + 1;
}
return (allLivingNations.size() < 2);
}
/**
* @return a string of nation that won or no one
*/
public String getWinner()
{
if (allLivingNations.size() == 0)
return "No Winner!";
else
return allLivingNations.get(0).getNationName();
}
}