/
sejeenn
/
python_basic
Обзор
Документация
Войти
/
sejeenn
/
python_basic
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Module24/09_tic-tac-toe/temp.py
55 строк
1 KB
Eugene Vorontsov
Copy repo python basic
03 фев 2025, 20:23
03 фев 2025, 20:23
a8fce4a
Код
Авторство
О чём код?
board = list(range(1, 10)) win_coordinates = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (1, 4, 7), (2, 5, 8), (3, 6, 9), (1, 5, 9), (3, 5, 7)] def draw_board(): print('-------------') for i in range(3): print('|', board[0 + i * 3], '|', board[1 + i * 3], '|', board[2 + i * 3], '|') print('-------------') def take_input(player_token): while True: value = input('Куда поставить? ' + player_token) if not (value in '123456789'): print('Ошибочный ввод, повторите!') continue value = int(value) if str(board[value - 1]) in 'XO': print('Эта клетка уже занята, повторите!') continue board[value - 1] = player_token break def check_win(): for each in win_coordinates: if board[each[0] - 1] == board[each[1] - 1] == board[each[2] - 1]: return board[each[1] - 1] else: return False def main(): counter = 0 while True: draw_board() if counter % 2 == 0: take_input('X') else: take_input('O') if counter > 3: winner = check_win() if winner: draw_board() print(winner, 'выиграл!') break counter += 1 if counter > 8: draw_board() print('Ничья!') break main()