联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2021-04-04 02:27

ITP 115 – Programming in Python

2021-03-30 Page 1 of 8

Assignment 8 – Tic TacToe

Goals

? Create a two player Tic Tac Toe game

? Continue working with loops and lists

? Create functions (parameters, return values, using a main function)

Requirements

? In PyCharm, create a new project named a8_last_first where last is your last/family

name and first is your preferred first name.

? Create a new Python file named a#_last_first, where # is replaced with the number of

this assignment, last is your last/family name, and first is your preferred first name.

? At the top of the Python file, create comments in the following format (replace the

name and email with your actual information and write text for the description):

# Name, USC email

# ITP 115, Spring 2021

# Assignment 8

# Description:

# Describe what this program does.

? 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):

0 1 2

3 4 5

6 7 8

Which is represented in list form as:

["0", "1", "2", "3", "4", "5", "6", "7", "8"]

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 o’s.

? You may not use any global variables or constants in your code.

ITP 115 – Programming in Python

2021-03-30 Page 2 of 8

Functionsprovidedforyou

? 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). They are located in the TicTacToeHelper.py file provided.

o You will need to put the TicTacToeHelper.py in the same folder as the Python

file for this assignment. Thus add it to the project you created for this

assignment.

o 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.

o To use the solver function, the syntax is:

TicTacToeHelper.checkForWinner(board_list, move_counter)

§ Parameter 1: board_list is a list of strings representing the Tic Tac Toe

board

§ Parameter 2: move_counter is an integer representing the total number

of moves that have been made

§ Return value: a single character string:

? "x" if x won

? "o" if o won

? "n" if there is not winner

? "s" if the game reached a stalemate (full board with no winners)

o To use the print board function, the syntax is:

TicTacToeHelper.printUglyBoard(board_list)

§ Parameter: board_list is a list of strings representing the Tic Tac Toe

board

§ Return value: None

§ 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).

ITP 115 – Programming in Python

2021-03-30 Page 3 of 8

Functionsforyoutocreate

? Implement the following functions:

o isValidMove(boardList, spot)

§ Parameter 1: a list representing the board

§ Parameter 2: an integer corresponding to the index position that a user

would like to place their letter on

§ Return value: a boolean value

§ This function should look at the specified spot on the board. Return

True if the spot is open. Return False if the spot is taken or if the spot

parameter is not a valid index.

o updateBoard(boardList, spot, playerLetter)

§ Parameter 1: a list representing the board

§ Parameter 2: an integer corresponding to the index position that a user

would like to place their letter on

§ Parameter 3: a string representing the user’s letter ("x" or "o")

§ Return value: None

§ Takes the current board list and places the player’s letter in the specified

spot on the board.

o playGame()

§ Parameter: None

§ Return value: None

§ Create a list variable to represent the board. It should be a list of strings

with the numbers 0-8.

§ Create an int variable to keep track of the total number of moves that

have been made. It is the move counter for the game.

§ 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 o.

§ 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 by calling

the isValidMove() function and then update the board with the

move by calling the updateBoard() function 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.

§ Call the checkForWinner() function that is located in the

TicTacToeHelper module to control the while loop to determine when

the game is over. The game is over when the checkForWinner() function

does not return an "n".

ITP 115 – Programming in Python

2021-03-30 Page 4 of 8

§ Print out final board at the end of the game and a message about who

won or if there was a stalemate. Look at the sample output.

o main()

§ Parameter: None

§ Return value: None

§ Print the message: "Welcome to Tic Tac Toe!".

§ Call the playGame() function to play a round of Tic Tac Toe.

§ Use a while loop to allow the user to keep playing a new game of Tic Tac

Toe until they decide to stop playing.

SampleOutput1

Welcome to Tic Tac Toe!

0 1 2

3 4 5

6 7 8

Player x, pick a spot: -1

Invalid move, please try again.

Player x, pick a spot: 0

x 1 2

3 4 5

6 7 8

Player o, pick a spot: 0

Invalid move, please try again.

Player o, pick a spot: 4

x 1 2

3 o 5

6 7 8

Player x, pick a spot: 1

x x 2

3 o 5

6 7 8

Player o, pick a spot: 1

Invalid move, please try again.

Player o, pick a spot: 2

x x o

3 o 5

6 7 8

Player x, pick a spot: 3

ITP 115 – Programming in Python

2021-03-30 Page 5 of 8

x x o

x o 5

6 7 8

Player o, pick a spot: 6

x x o

x o 5

o 7 8

Game Over!

Player o is the winner!

Would you like to play another round? (y/n): n

Goodbye!

SampleOutput2

Welcome to Tic Tac Toe!

0 1 2

3 4 5

6 7 8

Player x, pick a spot: 0

x 1 2

3 4 5

6 7 8

Player o, pick a spot: 1

x o 2

3 4 5

6 7 8

Player x, pick a spot: 8

x o 2

3 4 5

6 7 x

Player o, pick a spot: 4

x o 2

3 o 5

6 7 x

Player x, pick a spot: 7

x o 2

ITP 115 – Programming in Python

2021-03-30 Page 6 of 8

3 o 5

6 x x

Player o, pick a spot: 6

x o 2

3 o 5

o x x

Player x, pick a spot: 2

x o x

3 o 5

o x x

Player o, pick a spot: 5

x o x

3 o o

o x x

Player x, pick a spot: 3

x o x

x o o

o x x

Game Over!

Stalemate reached!

Would you like to play another round? (y/n): y

0 1 2

3 4 5

6 7 8

Player x, pick a spot:

.

.

.

(continue the game until the user does not want to play another round)

ITP 115 – Programming in Python

2021-03-30 Page 7 of 8

ExtraCredit

? Print pretty board:

o Instead of printing the board out as shown above, define a new function called

printPrettyBoard with the same inputs (board list) and return value (none)

as the printUglyBoard function provided to you, but instead prints the Tic

Tac Toe board out to look like:

0 | 1 | 2

---------

3 | 4 | 5

---------

6 | 7 | 8

o This function should use loops to print the board out. Wherever you print the

board out in your original code, use this new function instead.

? Play against the computer:

o Allow the user the option to play against the computer or another user. When

playing against the computer, the computer’s moves can be random.

? Allow the user to choose which player begins:

o Give the user the option of starting the game as either player x or player o. This

will determine who makes the first move.

ITP 115 – Programming in Python

2021-03-30 Page 8 of 8

Deliverables andSubmissionInstructions

? Since you created a project in PyCharm named a8_last_first, you already have a folder

with your Python source code (a8_last_first.py) and the TicTacToeHelper.py file. Delete

any extraneous files and/or folders.

? Compress the folder (make a zip file). This cannot be done within PyCharm. Find the

folder on your computer and compress it.

a. Windows:

1. Using File Explorer, select the appropriate folder

2. Right click

3. Send to ->

4. Compressed (zipped) folder

b. Mac OSX:

1. Using Finder, select the appropriate folder

2. Right click

3. Compress “FileName”

? Upload the zip file to your Blackboard section:

1. On Blackboard, navigate to the appropriate assignment item.

2. Click on the specific item for this assignment.

3. Click on the Browse My Computer button and select your zip file.

4. Click the Submit button.

Grading

Item Points

Tic Tac Toe 35

Total* 35

* Points will be deducted for poor code style, lack of error checking, improper submission.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp