-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWarGameClient.java
More file actions
276 lines (250 loc) · 8.93 KB
/
WarGameClient.java
File metadata and controls
276 lines (250 loc) · 8.93 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
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
import java.rmi.AccessException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.NotBoundException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Scanner;
import java.rmi.NotBoundException;
public class WarGameClient implements Runnable {//handle connection and use rmi function here from client side
// private static final long serialVersionUID = 1L;
private static final int PORT = 2020;
private String id;
boolean chkLog=false;
private WarGame server;
boolean boolean_ReadyBtnStatus;
boolean boolean_HitBtnStatus;
boolean boolean_DropBtnStatus;
Card top;
ArrayList<String> client;
WarGameGUI gui;
protected WarGameClient(WarGame wg,String id,String pw){
boolean_ReadyBtnStatus=false;
boolean_HitBtnStatus=false;
boolean_DropBtnStatus=false;
this.server=wg;
this.id=id;
try {
chkLog=server.checkClientCredentials(wg, id, pw);
if(chkLog==false) {
System.out.println("wrong id or wrong passworld");
System.exit(0);
}
}catch(RemoteException re) {
System.out.println("RemoteException: "+re);
}catch(java.lang.ArithmeticException ae) {
System.out.println("java.lang.ArithmeticException: "+ae);
}
}
void drop() {
boolean_DropBtnStatus=gui.returnDropBtnStatus();
try {
server.doDrop(id);//id가 필요한지 모르겠음...
}catch(Exception e) {
e.printStackTrace();
}
}
void hit() {
boolean_HitBtnStatus=gui.returnHitBtnStatus();
try {
server.doHit(id);
}catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
if(chkLog) {
System.out.println(id);
gui=new WarGameGUI(id);
gui.deactivatedHitDropBtn();
try {
while(server.checkAllReady()==false) {
boolean_ReadyBtnStatus=gui.returnReadyBtnStatus();
server.setReadyStatus(id, boolean_ReadyBtnStatus);
}
}catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯.. 예외처리 사이즈가
e.printStackTrace();
}
gui.activatedHitDropBtn();
System.out.println("game start");//this will be deleted in the end
gui.setGameInfo("game start");
gui.deactivateReadyBtn();
try {//최종 참가자 리스트 추가
client=server.getClientList();
String tmp=client.get(0);
String tmp2=client.get(1);
gui.setPlayerList("Player1: "+tmp);
gui.setPlayerList("Player2: "+tmp2);
}catch(Exception e) {
e.printStackTrace();
}
// try {//턴 돌아갈때 마다 올라가는 것으로 바꿔야 할것 같다. 1번에 국한되지 말고...
// if(id.contentEquals(server.whosTurn())) {
// String tmp=server.whosTurn();
// sendMsgToAll(tmp+"'s turn\n");
//
// }
//
//// System.out.println(tmp+"'s turn. good luck~!");
//// gui.setGameInfo(tmp+"'s turn. good luck~!");
//// String msg=receiveMsg();
//// gui.setMsgView(msg);
//
//
// }catch(Exception e) {
// e.printStackTrace();
// }
try {
while(server.checkEndingStatus()){//게임중...
if(!server.checkEndingStatus()) {
System.out.println("끝남ㅎㅎ");
server.refreshDeck(server.whosWin());
gui.deactivatedHitDropBtn();
gui.setCardNumLabel(0, server.getDeckLength(0));
gui.setCardNumLabel(1, server.getDeckLength(1));
gui.getMessageBox(client.get(server.whosWin()-1));
}
// int deckOneLength=server.getDeckLength(0);//get deck length of player1
// int deckTwoLength=server.getDeckLength(1);//get deck length of player2
gui.setCardNumLabel(0, server.getDeckLength(0));
// gui.setCardNumLabel(1,server.getDeckLength(1));
// top=server.getTop();
// if(top!=null)
// gui.updateCardImg(top.returnType(), top.returnValue());
if(server.getDeckZeroStatus()) {
// top=server.getTop();
// gui.updateCardImg(top.returnType(),top.returnValue());
// System.out.println("zeroStatusTrue");
String topType=server.getTopType();
String topValue=server.getTopValue();
gui.updateCardImg(topType, topValue);
// System.out.println(topType);
// System.out.println(topValue);
}else {
gui.updateCardImg();//아무것도 없는 경우 뒷장 보이게 함
}
while(id.equals(server.whosTurn())){//자신의 턴 동안...
int num = -1;
for(int i = 0 ; i < client.size(); i++) {
if(id.equals(client.get(i))) {
num = i;
break;
}
}
if(server.getDeckLength(num) == 0 || server.getDeckLength(num) == 52) {
break;
}
gui.setCardNumLabel(0, server.getDeckLength(0));
gui.setCardNumLabel(1,server.getDeckLength(1));
// top=server.getTop();
// if(top!=null)
// gui.updateCardImg(top.returnType(), top.returnValue());
//
if(server.getDeckZeroStatus()) {//바닥에 카드가 있을때만 작동
// top=server.getTop();
// gui.updateCardImg(top.returnType(),top.returnValue());
String topType=server.getTopType();
String topValue=server.getTopValue();
gui.updateCardImg(topType, topValue);
// System.out.println(topType);
// System.out.println(topValue);
}else {
gui.updateCardImg();
}
boolean_DropBtnStatus=gui.returnDropBtnStatus();
boolean_HitBtnStatus=gui.returnHitBtnStatus();
if(boolean_DropBtnStatus) {
drop();
gui.setMsgView("플레이어 "+ id+" 가 drop을 했습니다.");
// server.doDrop(id);//id는 안쓰고, serverImpl에서 turn보고 알아서 판단하고 있음. 필요 없는거 확실해지면 그때 지우기.
gui.setDropStatusFalse();//버튼 눌리면 true로 변하기 때문에 변경 안하면 항상 true인 효과 한번 버튼 누르면 다시 false로 변환
//server.changeTurn();
gui.repaint();
}else if(boolean_HitBtnStatus) {
hit();
// server.doHit(id);
gui.setHitStatusFalse();
}else {
}
}
if(!id.equals(server.whosTurn())){//player가 잘못누르는 경우...
boolean_DropBtnStatus=gui.returnDropBtnStatus();
boolean_HitBtnStatus=gui.returnHitBtnStatus();
if(boolean_DropBtnStatus){
System.out.println("it's not your turn..please wait");
gui.setGameInfo("it's not your turn..please wait");
gui.setDropStatusFalse();
}
if(boolean_HitBtnStatus){
// System.out.println("it's not your turn..please wait");
// gui.setGameInfo("it's not your turn..please wait");
hit();
gui.setHitStatusFalse();
}
}
}
// gui.deactivatedHitDropBtn();
server.refreshDeck(server.whosWin());
gui.setCardNumLabel(0, server.getDeckLength(0));
gui.setCardNumLabel(1, server.getDeckLength(1));
gui.deactivatedHitDropBtn();
gui.getMessageBox(client.get(server.whosWin()-1));
}catch(Exception e) {//디테일하게 예외 잡아 줘야 할 듯..예외처리 사이즈가 커
e.printStackTrace();
}
}
}
public void sendMsgToAll(String str) {//행동 취하고 나면 반드시 해당함수를 써서 메시지를 보내야한다. 그리고 바로 receiveMSG도 작동해야한다.
try {
server.sendMsg(str);
}catch(Exception e) {
e.printStackTrace();
}
}
// public String receiveMsg() {
// String msg="";
// try {
// msg=server.receiveMsg();
//// gui.setMsgView(msg+"\n");
//
// }catch(Exception e) {
// e.printStackTrace();
//
// }
// return msg;
// }
public static void main(String[] args) {
String mServer="127.0.0.1";
String mServName="WarGame";
System.out.println("Remote Method Invocate to "+mServer+", service name: "+mServName);
try {
System.setProperty("javax.net.ssl.trustStore", "trustedcerts");
System.setProperty("javax.net.ssl.trustStorePasssword", "123456");
String id;
String pw;
Scanner sc=new Scanner(System.in);//this is for temporary
System.out.print("enter id: ");
id=sc.nextLine();
System.out.print("enter pw: ");
pw=sc.nextLine();
Registry registry = LocateRegistry.getRegistry(
InetAddress.getLocalHost().getHostName(), PORT,
new RMISSLClientSocketFactory());
WarGame c=(WarGame)registry.lookup("WarGame"/*"rmi://"+mServer+":1099/"+mServName*/);
new Thread(new WarGameClient(c,id,pw)).start();
}catch(RemoteException re) {
System.out.println("RemoteException: "+re);
}catch(NotBoundException nbe) {
System.out.println("NotBoundException: "+nbe);
}catch(java.lang.ArithmeticException ae) {
System.out.println("java.lang.ArithmeticException: "+ae);
} catch (UnknownHostException e) {
System.out.println("UnknownHostException: "+e);
}
}
}