-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
136 lines (120 loc) · 3.33 KB
/
Game.java
File metadata and controls
136 lines (120 loc) · 3.33 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
import java.util.Random;
import java.util.LinkedList;
import java.util.Iterator;
/**
* Write a description of class Game here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Game
{
private static final Random RAND = new Random();
private LinkedList<Round> rounds = new LinkedList<Round>();
private final Player[] players;
public Game(Player[] thePlayers) {
players = thePlayers;
}
public Player getWinner() {
int leadScore = getLeadScore();
if(leadScore >= 20 && numLeadPlayers() < 2) {
for(Player p : players) {
if(p.getPoints() == leadScore) {
return p;
}
}
}
return null;
}
public void playRound() {
int c = (RAND.nextInt(3)+1)*2;
int entryNum = 0;
for(Player p : players) {
if(p.getDecision(this,c)) {
entryNum++;
}
}
for(Player p : players) {
if(!p.getLastDecision()) {
p.incrementPoints(1);
}else if(entryNum <= c) {
p.incrementPoints(2);
}
}
rounds.add(new Round(players,c));
}
public int getLeadScore() {
int highScore = 0;
for(Player p : players) {
if(p.getPoints() > highScore) {
highScore = p.getPoints();
}
}
return highScore;
}
public int numLeadPlayers() {
int num = 0;
int highScore = getLeadScore();
for(Player p : players) {
if(p.getPoints() == highScore) {
num++;
}
}
return num;
}
public int getTotalScore() {
int total = 0;
for(Player p : players) {
total += p.getPoints();
}
return total;
}
public int getNumRounds() {
return rounds.size();
}
public LinkedList<Round> getLastNumRounds(int num) {
LinkedList<Round> ret = new LinkedList<Round>();
Iterator<Round> iter = rounds.descendingIterator();
for(int i = 0; i < num; i++) {
if(!iter.hasNext()) break;
ret.addFirst(iter.next());
}
return ret;
}
public Round getLastRound() {
try {
return rounds.getLast();
} catch(RuntimeException re) {
return null;
}
}
public boolean isWinning(Player p) {
return p.getPoints() == getLeadScore() && getLeadScore() != 0;
}
public boolean isStrictlyWinning(Player p) {
return isWinning(p) && numLeadPlayers() == 0;
}
public float getAverageScore() {
return (float)getTotalScore()/(float)players.length;
}
public int getLeastScoreDiff() {
int diff = -1;
for(Player p1 : players) {
for(Player p2 : players) {
if(p1 == p2) continue;
int tempDiff = Math.abs(p1.getPoints() - p2.getPoints());
if(tempDiff < diff || diff < 0 ) {
diff = tempDiff;
}
}
}
return diff;
}
public int getNumPlayersBelowAverage() {
int num = 0;
for(Player p : players) {
if(p.getPoints() < getAverageScore()) num++;
}
return num;
}
}