Battleship ( 11/19)


#1

Hi,
On the lesson, they asked me to replace the guessed row and col by an X. I don’t have any trouble to pass this step, but I’m quite not sure about how we’ve obtained such a list composed of 2 coordinates…
For instance, print board[2][3] will print the character at the 3th row and the 4 th column…How can we get such a list like this ??
I’m sure the way of doing it is in the lesson, but I haven’t found it yet…


#2

Recall that board is a list of lists.

>>> board = [['O'] * 5 for i in range(5)]
>>> board
[['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
>>> board[2]
['O', 'O', 'O', 'O', 'O']
>>> board[2][4]
'O'
>>> board[2][4] = 'X'
>>> board[2]
['O', 'O', 'O', 'O', 'X']
>>> 

Some extra study…

>>> def view(s):
	print [' '.join(u) for u in s]

	
>>> view(board)
['O O O O O', 'O O O O O', 'O O O O X', 'O O O O O', 'O O O O O']
>>> def view(s):
	print '\n'.join([' '.join(u) for u in s])

	
>>> view(board)
O O O O O
O O O O O
O O O O X
O O O O O
O O O O O
>>> 

#3

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