-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
178 lines (165 loc) · 6.5 KB
/
Game.java
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
package porgorpatch2017;
public class Game {
private int playerALeft;
private int playerARight;
private int playerBLeft;
private int playerBRight;
private int round;
private final Client playerA;
private final Client playerB;
// instantiate a new game with initial game data
public Game(Client A, Client B) {
this.playerA = A;
this.playerB = B;
this.round = 0;
this.playerARight = 1;
this.playerALeft = 1;
this.playerBRight = 1;
this.playerBLeft = 1;
}
// check if game is finished
private boolean isFinished() {
if (this.playerALeft==0 && this.playerARight==0) {
return true;
} else if (this.playerBLeft==0 && this.playerBRight==0) {
return true;
} else {
return false;
}
}
// update this instance's attributes after A's turn
private void updateWithAsTurn(Turn turn) {
if (turn.getSwitchHands()) {
this.playerALeft = turn.getMyLeft();
this.playerARight = turn.getMyRight();
} else {
if (turn.getMyHand() == Hand.RIGHT) {
if (turn.getOppHand() == Hand.RIGHT) {
if (5>this.playerARight+this.playerBRight) {
this.playerBRight += this.playerARight;
} else {
this.playerBRight = 0;
}
} else {
if (5>this.playerARight+this.playerBLeft) {
this.playerBLeft += this.playerARight;
} else {
this.playerBLeft = 0;
}
}
} else {
if (turn.getOppHand() == Hand.RIGHT) {
if (5>this.playerALeft+this.playerBRight) {
this.playerBRight += this.playerALeft;
} else {
this.playerBRight = 0;
}
} else {
if (5>this.playerALeft+this.playerBLeft) {
this.playerBLeft += this.playerALeft;
} else {
this.playerBLeft = 0;
}
}
}
}
}
// update this instance's attributes after B's turn
private void updateWithBsTurn(Turn turn) {
if (turn.getSwitchHands()) {
this.playerBLeft = turn.getMyLeft();
this.playerBRight = turn.getMyRight();
} else {
if (turn.getMyHand() == Hand.RIGHT) {
if (turn.getOppHand() == Hand.RIGHT) {
if (5>this.playerBRight+this.playerARight) {
this.playerARight += this.playerBRight;
} else {
this.playerARight = 0;
}
} else {
if (5>this.playerBRight+this.playerALeft) {
this.playerALeft += this.playerBRight;
} else {
this.playerALeft = 0;
}
}
} else {
if (turn.getOppHand() == Hand.RIGHT) {
if (5>this.playerBLeft+this.playerARight) {
this.playerARight += this.playerBLeft;
} else {
this.playerARight = 0;
}
} else {
if (5>this.playerBLeft+this.playerALeft) {
this.playerALeft += this.playerBLeft;
} else {
this.playerALeft = 0;
}
}
}
}
}
// API method for playing the game instance, returns winner
public Client play() {
boolean AsTurn = Math.random()<0.5;
while (!isFinished()) {
if (this.round > 1000) {
return new Bot("tie");
}
if (AsTurn) {
Turn turn = this.playerA.getTurn();
if (!this.playerA.isLegalMove(turn)) return this.playerB;
this.playerA.updateWithMyTurn(turn);
this.playerB.updateWithOpponentTurn(turn);
this.updateWithAsTurn(turn);
printRound(AsTurn, turn);
} else {
Turn turn = this.playerB.getTurn();
if (!this.playerB.isLegalMove(turn)) return this.playerA;
this.playerA.updateWithOpponentTurn(turn);
this.playerB.updateWithMyTurn(turn);
this.updateWithBsTurn(turn);
printRound(AsTurn, turn);
}
this.round++;
AsTurn = !AsTurn;
}
if (this.playerALeft==0 && this.playerARight==0) {
return this.playerB;
} else {
return this.playerA;
}
}
// printing out the results after each round
private void printRound(boolean AsTurn, Turn turn) {
if (AsTurn) {
System.out.println("-----------");
System.out.println("Round " + this.round);
if (turn.getSwitchHands()) {
System.out.println("Player A switches hands");
System.out.println("A: " + this.playerALeft + " " + this.playerARight);
System.out.println("B: " + this.playerBLeft + " " + this.playerBRight);
} else {
System.out.println("Player A uses " + turn.getMyHand().toString()
+ " to hit player B's " + turn.getOppHand());
System.out.println("A: " + this.playerALeft + " " + this.playerARight);
System.out.println("B: " + this.playerBLeft + " " + this.playerBRight);
}
} else {
System.out.println("-----------");
System.out.println("Round " + this.round);
if (turn.getSwitchHands()) {
System.out.println("Player B switches hands");
System.out.println("A: " + this.playerALeft + " " + this.playerARight);
System.out.println("B: " + this.playerBLeft + " " + this.playerBRight);
} else {
System.out.println("Player B uses " + turn.getMyHand().toString()
+ " to hit player A's " + turn.getOppHand());
System.out.println("A: " + this.playerALeft + " " + this.playerARight);
System.out.println("B: " + this.playerBLeft + " " + this.playerBRight);
}
}
}
}