The before code mixes board state and player turns inside one Game class, making it hard to extend. The after code splits responsibilities: Board manages the grid, Player holds player info, and Game controls turns and rules. This separation improves clarity and flexibility.
### Before: Monolithic Game class without separation
class Game:
def __init__(self):
self.board = [['' for _ in range(3)] for _ in range(3)]
self.players = ['X', 'O']
self.current_player = 0
def make_move(self, row, col):
if self.board[row][col] == '':
self.board[row][col] = self.players[self.current_player]
self.current_player = 1 - self.current_player
### After: Separate Board, Player, and Game classes
class Board:
def __init__(self, size=3):
self.size = size
self.grid = [['' for _ in range(size)] for _ in range(size)]
def place_mark(self, row, col, mark):
if self.grid[row][col] == '':
self.grid[row][col] = mark
return True
return False
class Player:
def __init__(self, name, mark):
self.name = name
self.mark = mark
class Game:
def __init__(self, player1, player2, board):
self.board = board
self.players = [player1, player2]
self.current_index = 0
def make_move(self, row, col):
player = self.players[self.current_index]
if self.board.place_mark(row, col, player.mark):
self.current_index = 1 - self.current_index
return True
return False