-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
232 lines (200 loc) · 9.09 KB
/
Copy pathmain.py
File metadata and controls
232 lines (200 loc) · 9.09 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
import sys, getopt, socket
from Game import Game
PORT = 9008
def main():
args = sys.argv[1:]
chess = Game()
if (len(args) == 0):
chess.clientServerToggle = 0
else:
if (args[0] == "-s"):
print("Game is now running as server and waiting for a client too connect")
chess.clientServerToggle = 1
elif (args[0] == "-c"):
chess.clientServerToggle = 2
elif (args[0] == "--help"):
print("Usage: Chess [OPTION] ... [IP ADDRESS]\n" + \
"Chess will run with no argument as a standalone game or with options as client and server\n" + \
" -s run Chess as server\n" + \
" -c [IP ADDRESS] run Chess as client connecting to [IP ADDRESS]")
x = ''
xa = ''
y = ''
ya = ''
if (chess.clientServerToggle == 2):
chess.initialiseBoard()
chess.printBoardToTerminal()
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect(("127.0.0.1", PORT))
while (True):
while (True):
print("Please enter number for piece you wish to move or 9 too exit")
x = int(input())
if (x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6 or x == 7 or x == 8):
break
elif (x == 9):
return 0
else:
print("You have entered an illegal character please try again")
x = 0
while (True):
print("Please enter letter for piece you wish to move or 9 too exit")
y = input()
if (y == 'a' or y == 'A' or y == 'b' or y == 'B' or y == 'c' or y == 'C' or y == 'd' or y == 'D'
or y == 'e' or y == 'E' or y == 'f' or y == 'F' or y == 'g' or y == 'G' or y == 'h' or y == 'H'):
break
elif (y == '9'):
return 0
else:
print("You have entered an illegal character please try again")
y = 0
while (True):
print("Please enter number for were you wish piece to move too or 9 too exit")
xa = int(input())
if (xa == 1 or xa == 2 or xa == 3 or xa == 4 or xa == 5 or xa == 6 or xa == 7 or xa == 8):
break
elif (xa == 9):
return 0
else:
print("You have entered an illegal character please try again")
xa = 0
while (True):
print("Please enter letter for were you wish piece to move too or 9 too exit")
ya = input()
if (ya == 'a' or ya == 'A' or ya == 'b' or ya == 'B' or ya == 'c' or ya == 'C' or ya == 'd' or ya == 'D'
or ya == 'e' or ya == 'E' or ya == 'f' or ya == 'F' or ya == 'g' or ya == 'G' or ya == 'h' or
ya == 'H'):
break
elif (ya == '9'):
return 0
else:
print("You have entered an illegal character please try again")
ya = 0
chess.movePiece(x, y, xa, ya)
chess.printBoardToTerminal()
inputString = str(x) + y + str(xa) + ya
clientSocket.send(inputString.encode())
receiveMessage = clientSocket.recv(128).decode("utf-8", "ignore")
a = int(receiveMessage[0])
b = int(receiveMessage[2])
chess.movePiece(a, receiveMessage[1], b, receiveMessage[3])
chess.printBoardToTerminal()
if(chess.clientServerToggle == 1):
chess.initialiseBoard()
chess.printBoardToTerminal()
serverSocketHandler = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
serverSocketHandler.bind(("127.0.0.1", PORT))
serverSocketHandler.listen(5)
conn, addr = serverSocketHandler.accept()
while (True):
receiveMessage = conn.recv(128)
a = int(receiveMessage[0])
b = int(receiveMessage[2])
chess.movePiece(a, receiveMessage[1], b, receiveMessage[3])
chess.printBoardToTerminal()
while (True):
print("Please enter number for piece you wish to move or 9 too exit")
x = input()
if (x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6 or x == 7 or x == 8):
break
elif (x == 9):
return 0
else:
print("You have entered an illegal character please try again")
x = 0
while (True):
print("Please enter letter for piece you wish to move or 9 too exit")
y = input()
if (y == 'a' or y == 'A' or y == 'b' or y == 'B' or y == 'c' or y == 'C' or y == 'd' or y == 'D'
or y == 'e' or y == 'E' or y == 'f' or y == 'F' or y == 'g' or y == 'G' or y == 'h' or y == 'H'):
break
elif (y == '9'):
return 0
else:
print("You have entered an illegal character please try again")
y = 0
while (True):
print("Please enter number for were you wish piece to move too or 9 too exit")
xa = input()
if (xa == 1 or xa == 2 or xa == 3 or xa == 4 or xa == 5 or xa == 6 or xa == 7 or xa == 8):
break
elif (xa == 9):
return 0
else:
print("You have entered an illegal character please try again")
xa = 0
while (True):
print("Please enter letter for were you wish piece to move too or 9 too exit")
ya = input()
if (ya == 'a' or ya == 'A' or ya == 'b' or ya == 'B' or ya == 'c' or ya == 'C' or ya == 'd' or ya == 'D'
or ya == 'e' or ya == 'E' or ya == 'f' or ya == 'F' or ya == 'g' or ya == 'G' or ya == 'h' or
ya == 'H'):
break
elif (ya == '9'):
return 0
else:
print("You have entered an illegal character please try again")
ya = 0
chess.movePiece(int(x), y, int(xa), ya)
chess.printBoardToTerminal()
inputString = " "
inputString[0] = x
inputString[1] = y
inputString[2] = xa
inputString[3] = ya
serverSocketHandler.send(inputString)
else:
x = ''
xa = ''
y = ''
ya = ''
chess.initialiseBoard()
chess.printBoardToTerminal()
while (True):
while (True):
print("Please enter number for piece you wish to move or 9 too exit")
x = int(input())
if (x == 1 or x == 2 or x == 3 or x == 4 or x == 5 or x == 6 or x == 7 or x == 8):
break
elif (x == 9):
return 0
else:
print("You have entered an illegal character please try again")
x = 0
while (True):
print("Please enter letter for piece you wish to move or 9 too exit")
y = input()
if (y == 'a' or y == 'A' or y == 'b' or y == 'B' or y == 'c' or y == 'C' or y == 'd' or y == 'D'
or y == 'e' or y == 'E' or y == 'f' or y == 'F' or y == 'g' or y == 'G' or y == 'h' or y == 'H'):
break
elif (y == '9'):
return 0
else:
print("You have entered an illegal character please try again")
y = 0
while (True):
print("Please enter number for were you wish piece to move too or 9 too exit")
xa = int(input())
if (xa == 1 or xa == 2 or xa == 3 or xa == 4 or xa == 5 or xa == 6 or xa == 7 or xa == 8):
break
elif (xa == 9):
return 0
else:
print("You have entered an illegal character please try again")
xa = 0
while (True):
print("Please enter letter for were you wish piece to move too or 9 too exit")
ya = input()
if (ya == 'a' or ya == 'A' or ya == 'b' or ya == 'B' or ya == 'c' or ya == 'C' or ya == 'd' or ya == 'D'
or ya == 'e' or ya == 'E' or ya == 'f' or ya == 'F' or ya == 'g' or ya == 'G' or ya == 'h' or ya == 'H'):
break
elif (ya == '9'):
return 0
else:
print("You have entered an illegal character please try again")
ya = 0
chess.movePiece(x, y, xa, ya)
chess.printBoardToTerminal()
chess.engineMove()
chess.printBoardToTerminal()
main()