-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLPlayer.java
More file actions
66 lines (60 loc) · 1.91 KB
/
Copy pathMLPlayer.java
File metadata and controls
66 lines (60 loc) · 1.91 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
import java.io.*;
import java.net.*;
public class MLPlayer {
Player thePlayer;
public MLPlayer(Player p) {
thePlayer=p;
}
public void play() throws IOException,ClassNotFoundException {
//open socket and in/out streams
Socket sock = new Socket("localhost", Player.getSocketNumber(thePlayer));
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
//get the game rules
Rules gameRules = (Rules) in.readObject();
System.out.printf("Num Rows: %d, Num Cols: %d, Num Connect: %d\n", gameRules.numRows, gameRules.numCols, gameRules.numConnect);
//start playing the game
System.out.println("Waiting...");
GameMessage mess = (GameMessage) in.readObject(); //wait for initial message
int move;
while(mess.win == Player.EMPTY) {
if(mess.move != -1) {
System.out.printf("Player %s moves %d\n", Player.otherPlayer(thePlayer),mess.move);
}
System.out.println("Your move?");
move = Integer.parseInt(sysin.readLine());
mess.move=move;
out.writeObject(mess);
mess = (GameMessage) in.readObject();
}
System.out.printf("Player %s wins.\n", mess.win);
sock.close();
}
public static void main(String[] args) {
if(args.length != 1) {
System.out.println("Usage:\n java MPLayer [1|2]");
System.exit(-1);
}
int which_player = Integer.parseInt(args[0]);
Player p=null;
if(which_player == 1) {
p = Player.ONE;
}
else if ( which_player == 2) {
p = Player.TWO;
}
else {
System.out.println("Usage:\n java MPLayer [1|2]");
System.exit(-1);
}
MLPlayer me = new MLPlayer(p);
try {
me.play();
} catch(IOException ioe) {
ioe.printStackTrace();
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}