Skip to content

Commit 1fd3454

Browse files
authored
Create Main
1 parent da25aab commit 1fd3454

File tree

1 file changed

+153
-0
lines changed
  • Tic-Tac-Toe_Ayush92347

1 file changed

+153
-0
lines changed

Tic-Tac-Toe_Ayush92347/Main

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import os
2+
3+
import time
4+
5+
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
6+
7+
player = 1
8+
9+
Win = 1
10+
11+
Draw = -1
12+
13+
Running = 0
14+
15+
Stop = 1
16+
17+
Game = Running
18+
19+
Mark = 'X'
20+
21+
22+
23+
def DrawBoard():
24+
25+
print(" %c | %c | %c " % (board[1],board[2],board[3]))
26+
27+
print("___|___|___")
28+
29+
print(" %c | %c | %c " % (board[4],board[5],board[6]))
30+
31+
print("___|___|___")
32+
33+
print(" %c | %c | %c " % (board[7],board[8],board[9]))
34+
35+
print(" | | ")
36+
37+
38+
39+
def CheckPosition(x):
40+
41+
if(board[x] == ' '):
42+
43+
return True
44+
45+
else:
46+
47+
return False
48+
49+
50+
51+
def CheckWin():
52+
53+
global Game
54+
55+
if(board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
56+
57+
Game = Win
58+
59+
elif(board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
60+
61+
Game = Win
62+
63+
elif(board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
64+
65+
Game = Win
66+
67+
elif(board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
68+
69+
Game = Win
70+
71+
elif(board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
72+
73+
Game = Win
74+
75+
elif(board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
76+
77+
Game=Win
78+
79+
elif(board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
80+
81+
Game = Win
82+
83+
elif(board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
84+
85+
Game=Win
86+
87+
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]!=' '):
88+
89+
Game=Draw
90+
91+
else:
92+
93+
Game=Running
94+
95+
96+
97+
print("Player 1 [X] --- Player 2 [O]\n")
98+
99+
print()
100+
101+
102+
103+
time.sleep(3)
104+
105+
while(Game == Running):
106+
107+
os.system('cls')
108+
109+
DrawBoard()
110+
111+
if(player % 2 != 0):
112+
113+
print("Player 1's chance")
114+
115+
Mark = 'X'
116+
117+
else:
118+
119+
print("Player 2's chance")
120+
121+
Mark = 'O'
122+
123+
choice = int(input("Enter the position between [1-9] where you want to mark : "))
124+
125+
if(CheckPosition(choice)):
126+
127+
board[choice] = Mark
128+
129+
player+=1
130+
131+
CheckWin()
132+
133+
134+
135+
os.system('cls')
136+
137+
DrawBoard()
138+
139+
if(Game==Draw):
140+
141+
print("Game Draw")
142+
143+
elif(Game==Win):
144+
145+
player-=1
146+
147+
if(player%2!=0):
148+
149+
print("Player 1 Won")
150+
151+
else:
152+
153+
print("Player 2 Won")

0 commit comments

Comments
 (0)