View the step-by-step solution to:

Question

I need help with a Tic Tac Toe game using functions. I am confused on how to use functions.

/>


Screen Shot 2019-11-02 at 2.38.32 PM.pngScreen Shot 2019-11-02 at 2.38.51 PM.pngScreen Shot 2019-11-02 at 2.39.08 PM.pngScreen Shot 2019-11-02 at 2.39.28 PM.pngScreen Shot 2019-11-02 at 2.39.40 PM.png

Screen Shot 2019-11-02 at 2.38.32 PM.png

Requirements You will create a program that uses functions to simulate a two player game of Tic Tac
Toe. The program will allow the two players to place an "x" or an "o" somewhere on
the board and determine when someone wins or when a stalemate is reached. In order to keep track of the tic tac toe board, you will use a list of 9 single character
strings, each representing a spot on the board. The board begins with numbers (0—8): 9 1 2
3 4 5
6 7 8 Which is represented in list form as: [Ila II’ "1", "2 II’ "3 II, II4 II, "5 II’ "6 II’ "7 II’ "8 II]
Where the numbers on the board and in the list correspond with same index positions
in the list. As the game advances, the numbers will be replaced with X's and 0’s. You may not use any global variables or constants in your code. Functions provided for you To help you out, we have provided two functions for you to use: a tic tac toe solver
(which tells the current state of the game) and a print board function (which displays
the tic tac toe board) 0 In order to use these functions, you will need to place the given file,
TicTacToeSolver.py, in the same directory as your assignment 5 file. To use a
function in your code, place the line “import TicTacToeHelper" at the top
of your code. Importing TicTacToeHelper gives you access to functions that
were defined in a different file. 0 To use the solver function, the syntax is:
TicTacToeHelper.checkForwinner(boardList, move_counter) . Inputs: a list representing the tic tac toe board, and an integer
representing the total number of moves that have been made
I Return value: a single character string:

Screen Shot 2019-11-02 at 2.38.51 PM.png

o ‘x' if x won
0 ‘o’ if 0 won
a 'n’ if there is not winner
a '5’ if the game reached a stalemate (full board with no winners)
0 To use the print board function, the syntax is:
TicTacToeHelper.printUglyBaard(boardList)
I Input: a list representing the board
I Return value: none
I Takes the list representing the board and prints it out for the user to see
(the board does not have to exactly match the sample outputs, but it
should show the board as three rows) Functions for you to create
0 You will use the given function to create the game in addition to implementing the
following functions:
0 isValidMove(baard List, spot)
I Input: a list representing the board, an integer corresponding to the
index position that a user would like to placetheir letter on
I Return value: a boolean value
I This function should look at the specified spot on the board and return
True if the spot is open or False if the spot is taken or out of range
0 updateBoard(baard List, spot, playerLetter)
I Input: a list representing the board, an integer corresponding to the
index position that a user would like to place their letter on, a string
representing the user’s letter ("x” or "0”)
I Return value: none
I Takes the current board list and places the player's letter in the specified
spot on the board.

Screen Shot 2019-11-02 at 2.39.08 PM.png

o p1ayGame() I Input: none I Return value: none I Initialize the list representing the board to a list of strings with the
numbers 0-8. I Keep track ofthe total number of moves that have been made using a
counter. I Using a while loop, allow each player to take a turn until the game ends.
Note: only one player should take a turn per loop iteration, meaning
you should use a variable that alternates between player x and player 0. ITP 115 — Programming in Python p. 3 of? I At each turn, ask the current player what position they would like to put
their letter on the board. Check that the selected spot is valid using
isValidMove and then update the board with the move using
updateBoard if the move is valid. If the spot is not valid, keep asking
the user for a position on the board until their move is valid. I Use the checkFor'lnlinner' method provided to you to control the while
loop to determine when the game is over. Hint: the game is over only
when either x or 0 wins, or if there is a stalemate. I Print out final board at the end of the game and a message about who
won or if there was a stalemate. 0 main () I Input: none I Return value: none I Use a while loop to allow the user to keep playing a new game oftic tac
toe until they decide to stop playing again.

Screen Shot 2019-11-02 at 2.39.28 PM.png

Sample Output 1
Welcome to Tic Tac Toe!
w O
1
4
UI N
6
7
8
Player x, pick a spot: -1
Invalid move, please try again.
Player x, pick a spot: 0
X
1
3
4
UI N
7
8
Player o, pick a spot: 0
Invalid move, please try again.
Player o, pick a spot: 4
X
3
O
UN
6
7
8
Player x, pick a spot: 1
X
X
3
0
UI N
6
7
8
Player o, pick a spot: 1
Invalid move, please try again.
Player o, pick a spot: 2
X
X
3
UI O
6
7
8
Player x, pick a spot: 3

Screen Shot 2019-11-02 at 2.39.40 PM.png

X
x o
X o
6
Player o, pick a spot: 6
ITP 115 - Programming in Python
p. 5 of 7
X X
X
0 5
O
7
8
Game Over!
Player o is the winner!
Would you like to play another round? (y/n) : n
Goodbye!

Top Answer

The code is provided below... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question