-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic tac toee.py
More file actions
162 lines (138 loc) · 5.17 KB
/
Tic tac toee.py
File metadata and controls
162 lines (138 loc) · 5.17 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
"""
Created on Thu May 26 08:39:39 2022
@author: Amy-Jay
"""
print("The game is ready")
#The function below checks if there are still available spaces to be played in.
def empty(list_):
for item in list_:
if "_" in item:
return False
else:
return True
return False
#The function below checks if a player has won.
def iswon(subject, listt):
#The function below caters for horizontal matching.
def func(rr, y, list__):
if list__[0][y] == list__[1][y] == list__[2][y]== rr:
return True
else:
return False
#The function below caters for row or vertical matching.
def func2 (aa, list_):
for row in list_:
if all(i == aa for i in row):
return True
return False
#The function below caters for diagonal or cross-wise matching.
def diagonal(ss,lis):
if lis[0][0] == lis[1][1] == lis[2][2] == ss:
return True
if lis[0][2] == lis[1][1] == lis[2][0] == ss:
return True
list_ = (0,1,2)
#The following code merges all the functions such that any case where True, the player wins.
if func2(subject, listt) == True or any(func(subject,x, listt) == True for x in list_ ) or diagonal(subject, listt):
return True
else:
return False
#This is the main function for the progress of the game.
def playnow(xx):
#The following code confirms a value, X or O for the computer.
comp= "O"
if xx== "O":
comp ="X"
ticlist= [["_","_","_"], ["_","_","_"], ["_","_","_"]]
#This function prints the display of the game in an orderly arrangement.
def print_():
print("\n")
for item in ticlist:
print(*item)
print_()
#The loop below progresses the game, but breaks when 'empty' returns true or when any player wins.
while True:
#This variable is needed to break the outer while loop.
det = 0
x,y= [int(input("Row ")), int(input("Column "))]
ticlist[(x-1)][y-1] = xx
print_()
if not iswon(xx, ticlist):
import random
import Connecto
#The following loop helps the computer play only in a place that is empty for playing.
while True:
a = random.randint(1,3)
b = random.randint(1,3)
compchoose = ticlist[a-1][b-1]
if compchoose == "_":
brtlist = Connecto.intelligence(ticlist, xx)
if brtlist != 0:
r = brtlist[0]
s = brtlist[1]
if ticlist[r][s] == "_":
ticlist[r][s] = comp
else:
ticlist[a-1][b-1] = comp
else:
ticlist[a-1][b-1] = comp
if iswon(comp, ticlist):
print_()
print("You Lose")
det = 1
input1 = input("Do you want to play again?/nYes or No? ")
if input1.lower() == "yes":
send()
else:
print("Thank you for playing this game.")
break
else:
if empty(ticlist) == True:
print("Game Over")
input1 = input("Do you want to play again?/nYes or No? ")
if input1.lower() == "yes":
send()
else:
print("Thank you for playing this game.")
det = 1
elif empty(ticlist) == False:
continue
if det == 1:
break
print_()
else:
print("You Won!")
input1 = input("Do you want to play again?/nYes or No? ")
if input1.lower() == "yes":
send()
else:
print("Thank you for playing this game.")
break
#This is the initial function which collects user input and calls the inner functions.
def play(x):
if x== 0:
print("Coward!")
elif x== 1:
print("Ready to play!")
while True:
try:
XO= input("Are you X or O? ").lower()
if XO != "x" and XO != "o":
raise ValueError
except ValueError:
continue
else:
playnow(XO.upper())
break
elif x=="2":
print("Input yes or no")
send()
dict_rez= {"yes":1,
"no" : 0}
def send():
input_= input("Are you ready to play? ").lower()
value = dict_rez.get(input_,"2")
play(value)
#The calling of the first operation starts here.
send()