Welcome to part 5 of the Python 3 basics series, in this series we've been working on building out a TicTacToe game to learn the ins and outs of Python. We left off with a visualization of the game's grid like so:
game = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] print(" 0 1 2") for count, row in enumerate(game): print(count, row)
Which gives us:
0 1 2 0 [0, 0, 0] 1 [0, 0, 0] 2 [0, 0, 0] >>>
Next, we need some way to actually modify the positions. Right now we're assuming all of the zeros are empty, and we'd like to have some way to denote a player actually maintaining one of the positions! This has 2 parts to it: actually taking in the player's intentions via some sort of input, and then actually marking. We can do the actual player input later and right now focus on how we'd actually mark the board.
To do this, we're going to use indexing. We can use indexes to get values or set new ones. Let's see some examples.
let's say we've got a list:
l = [1, 2, 3, 4, 5]
Each item in the list has an index value as we've seen before with our enumerate
function that returns index and the item. We start at 0 and go up. So what if we wanted to reference the 2
in this case? That would have an index of one. We can do this by:
print(l[1])
Notice that the index goes inside of square brackets. The output is:
2 >>>
We can also go backwards with indexes. So right now the 5 has an index of 4. Like so:
l = [1, 2, 3, 4, 5] print(l[4])
5 >>>
But we can also reference the 5 with the index of .... -1
!
l = [1, 2, 3, 4, 5] print(l[-1])
5 >>>
Pretty cool!
While we're here, we might as well see slices. To do a slice, it's just an index to another index and everything in between. We denote this by passing the first index, then a colon, then the ending index. For example:
l = [1, 2, 3, 4, 5] print(l[2:4])
[3, 4] >>>
Finally, we can set values at a certain index. For example:
l = [1, 2, 3, 4, 5] l[1] = 99 print(l)
[1, 99, 3, 4, 5] >>>
Okay, back to TicTacToe, let's use what we've learned to modify our game board.
In our case, this is a list of lists. We need 1 index to reference the specific list we want, then we need the index of the item from within that list. We can do this by stacking up the indexes. Let's see an example. Let's pretend we have the following:
game = [[0, 2, 0], [0, 0, 0], [0, 0, 0]]
What position is that 2
in? Iterating over game, means we iterate FIRST over the lists. So this is in the first list, so the one with the index of 0
. Inside of that list, what's the index of the 2? 1
. Okay, so all together, that's: game[0][1]
game = [[0, 2, 0], [0, 0, 0], [0, 0, 0]] print(game[0][1])
2 >>>
Great, we can also set values now. So let's start with an empty game:
game = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Then set that same spot:
game[0][1] = 2
Then print out our game:
print(" 0 1 2") for count, row in enumerate(game): print(count, row)
Full code is:
game = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] game[0][1] = 2 print(" 0 1 2") for count, row in enumerate(game): print(count, row)
Output:
0 1 2 0 [0, 2, 0] 1 [0, 0, 0] 2 [0, 0, 0] >>>
Great, so that's how we can actually set a value. Now we're almost ready to start playing on the map, but, every time we do that, we're going to be re-printing out the game after a player plays. That's going to be a lot of repeated code, so it's time we talk about functions
! So, I will see you in the next tutorial for that!