Skip to content

Commit bcb2856

Browse files
authored
Create TIC TAC TOE GAME USING PYTHON
IN OUR CHILDHOOD DAYS WE DAILY PLAY THIS TIC TAC TOE GAME WHICH IS VERY INTERESTING.WE PLAY THE IN OUR NOTEBOOKS MANUALLY NOW WE CAN PLAY WITH PROGRAMMING.
1 parent d99f6d6 commit bcb2856

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

TIC TAC TOE GAME USING PYTHON

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#TIC TAC TOE
2+
import numpy
3+
board=numpy.array([["-","-","-"],["-","-","-"],["-","-","-"]])
4+
p1s="X"
5+
p2s="O"
6+
def check_rows(symbol):
7+
for r in range(3):
8+
count=0
9+
for c in range(3):
10+
if board[r][c]==symbol:
11+
count=count+1
12+
if count==3:
13+
print(symbol,"WON")
14+
return True
15+
return False
16+
def check_cols(symbol):
17+
for c in range(3):
18+
count=0
19+
for r in range(3):
20+
if board[r][c]==symbol:
21+
count=count+1
22+
if count==3:
23+
print(symbol,"WON")
24+
return True
25+
return False
26+
def check_diagonals(symbol):
27+
if board[0][2]==board[1][1] and board[1][1]==board[2][0] and board[1][1]==symbol:
28+
print(symbol,"WON")
29+
return True
30+
if board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[1][1]==symbol:
31+
print(symbol,"WON")
32+
return True
33+
return False
34+
def WON(symbol):
35+
return check_rows(symbol) or check_cols(symbol) or check_diagonals(symbol)
36+
def place(symbol):
37+
print(numpy.matrix(board))
38+
while(1):
39+
row=int(input("ENTER ROW-1 or 2 or 3:"))
40+
col=int(input("ENTER COLUMN-1 or 2 or 3:"))
41+
if row>0 and row<4 and col>0 and col<4 and board[row-1][col-1]=="-":
42+
board[row-1][col-1]=symbol
43+
break
44+
else:
45+
print("invalid input.please enter again")
46+
board[row-1][col-1]==symbol
47+
def play():
48+
for turn in range(9):
49+
if turn%2==0:
50+
print("X turn")
51+
place(p1s)
52+
if WON(p1s):
53+
break
54+
else:
55+
print("O turn")
56+
place(p2s)
57+
if WON(p2s):
58+
break
59+
if not WON(p1s) and not WON(p2s):
60+
print("DRAW")
61+
play()

0 commit comments

Comments
 (0)