-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerHelper.py
127 lines (117 loc) · 3.44 KB
/
PlayerHelper.py
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
board = ['-','-','-','-','-','-','-','-','-']
player01Wins = False
player02Wins = False
isGameDraw = False
def GameResult():
res = -1
if player01Wins:
global res
res = 1
return res
elif player02Wins:
global res
res = 2
return res
elif isGameDraw:
global res
res = 0
return res
return res
def PrintBoard():
print "\n\n"
print "*********Tic Tac Toe*********"
print " " + board[0] + " " + board[1] + " " + board[2]
print " " + board[3] + " " + board[4] + " " + board[5]
print " " + board[6] + " " + board[7] + " " + board[8]
print "*****************************"
def Player01(cellNo):
cellNo = cellNo - 1
if cellNo >= len(board):
print "No such cell!"
pass
else:
board[cellNo] = 'X'
PrintBoard()
def Player02(cellNo):
cellNo = cellNo - 1
if cellNo >= len(board):
print "No such cell!"
pass
else:
board[cellNo] = 'O'
PrintBoard
def Reset():
print "Resetting Board"
global board
global isGameDraw
global player01Wins
global player02Wins
i = 0
for c in board:
board[i] = '-'
i += 1
# print c
isGameDraw = False
player01Wins = False
player02Wins = False
# print board
def CheckResult():
i = 0
for b in board:
global player01Wins
global player02Wins
global isGameDraw
if i == 0 or i == 3 or i == 6:
if board[i] == board[i+1] == board[i+2] == 'X':
print "Player1 wins"
player01Wins = True
player02Wins = False
isGameDraw = False
elif board[i] == board[i+1] == board[i+2] == 'O':
print "Player2 wins"
player01Wins = False
player02Wins = True
isGameDraw = False
if i == 0 or i == 1 or i == 2:
if board[i] == board[i+3] == board[i+6] == 'X':
print "Player1 wins"
player01Wins = True
player02Wins = False
isGameDraw = False
elif board[i] == board[i+3] == board[i+6] == 'O':
print "Player2 wins"
player01Wins = False
player02Wins = True
isGameDraw = False
if i == 0:
if board[i] == board[i+4] == board[i+8] == 'X':
print "Player1 wins"
player01Wins = True
player02Wins = False
isGameDraw = False
elif board[i] == board[i+4] == board[i+8] == 'O':
print "Player2 wins"
player01Wins = False
player02Wins = True
isGameDraw = False
if i == 2:
if board[i] == board[i+2] == board[i+4] == 'X':
print "Player1 wins"
player01Wins = True
player02Wins = False
isGameDraw = False
elif board[i] == board[i+2] == board[i+4] == 'O':
print "Player2 wins"
player01Wins = False
player02Wins = True
isGameDraw = False
i = i + 1
counter = 0
for c in board:
if c == '-':
counter += 1
if counter <= 1:
print "That's a Draw!"
isGameDraw = True
player01Wins = False
player02Wins = False