-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComp.java
279 lines (255 loc) · 7.56 KB
/
Comp.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import java.io.*;
import java.net.*;
import java.util.*;
public class Comp extends Player{
ServerSocket server = null;
Socket socks = null;
PrintWriter out = null;
BufferedReader in = null;
BufferedReader userIn = null;
private final static String HELLO = "hello";
private final static String BYE = "bye";
private final static String NEWGAME = "newgame";
private final static String READY = "ready";
private final static String REJECT = "reject";
private final static String WIN = "you-win";
private final static String PASS = "pass";
/**
* Constructs an online player, establishing a socket and acting as either a clent or a server
* @param board the game board
* @param colour the colour of the player
*/
public Comp(Space [] board, Colour colour)
{
super(board, colour);
int portNumber = 20101 + 30000;
try
{
userIn = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of a server, or enter 'wait' if you want a client to make a connection.");
String keys = userIn.readLine();
if (keys.equals("wait")) { //This means this program is acting as server
server = new ServerSocket(portNumber);
this.colour = Colour.WHITE;
socks = server.accept();
}
else
{
this.colour = Colour.RED; //program acts as client
socks = new Socket(keys, portNumber);
}
out = new PrintWriter(socks.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socks.getInputStream()));
if (!establishGame(this.colour == Colour.WHITE)) //Use protocols to establish game with another program
{
quit(BYE); //end connection if game fails to establish
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
/**
* Begins a game using protocols
* @param server If this player is the server or not
* @return whether or not the game successfully got made
* @throws IOException
*/
public boolean establishGame(boolean server) throws IOException
{
if (server) {
String input = in.readLine();
if (input.equals(HELLO)) {
out.println(HELLO);
} else {
return false;
}
input = in.readLine();
if (input.equals(NEWGAME)) {
out.println(READY);
} else {
return false;
}
}
else
{
out.println(HELLO);
String input = in.readLine();
if (input.equals(HELLO)) {
out.println(NEWGAME);
}
else
{
return false;
}
input = in.readLine();
if (!input.equals(READY)) {
return false;
}
}
return true;
}
/**
* Shuts everything down
*/
public void close()
{
try
{
if (in != null)
{
in.close();
}
if (userIn != null)
{
userIn.close();
}
if (out != null)
{
out.close();
}
if (socks != null)
{
socks.close();
}
if (server != null)
{
server.close();
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
/**
* Returns a blank move, called when there is no possible moves, must be written
* to override method in superclass
* @param rolls the possible dice rolls
* @return No possible move
*/
public Move getMove(ArrayList<Integer> rolls)
{
return new Move(-1,-1, this);
}
/**
* Tells the online player it has won
*/
public void tellWin()
{
out.println(WIN + ";" + BYE);
}
/**
* Gets the moves, validates them, then runs them on the board and prints the
* board
* @return String format of the turn, null in this case as this is for
* players to pass their moves to an online player
*/
public String takeTurn()
{
try
{
String turn = in.readLine();
quit(turn);
String[] turnPieces = turn.split(":");
ArrayList<Integer> rolls = getRolls(turnPieces[0]);
ArrayList<Move> moves = extractMoves(turnPieces[1]);
for (Move move: moves)
{
if (!move.validate(rolls, board)) //If invalid, quit
{
System.out.println(move);
quit(BYE);
}
System.out.println(move);
if (run(move, board))
{
int diff = move.getEnd() - move.getStart();
diff = Math.abs(diff);
rolls.remove((Integer) diff); //removes roll that has been used from roll list
}
}
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
Game.printBoard();
return null;
}
/**
* Quits if time to quit
* @param maybye the string that might be bye, that would idnicate a quit
*/
public void quit(String maybye)
{
if (BYE.equals(maybye))
{
out.println(BYE);
close();
System.exit(0);
}
else if (maybye.equals(WIN) || maybye.equals(WIN + ";" + BYE))
{
System.out.println("Winner!");
out.println(BYE);
close();
System.exit(0);
}
}
/**
* Gets rolls from the players String
* @param stringRoll
* @return
*/
public ArrayList<Integer> getRolls(String stringRoll)
{
String [] rolls = stringRoll.split("-");
int roll1 = Integer.parseInt(rolls[0]);
int roll2 = Integer.parseInt(rolls[1]);
System.out.println("A " + roll1 + " and a " + roll2 + " are rolled.");
ArrayList<Integer> intRolls = new ArrayList<Integer>();
intRolls.add(roll1);
intRolls.add(roll2);
if (roll1 == roll2)
{
for (int i = 0; i<2; i++)
{
intRolls.add(roll1);
}
}
return intRolls;
}
/**
* Gets the moves from the online players String
* @param stringMoves
* @return
*/
public ArrayList<Move> extractMoves(String stringMoves)
{
ArrayList<Move> moves = new ArrayList<Move>();
String [] sepStringMoves = stringMoves.split(",");
for (int i = 0; i< sepStringMoves.length; i++)
{
String brackRemove = sepStringMoves[i].replace("(", "");
brackRemove = brackRemove.replace(")", "");
brackRemove = brackRemove.replace(";", "");
String [] nums = brackRemove.split("\\|");
moves.add(new Move(Integer.parseInt(nums[0]), Integer.parseInt(nums[1]), this));
}
return moves;
}
/**
* Passes the other players move to the online player
* @param turn Other players turn, formatted correctly
*/
public void pass(String turn)
{
out.println(turn);
}
public String toString()
{
return "Online Player";
}
}