-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
120 lines (99 loc) · 2.9 KB
/
tic_tac_toe.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
#!/usr/bin/python3
board = [1,2,3,4,5,6,7,8,9]
turn = 'X'
win = False
draw = False
count = 0
print("\n")
print("Welcome to TIC TAC TOE GAME")
print("---------------------------\n")
print("Starting with ", turn,"")
def showBoard():
count = 1
print('\n-----------')
for b in board:
if(count % 3 == 0):
print('', b)
print('-----------')
else:
print('',b,'|', end="")
count+=1
def placing():
global count
global win
global draw
count = count + 1
if(count == 9 and win is not True):
win = True
draw = True
return
print("[",turn, "]", end="")
try:
pos = int(input(" , which position you want to place : "))
if(pos < 1 or pos > 9):
print("\n===================================")
print("Please insert number between 1 to 9")
print("===================================\n")
return
except ValueError:
print("\n======================")
print("Wrong input, try again")
print("======================\n")
return
if(chkPlace(pos) is False):
print("\n=====================================================")
print("You cannot place on this position! Please try again.")
print("=====================================================\n")
else:
place(pos)
return
def place(pos):
board[pos-1] = turn
showBoard()
print('>> ',turn, ' place at ', pos, '\n')
if(chkWin() is not True):
setTurn()
return
def setTurn():
global turn
turn = (turn is 'X') and 'O' or 'X'
return
def chkPlace(pos):
if(board[pos-1] is 'X' or board[pos-1] is 'O'):
return False
else:
return True
def chkWin():
pos1 = board[0]
pos2 = board[1]
pos3 = board[2]
pos4 = board[3]
pos7 = board[6]
global win
#Hoz
if((board[0] == pos1 and board[1] == pos1 and board[2] == pos1) or (board[3] == pos4 and board[4] == pos4 and board[5] == pos4) or (board[6] == pos7 and board[7] == pos7 and board[8] == pos7)):
win = True
return True
#Vert
if((board[0] == pos1 and board[3] == pos1 and board[6] == pos1) or (board[1] == pos2 and board[4] == pos2 and board[7] == pos2) or (board[2] == pos3 and board[5] == pos3 and board[8] == pos3)):
win = True
return True
#\
if((board[0] == pos1 and board[4] == pos1 and board[8] == pos1) or (board[2] == pos2 and board[4] == pos2 and board[6] == pos2)):
win = True
return True
return False
def printEnd():
if(draw is not False):
print("\n==================")
print("Game ended!\nDraw!")
print("==================\n")
return
print("\n==================")
print("Game ended!\nThe winner is ", turn,"!")
print("==================\n")
showBoard()
print()
while(win is not True):
placing()
printEnd()