class Board:

    def __init__(self):
        # board is a list of cells that are represented 
        # by strings (" ", "O", and "X")
        # initially it is made of empty cells represented 
        # by " " strings
        self.cells = {'A1': ' ', 'A2': ' ', 'A3': ' ',
                      'B1': ' ', 'B2': ' ', 'B3': ' ',
                      'C1': ' ', 'C2': ' ', 'C3': ' '}
        self.sign = " "
        self.size = 3
        self.board = [self.sign] * self.size**2
        # the winner's sign O or X
        self.winner = ""
    
    def keys(self):
        return self.cells.keys()

    def get_size(self): 
        return self.size

    def get_winner(self):
        return self.winner

    def set(self, cell, sign):
        # Convert cell to index values from 0 to 8
        row, col = int(cell[1]) - 1, ord(cell[0]) - ord('A')
        index = row * self.size + col

        # Mark the cell on the board with the sign X or O
        if self.board[index] == self.sign:
            self.board[index] = sign
        else:
            print(" B Cell is already occupied. Choose another cell.")
    
    def copy_board(self):
        # Create a new instance of Board and copy the state
        new_board = Board()
        new_board.cells = self.cells.copy()
        new_board.sign = self.sign
        new_board.size = self.size
        new_board.board = self.board.copy()
        new_board.winner = self.winner
        return new_board
            

            
    def set1(self, cell, sign):
        # Convert cell to index values from 0 to 8
        row, col = int(cell[1]) - 1, ord(cell[0]) - ord('A')
        index = row * self.size + col

        # Check if the cell is empty before marking it
        if self.board[index] == ' ':
            self.board[index] = sign
        else:
            pass  # Handle the case where the cell is already occupied
        
    def isempty(self, cell):
        # Convert cell to index values from 0 to 8
        row, col = int(cell[1]) - 1, ord(cell[0]) - ord('A')
        index = row * self.size + col

        # Return True if the cell is empty (not marked with X or O)
        return self.board[index] == self.sign
    


    def isdone(self):
        # Check rows, columns, and diagonals for a winner
        for i in range(self.size):
            # Check rows
            if self.board[i * self.size] == self.board[i * self.size + 1] == self.board[i * self.size + 2] != self.sign:
                self.winner = self.board[i * self.size]
                return True
            # Check columns
            if self.board[i] == self.board[i + self.size] == self.board[i + 2 * self.size] != self.sign:
                self.winner = self.board[i]
                return True

        # Check diagonals
        if self.board[0] == self.board[4] == self.board[8] != self.sign:
            self.winner = self.board[0]
            return True
        if self.board[2] == self.board[4] == self.board[6] != self.sign:
            self.winner = self.board[2]
            return True

        # Check for a tie (all cells filled)
        if all(cell != self.sign for cell in self.board):
            self.winner = "Tie"
            return True

        return False

    def show(self):
        # Draw the board
        print("   A   B   C  ")
        print(" +---+---+---+")
        for i in range(self.size):
            row = " | ".join(self.board[i * self.size : (i + 1) * self.size])
            print(f"{i + 1}| {row} |")
            print(" +---+---+---+")
            
