list - For loop over dict.items() in battleships python game -
i making game battleships in python , have gotten stuck on piece of code. have made 10x10 grid board player/computer place 5 ships different sizes. ships stored in dictionary.
i have #hastagged spot stuck. when player tries place ship spot not available, prints "invalid choice" , player should able place again. loop continues , therefore skips placing ship. i've tried calling function "player_place_ships" starts on , places duplicates of ships placed.
i thinking of creating count in loop , starting loop again left off before "invalid choice" unsure if it's doable start loop dict.items @ specific spot?
hoping there kind soul out there advice, i'm rather new @ python might using bad/unorthodox codes here.
here code:
#dictionary ships ships = {'a': 5, 'b': 4, 'c': 3, 's': 3, 'd': 2} #create player board player_board = [] player_row in range(10): player_board.append([]) player_col in range(10): player_board[player_row].append('.') #print player board def print_player_board(player_board): player_row in player_board: print(" ".join(player_row)) def player_place_ships(player_board, ships): i, j in ships.items(): ori = input('enter orientation, v or h: ') x = int(input('enter row: ')) y = int(input('enter col: ')) place = x,y placement = player_board[x][y] if ori == 'v' , placement == '.': k in range(j): player_board[x][y] = player_board[x+k][y] = elif ori == 'h' , placement == '.': player_board[x][y] = player_board[x][y+k] = elif ori != 'v' or 'h' , placement != '.': print('invalid choice, please try again.') #this i'm stuck player_place_ships(player_board, ships) print_player_board(player_board)
you might fix issue while ship_not_placed
def player_place_ships(player_board, ships): i, j in ships.items(): ship_not_place = true while ship_not_placed : ori = input('enter orientation, v or h: ') x = int(input('enter row: ')) y = int(input('enter col: ')) place = x,y placement = player_board[x][y] if ori == 'v' , placement == '.': k in range(j): player_board[x][y] = player_board[x+k][y] = ship_not_place = false elif ori == 'h' , placement == '.': player_board[x][y] = player_board[x][y+k] = ship_not_place = false elif ori != 'v' or 'h' , placement != '.': print('invalid choice, please try again.')
or while true
, break out of while instead of changing ship_not_placed
(i never understood best practice between two)
Comments
Post a Comment