Файл: Игра(Генерирующийся лабиринт случайным образом, проходимый искусственным интеллектом).docx
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 09.11.2023
Просмотров: 17
Скачиваний: 2
ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.
САНКТ-ПЕТЕРБУРГСКОЕ ГОСУДАРСТВЕННОЕ БЮДЖЕТНОЕ ПРОФЕССИОНАЛЬНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ <<ОХТИНСКИЙ КОЛЛЕДЖ>>
Игра(Генерирующийся лабиринт случайным образом, проходимый искусственным интеллектом)
Работу выполнил:
Студент группы 291 Комлев Ю.П
Преподаватель:
Резунков.Д.А
Санкт-Петербург
2022
Задание:
Написать какую-нибудь игру с любыми правилами
Что выполнено в работе:
-
Случайная генерация лабиринта -
ИИ, проходящий лабиринт самостоятельно. -
Отрисовка лабиринта
Что не выполнено:
Всё выполнено
Листинг программы:
import random
from graphics import *
row = int(input("Введите кол-во строк: "))
column = int(input("Введите кол-во столбцов: "))
passingType = int(input(" Введите 2, если хотите чтобы AI прошел лабиринт: "))
mas1 = [["☒"] * column for i in range(row)]
indexi = 0
indexj = 0
startRow = random.randint(0, 1)
startColumn = 0
endRow = random.randint(len(mas1)-3, len(mas1)-1)
endColumn = 0
if startRow == 0:
startColumn = random.randint(0, len(mas1[1])-1)
else:
if random.randint(0, 1) == 0:
startColumn = 0
else:
startColumn = len(mas1[1])-1
if endRow == len(mas1)-1:
endColumn = random.randint(0, len(mas1[1])-1)
else:
if random.randint(0, 1) == 0:
endColumn = 0
else:
endColumn = len(mas1[1])-1
mas1[startRow][startColumn] = "s"
mas1[endRow][endColumn] = "e"
indexLocationGenertatorRow = startRow
indexLocationGenertatorColumn = startColumn
def creategrid():
global indexi
global indexj
if (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 = i
indexj = j
print()
print()
else:
for i in range(0, len(mas1)):
for j in range(0, len(mas1[i])):
if mas1[i][j] == "????":
indexi = i
indexj = j
def renderMaze(intRow, intColumn):
global mas1
global indexLocationGenertatorRow
global indexLocationGenertatorColumn
mas1[indexLocationGenertatorRow][indexLocationGenertatorColumn] = "☐"
if intRow != 0 or intColumn != 0:
mas1[indexLocationGenertatorRow + intRow][indexLocationGenertatorColumn + intColumn] = "●"
indexLocationGenertatorRow += intRow
indexLocationGenertatorColumn += intColumn
else:
mas1[endRow][endColumn] = "☐"
indexLocationGenertatorRow = endRow
indexLocationGenertatorColumn = endColumn
def 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:
break
elif 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:
break
elif 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:
break
generateMaze()
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)
break
mas1[startRow][startColumn] = "s"
mas1[endRow][endColumn] = "e"
creategrid()
win = GraphWin("Окно для графики", len(mas1)*21, len(mas1[1])*21)
objLocationXStart = 20
objLocationYStart = 20
def 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 += 20
objLocationXStart = 20
objLocationYStart +=20
while 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()
Скрины работы кода (консоль):
Вывод
Создал игру “Лабиринт” генерируемый случайным образом. За игрока лабиринт проходит ИИ.