I'm trying to make a simple game where a building placed in a nested list must be next to another building. The problem I face is that if the building was placed at the sides, I can't use for loops to check for existing buildings since it would not be part of the list anymore, since there is nothing on some sides of the building.
board = [['', '', '', ''],['', '', '', ''],['', '', '', ''],['', '', '', '']]building_list = ['HSE', 'FAC', 'SHP', 'HWY', 'BCH']
Output:
Turn 2A B C D+-----+-----+-----+-----+
1| | | | |+-----+-----+-----+-----+
2| | HSE | | |+-----+-----+-----+-----+
3| | | | |+-----+-----+-----+-----+
4| | | | |+-----+-----+-----+-----+
1. Build a HWY
2. Build a HWY
3. See remaining buildings
4. See current score5. Save game
0. Exit to main menu
Your choice? 1
Build where? a3
You must build next to an existing building.
My current code:
def build_buildings():global turna = 0m = 1j = 1k = 1i = 1if option == 1:building = option1else:building = option2location = input('Build where? ')location.split()col = location[0]row = int(location[1]) - 1if col == 'a':col = 0elif col == 'b':col = 1elif col == 'c':col = 2else:col = 3if turn > 1:if col == 0:m = -1if row == 0:j = 0if col == 3:k = -1if row == 3:i = 0if (board[int(row + i)][col] or board[int(row - j)][col] or board[int(row)][col + k]or board[int(row)][col - m]) != '':board[int(row)][col] = buildingelse:board[int(row)][col] = ''print('You must build next to an existing building.')print()turn -= 1else:board[int(row)][col] = building
Try this code:
def build(place: str, what: int, first_build: bool = False):if len(place) == 2 and 0 <= what < len(building_list): # check the argumentsx, y = x_axe.get(place[0].upper(), -1), int(place[1]) if place[1].isdigit() else -1 # get the x,y coordinates# check the conditions to buildif 0 <= x <= width and 0 <= y <= height and board[y][x] == '' and \(first_build or any(0 <= x + dx <= width and 0 <= y + dy <= height and board[y + dy][x + dx] != '' for dy, dx in((-1, 0), (0, 1), (1, 0), (0, -1)))):board[y][x] = building_list[what] # build!# global variables
board = [['', '', '', ''],['', '', '', ''],['', '', '', ''],['', '', '', '']]
building_list = ['HSE', 'FAC', 'SHP', 'HWY', 'BCH']
x_axe = {'A': 0, 'B': 1, 'C': 2, 'D': 3} # make dict with x indexes for use in build()
# get the max coordinates of the board
height = len(board) - 1
width = len(board[0]) - 1# simple examples; replace with your code
build('a3', 0, True) # first build with True as third argument
build('b3', 1) # other buildings; will be built
build('d3', 2) # will not be built because of conditions
build('B2', 2) # will be built
build('S9', 3) # will not be built due to wrong symbol and wrong y coordinate# output the board. first, make the format strings for the table
line = f" {'+-----' * (width + 1)}+" # +-----+-----+-----+-----+
row = f" {'|{:^5}' * (width + 1)}|" # |{:^5}|{:^5}|{:^5}|{:^5}|
header = f" {'{:^6}' * (width + 1)}" # {:^6}{:^6}{:^6}{:^6}
print(header.format(*x_axe.keys()), line, sep='\n') # header
for i in range(height + 1):print(str(i) + row.format(*board[i]), line, sep='\n')
Output:
A B C D +-----+-----+-----+-----+
0 | | | | |+-----+-----+-----+-----+
1 | | | | |+-----+-----+-----+-----+
2 | | SHP | | |+-----+-----+-----+-----+
3 | HSE | FAC | | |+-----+-----+-----+-----+