-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtictactoe.py
134 lines (113 loc) · 3.59 KB
/
tictactoe.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import random
def display_board(board):
print("\n")
print(" | |")
print(''+ board[7] + " |" + board[8] + ' |' + board[9])
print(" | |")
print(" -----------------")
print(" | |")
print(''+ board[4]+ " |" + board[5] + ' |' + board[6])
print(" | |")
print(" ------------------")
print(" | |")
print(''+ board[1]+ " |" + board[2] + ' |' + board[3])
print(" | |")
def player_input(a,b):
n=""
if n!="X" or n!="O":
while n!="X" and n!="O":
n=input(a +" choose the marker X or O ").upper()
if n=="X":
return "X","O"
else:
return "O","X"
def placemarker(board,marker,position):
if board[position]=="":
board[position]=marker
else:
print()
print("oops!! Place Occupied You Lost Your Chance ")
def wincheck(b,m):
if (b[1]==m and b[2]==m and b[3]==m) or (b[4]==m and b[5]==m and b[6]==m) or (b[7]==m and b[8]==m and b[9]==m) or (b[4]==m and b[7]==m and b[1]==m)or (b[9]==m and b[6]==m and b[3]==m) or (b[5]==m and b[8]==m and b[2]==m) or (b[7]==m and b[5]==m and b[3]==m) or (b[9]==m and b[5]==m and b[1]==m):
return True
else:
return False
def spacecheck(b,pos):
return b[pos]==""
def fullboardcheck(b):
for i in range(1,10):
if spacecheck(b,i):
return False
return True
def playerchoice(b):
pos=0
print()
while pos not in [1,2,3,4,5,6,7,8,9] and spacecheck(b,pos):
pos=int(input("enter the position from 1-9 "))
return pos
def choosefirst(a,b):
flip=random.randint(0,1)
if flip==0:
return a
else:
return b
def replay():
c=input("want to play again? yes or no")
return c=="yes"
print("WELCOME TO TIC TAC TOE")
print()
a=input("PLAYER ONE ENTER YOUR NAME : ")
print()
b=input("PLAYER TWO ENTER YOUR NAME : ")
print()
while True:
boards=[""]*10
player1_marker,player2_marker=player_input(a,b)
turn=choosefirst(a,b)
print()
print(turn+" will start the game ")
print()
play_game=input("Ready to Play? Y or N ")
if play_game=="Y":
game_on=True
else:
game_on=False
while game_on:
if turn== a:
display_board(boards)
pos=playerchoice(boards)
placemarker(boards,player1_marker,pos)
if wincheck(boards,player1_marker):
display_board(boards)
print(a.upper()+" IS THE WINNER")
print()
game_on=False
else:
if fullboardcheck(boards):
display_board(boards)
print("TIE MATCH")
print()
game_on=False
else:
turn=b
else:
display_board(boards)
pos=playerchoice(boards)
placemarker(boards,player2_marker,pos)
if wincheck(boards,player2_marker):
display_board(boards)
print()
print(b.upper()+" IS THE WINNER")
print()
game_on=False
else:
if fullboardcheck(boards):
display_board(boards)
print()
print("TIE MATCH")
print()
game_on=False
else:
turn=a
if not replay():
break