Recentemente ho iniziato a imparare Python e ho deciso di provare e realizzare il mio primo progetto. Sto provando a creare un gioco di corazzata che posiziona casualmente due navi di 3 blocchi lungo su una tavola. Ma non funziona abbastanza bene. Ho fatto un ciclo while per la nave n. 2 che dovrebbe controllare e vedere se due spazi adiacenti sono liberi, quindi costruirsi lì. Ma a volte si schiaffeggia in cima a dove già si trova la nave # 1. Qualcuno mi può aiutare?Simple Python Battleship gioco
Ecco la prima parte del codice:
from random import randint
###board:
board = []
for x in range(7):
board.append(["O"] * 7)
def print_board(board):
for row in board:
print " ".join(row)
###ships' positions:
#ship 1
def random_row(board):
return randint(0, len(board) - 1)
def random_col(board):
return randint(0, len(board[0]) - 1)
row_1 = random_row(board)
col_1 = random_col(board)
#ship 2
row_2 = random_row(board)
col_2 = random_col(board)
def make_it_different(r,c):
while r == row_1 and c == col_1:
r = random_row(board)
c = random_col(board)
row_2 = r
col_2 = c
make_it_different(row_2,col_2)
### Makes the next two blocks of the ships:
def random_dir():
n = randint(1,4)
if n == 1:
return "up"
elif n == 2:
return "right"
elif n == 3:
return "down"
elif n == 4:
return "left"
#ship one:
while True:
d = random_dir() #reset direction
if d == "up":
if row_1 >= 2:
#building...
row_1_2 = row_1 - 1
col_1_2 = col_1
row_1_3 = row_1 - 2
col_1_3 = col_1
break
if d == "right":
if col_1 <= len(board[0])-3:
#building...
row_1_2 = row_1
col_1_2 = col_1 + 1
row_1_3 = row_1
col_1_3 = col_1 + 2
break
if d == "down":
if row_1 <= len(board)-3:
#building...
row_1_2 = row_1 + 1
col_1_2 = col_1
row_1_3 = row_1 + 2
col_1_3 = col_1
break
if d == "left":
if col_1 >= 2:
#building...
row_1_2 = row_1
col_1_2 = col_1 - 1
row_1_3 = row_1
col_1_3 = col_1 - 2
break
ship_1 = [(row_1,col_1),(row_1_2,col_1_2),(row_1_3,col_1_3)]
Ed ecco dove la parte della nave 2 è:
#ship two:
while True:
d = random_dir() #reset direction
if d == "up":
if row_2 >= 2:
if (row_2 - 1,col_2) not in ship_1 and (row_2 - 2,col_2) not in ship_1:
#building...
row_2_2 = row_2 - 1
col_2_2 = col_2
row_2_3 = row_2 - 2
col_2_3 = col_2
break
if d == "right":
if col_2 <= len(board[0])-3:
if (row_2 ,col_2 + 1) not in ship_1 and (row_2,col_2 + 2) not in ship_1:
#building...
row_2_2 = row_2
col_2_2 = col_2 + 1
row_2_3 = row_2
col_2_3 = col_2 + 2
break
if d == "down":
if row_2 <= len(board)-3:
if (row_2 + 1 ,col_2) not in ship_1 and (row_2 + 2,col_2) not in ship_1:
#building...
row_2_2 = row_2 + 1
col_2_2 = col_2
row_2_3 = row_2 + 2
col_2_3 = col_2
break
if d == "left":
if col_2 >= 2:
if (row_2 ,col_2 - 1) not in ship_1 and (row_2,col_2 - 2) not in ship_1:
#building...
row_2_2 = row_2
col_2_2 = col_2 - 1
row_2_3 = row_2
col_2_3 = col_2 - 2
break
###test
board[row_1][col_1] = "X"
board[row_1_2][col_1_2] = "X"
board[row_1_3][col_1_3] = "X"
board[row_2][col_2] = "Y"
board[row_2_2][col_2_2] = "Y"
board[row_2_3][col_2_3] = "Y"
#Ship1 = X's and Ship2 = Y's
print_board(board)
http://www.SSCCE.org – Stephan
Non sembra che make_it_different stia facendo qualcosa. Esegui un ritorno alla fine della funzione e prova questo invece 'row_2, col_2 = make_it_different (row_2, col_2)'.Quando imposti una variabile all'interno di una funzione non la imposti su una scala globale (a meno che tu non voglia utilizzare la riga globale_2 e la colonna_2 vars nella funzione che non suggerisco) – scohe001
Ti consiglio di scegliere l'orientamento di la nave prima della sua posizione. –