OiO.lk Blog python Issue with dictionaries/lists of lists in my python board game project
python

Issue with dictionaries/lists of lists in my python board game project


my name is Jeremiah, this is my first time posting here so if I missed a good post on this I apologize in advance.The main goal of this project is to make a text based board game with a procedurally generated game board. Basically, the way my code works is that I start by defining dimensions of a 2d list as I understand it where we then make a dictionary for each individual space that holds the attributes. The computer then does a bunch of random calculation to make a bunch of floats that it then checks against a few parameters to label the different attributes as strings that I can then hopefully use later. The problem that I am running into is in my findConnectedSpaces() function. The way that I have it working at least in my head is that it will go through a bunch of try except blocks and try to modify the coordinates of the space it is currently on and add or subtract 1 to the coordinate value and check if there is an index error. Basically the idea is that I have a 6 column 5 row, grid with all of the spaces on it and I am trying to check the surrounding 8 squares to see if those squares "exist" in my 2d list. If they do it counts the squares as "connected" the idea is that I want the players to eventually be able to move like a king in chess. When I run my program however, the first row doesn’t acknowledge any connections at all and the only things that end up appending into the connectedSpaces[] array are spaces that involve yUp in someway. Basically the square up, upRight, and upLeft, connect but nothing else? Does anyone have an idea of how I might try to fix this? I have been working at it for a few hours now.

#Importing my modules
import random
import math

#Define all of my variables to be able to add them to my list later
spaceType = float
exists = bool
number = int
connected = bool
connectedSpaces = []
spaceX = 'x'
spaceY = 'y'


#Define the 2d Array and add all of the tiles.
width = 6
height = 5
board = []



#Find the connected spaces

def findConnectedSpaces():
    
    #define a list for the connected spaces
    
    #define all of the coordinates as a variable so we can check them later
    xRight = space[spaceX] + 1
    xLeft = space[spaceX] - 1
    yUp = space[spaceY] - 1
    yDown = space[spaceY] + 1
    try:
        spaceRight = board[space[spaceY]][xRight][number]
        connectedSpaces.append(spaceRight)
        print('Connected: R')
    except IndexError:
        print('Couldnt connect: R')
        pass

    try:
        spaceLeft = board[space[spaceY]][xLeft][number]
        connectedSpaces.append(spaceLeft)
        print('Connected: L')
    except IndexError:
        print('Couldnt connect: L')
        pass  # Skip if out of bounds

    try:
        spaceDown = board[yDown][space[spaceX]][number]
        connectedSpaces.append(spaceDown)
        print('Connected: D')
    except IndexError:
        print('Couldnt connect: D')
        pass  # Skip

    try:
        spaceUp = board[yUp][space[spaceX]][number]
        connectedSpaces.append(spaceUp)
        print('Connected: U')
    except IndexError:
        print('Couldnt connect: U')
        pass  # Skip
    
    try:
        spaceRightDown = board[yDown][xRight][number]
        connectedSpaces.append(spaceRightDown)
        print('Connected: RD')
    except IndexError:
        print('Couldnt connect: RD')
        pass  # Skip
    
    try:
        spaceLeftDown = board[yDown][xLeft][number]
        connectedSpaces.append(spaceLeftDown)
        print('Connected: LD')
    except IndexError:
        print('Couldnt connect: LD')
        pass  # Skip
    
    try:
        spaceRightUp = board[yUp][xRight][number]
        connectedSpaces.append(spaceRightUp)
        print('Connected: RU')
    except IndexError:
        print('Couldnt connect: RU')
        pass  # Skip
    
    try:
        spaceLeftUp = board[yUp][xLeft][number]
        connectedSpaces.append(spaceLeftUp)
        print('Connected: LU')
    except IndexError:
        print('Couldnt connect: LU')
        pass  # Skip
    

    
    # Print the connected spaces
    print(f"{space[number]} is connected to: {connectedSpaces}")

    return connectedSpaces  # Return the list of valid connected spaces
    
# Create a loop to create all of the dictionaries.
for y in range(height):
    row = []
    for x in range(width):
        #create the dictionary for the space
        space = {}
        #use a random number to find the space type
        space[spaceType] = random.random()
        #use a random number to find out if the space exists
        space[exists] = random.random()
        #number the space
        space[number] = (x + 1) + (y * width)
        space[spaceX] = x
        space[spaceY] = y
        connectedSpaces = []
        #run my functions
        findSpaceType()
        doesSpaceExist()
        findConnectedSpaces()
        print(space[spaceY])
        print(space[spaceX])
        row.append(space)
    board.append(row)
    print(board)

I have tried to do this based on the Space number as well and decided to do this instead to hopefully allow the board to expand easier.



You need to sign in to view this answers

Exit mobile version