Inserting multiple values into list Python /Battleship -
this first time have posted question bear me if post wrong.
i creating battleships game in python , stuck on particular part of code. manage add ships in size of 1 not add ships bigger 1. have used list 10x10 gridboard , dictionary hold ships , sizes of ships. there kind soul out there might able me understand how solve this? here code far:
def comp_place_ship(comp_board): ships = {"a": 4, "b": 3, "c": 2, "s": 2, "d": 1} i, j in ships.items(): x = random.randint(0,9) y = random.randint(0,9) place = x,y if place != 0: print(place) comp_board[x][y] = comp_board[x+j][y] = #this i'm stuck print('the computer has placed ship: ', i) comp_place_ship(comp_board) print("--------------------------------------------") print("--------------------------------------------") print_comp_board(comp_board)
edit: might show output know mean:this output
i marked area "a" , not 0.
this i've got:
from pprint import pprint import random comp_board = []*10 in xrange(10): comp_board.append(['0']*10) def comp_place_ship(comp_board): ships = {"a": 4, "b": 3, "c": 2, "s": 2, "d": 1} i, j in ships.items(): x = random.randint(0,9-j) # fix index error y = random.randint(0,9) place = x,y print(place) k in range(j): # alter variable number of cells based on length of ship comp_board[x][y] = comp_board[x+k][y] = print('the computer has placed ship: ', i) comp_place_ship(comp_board) pprint(comp_board)
Comments
Post a Comment