Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

def getDimension():
    row = int(input("Enter number of rows (between 4 and 10): "))
    while row < 4 or row > 10:
        row = int(input("Invalid input! Enter number of rows (between 4 and 10): "))
    column = int(input("Enter number of columns (between 4 and 10): "))
    while column < 4 or column > 10:
        column = int(input("Invalid input! Enter number of columns (between 4 and 10): "))
    return row, column


def printBoard(board):
    for row in board:
        print("|", end="")
        for cell in row:
            if cell == "":
                print(" ", end="")
            else:
                print(cell, end="")
            print("|", end="")
        print()
    print("-" * (len(board[0]) * 2 + 1))


def dropToken(board, col, token):
    row = len(board) - 1
    while row >= 0:
        if board[row][col] == "":
            board[row][col] = token
            return row
        row -= 1
    return -1


def isValidLocation(board, col):
    return board[0][col] == ""


def isTie(board):
    for row in board:
        for cell in row:
            if cell == "":
                return False
    return True


def checkWinner(board, token):
    # Check horizontal
    for row in board:
        for col in range(len(row) - 3):
            if row[col] == token and row[col+1] == token and row[col+2] == token and row[col+3] == token:
                return True
    # Check vertical
    for col in range(len(board[0])):
        for row in range(len(board) - 3):
            if board[row][col] == token and board[row+1][col] == token and board[row+2][col] == token and board[row+3][col] == token:
                return True
    # Check diagonal positive
    for col in range(len(board[0]) - 3):
        for row in range(len(board) - 3):
            if board[row][col] == token and board[row+1][col+1] == token and board[row+2][col+2] == token and board[row+3][col+3] == token:
                return True
    # Check diagonal negative
    for col in range(len(board[0]) - 3):
        for row in range(3, len(board)):
            if board[row][col] == token and board[row-1][col+1] == token and board[row-2][col+2] == token and board[row-3][col+3] == token:
                return True
    return False


# Main game loop
row, column = getDimension()
board = [["" for j in range(column)] for i in range(row)]
game_over = False
current_player = "X"

while not game_over:
    printBoard(board)
    if current_player == "X":
        col = int(input("Player X, enter column to drop your token (0-{}): ".format(column-1)))
        while not isValidLocation(board, col):
            col = int(input("Column is full or invalid! Player X, enter column to drop your token (0-{}): ".format(column-1)))
       
        row = dropToken(board, col, current_player)
        if row == -1:
            print("Error: invalid row")
            continue

        if checkWinner(board, current_player):
            printBoard(board)
            print("Congratulations! Player {} wins!".format(current_player))
            game_over = True
        elif isTie(board):
            printBoard(board)
            print("The game is a tie.")
            game_over = True

        current_player = "O"  # Switch player

    else:
        col = int(input("Player O, enter column to drop your token (0-{}): ".format(column-1)))
        while not isValidLocation(board, col):
            col = int(input("Column is full or invalid! Player O, enter column to drop your token (0-{}): ".format(column-1)))
    
        row = dropToken(board, col, current_player)
        if row == -1:
            print("Error: invalid row")
            continue
    
        if checkWinner(board, current_player):
            printBoard(board)
            print("Congratulations! Player {} wins!".format(current_player))
            game_over = True
        elif isTie(board):
            printBoard(board)
            print("The game is a tie.")
            game_over = True
        
        current_player = "X"  # Switch player

print("Thanks for playing!")

 

===================================================================================

Question:

can you change the else statement into the picture attached

getCompRandom Move (): The simplest strategy for the computer is by having it randomly
select a column and drop a piece into the top row of that column if it is empty. If the top row of
the column is not empty, the computer could randomly select another column to try again. This
strategy would not be very sophisticated, but it would provide a basic level of gameplay for the
computer. This function simply returns a random column number.
expand button
Transcribed Image Text:getCompRandom Move (): The simplest strategy for the computer is by having it randomly select a column and drop a piece into the top row of that column if it is empty. If the top row of the column is not empty, the computer could randomly select another column to try again. This strategy would not be very sophisticated, but it would provide a basic level of gameplay for the computer. This function simply returns a random column number.
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education