Trying to understand how its all connected at this point


#1



7.9 battle ship


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)

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)

Add your code below!

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

print ship_col
print ship_row


How is guess_row tied into any of the other variables or pass thru arguments. At this point can't see how the raw input
Guess col is tied into random_col or ship_col etc.


Replace this line with your code.


#2

For brevity, let r, c = guess_row, guess_col.

r and c are both integers (barring input errors). They act collectively as the the indices of the two dimension list that represents the board.

  C 0 1 2 3 4
R
0   O O O O O
1   O O O O O
2   O O O O O
3   O O O O O
4   O O O O O

Recall that each row is itself a list (now lain on its side) which rows we refer to as columns.

board[r]  =>  [ 'O', 'O', 'O', 'O', 'O' ]

To access a point in the grid (element in the list), we need both indices.

board[r][c]

The ship coordinates are safely generated by the program, so we know they will be integers. The user inputs may be suspect, though, so are cast to integer so errant input will raise an exception. Given valid inputs, the guess will or will not match that of the ship.

Critical aside.

It's important to have a feel for the semantics in a lot of programming terminology, and the expectations and assertions that follow it. In particular, the term, list may have varying implications in differing native languages. It is fairly safe to assume that a list would be viewed the way W3C defines it, having distinct lines stacked vertically with bullets, alpha-numerics or symbols prefacing each.

By this definition, a list is a stack of items. It's also why <li></li> is natively a block level element of character height and character width (meaning it expands with increase in content, restrained only in width by parent width).

This is the long and short of why I wrote, lain on its side. It's the only way we can envision it as a list of columns.


#3

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