-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.py
More file actions
176 lines (146 loc) · 4.98 KB
/
tictactoe.py
File metadata and controls
176 lines (146 loc) · 4.98 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Tic Tac Toe - Python #
import random
class TicTacToe():
board = []
player = "X"
bot = "O"
def __init__(self):
print("Welcome to Tic Tac Toe")
self.choose_marker(input("Choose you marker (X/O): "))
self.reset_board()
def play(self):
while True:
self.print_instructions()
self.show_board()
if self.board_has_free_cells():
user_input = input("Your move: ")
print("\n")
user_move = self.process_user_input(
user_input,
self.validate_user_input(user_input)
)
if not user_move.isspace():
print(user_move)
else:
winner = self.check_winner()
if winner.isspace():
self.bots_move()
winner = self.check_winner()
if not winner.isspace():
self.show_board()
print(f"{winner}\n")
self.reset_board()
print("\nLet's play again !")
else:
print("\n")
print("It's a draw. Let's start again.")
self.reset_board()
def show_board(self):
for index, value in enumerate(self.board):
if (index + 1) % 3 != 0:
self.print_position(value, True)
else:
self.print_position(value)
self.print_game_separator(index)
print("")
def process_user_input(self, user_input, isValid):
if isValid == 1:
return self.player_move(int(user_input))
elif isValid == 0:
print("Thanks for playing. Good Bye!")
exit()
else:
return "Wrong Input. Try again"
def player_move(self, pos):
if empty_cell(self.board[pos-1]):
self.board[pos-1] = self.player
return " "
else:
return f"The position {pos} is occupied"
def bots_move(self):
if self.board_has_free_cells():
pos = self.get_random_pos()
while self.board[pos-1] != " ":
pos = self.get_random_pos()
self.board[pos-1] = self.bot
def check_winner(self):
cell = self.check_consecutives()
if cell == self.player or cell == self.bot:
return self.winners_msg(cell)
else:
return " "
def winners_msg(self, winner):
if winner == self.player:
return "Congratulations! You won the round."
else:
return "Shoot! Better luck next time."
def check_consecutives(self):
# TODO: Akhil - Need to improve the logic here
# horizontal
for row in [0, 3, 6]:
curr = self.board[row]
if curr == self.board[row+1] and curr == self.board[row+2] and not empty_cell(curr):
return curr
# vertical
for col in [0, 1, 2]:
curr = self.board[col]
if curr == self.board[col+3] and curr == self.board[col+6] and not empty_cell(curr):
return curr
# cross
for cross in [[0, 4, 8], [2, 4, 6]]:
if self.board[cross[0]] == self.board[cross[1]] == self.board[cross[2]]:
return self.board[cross[0]]
return " "
def validate_user_input(self, user_input):
if user_input.isdigit() and int(user_input) in range(1, 10):
return 1
elif user_input.isalpha() and user_input == 'q':
return 0
else:
return -1
def choose_marker(self, marker):
if marker.upper() == "X" or marker.upper() == "O":
print(f"You have chosen '{marker.upper()}'")
self.player = marker.upper()
self.bot = opposite(marker)
else:
print("You choice is weird. Defaulting to 'X'")
def print_position(self, value, separator=False):
if separator:
print(f" {value} ", end="|")
else:
print(f" {value} ", end="")
def print_game_separator(self, index):
if index + 1 != 9:
print("\n---|---|---")
else:
print("")
def board_has_free_cells(self):
return " " in self.board
def reset_board(self):
self.board = [" "] * 9
def print_instructions(self):
self.separator()
print("Enter position number (1-9) to play your move")
print("Enter 'q' to exit")
self.separator()
def separator(self):
print(
"".join(
list(
map(
lambda n: "-",
range(0, 100)
)
)
)
)
def get_random_pos(self):
return random.randint(1, 9)
def empty_cell(cell):
return cell.isspace()
def opposite(marker):
if marker.upper() == "X":
return "O"
else:
return "X"