Ce script fait partie des projets Python de HyperSkill.
j'ai fait toute la formation python de Hyperskill pendant le Confinement 2020.
# write your code here | |
class TicTacToe: | |
def __init__(self): | |
self.char = ["X", "O"] | |
self.turn = 0 | |
self.raw_cells = " " | |
self.cells = [self.raw_cells[i:i + 3] for i in range(0, len(self.raw_cells), 3)] | |
self.vertical = [[self.cells[i][r] for i in range(3)] for r in range(3)] | |
self.cells_dict = {k: v for k, *v in zip([3, 2, 1], *self.vertical)} | |
for _ in self.cells_dict: | |
self.cells_dict[_] = dict(zip(range(1, len(self.cells_dict[_]) + 1), self.cells_dict[_])) | |
def update(self, data): | |
self.raw_cells = ''.join([data[i][x] for i in data for x in data[i]]) | |
self.cells = [self.raw_cells[i:i + 3] for i in range(0, len(self.raw_cells), 3)] | |
self.vertical = [[self.cells[i][r] for i in range(3)] for r in range(3)] | |
self.cells_dict = {k: v for k, *v in zip([3, 2, 1], *self.vertical)} | |
for _ in self.cells_dict: | |
self.cells_dict[_] = dict(zip(range(1, len(self.cells_dict[_]) + 1), self.cells_dict[_])) | |
def check_horizontal(self): | |
check = [set(self.cells[i]) for i in range(3)] | |
if any(True if i == {' '} else False for i in list(check)): | |
return False | |
if any([True if len(set(self.cells[i])) == 1 else False for i in range(3)]): | |
return [r.pop() for r in check if len(r) == 1] | |
return False | |
def check_vertical(self): | |
check = [set(self.vertical[i]) for i in range(3)] | |
if any(True if i == {' '} else False for i in list(check)): | |
return False | |
if any([True if len(set(self.vertical[i])) == 1 else False for i in range(3)]): | |
return [r.pop() for r in check if len(r) == 1] | |
return False | |
def check_diagonal(self): | |
numbers = [0, 1, 2], [2, 1, 0] | |
diagonal_cells = [[self.cells[i][i] for i in numbers[0]], | |
[self.cells[i][numbers[1][i]] for i in numbers[0]]] | |
check = [set(diagonal_cells[i]) for i in range(2)] | |
if any(True if i == {' '} else False for i in list(check)): | |
return False | |
if any([True if len(set(diagonal_cells[i])) == 1 else False for i in range(2)]): | |
return [r.pop() for r in check if len(r) == 1] | |
return False | |
def check_victory(self): | |
hor_check = self.check_horizontal() | |
if hor_check and len(hor_check) == 1: | |
return f"{hor_check[0]} wins" | |
ver_check = self.check_vertical() | |
if ver_check and len(ver_check) == 1: | |
return f"{ver_check[0]} wins" | |
dia_check = self.check_diagonal() | |
if dia_check and len(dia_check) == 1: | |
return f"{dia_check[0]} wins" | |
if all([set(self.raw_cells) == {'X', 'O'}, | |
abs(self.raw_cells.count("X") - self.raw_cells.count("O")) == 1]): | |
return "Draw" | |
if any([len(hor_check) > 1 if type(hor_check) is not bool else False, | |
len(ver_check) > 1 if type(ver_check) is not bool else False, | |
len(dia_check) > 1 if type(dia_check) is not bool else False]): | |
return "Impossible" | |
if abs(self.raw_cells.count("X") - self.raw_cells.count("O")) > 1: | |
return "Impossible" | |
return False | |
def show_table(self): | |
print("---------\n" | |
"| {0} {1} {2} |\n" | |
"| {3} {4} {5} |\n" | |
"| {6} {7} {8} |\n" | |
"---------".format(*self.raw_cells)) | |
def ask_player(self): | |
raw_coordinates = input("Enter the coordinates: ").split(" ") | |
if not raw_coordinates[0].isdigit() and not raw_coordinates[1].isdigit(): | |
print("You should enter numbers!") | |
return self.ask_player() | |
if not all([1 <= int(raw_coordinates[0]) <= 3, 1 <= int(raw_coordinates[1]) <= 3]): | |
print("Coordinates should be from 1 to 3!") | |
return self.ask_player() | |
coordinates = [int(raw_coordinates[0]), int(raw_coordinates[1])] | |
self.add_entry(*coordinates) | |
def add_entry(self, x, y): | |
if self.cells_dict[y][x] != " ": | |
print("This cell is occupied! Choose another one!") | |
return self.ask_player() | |
self.cells_dict[y][x] = self.char[self.turn] | |
self.turn = int(not self.turn) | |
self.update(self.cells_dict) | |
def run(self): | |
while True: | |
self.show_table() | |
victory = self.check_victory() | |
if victory: | |
print(victory) | |
break | |
self.ask_player() | |
TicTacToe().run() |
cyvax - 2024