Skip to content

Commit 6bf727b

Browse files
Merge pull request #1 from sucheta-nandy/sucheta-nandy-tictactoe
Add files via upload
2 parents f123847 + 2bfb5f8 commit 6bf727b

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tic_tac_toe.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Sat Mar 9 11:20:55 2019
4+
5+
@author: Sucheta
6+
"""
7+
8+
import numpy
9+
board=numpy.array([['-','-','-'],['-','-','-'],['-','-','-']])
10+
p1s='X'
11+
p2s='O'
12+
13+
def check_rows(symbol):
14+
for r in range(3):
15+
count=0
16+
for c in range(3):
17+
if board[r][c]==symbol:
18+
count=count+1
19+
if count==3:
20+
print(symbol,"Winner")
21+
return True
22+
return False
23+
24+
def check_cols(symbol):
25+
for r in range(3):
26+
count=0
27+
for c in range(3):
28+
if board[r][c]==symbol:
29+
count=count+1
30+
if count==3:
31+
print(symbol,"Winner")
32+
return True
33+
return False
34+
35+
def check_diagonals(symbol):
36+
if board[0][2]==board[1][1] and board[1][1]==board[2][0] and board[1][1]==symbol:
37+
print(symbol,"Winner")
38+
return True
39+
if board[0][2]==board[1][1] and board[1][1]==board[2][0] and board[1][1]==symbol:
40+
print(symbol,"Winner")
41+
return True
42+
return False
43+
44+
def won(symbol):
45+
check_rows(symbol) or check_cols(symbol) or check_diagonals(symbol)
46+
47+
def place(symbol):
48+
print(numpy.matrix(board))
49+
while(1):
50+
row=int(input("Enter your row position 1, 2 or 3: "))
51+
col=int(input("Enter your column position 1, 2 or 3: "))
52+
if row>0 and row<4 and col>0 and col<4 and board[row-1][col-1] == '-':
53+
break
54+
else:
55+
print("Invalid input. PLease enter again")
56+
board[row-1][col-1]=symbol
57+
58+
def play():
59+
for turn in range(9):
60+
if (turn%2==0):
61+
print("X Turn")
62+
place(p1s)
63+
if won(p1s):
64+
break
65+
else:
66+
print("O Turn")
67+
place(p2s)
68+
if won(p2s):
69+
break
70+
if not(won(p1s)) and not(won(p2s)):
71+
print("It is a draw.")
72+
73+
play()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy