-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
89 lines (70 loc) · 1.78 KB
/
client.py
File metadata and controls
89 lines (70 loc) · 1.78 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
#client.py
#---------
#Mike Moniz - 0950795
#Adam Axtmann -
import socket
import sys
import time
import string
#printBoard
#prints to the screen the current version of the board
def printBoard():
i = 0;
line = ""
for item in board:
line = line + (item).rjust(1)
if (i + 1)% 3 == 0 and i < 6:
line = line + "\n---------\n"
elif i < 8:
line = line + " | "
i= i + 1
print line
board = ['0','1','2','3','4','5','6','7','8'] #initialize the board
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = sys.argv[1]
port = sys.argv[2]
#connect to the server
try:
s.connect((ip,int(port)))
# Receive data from the server and shut down
while 1:
state = s.recv(1)
#This state is to allow player 1 to make their move (ignoring invalid moves)
if (state == '1'):
printBoard()
data = raw_input('your turn: ')
s.send(data)
valid = s.recv(1)
#once valid, mark the board
if(valid == 'v'):
board[int(data)] = 'x'
printBoard()
#This state is to allow player 2 to make their move (ignoring invalid moves)
elif (state == '2'):
printBoard()
data = raw_input('your turn: ')
s.send(data)
valid = s.recv(1)
#once valid, mark the board
if(valid == 'v'):
board[int(data)] = 'o'
printBoard()
#This state is to update player 1's board with player 2's move
elif (state == 'q'):
val = s.recv(1)
board[int(val)] = 'x'
#This state is to update player 2's board with player 1's move
elif (state == 'w'):
val = s.recv(1)
board[int(val)] = 'o'
#This state is to terminate the client
elif (state == 't'):
break
#This state is to terminate the client and declare a winner
elif (state == 'x' or state == 'o'):
print state, " wins!"
break
except:
print "exception"
finally:
s.close()