board = [[' ' for x in range(3)] for y in range(3)] # инициализация игрового поля
player = 'X' # начать с игрока X
def draw_board():
print("---------")
for row in board:
print("|", end='')
for cell in row:
print(cell, end='|')
print("")
def check_winner():
# check rows
if row[0] == row[1] == row[2] and row[0] != ' ':
return row[0]
# check columns
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != ' ':
return board[0][col]
# check diagonals
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != ' ':
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != ' ':
return board[0][2]
return None
while True:
draw_board()
print("Игрок", player, "ход")
row = int(input("Введите ряд (1-3): ")) - 1
col = int(input("Введите колону (1-3): ")) - 1
if board[row][col] != ' ':
print("Эта ячейка уже занята! Попробуйте еще раз.")
continue
board[row][col] = player
winner = check_winner()
if winner:
print("Игрок", winner, "побеждает!")
break
if all(all(cell != ' ' for cell in row) for row in board):
print("Это ничья!")
player = 'O' if player == 'X' else 'X' # переключать игроков
print("Конец игры!")
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
board = [[' ' for x in range(3)] for y in range(3)] # инициализация игрового поля
player = 'X' # начать с игрока X
def draw_board():
print("---------")
for row in board:
print("|", end='')
for cell in row:
print(cell, end='|')
print("")
print("---------")
def check_winner():
# check rows
for row in board:
if row[0] == row[1] == row[2] and row[0] != ' ':
return row[0]
# check columns
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] and board[0][col] != ' ':
return board[0][col]
# check diagonals
if board[0][0] == board[1][1] == board[2][2] and board[0][0] != ' ':
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] and board[0][2] != ' ':
return board[0][2]
return None
while True:
draw_board()
print("Игрок", player, "ход")
row = int(input("Введите ряд (1-3): ")) - 1
col = int(input("Введите колону (1-3): ")) - 1
if board[row][col] != ' ':
print("Эта ячейка уже занята! Попробуйте еще раз.")
continue
board[row][col] = player
winner = check_winner()
if winner:
print("Игрок", winner, "побеждает!")
break
if all(all(cell != ' ' for cell in row) for row in board):
print("Это ничья!")
break
player = 'O' if player == 'X' else 'X' # переключать игроков
print("Конец игры!")