-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe Game.py
More file actions
74 lines (64 loc) · 1.71 KB
/
TicTacToe Game.py
File metadata and controls
74 lines (64 loc) · 1.71 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
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 1 11:49:52 2023
@author: HP
"""
import numpy as np
board=np.array(['_','_','_','_','_','_','_','_','_'])
print(board.reshape(3,3))
p1s="X"
p2s="O"
def place(sym):
pos=int(input("Enter position(1-9):"))
if pos>9 or pos<1:
print('Invalid input')
elif (board[pos-1]=='_'):
board[pos-1]=sym
print(board.reshape(3,3))
else:
print('Position already filled')
place(sym)
def check_rows(sym):
if(board[0]==board[1]==board[2]==sym):
return True
elif(board[3]==board[4]==board[5]==sym):
return True
elif(board[6]==board[7]==board[8]==sym):
return True
else:
return False
def check_column(sym):
if(board[0]==board[3]==board[6]==sym):
return True
elif(board[1]==board[4]==board[7]==sym):
return True
elif(board[2]==board[5]==board[8]==sym):
return True
else:
return False
def check_diagonal(sym):
if(board[0]==board[4]==board[8]==sym):
return True
elif(board[2]==board[4]==board[6]==sym):
return True
else:
return False
def won(symbol):
return check_rows(symbol) or check_column(symbol) or check_diagonal(symbol)
def play():
for turn in range(9):
if turn%2==0:
print('\nTurn of',p1s)
place(p1s)
if won(p1s):
print(p1s,'Won!')
break
else:
print('\nTurn of',p2s)
place(p2s)
if won(p2s):
print(p2s,'Won!')
break
if not(won(p1s)) and not(won(p2s)):
print("Draw")
play()