-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe.py
147 lines (125 loc) · 3.57 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
#tic tac toe using python
#by PRADEEP RAGUL S
#NON-GRAPHICAL PROGRAM.
board = ["-","-","-","-","-","-","-","-","-"]
import os
postions_avilable=[1,2,3,4,5,6,7,8,9]
game_counter=0
position=0
#function for showning the board
def show_board():
clear()
for i in range(5):
print()
print(" ",end="")
print(board[0]+" | "+board[1]+" | "+board[2])
print(" ",end="")
print(board[3]+" | "+board[4]+" | "+board[5])
print(" ",end="")
print(board[6]+" | "+board[7]+" | "+board[8])
for i in range(5):
print()
def clear():
if(os.name == "posix" ):
os.system("clear")
else:
os.system("cls")
def check_pos(post):
if(board[post-1] == "-"):
return True
else:
return False
def get_pos():
global postions_avilable
try:
post = int(input("Enter position : "))
except ValueError :
post=0
while post not in postions_avilable:
try:
post = int(input("Enter position : "))
except ValueError :
post=0
check_pos(post)
postions_avilable.remove(post)
return post
def swap_player(g):
if(g%2==0):
print(" ",end="")
print("player X turn ")
for i in range(5):
print()
return "X"
else:
print(" ",end="")
print("player O turn ")
for i in range(5):
print()
return "O"
def check_win():
if(check_row()):
return True
elif (check_coloum()):
return True
elif(check_diagonal()):
return True
else:
False
def check_row():
global board
if((board[0]==board[1]==board[2]=="X") or (board[0]==board[1]==board[2]=="O")):
return True
elif((board[3]==board[4]==board[5]=="X") or (board[3]==board[4]==board[5]=="O")):
return True
elif((board[6]==board[7]==board[8]=="X") or (board[6]==board[7]==board[8]=="O")):
return True
else:
return False
def check_coloum():
global board
if ((board[0]==board[3]==board[6]=="X") or (board[0]==board[3]==board[6]=="O")):
return True
elif((board[1]==board[4]==board[7]=="X") or (board[1]==board[4]==board[7]=="O")):
return True
elif ((board[2]==board[5]==board[8]=="X") or (board[2]==board[5]==board[8]=="O")):
return True
else:
return False
def check_diagonal():
global board
if ((board[0]==board[4]==board[8]=="X") or (board[0]==board[4]==board[8]=="O")):
return True
elif ((board[2]==board[4]==board[6]=="X") or (board[2]==board[4]==board[6]=="O")):
return True
else:
return False
clear()
player1=input("Enter player X name : ")
player2= input("Enter player O name: ")
while (1):
show_board()
value=swap_player(game_counter)
print("postions avilable ",postions_avilable)
position=get_pos()
board[position-1]=value
game_counter+=1
if(check_win()):
show_board()
if (value == "X"):
print(" ",end="")
print("player "+player1+" wins")
for i in range (5):
print()
else:
print(" ",end="")
print("player "+player2+" wins")
for i in range (5):
print()
break
if (game_counter == 9 ):
show_board()
print(" ",end="")
print(" match draw ")
for i in range (5):
print()
break