Код: Выделить всё
#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)
Подробнее здесь: https://stackoverflow.com/questions/791 ... me-project