-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC4Server2.java
More file actions
222 lines (178 loc) · 6.25 KB
/
C4Server2.java
File metadata and controls
222 lines (178 loc) · 6.25 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
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
/*
Nathan Robinson
Intro to Networks
Dr. An
04/26/2017
Network server for java-based multiplayer Connect Four game.
*/
//Used for socket connection
import java.net.Socket;
import java.net.ServerSocket;
//Used for communication stream
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
//Create board, check for end game conditions, simulate user
class Match {
/**
ConnectFour board simulated by an array of 42 cells that are
initialized as null( = unclaimed). When claimed by a player,
the array cell stores a reference to the player that owns it */
User liveUser;
private User[] board = {
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null, null, null, null, null, null,
null, null, null, null, null, null, null};
//Checks each cell to see if board is full
public boolean boardFull() {
for (int i = 0; i < board.length; i++) {
if (board[i] == null)
return false;
//Board is full
} return true;
}
//Called by user thread when a move is attempted to check validity
public synchronized boolean validMove(int location, User player) {
//Move is valid: must be players turn, cell must be empty,
//and must be on the lowest row or have a filled cell below it
if (player == liveUser && board[location] == null &&
(location > 34 || board[location+7] != null)) {
//If legal, cell is claimed for by user, appears on both clients
board[location] = liveUser;
liveUser = liveUser.foe;
liveUser.foePlayed(location);
return true;
//Move is invalid
} return false;
}
//Checks the whole board array to see if there is four in a row
public boolean checkWin() {
for(int i = 0; i < board.length; i++) {
if (board[i] == null)
continue;
//horizontal: right
if(((i < 4) || (i > 6 && i < 11) || (i > 13 && i < 18) ||
(i > 20 && i < 25) || (i > 27 && i < 32) || (i > 34 && i < 39))
&& (board[i] == board[i+1] && board[i] == board[i+2] &&
board[i] == board[i+3]))
return true;
//vertical: down
if((i < 21) && (board[i] == board[i+7] && board[i] == board[i+14]
&& board[i] == board[i+21]))
return true;
//diagonal: down-right
if(((i < 21) && ((i < 4) || (i > 6 && i < 11) ||
(i > 13 && i < 18))) && (board[i] == board[i+8] &&
board[i] == board[i+16] && board[i] == board[i+24]))
return true;
//diagonal: up-right
if(((i > 20) && ((i > 20 && i < 25) || (i > 27 && i < 32) ||
(i > 34 && i < 39))) && (board[i] == board[i-6] && board[i]
== board[i-12] && board[i] == board[i-18]))
return true;
//No four connected on board yet
} return false;
}
//Server uses socket to communicate w/ user(client) via I/O text stream
class User extends Thread {
//Only text is transfered, only reader and writer needed
BufferedReader inp;
PrintWriter outp;
User foe;
Socket socket;
char chip;
//C4Client c4c = new C4Client();
//Given socket and chip
public User(Socket socket, char chip) {
this.socket = socket;
this.chip = chip;
try {
//Initialize Input/Output stream
inp = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outp = new PrintWriter(socket.getOutputStream(), true);
//Display welcome message
outp.println("WELCOME " + chip);
outp.println("MESSAGE Waiting for opponent to connect");
} catch (IOException e) {
System.out.println("Player died: " + e);
//outp.println("TIE");
}
}
//When new client initialized, opponent is set
public void setFoe(User foe) {
this.foe = foe;
}
//When other player moves, end game conditions checked
public void foePlayed(int location) {
outp.println("OPPONENT_MOVED " + location);
outp.println(checkWin() ? "DEFEAT" : boardFull() ? "TIE" : "");
}
//Runs User thread
public void run() {
try {
//Displays when both opponents connect and are matched
outp.println("MESSAGE All players connected");
//Displays for player who connected first
if (chip == 'R')
outp.println("MESSAGE Your move");
//Recieve and process prompt messages from the client
while (true) {
String prompt = inp.readLine();
//'MOVE <n>' Sent from client to server
if (prompt.startsWith("MOVE")) {
int location = Integer.parseInt(prompt.substring(5));
if (validMove(location, this)) {
//'WELCOME <char>','VALID_MOVE','FOE_PLAYED <n>',
//'MESSAGE <text>','WIN','LOSE', & 'TIE' sent by server to client
outp.println("VALID_MOVE");
outp.println(checkWin() ? "VICTORY" : boardFull() ? "TIE" : "");
//Invalid move
} else
outp.println("MESSAGE Invalid move");
//'QUIT' Sent from Client to Server
} else {// if (prompt.startsWith("QUIT")) {
outp.println("TIE");
//return;
}
}
} catch (IOException e) {
System.out.println("Player died: " + e);
outp.println("TIE");
//close User socket for client
} finally {
try {socket.close();} catch (IOException e) {}
}
}
}
}
//Connects clients
public class C4Server2 {
//Run server as application
public static void main(String[] args) throws Exception {
//Setup socket
ServerSocket finder = new ServerSocket(1013);
System.out.println("ConnectFour server online");
try {
//unlimited pairs can connect to server socket via while loop
while (true) {
Match game = new Match();
//Establish clients
Match.User red = game.new User(finder.accept(), 'R');
Match.User yellow = game.new User(finder.accept(), 'Y');
//Pair up clients
red.setFoe(yellow);
yellow.setFoe(red);
//First user to connect plays first
game.liveUser = red;
red.start();
yellow.start();
}
} finally {
finder.close();
}
}
}