Question

In: Computer Science

Design and implement a Python program which will allow two players to play the game of...

Design and implement a Python program which will allow two players to play the game of Tic-Tac-Toe in a 4x4 grid!

X | O | X | O

--------------

O | O | X | O

--------------

X | X | O | X

--------------

X | X | O | X

The rules for this game is the same as the classic, 3x3, game –

  • Each cell can hold one of the following three strings: "X", "O", or " " (Blank)
  • 4 X’s (or 4 O’s) in a row or column; or across a diagonal wins
  • For additional information, check this site out
    • https://www.thesprucecrafts.com/tic-tac-toe-game-rules-412170.

Requirements

Part A: 20 Points – At a minimum, implement the following three functions –

  • create_board() which will allow a user to build an “empty” board (so that we can start a game
  • display_board() which will show the current board
  • check_winner() that determine if there is a winner for the game
  • If "X" appears in a winning Tic-Tac-Toe pattern, the function should return
  • the string "X"
  • If "O" appears in a winning Tic-Tac-Toe pattern, the function should return the string "O"
  • If no winning pattern exists, the function should return the string " "
  • Break your logic into functions, don’t implement the entire game in a single function, like “main”

Part B: 20 Points – Implement logic so that two players can play the game for as many times as they wish to. In addition -

  • Ask users for their names
  • Display the numbers of wins vs. losses for each player once they are done with playing

Part C: 5 Points – Enhance your project to include an option for a “player” to play against a “computer” instead

Assumptions

  • You may include additional functions, however, you MUST implement the specified functions – they are required.

Concepts Utilized in Project

  • Previously covered topics
  • Lists & Functions

Solutions

Expert Solution

Given data,

Python solution with output screenshot:

Code :

def create_board():
    board = [' '] * 16
    return board

def display_board(board):
    for i in range(16):
        if((i+1)%4==0):
            print(board[i])
            if(i<15):
                print("--------------")
        else:
            print(board[i], end=" | ")
            

def check_winner(board):
    if((board[0] == 'X' and board[1] == 'X' and board[2] == 'X' and board[3] == 'X') or
        (board[4] == 'X' and board[5] == 'X' and board[6] == 'X' and board[7] == 'X') or
        (board[8] == 'X' and board[9] == 'X' and board[10] == 'X' and board[11] == 'X') or
        (board[12] == 'X' and board[13] == 'X' and board[14] == 'X' and board[15] == 'X') or
        (board[0] == 'X' and board[4] == 'X' and board[8] == 'X' and board[12] == 'X') or
        (board[1] == 'X' and board[5] == 'X' and board[9] == 'X' and board[13] == 'X') or
        (board[2] == 'X' and board[6] == 'X' and board[10] == 'X' and board[14] == 'X') or
        (board[3] == 'X' and board[7] == 'X' and board[11] == 'X' and board[15] == 'X') or
        (board[0] == 'X' and board[5] == 'X' and board[10] == 'X' and board[15] == 'X') or
        (board[3] == 'X' and board[6] == 'X' and board[9] == 'X' and board[12] == 'X')):
            return 'X'
    elif((board[0] == 'O' and board[1] == 'O' and board[2] == 'O' and board[3] == 'O') or
        (board[4] == 'O' and board[5] == 'O' and board[6] == 'O' and board[7] == 'O') or
        (board[8] == 'O' and board[9] == 'O' and board[10] == 'O' and board[11] == 'O') or
        (board[12] == 'O' and board[13] == 'O' and board[14] == 'O' and board[15] == 'O') or
        (board[0] == 'O' and board[4] == 'O' and board[8] == 'O' and board[12] == 'O') or
        (board[1] == 'O' and board[5] == 'O' and board[9] == 'O' and board[13] == 'O') or
        (board[2] == 'O' and board[6] == 'O' and board[10] == 'O' and board[14] == 'O') or
        (board[3] == 'O' and board[7] == 'O' and board[11] == 'O' and board[15] == 'O') or
        (board[0] == 'O' and board[5] == 'O' and board[10] == 'O' and board[15] == 'O') or
        (board[3] == 'O' and board[6] == 'O' and board[9] == 'O' and board[12] == 'O')):
            return 'O'
    else:
        return ''


if __name__ == "__main__":
    player1 = 'X'
    player2 = 'O'
    print("------ Welcome to Tic-Tac-Toe Game -------\n")
    print("Player 1 - X")
    print("Player 2 - O\n")
    player1_name = input("Enter Player 1 Name: ")
    player2_name = input("Enter Player 2 Name: ")
    print()
    total_games = 0
    player1_win = 0
    player1_losses = 0
    player2_win = 0
    player2_losses = 0

    while(True):
        board = create_board()
        turn = 1
        while(True):
            display_board(board)
            if(turn == 1):
                print("\nPlayer 1 turn, Choose position from (1-16)")
                choice = int(input("Choice: "))
                board[choice-1] = 'X'
                turn = 2
            else:
                print("\nPlayer 2 turn, Choose position from (1-16)")
                choice = int(input("Choice: "))
                board[choice-1] = 'O'
                turn = 1
            
            winner = check_winner(board)
            if(winner == 'X'):
                display_board(board)
                print("----------------------")
                print("\nGame Over")
                print("** Player 1 Won **")
                player1_win += 1
                player2_losses += 1
                total_games += 1
                break

            elif(winner == 'O'):
                display_board(board)
                print("\nGame Over")
                print("** Player 2 Won **")
                player2_win += 1
                player1_losses += 1
                total_games += 1
                break

            else:
                if(' ' in board):
                    continue
                display_board(board)
                print("\nGame Over")
                print("** Draw **")
                total_games += 1
                break
        
        print("\nDo you want to play again? (yes/no): ")
        again = input()
        if(again == 'yes' or again  == "Yes" or again == "YES"):
            continue
        else:
            break

    print("\nTotal Games Played:", total_games)
    print("------------------------")
    print(player1_name, "(Player 1) wins:", player1_win)
    print(player1_name, "(Player 1) losses:", player1_losses)
    print(player1_name, "(Player 2) wins:", player2_win)
    print(player1_name, "(Player 2) looses:", player2_losses)

Output :

  • I have not included whole screenshot because it is too long

    Note : for better formatting please refer to the code formatting below.

<<<<Please Give Me Like>>>>


Related Solutions

PYTHON: Write the code to play a card game called Battle. Two players each have a...
PYTHON: Write the code to play a card game called Battle. Two players each have a card deck consisting of the following cards: two, three, four, … jack, queen, king, ace, in increasing order. One card deck could be represented by a list such as: cardsPlayer1 = ["two", "three", "four"..."jack", "queen", "king", "ace"] Both players have a card randomly selected. When a card is selected, remove it from the player’s deck. The player that plays the higher of the two...
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
Write a program to allow a user to play the game Hangman. DO NOT USE AN...
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess. Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the...
I need to design and implement a JAVASCRIPT program that will allow us to determine the...
I need to design and implement a JAVASCRIPT program that will allow us to determine the length of time needed to pay off a credit card balance, as well as the total interest paid. The program must implement the following functions: displayWelcome This function should display the welcome message to the user explaining what the program does. calculateMinimumPayment This function calculates the minimum payment. It should take balance and minimum payment rate as arguments and return the minimum payment. So...
Two players A and B play a game of dice . They roll a pair of...
Two players A and B play a game of dice . They roll a pair of dice alternately . The player who rolls 7 first wins . If A starts then find the probability of B winning the game ?
Top-down design: Design a program called TeamGame to simulate a simple game of drafting players to...
Top-down design: Design a program called TeamGame to simulate a simple game of drafting players to teams. Rules of the game: • There are two teams: Team A and Team B. • There are 50 players, with numbers from 1 to 50. • Each team will get 10 of the players. The program should randomly select 10 players for each team. To accomplish this, you may select each player randomly, or you may shuffle the list of players before making...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
Design and implement a program in python that takes a list of items along with quantities...
Design and implement a program in python that takes a list of items along with quantities or weights. The program should include at least two function definition that is called within the main part of your program. Each item has a price associated by quantity or weight. The user enters the item along with the quantity or weight and the program prints out a table for each item along with the quantity/weight and total price. Your program should be able...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT