-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode for tic tac toe
More file actions
83 lines (76 loc) · 2.07 KB
/
code for tic tac toe
File metadata and controls
83 lines (76 loc) · 2.07 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
def valid(lis):
while True:
x=input()
if not x in lis:
print('Enter a valid choice...')
continue
else:
break
return x
def display(board):
print(board[1] + '|' + board[2] + '|' + board[3])
print(board[4] + '|' + board[5] + '|' + board[6])
print(board[7] + '|' + board[8] + '|' + board[9])
def start(s):
return s=='yes'
def check(board):
t=False
for i in range(1,8,3):
if board[i]==board[i+1]==board[i+2]!=' ':
t=True
for i in range(1,4):
if board[i]==board[i+3]==board[i+6]!=' ':
t=True
if board[1]==board[5]==board[9]!=' ':
t = True
if board[3]==board[5]==board[7]!=' ':
t = True
return t
def play(board,f,x):
if f==0:
while True:
n = int(input(f'Enter the place where Player1 want to insert {x} '))
if (not n in range(1,10)) or board[n]!=' ':
print('This place is not empty or invalid')
continue
else:
break
board[n]=x
else:
while True:
n = int(input(f'Enter the place where Player2 want to insert {y} '))
if (not n in range(1,10)) or board[n]!=' ':
print('This place is not empty')
continue
else:
break
board[n]=y
return check(board)
print('Welcome to the tic tac toe game')
s = 'yes'
YN = ['yes','no']
while(start(s)):
XO = ['X','O']
print('What do Player 1 want to choose X or O? ')
x=valid(XO)
if x=='X':
y='O'
else:
y='X'
board=['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']
display((board))
f=0
cnt=0
while cnt<9:
val = play(board,f,x)
display(board)
if val:
print(f'Congratulations !!! Player {f+1} has won the match')
break
f=1-f
cnt+=1
if cnt==9:
print('Game tie :( ')
print('Do you want to play the game? yes or no ')
s = valid(YN)
print('Thanks for Playing, hope you enjoyed it!!')