Файл: Игра(Генерирующийся лабиринт случайным образом, проходимый искусственным интеллектом).docx

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 09.11.2023

Просмотров: 106

Скачиваний: 2

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
САНКТ-ПЕТЕРБУРГСКОЕ ГОСУДАРСТВЕННОЕ БЮДЖЕТНОЕ ПРОФЕССИОНАЛЬНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ <<ОХТИНСКИЙ КОЛЛЕДЖ>>Игра(Генерирующийся лабиринт случайным образом, проходимый искусственным интеллектом)Работу выполнил:Студент группы 291 Комлев Ю.П Преподаватель:Резунков.Д.АСанкт-Петербург2022Задание:
Написать какую-нибудь игру с любыми правиламиЧто выполнено в работе:

  1. Случайная генерация лабиринта

  2. ИИ, проходящий лабиринт самостоятельно.

  3. Отрисовка лабиринта
Что не выполнено:
Всё выполнено Листинг программы:import randomfrom graphics import *row = int(input("Введите кол-во строк: "))column = int(input("Введите кол-во столбцов: "))passingType = int(input(" Введите 2, если хотите чтобы AI прошел лабиринт: "))mas1 = [[""] * column for i in range(row)]indexi = 0indexj = 0startRow = random.randint(0, 1)startColumn = 0endRow = random.randint(len(mas1)-3, len(mas1)-1)endColumn = 0if startRow == 0:startColumn = random.randint(0, len(mas1[1])-1)else:if random.randint(0, 1) == 0:startColumn = 0else:startColumn = len(mas1[1])-1if endRow == len(mas1)-1:endColumn = random.randint(0, len(mas1[1])-1)else:if random.randint(0, 1) == 0:endColumn = 0else:endColumn = len(mas1[1])-1mas1[startRow][startColumn] = "s"mas1[endRow][endColumn] = "e"indexLocationGenertatorRow = startRowindexLocationGenertatorColumn = startColumndef creategrid():global indexiglobal indexjif (indexi == endRow and indexj == endColumn) or passingType == 1:for i in range(0, len(mas1)):for j in range(0, len(mas1[i])):print(mas1[i][j], end=' ')if mas1[i][j] == "????":indexi = iindexj = j print()print()else:for i in range(0, len(mas1)):for j in range(0, len(mas1[i])): if mas1[i][j] == "????":indexi = iindexj = j def renderMaze(intRow, intColumn):global mas1global indexLocationGenertatorRowglobal indexLocationGenertatorColumnmas1[indexLocationGenertatorRow][indexLocationGenertatorColumn] = ""if intRow != 0 or intColumn != 0:mas1[indexLocationGenertatorRow + intRow][indexLocationGenertatorColumn + intColumn] = "●"indexLocationGenertatorRow += intRowindexLocationGenertatorColumn += intColumnelse:mas1[endRow][endColumn] = ""indexLocationGenertatorRow = endRowindexLocationGenertatorColumn = endColumndef generateMaze():while indexLocationGenertatorRow != endRow or indexLocationGenertatorColumn != endColumn:if random.randint(1, 2) == 1:randomPositionGenerateMaze(random.randint(1, 3), random.randint(1, 3))else: if abs(indexLocationGenertatorColumn - endColumn)-1 > 0:#Генерациялабиринтавлевоif indexLocationGenertatorColumn > endColumn and indexLocationGenertatorColumn > 1 :
if indexLocationGenertatorRow != 0 and mas1[indexLocationGenertatorRow+1][indexLocationGenertatorColumn-1] != "":renderMaze(0,-1)elif indexLocationGenertatorRow != endRow:renderMaze(1,0)#Генерациялабиринтавправоelif indexLocationGenertatorColumn < endColumn:if indexLocationGenertatorRow != 0 and mas1[indexLocationGenertatorRow+1][indexLocationGenertatorColumn+1] != "":renderMaze(0,1)elif indexLocationGenertatorRow != endRow:renderMaze(1,0)#Генерациялабиринтавлевоиливправоеслистолбецсовпадаетelif indexLocationGenertatorColumn == endColumn:if indexLocationGenertatorColumn == len(mas1)-1:renderMaze(0,-1)elif indexLocationGenertatorColumn == 0:renderMaze(0,1)else:if random.randint(1, 2) == 1:renderMaze(0,-1)else:renderMaze(0,1)#Генерациялабиринтавнизelif indexLocationGenertatorRow != endRow:renderMaze(1,0)#Генереация лабиринта к конечной точкиelse:renderMaze(0,0)mas1[startRow][startColumn] = "????"creategrid()def randomPositionGenerateMaze(randomNumber, randomCountMovement):if randomNumber == 1:for a in range(randomCountMovement):if indexLocationGenertatorColumn+1 < len(mas1[1]) and indexLocationGenertatorRow != 0 and indexLocationGenertatorRow != len(mas1)-1 and mas1[indexLocationGenertatorRow+1][indexLocationGenertatorColumn+1] != "" and mas1[indexLocationGenertatorRow-1][indexLocationGenertatorColumn+1] != "" and (indexLocationGenertatorRow != endRow or indexLocationGenertatorColumn != endColumn):renderMaze(0,1)if indexLocationGenertatorRow == endRow and indexLocationGenertatorColumn == endColumn:renderMaze(0,0)else:breakelif randomNumber == 2:for a in range(randomCountMovement):if indexLocationGenertatorColumn != 0 and indexLocationGenertatorRow != 0 and indexLocationGenertatorRow != len(mas1)-1 and mas1[indexLocationGenertatorRow-1][indexLocationGenertatorColumn-1] != "" and mas1[indexLocationGenertatorRow+1][indexLocationGenertatorColumn-1] != "" and (indexLocationGenertatorRow != endRow or indexLocationGenertatorColumn != endColumn):renderMaze(0,-1)if indexLocationGenertatorRow == endRow and indexLocationGenertatorColumn == endColumn:renderMaze(0,0)else:breakelif randomNumber == 3:for a in range(randomCountMovement):if indexLocationGenertatorRow != 0 and indexLocationGenertatorColumn != 0 and indexLocationGenertatorColumn != len(mas1[1])-1 and mas1[indexLocationGenertatorRow-1][indexLocationGenertatorColumn+1] != "" and mas1[indexLocationGenertatorRow-1][indexLocationGenertatorColumn-1] != "" and (indexLocationGenertatorRow != endRow or indexLocationGenertatorColumn != endColumn):renderMaze(0,-1)if indexLocationGenertatorRow == endRow and indexLocationGenertatorColumn == endColumn:renderMaze(0,0)else:breakgenerateMaze()def movementAi(directionI, directionJ):mas1[indexi][indexj] = ""mas1[directionI][directionJ] = "????"creategrid()while (indexi != endRow or indexj != endColumn) and passingType == 2:#Движениевправоивверх if indexj+1 < len(mas1[1]) and mas1[indexi][indexj+1] == "

":movementAi(indexi, indexj+1)while indexi != 0 and indexi != len(mas1)-1 and mas1[indexi-1][indexj] == "" and mas1[indexi+1][indexj] != "":movementAi(indexi-1, indexj)#Движениевнизelif indexi != len(mas1)-1 and mas1[indexi+1][indexj] == "":movementAi(indexi+1, indexj)#Движение влево и вниз или вверхelif indexj != 0 and mas1[indexi][indexj-1] == "":while indexj != 0 and mas1[indexi][indexj-1] != "":movementAi(indexi, indexj-1)if indexi != len(mas1)-1 and mas1[indexi+1][indexj] == "":while indexi != len(mas1)-1 and mas1[indexi+1][indexj] == "":movementAi(indexi+1, indexj)elif indexi != 0 and mas1[indexi-1][indexj] == "":while mas1[indexi-1][indexj] != "":movementAi(indexi-1, indexj)#Движениевверхивлевоelif indexi != 0 and mas1[indexi-1][indexj] == "":while indexi != 0 and mas1[indexi-1][indexj] != "":movementAi(indexi-1, indexj)if indexj != 0 and mas1[indexi][indexj-1] == "":while mas1[indexi][indexj-1] != "":movementAi(indexi, indexj-1)if indexi != len(mas1)-1 and mas1[indexi+1][indexj] == "":movementAi(indexi+1, indexj)breakmas1[startRow][startColumn] = "s"mas1[endRow][endColumn] = "e"creategrid()win = GraphWin("Окнодляграфики", len(mas1)*21, len(mas1[1])*21)objLocationXStart = 20objLocationYStart = 20def renderWall(currentWall):obj = Image(Point(objLocationXStart,objLocationYStart), currentWall)obj.draw(win)if (indexi == endRow and indexj == endColumn) or passingType == 1:for i in range(0, len(mas1)):for j in range(0, len(mas1[i])):if mas1[i][j] != "":if j != len(mas1[1])-1 and mas1[i][j+1] == "":if j != 0 and mas1[i][j-1] == "":if i == 0:if mas1[i+1][j] == "":if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/RightBottomLeftBorder.png")else:renderWall("img/TopRightBottomLeftBorder.png")else:if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/RightLeftBorder.png")else:renderWall("img/TopRightLeftBorder.png")elif i == len(mas1)-1 and mas1[i-1][j] == "":if mas1[i-1][j] == "":if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/TopRightLeftBorder.png")else:renderWall("img/TopRightBottomLeftBorder.png")else:if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/RightLeftBorder.png")else:renderWall("img/RightBottomLeftBorder.png")else:if mas1[i-1][j] == "" and mas1[i+1][j] == "":renderWall("img/TopRightBottomLeftBorder.png")elif mas1[i-1][j] == "":renderWall("img/TopRightLeftBorder.png")elif mas1[i+1][j] == "":renderWall("img/RightBottomLeftBorder.png")else:renderWall("img/RightLeftBorder.png")else:if i == 0:if mas1[i+1][j] == "":if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/RightBottomBorder.png")else:renderWall("img/TopRightBottomBorder.png")else:if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/RightBorder.png")
else:renderWall("img/TopRightBorder.png")elif i == len(mas1)-1:if mas1[i-1][j] == "":if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/TopRightBorder.png")else:renderWall("img/TopRightBottomBorder.png")else:if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/RightBorder.png")else:renderWall("img/RightBottomBorder.png")else:if mas1[i-1][j] == "" and mas1[i+1][j] == "":renderWall("img/TopRightBottomBorder.png")elif mas1[i-1][j] == "":renderWall("img/TopRightBorder.png")elif mas1[i+1][j] == "":renderWall("img/RightBottomBorder.png")else:renderWall("img/RightBorder.png")elif j != 0 and mas1[i][j-1] == "":if i == 0 and mas1[i+1][j] == "":if mas1[i+1][j] == "":if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/BottomLeftBorder.png")else:renderWall("img/TopBottomLeftBorder.png")else:if masl[i][j] == "s" or mas1[i][j] == "e":renderWall("img/LeftBorder.png")else:renderWall("img/TopLeftBorder.png")elif i == len(mas1)-1:if mas1[i-1][j] == "":if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/TopLeftBorder.png")else:renderWall("img/TopBottomLeftBorder.png")else:if mas1[i][j] == "s" or mas1[i][j] == "e":renderWall("img/LeftBorder.png")else:renderWall("img/BottomLeftBorder.png")else:if mas1[i-1][j] == "" and mas1[i+1][j] == "":renderWall("img/TopBottomLeftBorder.png")elif mas1[i-1][j] == "":renderWall("img/TopLeftBorder.png")elif mas1[i+1][j] == "":renderWall("img/BottomLeftBorder.png")else:renderWall("img/LeftBorder.png")elif i != 0 and mas1[i-1][j] == "" and i != len(mas1)-1 and mas1[i+1][j] == "":if j == 0 and not(mas1[i][j] == "s" or mas1[i][j] == "e"):renderWall("img/TopBottomLeftBorder.png")elif j == len(mas1[j])-1 and not(mas1[i][j] == "s" or mas1[i][j] == "e"):renderWall("img/TopRightBottomBorder.png")else:renderWall("img/TopBottomBorder.png")elif i != 0 and mas1[i-1][j] == "":if j == 0 and not(mas1[i][j] == "s" or mas1[i][j] == "e"):renderWall("img/TopLeftBorder.png")elif j == len(mas1[j])-1 and not(mas1[i][j] == "s" or mas1[i][j] == "e"):renderWall("img/TopRightBorder.png")else:renderWall("img/TopBorder.png")elif i != len(mas1)-1 and mas1[i+1][j] == "":if j == 0 and not (mas1[i][j] == "s" or mas1[i][j] == "e"):renderWall("img/BottomLeftBorder.png")print(mas1[i][j])elif j == len(mas1[j])-1 and not (mas1[i][j] == "s" or mas1[i][j] == "e"):renderWall("img/RightBottomBorder.png")else:renderWall("img/BottomBorder.png")else:renderWall("img/WithOutBorder.png")objLocationXStart += 20objLocationXStart = 20objLocationYStart +=20while passingType == 1 and (indexi != endRow and indexj != endColumn):movementInput = input("Движение w, a, s, d: ")if movementInput == "w" and indexi != 0 and mas1[indexi-1][indexj] != "":movementAi(-1,0)elif movementInput == "s" and indexi != len(mas1)-1 and mas1[indexi+1][indexj] != "":movementAi(1,0)elif movementInput == "a" and indexj != 0 and mas1[indexi][indexj-1] != "
":movementAi(0,-1)elif movementInput == "d" and indexj != len(mas1[1])-1 and mas1[indexi][indexj+1] != "":movementAi(0,1)win.getMouse()win.close() Скрины работы кода (консоль): ВыводСоздал игру “Лабиринт” генерируемый случайным образом. За игрока лабиринт проходит ИИ.