-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
184 lines (170 loc) · 4.93 KB
/
game.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Tic Tac Toe Game
import random
board = " 1 | 2 | 3 \n-----------\n 4 | 5 | 6 \n-----------\n 7 | 8 | 9 "
chars = ['x', 'o']
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def check_for_solution(board, list):
'''Method that checks for a solution.'''
if 1 in list:
if 2 in list:
if 3 in list:
return True
else:
return False
elif 4 in list:
if 7 in list:
return True
else:
return False
elif 5 in list:
if 9 in list:
return True
else:
return False
elif 2 in list:
# List doesn't contain 1 and therefore match across top is ruled out
if 5 in list:
if 8 in list:
return True
else:
return False
else:
return False
elif 3 in list:
# List doesn't contain 1 or 2 and therefore match across top is ruled out
if 5 in list:
if 7 in list:
return True
else:
return False
elif 6 in list:
if 9 in list:
return True
else:
return False
else:
return False
elif 4 in list:
# List doesn't contain 1, 2, or 3 and therefore match left column is ruled out
if 5 in list:
if 6 in list:
return True
else:
return False
else:
return False
elif 5 in list:
# List doesn't contain 1, 2, 3 or 4 and therefore everything is ruled out
return False
elif 6 in list:
# List doesn't contain 1, 2, 3, 4 or 5 and therefore everything is ruled out
return False
elif 7 in list:
# List doesn't contain 1, 2, 3, 4, 5 or 6 and therefore match left column and
# top right to bottom left are ruled out
if 8 in list:
if 9 in list:
return True
else:
return False
else:
return False
elif 8 in list:
# List doesn't contain 1, 2, 3, 4, 5, 6 or 7 and therefore everything is ruled
# out
return False
elif 9 in list:
# List doesn't contain 1, 2, 3, 4, 5, 6, 7 or 8 and therefore everything is
# ruled out
return False
else:
return False
try:
playerchar = input('Choose your marker: ')
if playerchar.lower() == 'x':
playerchar = 'x'
compchar = 'o'
print("X is selected.")
elif playerchar.lower() == 'o':
playerchar = 'o'
compchar = 'x'
print("O is selected.")
else:
playerchar = random.choice(chars)
if playerchar == "x":
compchar = "o"
else:
compchar = "x"
print(f"Invalid input. Your randomly selected marker will be {playerchar}.")
except Exception as e:
print(f"Error: {e}")
index = random.randint(0, 1)
if index == 0:
print("Player has been selected to go first!")
isPlayerTurn = True
else:
print("Computer has been selected to go first!")
isPlayerTurn = False
gameIsOver = False
userList = []
compList = []
print(board)
while not gameIsOver:
if isPlayerTurn:
if check_for_solution(board, compList):
winner = "Computer"
break
invalid = True
while invalid:
if not nums:
winner = "Nobody"
break
move = input('Make your move.\n>')
try:
move = int(move)
except:
pass
if move not in nums:
print("Invalid selection. Please try again.")
else:
print("Move selected!")
invalid = False
userList.append(move)
try:
board = board.replace(str(move), playerchar)
nums.remove(move)
except:
winner = "Nobody"
break
print(board)
isPlayerTurn = False
if check_for_solution(board, userList):
winner = "User"
break
continue
else:
if check_for_solution(board, userList):
winner = "User"
break
print("Computer is thinking...")
try:
move = random.choice(nums)
board = board.replace(str(move), compchar)
nums.remove(move)
compList.append(move)
except Exception as e:
if check_for_solution(board, userList):
winner = "User"
elif check_for_solution(board, compList):
winner = "Computer"
else:
winner = "Nobody"
print(e)
break
print(board)
isPlayerTurn = True
if check_for_solution(board, compList):
winner = "Computer"
break
continue
print(f"The winner is {winner}!")