Hello Sorry if this is confusing I'm really new to python and have been stuck trying to figure this out for hours now.
I'm trying to write a function for a text based battlship game. I need the function to do this is_occupied:
(int, int, int, int, list of list of strs) -> bool
The first to int's are suppose to refer to indices in the list of list of strings, same with the second 2 int's. The idea is to check the indices from range 1st set to 2nd set including themselves for a specific string element.
This is what I have so far the quoted out part was a different approach I took however so far both approaches have been unsuccessful.

def is_occupied(row1, col1, row2, col2, board):
    #row_1_col1 = board[row1],[col1]
    #row_2_col2 = board[row2],[col2]
    board = board
    r1 =[row1],[col1]
    r2 =[row2],[col2]
    if r1 >= r2:
	return True
    else:
    #if row_1_col1 >= row_2_col2:
	#return True
    #else:
	#for item in range(row_1_col1,row_2_col2):
	    #if item != (VACANT):
		#return True
	    #else:
		#return False
	for col in range(board(r1,r2)):
	    for item in col:  
		if not item == (VACANT):
		    return True
	    else:
		return False

Any help would be greatly appreciated I have also made some attemps to use multiple nested loops however I haven't got anything to work yet.

Recommended Answers

All 2 Replies

First, test that row1 < row2 and the same for columns. Also you should test for them being in the correct range. Then you can iterate using a for loop for rows and a sub-loop for columns and this code assumes that "board" is a list of lists.

def is_occupied(row1, col1, row2, col2, board):
    if row1 >= row2:
        print "row2 must be greater than row1"       
        return -1

        ## or switch them
        row2, row1 = row1, row2

    if col1 >= col2:
        print "column2 must be greater than column1" ## or switch them
        return -1

    one_row = len(board[0])                  ## max value for row and column
    for num in [row1, row2, col1, col2]:     ## assumes a square board
        if not -1 < num < one_row:           ## must be 0-->9 if 10 rows and columns
            print "all rows and columns must be in the range 1 through 9"
            return -1

    for row in range(row1, row2+1):
        for col in range(col1, col2+1):
            square_to_test = board[row][col]

I'm having some problems making this work, its probably something I'm doing wrong
Board is a list of lists of strings. Also I'm not sure why col needs to be tested because col1 and col2 can be the same #? This Error keeps coming up when I try to test this function.

is_occupied(0, 2, 2, 2, board)
column2 must be greater than column1
True
is_occupied(0, 2, 2, 7, board)
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "C:\Program Files\Wing IDE 101 4.1\src\debug\tserver\_sandbox.py", line 90, in is_occupied
TypeError: can only concatenate list (not "int") to list

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.