# 11. Danger, Will Robinson!

#1

I’m stuck on this exercise. How do I set the list element to ‘‘X’’?

``````
for x in range(0, 5):
board.append(["O"] * 5)

def print_board(board):
for row in board:
print " ".join(row)

print_board(board)

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleshp!"

``````

#2

the general syntax for updating a list element is:

``````the_list[index] = "new value"
``````

but keep in mind your list is multi-dimensional.

#3
``````from random import randint

board = []

for x in range(0, 5):
board.append(["O"] * 5)

def print_board(board):
for row in board:
print " ".join(row)

print_board(board)

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
guess_row = "X"
guess_col = "X"
else:
print "You missed my battleship!"

print_board(board)

``````

Ok still stuck here. So my thoughts are that, why does it matter if it’s 2-dimensional or multi-dimensional if I’m trying to put an “X” where it’s necessary?

#4

I’ve completed the exercise by using the 2-dimensional list syntax in the example. So could you explain your dimensional point abit further please.

#5

OOOOO
OOOOO
OOOOO
OOOOO
OOOOO

You have 5 rows and five columns
To select an individual element (O) you need to give the cpu a row and a col value
To change the first O into an x
`board[0][0] = 'X'`
The first [0] selects a row, the second [0] selects a col.
Now the board looks like this
XOOOO
OOOOO
OOOOO
OOOOO
OOOOO

An array is just continuous memory
char array[5] asks for five consecutive spaces in memory big enough for a char
char array[5][5] asks for five consecutive spaces in memory big enough to hold five chars each aka 25 continuous blocks of memory each holding a char.
So your row dimension selects a one of the five blocks of memory, your col dimension selects one of the five memory blocks within that block.

That was all a bit abstracted but should get what dimensions do across.

#6

#7

the reason this doesn’t work:

``````  guess_row = "X"
guess_col = "X"
``````

is because you never touch the list (which is stored in `board` variable)

back to basics, we have a game board:

``````board = []

for x in range(0, 5):
board.append(["O"] * 5)
``````

which is a list within a list (2d list), for which we have a function to nicely represent the board to the user:

``````def print_board(board):
for row in board:
print " ".join(row)
``````

to access a row within the board we can do:

``````print board[index]
``````

where index is the row we want to access. (so 0 till 4 (inclusive))

but this also gives us a list, this list represents columns on the board, to access a specific value within this column we get:

``````print board[index][index]
``````

we can even break this down into two steps if we want to see this better:

``````row = board[index]
print row
column = board[index]
print column
``````

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.