my first question! So, I'm self-learning python and wanted to write battleships as exercise. Some context about this before I ask the question. I'm keeping 4 "boards", 2 for each player. The boards are list of lists, 8x8
Board1 is their placement of ships. 0 means empty, 1 means something. Second one keeps track of where they hit so far. 0 means not shot yet, 1 means shot. (later I'll implement a 2, which means the cell was hit and something was shot)
I have function (checkProx) that checks if the square I hit has something and if it did, if it's a mine (1x1 piece) or a ship (bigger than 1x1)
The way I do it is to check the enemy "placement board" the up, down, left and right squares and see if there is a 1. If not, it means it's surrounded by empty cells and is a "mine". If it's elongated, it's a ship.
Here is my code:
def checkProx(mainListObj,corX,corY):
print("Starting check Prox with coords", corX, corY)
proxOK = 0
boardMax = 7
boardMin = 0
#boardMax Min not needed in this version, added for better modularity, they can be passed as arguments
if mainListObj[corX][corY] == 0:
print("Not hit - ",mainListObj[corX][corY])
#proxOK stays 0 = hit cell empty
return proxOK
else: #mainListObj[corX][corY] not equal to 0, we hit something
#checking proximity to see if the upper, lower, left or right cells have something. if we find a 1, it means we hit a ship, we can return the val
if corX > boardMin:
if mainListObj[corX-1][corY] == 1:
print("x-1, y")
proxOK = 1
return proxOK
elif corX < boardMax:
if mainListObj[corX+1][corY] == 1:
print("x+1, y")
proxOK = 1
return proxOK
elif corY > boardMin:
if mainListObj[corX][corY-1] == 1:
print("x, y-1")
proxOK = 1
return proxOK
elif corY < boardMax:
if mainListObj[corX][corY+1] == 1:
print("x, y+1")
proxOK = 1
return proxOK
else:
print("mine")
proxOK = 2 #we hit something but it's an isolated cell, meaning it's a mine
return proxOK
I have the not-great-looking if-elif cluster because I want to check if the numbers are out of boundaries. (I'm happy to hear suggestions on how to do it more elegantly, but not my main point of this question.)
My problem is if this if check fails(ie the value does NOT equal to 1), the function exists right away
if mainListObj[corX-1][corY] == 1:
and because a return isn't executed, the variable I'm assigning the return of checkProx gets a None value. If relevant, this is the assignment line:
prox = checkProx(playerBoard,x,y)
I don't understand the issue. It's coming to that stage normally. If the check isn't valid (ie mainListObj[corX-1][corY] does NOT equal to 1), shouldn't it just not enter that if, and move to the next line (elif corX < boardMax:)?
I tried: using a pass after if using an elif and pass after if nesting elifs under the if that comes before
nothing worked. I'd appreciate not only a solution but an explanation of what's really happening. To give you a better idea, I'll enter the values at the time of the if check: They are all int type, except mainListObj which is a list.
mainListObj:
[[1, 1, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 1]]
These coords DO work:
corX: 7
corY: 1
proxOK:0
mainListObj[corX][corY]:1
mainListObj[corX-1][corY]:1 (in this case it normally enters the if, prints and exist the function with a return)
These coords DO NOT work:
corX: 2
corY: 0
proxOK:0
mainListObj[corX][corY]:1
mainListObj[corX-1][corY]:0 (I place a breakpoint at the if. When I step in, it exists to the function call and assigns a None.
Aucun commentaire:
Enregistrer un commentaire