-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe
74 lines (64 loc) · 1.79 KB
/
TicTacToe
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
# Tic Tac Toe, text based (no gui)
# Demo of 2D arrays
# define some constants
EMPTY = "."
X = "X"
O = "O"
board = []
def clearBoard():
global board
board = []
for row in range(3):
board.append([])
for col in range(3):
board[row].append(EMPTY)
# Could also be done like this:
#board = [ [EMPTY, EMPTY, EMPTY],
# [EMPTY, EMPTY, EMPTY],
# [EMPTY, EMPTY, EMPTY] ]
def showBoard():
print # empty line before
for row in range(3):
for col in range(3):
print board[row][col],
print # end of row
print # empty line after
def checkRows(who):
for row in range(3):
if board[row][0]==who and board[row][1]==who and board[row][2]==who:
return True
return False
def checkCols(who):
for col in range(3):
if board[0][col]==who and board[1][col]==who and board[2][col]==who:
return True
return False
def checkDiagonals(who):
d1 = board[0][0]==who and board[1][1]==who and board[2][2]==who
d2 = board[2][0]==who and board[1][1]==who and board[0][2]==who
return d1 or d2
def checkWin(who):
return checkRows(who) or checkCols(who) or checkDiagonals(who)
def play():
turn = X # X always plays first
clearBoard()
while True:
if checkWin(X):
print "X wins!"
return
if checkWin(O):
print "O wins!"
return
row = int(raw_input(turn + ", what row? "))
col = int(raw_input(turn + ", what col? "))
if board[row][col] != EMPTY:
print "You Cheated!"
print turn, "loses!"
return
board[row][col] = turn
showBoard()
if turn==X:
turn = O
else:
turn = X
play() # start the game