forked from mayankgb2/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic-Tac-Tao.py
More file actions
115 lines (97 loc) · 2.46 KB
/
Tic-Tac-Tao.py
File metadata and controls
115 lines (97 loc) · 2.46 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#https://www.facebook.com/jyothiprakash.patnaikuni/posts/107593291135289
#subscribed by jyothiprakash patnaik
'''
1|2|3
-+-+-
4|5|6
-+-+-
7|8|9
'''
import random
def display(board):
print(f"""
{board[7]}|{board[8]}|{board[9]} 7 | 8 | 9
---+---+--- ---+---+---
{board[4]}|{board[5]}|{board[6]} -- Position --> 4 | 5 | 6
---+---+--- ---+---+---
{board[1]}|{board[2]}|{board[3]} 1 | 2 | 3
""")
def check(board):
check = 0
# row
if board[1] == board[2] == board[3] != ' ' or \
board[4] == board[5] == board[6] != ' ' or \
board[7] == board[8] == board[9] != ' ':
check = 1
# col
elif board[1] == board[4] == board[7] != ' ' or \
board[2] == board[5] == board[8] != ' ' or \
board[3] == board[6] == board[9] != ' ':
check = 1
# diagonal
elif board[1] == board[5] == board[9] != ' ' or \
board[3] == board[5] == board[7] != ' ':
check = 1
return check
def valid_input():
while True:
pos = input("Enter position:")
if pos != ' ':
try :
if int(pos) in range(1,10):
pos = int(pos)
break
print("\nNot a valid Number")
except:
print("Enter a Number")
else:
print("Thank you for Playing Tic Tac Toe!")
exit()
return int(pos)
def valid_pos(turn):
print(f"{turn} chance")
pos = valid_input()
while True:
print(pos)
if board[pos] == ' ':
board[pos] = turn
break
else:
print("Cannot Overwrite, Plz Select new loc")
pos = valid_input()
def userInput(board,symbol):
sym1, sym2 = symbol[random.randint(0,1)]
# print(sym1,sym2)
print(f"\n\n{sym1} is going first\n")
display(board)
for i in range(9):
if i%2 == 0:
turn = ' '+sym1+' '
valid_pos(turn)
display(board)
else:
turn = ' '+sym2+' '
valid_pos(turn)
display(board)
if i >= 4:
if check(board):
display(board)
print(f"'{turn}' Won")
break
if i == 8:
print("None Won. It's a TIE")
def main():
global board
board = ["Just to adjust loc :D",' ',' ',' ',' ',' ',' ',' ',' ',' ']
symbol = [("X","O"),("O","X")]
again = 'y'
while True and again == 'y':
marker = input("\nEnter the Marker: ").upper()
if marker == "X" or marker == "O":
userInput(board, symbol)
again = input("\nDo you wana play again? [y/n] ")
else:
print("plz enter a correct marker (X,O)")
# display(board)
print("Thank you for Playing My game.")
main()