Advertisement
Hasli4

Untitled

Apr 18th, 2025
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. class Cell:
  2.     # Клетка, у которой есть:
  3.     #  - номер (1–9)
  4.     #  - статус (либо сам номер, либо 'X' / 'O')
  5.     def __init__(self, number, status):
  6.         self.number = number
  7.         self.status = status
  8.  
  9.  
  10. class Board:
  11.     # Поле из 9 клеток
  12.     def __init__(self):
  13.         self.cells = []
  14.         for i_cell in range(1, 10):
  15.             # Стартово в каждой клетке хранится её номер
  16.             cell = Cell(i_cell, i_cell)
  17.             self.cells.append(cell)
  18.  
  19.     def print_board(self):
  20.         # Печатаем 3×3 доску
  21.         for cell in self.cells:
  22.             # Если клетка не крайняя в строке, выводим с " |"
  23.             if cell.number % 3 != 0:
  24.                 print(f' {cell.status} |', end='')
  25.             else:
  26.                 # Крайний столбец, печатаем и перехід на новую строку
  27.                 print(f' {cell.status} ')
  28.                 print('------------')
  29.  
  30.     def end_of_game(self, view):
  31.         # Собираем все 8 возможных линий-побед
  32.         lines = [
  33.             [self.cells[0].status, self.cells[1].status, self.cells[2].status],
  34.             [self.cells[3].status, self.cells[4].status, self.cells[5].status],
  35.             [self.cells[6].status, self.cells[7].status, self.cells[8].status],
  36.             [self.cells[0].status, self.cells[3].status, self.cells[6].status],
  37.             [self.cells[1].status, self.cells[4].status, self.cells[7].status],
  38.             [self.cells[2].status, self.cells[5].status, self.cells[8].status],
  39.             [self.cells[0].status, self.cells[4].status, self.cells[8].status],
  40.             [self.cells[2].status, self.cells[4].status, self.cells[6].status],
  41.         ]
  42.         # 1 — победа текущего игрока
  43.         if [view] * 3 in lines:
  44.             return 1
  45.         # 2 — ничья, если ни одной клетке не осталось числа
  46.         elif all(isinstance(cell.status, str) for cell in self.cells):
  47.             return 2
  48.         # 0 — игра продолжается
  49.         else:
  50.             return 0
  51.  
  52.  
  53. class Player:
  54.     # У игрока есть имя и знак ('X' или 'O')
  55.     def __init__(self, name, view):
  56.         self.name = name
  57.         self.view = view
  58.  
  59.     def go(self, board, number):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement