-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameThread.java
137 lines (115 loc) · 3.6 KB
/
GameThread.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
/*
Tic Tac Toe Multiplayer
Author: E/14/009 Adikari A.M.H.I @ EFAC, University of Peradeniya
Date:30/08/2017
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.net.ServerSocket;
import java.net.Socket;
class GameThread extends Thread {
private Thread t;
private String threadName;
private GameController game;
Socket socket;
public static final int BASE_PORT = 1250;
private ServerSocket serverSocket;
GameThread( GameController game){
this.game = game;
threadName = "gamethread";
}
@Override
public void run() {
try {
serverSocket = new ServerSocket(BASE_PORT);
server_loop();
} catch (Exception e) {
}
}
public void server_loop() throws IOException {
while(true) {
socket = serverSocket.accept();
try {
handle(socket);
} catch (IOException e) {
} finally {
socket.close();
}
}
}
private void handle(Socket socket) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
for(line = in.readLine();
line != null && !line.equals("quit");
line = in.readLine()) {
if(line.trim().equals("check"+ game.thisPlayer)){
bw.write("exist"+"\n");
bw.flush();
}else if(line.trim().equals("check"+ (game.thisPlayer==1?2:1))) {
bw.write("notexist"+"\n");
bw.flush();
}else{
btnaction(line);
}
}
}
public void closeServer(){
try{
serverSocket.close();
} catch (IOException e) {
}
}
public void startt ()
{
if (t == null)
{
t = new Thread (this, threadName);
t.start ();
}
}
private void btnaction(String key){
switch(key){
case "1" : if(game.contentPane.getBtn1().isEnabled()){
game.btn1ActionPerformed();
}
break;
case "2" : if(game.contentPane.getBtn2().isEnabled()){
game.btn2ActionPerformed();
}
break;
case "3" : if(game.contentPane.getBtn3().isEnabled()){
game.btn3ActionPerformed();
}
break;
case "4" : if(game.contentPane.getBtn4().isEnabled()){
game.btn4ActionPerformed();
}
break;
case "5" : if(game.contentPane.getBtn5().isEnabled()){
game.btn5ActionPerformed();
}
break;
case "6" : if(game.contentPane.getBtn6().isEnabled()){
game.btn6ActionPerformed();
}
break;
case "7" : if(game.contentPane.getBtn7().isEnabled()){
game.btn7ActionPerformed();
}
break;
case "8" : if(game.contentPane.getBtn8().isEnabled()){
game.btn8ActionPerformed();
}
break;
case "9" : if(game.contentPane.getBtn9().isEnabled()){
game.btn9ActionPerformed();
}
break;
}
}
}