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

PROGRAMING in C RPSLS: The program that we must implement must allow two players to play...
PROGRAMING in C RPSLS: The program that we must implement must allow two players to play in the same console (there will be only one program) The program will initialize the information necessary to be able to store the name of two players and their respective markers throughout the game, to present a result message at the end of the game. 1. When the program is ready for the game to start, it will ask PLAYER1 enter your name. 2....
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...
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 ?
Using Python, Assignment Write a program that plays a game of craps. The program should allow...
Using Python, Assignment Write a program that plays a game of craps. The program should allow the player to make a wager before each “turn”. Before each turn, allow the user to either place a bet or exit the game. After each turn display the player’s current balance. Specifics Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program. Implement this any way you wish, but make it...
PYTHON In this lab we will design a menu-based program. The program will allow users to...
PYTHON In this lab we will design a menu-based program. The program will allow users to decide if they want to convert a binary number to base 10 (decimal) or convert a decimal number to base 2 (binary). It should have four functions menu(), reverse(), base2(), and base10(). Each will be outlined in detail below. A rubric will be included at the bottom of this document. Menu() The goal of menu() is to be the function that orchestrates the flow...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write...
FileWrite a program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assume they know how to play the game). You should code the program to do the five items below only. You do not have to write the entire program, just the five items below. • Use a 2D array(3 rows, 3 columns) of datatype char. Initialize the 2D array with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT