-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic tac toe.py
88 lines (80 loc) · 2.41 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
import os
import time
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
player = 1
win = 1
draw = -1
running = 0
stop = 1
Game = running
Mark = 'O'
#This Function displays Game Board
def GameBoard():
print(" %c | %c | %c " % (board[1],board[2],board[3]))
print(" _|___|_ ")
print(" %c | %c | %c " % (board[4],board[5],board[6]))
print(" _|___|_ ")
print(" %c | %c | %c " % (board[7],board[8],board[9]))
print(" | | ")
#Check whether the position is empty or not
def checkPosition(x):
if(board[x] == ' '):
return True
else:
return False
#Check whether player has won or not
def Checkwin():
global Game
#Horizontal win
if(board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
Game = win
elif(board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
Game = win
elif(board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
Game = win
#Vertical win
elif(board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
Game = win
elif(board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
Game = win
elif(board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
Game=win
#Diagonal win
elif(board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
Game = win
elif(board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
Game=win
#Draw condition
elif(board[1]!=' ' and board[2]!=' ' and board[3]!=' ' and board[4]!=' ' and board[5]!=' ' and board[6]!=' ' and board[7]!=' ' and board[8]!=' ' and board[9]!=' '):
Game=draw
else:
Game=running
print("TIC TAC TOE GAME")
print("Player 1 [0] Player 2 [X]\n")
print()
print()
time.sleep(2)
while(Game == running):
os.system('cls')
GameBoard()
if(player % 2 != 0):
print("Player 1's turn")
Mark = 'O'
else:
print("Player 2's turn")
Mark = 'X'
choice = int(input("Enter the position [1-9] : "))
if(checkPosition(choice)):
board[choice] = Mark
player+=1
Checkwin()
os.system('cls')
GameBoard()
if(Game==draw):
print("Game Draw")
elif(Game==win):
player-=1
if(player%2!=0):
print("Player 1 is the WINNER")
else:
print("Player 2 is the WINNER")