-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC4Client2.java
More file actions
173 lines (147 loc) · 5.8 KB
/
C4Client2.java
File metadata and controls
173 lines (147 loc) · 5.8 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
/*
Nathan Robinson
Intro to Networks
Dr. An
04/26/2017
Network client for Connect Four game. Connects to server using server socket
*/
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
//Connects client to server, initialize class data
public class C4Client2 {
//for GUI
private JFrame frame = new JFrame("C4Client2");
private JLabel messageBar = new JLabel("");
private ImageIcon icon;
private ImageIcon foeIcon;
//for board set up
private Square[] board = new Square[42];//7 width, 6 height
private Square clickSquare;
//to communicate with server and other clients
private static int port = 1013;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
//Runs the client as an application
public static void main(String[] args) throws Exception {
while (true) {
String serverAddress = (args.length == 0) ? "localhost" : args[1];
C4Client2 client = new C4Client2(serverAddress);
//set up game window
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setSize(640, 480);
client.frame.setResizable(false);
client.frame.setVisible(true);
//play or replay
client.play();
if (!client.playAgain()) {
break;
}
}
}
//Creates grapical squares in client window
static class Square extends JPanel {
JLabel cell = new JLabel((Icon)null);
//makes squares white panels
public Square() {
setBackground(Color.white);
add(cell);
}
//Calls setIcon() to fill square with an red or yellow icon
public void setIcon(Icon icon) {
cell.setIcon(icon);
}
}
//Creates client by establishing connection to server and setting up the GUI
public C4Client2(String serverAddress) throws Exception {
//Setup networking to server
socket = new Socket(serverAddress, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//GUI Setup
messageBar.setBackground(Color.lightGray);
frame.getContentPane().add(messageBar, "South");
//board colors
JPanel boardPanel = new JPanel();
boardPanel.setLayout(new GridLayout(6, 7, 3, 3));
boardPanel.setBackground(Color.cyan);
//Reads mouse clicks on board cells and send MOVE command
for (int i = 0; i < board.length; i++) {
final int j = i;
board[i] = new Square();
board[i].addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
clickSquare = board[j];
out.println("MOVE " + j);}});
boardPanel.add(board[i]);
} frame.getContentPane().add(boardPanel, "Center");
}
//Listens for server messages
public void play() throws Exception {
String reply;
try {
reply = in.readLine();
//first message "WELCOME": chip recieved
if (reply.startsWith("WELCOME")) {
//Player is identified by chip 'R'(red) or 'Y'(yellow).
char chip = reply.charAt(8);
icon = new ImageIcon(chip == 'R' ? "red.gif" : "yellow.gif");
foeIcon = new ImageIcon(chip == 'R' ? "yellow.gif" : "red.gif");
frame.setTitle("Connect Four :: Player " + chip);
}
while (true) {
reply = in.readLine();
//you moved, opponent's turn
if (reply.startsWith("VALID_MOVE")) {
messageBar.setText("You moved, opponent's turn");
clickSquare.setIcon(icon);
clickSquare.repaint();
//opponent moved, your turn
} else if (reply.startsWith("OPPONENT_MOVED")) {
int location = Integer.parseInt(reply.substring(15));
board[location].setIcon(foeIcon);
board[location].repaint();
messageBar.setText("Opponent moved, your turn");
//'WIN', 'LOSE', 'TIE' messages prompt user to play another game
} else if (reply.startsWith("VICTORY")) {
messageBar.setText("You win");
break;
} else if (reply.startsWith("DEFEAT")) {
messageBar.setText("You lose");
break;
} else if (reply.startsWith("TIE")) {
messageBar.setText("You tied");
break;
} else if (reply.startsWith("EXIT")) {
messageBar.setText("Opponent left, you win");
out.println("QUIT");
break;
} else if (reply.startsWith("MESSAGE")) {
messageBar.setText(reply.substring(8));
}
//if new game is refused or opponents leaves, 'QUIT' is sent
} out.println("QUIT");
} finally {
socket.close();
}
}
//Initiated by 'WIN', 'LOSE', 'TIE' command prompts
private boolean playAgain() {
int reply = JOptionPane.showConfirmDialog(frame, "Play again?",
"Connect Four is fourtastic!", JOptionPane.YES_NO_OPTION);
frame.dispose();
return reply == JOptionPane.YES_OPTION;
}
}