



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
1) In your Python 3 IDLE click File and create a New File. ... In order to play Tic Tac Toe, we are going to need to print out the game board.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Today we’re going to be making a text based version of the game Tic Tac Toe, also known as "Naughts and Crosses".
To get started first we need to create a file where we are going to write the code for our game.
Now when you want to run your code, just click Run (or press F5 on the keyboard)!
Great! Now we’re ready to code!
In order to play Tic Tac Toe, we are going to need to print out the game board.
Each square has a number in it so we can tell the computer which square we want!
To do this, we'll need to use print. Want a hint on how to print? Look at your cheat sheet!
In your file tictactoe.py write some code that will print out the grid above.
Hint: You might need to use multiple print statements Hint: There are 13 dashes in the first line, Hint: What happens when you multiply a character by a number?
Part 2: Storing the players’ names
We need to remember the name of the two players. Let’s store each name in a variable.
Use input to ask each player for their name and store them in variables. (add it above your printer).
Before you print the grid, ask these 2 questions using input to get the names of the players: ● "Who will be naughts? " ● "Who will be crosses? "
Name your variables player0 for naughts and playerX for crosses.
Now you have the names, you'll need to show them!
Print a message welcoming both of the players by their name. Here's an example of how your code should run: Who will be naughts? Julia Who will be crosses? Maddie Welcome Julia and Maddie!
Remember how we learnt to add strings together? We’ll need to do that here to add all the names and text together!
Part 3: Storing the game
We’re going to store our game board in lists!
When we start the game we have no naughts or crosses. Remember we started out by printing a grid with the numbers 1-9 in it.
Let’s store these numbers in our list to start with. Later they will be replaced with X’s and O’s.
Part 5: The first turn of the game!
Now we are getting to the exciting part! We get to start making the game playing part! To put a piece down, we need to know two things: ● Whether it is a O or an X ● Where the O or X goes
We need to find out what symbol to use when a player takes a square. When you play naughts and crosses you will be using ‘X’ and ‘O’, but really, you can put in anything.
The next thing we need to do is find out which square the player wants to put their symbol in. We can do this using input.
We now know which square the player wants to take. It’s stored in input_square; the only problem is it’s not a number, it’s a string!!
We can’t do maths or counting using a symbol (even though it is a number symbol)! We need to convert it to a number first.
Transform input_square to a number and store it in a new variable input_square_int Hint: Do you remember how we used int() to turn a string into a number?
Before we can update grid with the symbol to play we need to work out which item in the list we want to update. This is a bit tricky because we need to start counting from 0.
------------- Count from 0! | 1 | 2 | 3 | 0 1 2 3 4 5 6 7 8
We can see that the indexes don’t quite line up with the numbers in our grid because we have to start counting from 0 when using a list.
When we want to put a X in square 1, that is actually item 0 in the list. If we want to put a X in square 9 that is actually item 8 in the list.
Putting an X in square 9: ------------- Count from 0! | 1 | 2 | 3 | 0 1 2 3 4 5 6 7 8
For the example above, input_square_int is 9 but we want grid_index to be 8. We just need to write some code to do that for us. (P.S. "Index" is the name we give to a position in a list!)
We need to find the index that corresponds to the value in input_square_int and store it in grid_index. So if the user wants to edit square 5, grid_index should be 4.
Which of these options do you think will work? a. grid_index = input_square_int - 1 b. grid_index = input_square_int - 2 c. grid_index = input_square_int + 1 d. grid_index = input_square_int + 2
Add it to your code after you get input_square_int
Now we know which list item we need to update! Let’s get updating!
Goal: Update the the chosen index of grid to the chosen symbol
Part 6: Now repeat!
Now we have finished one turn of the game. The rest of the game is basically just doing the same thing over and over again!
But writing out all of that code again would be boring and computer programmers are known for being lazy! So instead we are going to use a while loop to make our code run for more than one turn!
Goal: Use a while loop to make the game continue for 9 turns.
Above the code you wrote for Part 5 , set up a variable to keep track of how many turns it has been and call it turns. At the start of the game there has been 0 turns.
Inside the loop you just created, at the end of the turn, update the number of turns it’s been. Hint use maths!
When you write a program you can rig it however you want! To make it unriggable we need more code!
Are you finished with all of this?
Let a tutor know. We’ve got more stuff for you!