Question

Objective: To write a program to allow a game of Tic Tac Toe to be played,...

Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over

Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner.

Details:

  • The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game (an empty board). Each time a player makes a move, an 'X' or 'O' is put in the proper location in the array.
  • Your "clear" function should initialize the board to store spaces
  • Your "display" function should display the board, with gridlines between each position (using the characters '-' and '|', which are not stored in the array)
  • Your "takeTurn" function should ask the user to enter amove, and check for errors in input. Specifically, you should check for the following 2 kinds of errors:
    1. Row or Column is out of range (less than 0 or greater than 2)
    2. Specified position is already occupied (a player can only move to an empty spot)
    If either of the above errors are found, the same player should be asked to re-enter their move until they get it right.
  • Your "winner" function should examine the board and return one of the following characters:
    • ' ' (a space) meaning the game is not yet over
    • 'X' meaning that player X has won
    • 'O' meaning that player O has won
    • '?' meaning that the game is over because the board is full, but no one won.
  • I recommend you write some helper functions to help with all the various pieces of this program. Remember you should never copy and paste code if you can avoid it. Write a function to perform a common, generalizable task, and call that function every time you need it.
  • As always, don't use any global variables.
  • Following is the beginning of the TicTacToe program and an example of how your program should behave. You should copy this TicTacToe.cpp file exactly and use it as a starting point and to test your code once you've finished it.

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

#    Starting point for Extra Credit
#    Tic Tac Toe game program
    
def clear():
  return [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]

# ///////  display  ////////
# // Display the current status of the board on the
# // screen, using hyphens (-) for horizontal lines
# // and pipes (|) for vertical lines.
def display(board):
  return True

# ///////  takeTurn  ////////
# // Allow the nextPlayer to take a turn.
# // Send output to screen saying whose turn
# // it is and specifying the format for input.
# // Read user's input and verify that it is a
# // valid move.  If it's invalid, make them
# // re-enter it.  When a valid move is entered,
# // put it on the board.
def takeTurn(board, nextPlayer):
  return nextPlayer

# ///////  winner  /////////
# // Examines the board and returns one of the following:
# // ' ' (a space) meaning the game is not yet over
# // 'X' meaning that player X has won
# // 'O' meaning that player O has won
# // '?' meaning that the game is over because the board
# //     is full, but no one won.
def winner(board):
  return winner

# ///////  main  ////////
# // No changes needed in this function.
# // It declares the variables, initializes the game,
# // and plays until someone wins or the game becomes unwinnable.
def main():
  board = clear()
  nextPlayer = 'X'
  winningPlayer = ' '
  
  display(board)
  
  while winningPlayer == ' ':
    nextPlayer = takeTurn(board, nextPlayer)
    display(board)
    winningPlayer = winner(board)
    if winningPlayer == '?':
      print("Nobody won. Please play again.")
    else:
      print("Congratulations, ", winningPlayer, " YOU WON!")
  return True

main()
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#    Starting point for Extra Credit
#    Tic Tac Toe game program

def clear():
    return [[' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]


# ///////  display  ////////
# // Display the current status of the board on the
# // screen, using hyphens (-) for horizontal lines
# // and pipes (|) for vertical lines.
def display(board):
    print('-------------')
    print('| {0} | {1} | {2} |'.format(board[0][0],board[0][1],board[0][2]))
    print('-------------')
    print('| {0} | {1} | {2} |'.format(board[1][0], board[1][1], board[1][ 2]))
    print('-------------')
    print('| {0} | {1} | {2} |'.format(board[2][0], board[2][1], board[2][ 2]))
    print('-------------')

# ///////  takeTurn  ////////
# // Allow the nextPlayer to take a turn.
# // Send output to screen saying whose turn
# // it is and specifying the format for input.
# // Read user's input and verify that it is a
# // valid move.  If it's invalid, make them
# // re-enter it.  When a valid move is entered,
# // put it on the board.
def takeTurn(board, nextPlayer):

    while True:
        move=input('Enter row and col separated by space: ').split()
        try:
            x,y=int(move[0])-1,int(move[1])-1
            if 0<=x and x<=2 and 0<=y and y<=2:
                if board[x][y]==' ':
                    board[x][y]=nextPlayer
                    break
                else:
                    print('The place is already taken')
            else:
                print('That was an invalid move. Please enter values between 1 and 3 only')
        except:
            print('That was an invalid move. Please reenter.')
    if nextPlayer=='X':        return 'O'
    else:        return 'X'



# ///////  winner  /////////
# // Examines the board and returns one of the following:
# // ' ' (a space) meaning the game is not yet over
# // 'X' meaning that player X has won
# // 'O' meaning that player O has won
# // '?' meaning that the game is over because the board
# //     is full, but no one won.
def winner(board):

    if board[0][0]==board[1][0] and board[1][0]==board[2][0] and board[0][0] is not ' ':
        return board[0][0]
    elif board[0][1]==board[1][1] and board[1][1]==board[2][1] and board[0][1] is not ' ':
        return board[0][1]
    elif board[0][2]==board[1][2] and board[1][2]==board[2][2] and board[0][2] is not ' ':
        return board[0][2]
    elif board[0][0]==board[0][1] and board[0][1]==board[0][2] and board[0][0] is not ' ':
        return board[0][0]
    elif board[1][0]==board[1][1] and board[1][1]==board[1][2] and board[1][0] is not ' ':
        return board[1][0]
    elif board[2][0]==board[2][1] and board[2][1]==board[2][2]and board[2][0] is not ' ':
        return board[2][0]
    elif board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[0][0] is not ' ':
        return board[0][0]
    elif board[0][2]==board[1][1] and board[1][1]==board[2][0] and board[0][2] is not ' ':
        return board[0][2]
    for row in board:
        for cell in row:
            if cell==' ':return '?'

    return '?'



# ///////  main  ////////
# // No changes needed in this function.
# // It declares the variables, initializes the game,
# // and plays until someone wins or the game becomes unwinnable.
def main():
    board = clear()
    nextPlayer = 'X'
    winningPlayer = ' '

    display(board)

    while winningPlayer == ' ':
        nextPlayer = takeTurn(board, nextPlayer)
        display(board)
        winningPlayer = winner(board)
        if winningPlayer=='X' or winningPlayer=='O':
            print("Congratulations, ", winningPlayer, " YOU WON!")
        else:
            winningPlayer=' '
    return True


main()

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

thanks !

Add a comment
Know the answer?
Add Answer to:
Objective: To write a program to allow a game of Tic Tac Toe to be played,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe....

    (Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...

  • You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people....

    You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use 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: Write . 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...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an...

    (Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • Write a c program that will allow two users to play a tic-tac-toe game. You should...

    Write a c 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. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT