What Does This Code Do?


#1

I don’t understand what

“board[guess_row][guess_col] = “X”
print_board(board)”

does?


Where am I supposed to write this elif to check if the coordinate has already been guessed?
#2

Hi @imntesta,

The board is being stored as a two-dimensional array, which means there are arrays inside the array. An example 2D array might look like this:

[[1,2,3],
 [4,5,6],
 [7,8,9]]

So this is what the board variable looks like initially:

[['O','O','O','O','O'], # board[0]
 ['O','O','O','O','O'], # board[1]
 ['O','O','O','O','O'], # board[2]
 ['O','O','O','O','O'], # board[3]
 ['O','O','O','O','O']] # board[5]

By selecting board[0] out of the array, we get the whole first row, which is an array itself:

# [0   1   2   3   4]
 ['O','O','O','O','O']

To get an individual element out of there, we need to make another selection. So, to get the middle item of the first row, we take board[0][2].

The code you’ve posted uses the two integer variables, guess_row and guess_col, and selects that item out of the 2D array, then changes it to an X instead of an O. Then, it prints out the board variable in a slightly prettified form with the print_board function.