Write an outline and logic for the following code: EMPTY = '-' BLACK = ‘X’ WHITE = ‘O’ BOARD_SIZE = 8 DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1)]   def initialize_board():     board = [[EMPTY for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]     board[3][3] = WHITE     board[3][4] = BLACK     board[4][3] = BLACK     board[4][4] = WHITE     return board   def print_board(board):     print("   1  2  3  4  5  6  7  8")     for i in range(BOARD_SIZE):         print(f"{i + 1} ", end="")         for j in range(BOARD_SIZE):             print(f" {board[i][j]}", end="")         print()   def is_valid_move(board, row, col, player):     if board[row][col] != EMPTY:         return False       for direction in DIRECTIONS:         dr, dc = direction         r, c = row + dr, col + dc         while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE:             if board[r][c] == EMPTY:                 break             if board[r][c] == player:                 return True             r += dr             c += dc     return False   def make_move(board, row, col, player):     if not is_valid_move(board, row, col, player):         return False       board[row][col] = player     for direction in DIRECTIONS:         dr, dc = direction         r, c = row + dr, col + dc         to_flip = []         while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE:             if board[r][c] == EMPTY:                 break             if board[r][c] == player:                 for flip_row, flip_col in to_flip:                     board[flip_row][flip_col] = player                 break             to_flip.append((r, c))             r += dr             c += dc       return True   def is_game_over(board):     for i in range(BOARD_SIZE):         for j in range(BOARD_SIZE):             if board[i][j] == EMPTY:                 return False     return True   def count_pieces(board):     black_count = 0     white_count = 0     for i in range(BOARD_SIZE):         for j in range(BOARD_SIZE):             if board[i][j] == BLACK:                 black_count += 1             elif board[i][j] == WHITE:                 white_count += 1     return black_count, white_count   def determine_winner(black_count, white_count):     if black_count > white_count:         return BLACK     elif white_count > black_count:         return WHITE     else:         return EMPTY   def play_game():     board = initialize_board()     player = BLACK       while True:         print_board(board)         print("Player", player, "'s turn.")         print("Enter your move (row col): ")         move = input().split()         if len(move) != 2:             print("Invalid move. Please enter row and col separated by space.")             continue         row, col = int(move[0]) - 1, int(move[1]) - 1         if not is_valid_move(board, row, col, player):             print("Invalid move. Please choose a valid move.")             continue         make_move(board, row, col, player)         if is_game_over(board):             break         player = WHITE if player == BLACK else BLACK       black_count, white_count = count_pieces(board)     winner = determine_winner(black_count, white_count)       print_board(board)     print("Game Over!")     print("Black: ", black_count)     print("White: ", white_count)     if winner == BLACK:         print("Black wins!")     elif winner == WHITE:         print("White wins!")     else:         print("It's a draw!")   if __name__ == "__main__":     play_game()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write an outline and logic for the following code:

EMPTY = '-'

BLACK = ‘X’

WHITE = ‘O’

BOARD_SIZE = 8

DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1)]

 

def initialize_board():

    board = [[EMPTY for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]

    board[3][3] = WHITE

    board[3][4] = BLACK

    board[4][3] = BLACK

    board[4][4] = WHITE

    return board

 

def print_board(board):

    print("   1  2  3  4  5  6  7  8")

    for i in range(BOARD_SIZE):

        print(f"{i + 1} ", end="")

        for j in range(BOARD_SIZE):

            print(f" {board[i][j]}", end="")

        print()

 

def is_valid_move(board, row, col, player):

    if board[row][col] != EMPTY:

        return False

 

    for direction in DIRECTIONS:

        dr, dc = direction

        r, c = row + dr, col + dc

        while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE:

            if board[r][c] == EMPTY:

                break

            if board[r][c] == player:

                return True

            r += dr

            c += dc

    return False

 

def make_move(board, row, col, player):

    if not is_valid_move(board, row, col, player):

        return False

 

    board[row][col] = player

    for direction in DIRECTIONS:

        dr, dc = direction

        r, c = row + dr, col + dc

        to_flip = []

        while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE:

            if board[r][c] == EMPTY:

                break

            if board[r][c] == player:

                for flip_row, flip_col in to_flip:

                    board[flip_row][flip_col] = player

                break

            to_flip.append((r, c))

            r += dr

            c += dc

 

    return True

 

def is_game_over(board):

    for i in range(BOARD_SIZE):

        for j in range(BOARD_SIZE):

            if board[i][j] == EMPTY:

                return False

    return True

 

def count_pieces(board):

    black_count = 0

    white_count = 0

    for i in range(BOARD_SIZE):

        for j in range(BOARD_SIZE):

            if board[i][j] == BLACK:

                black_count += 1

            elif board[i][j] == WHITE:

                white_count += 1

    return black_count, white_count

 

def determine_winner(black_count, white_count):

    if black_count > white_count:

        return BLACK

    elif white_count > black_count:

        return WHITE

    else:

        return EMPTY

 

def play_game():

    board = initialize_board()

    player = BLACK

 

    while True:

        print_board(board)

        print("Player", player, "'s turn.")

        print("Enter your move (row col): ")

        move = input().split()

        if len(move) != 2:

            print("Invalid move. Please enter row and col separated by space.")

            continue

        row, col = int(move[0]) - 1, int(move[1]) - 1

        if not is_valid_move(board, row, col, player):

            print("Invalid move. Please choose a valid move.")

            continue

        make_move(board, row, col, player)

        if is_game_over(board):

            break

        player = WHITE if player == BLACK else BLACK

 

    black_count, white_count = count_pieces(board)

    winner = determine_winner(black_count, white_count)

 

    print_board(board)

    print("Game Over!")

    print("Black: ", black_count)

    print("White: ", white_count)

    if winner == BLACK:

        print("Black wins!")

    elif winner == WHITE:

        print("White wins!")

    else:

        print("It's a draw!")

 

if __name__ == "__main__":

    play_game()

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Hiring Problem
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education