from random import randint
from time import sleep
import os
def print_board(board):
string=""
for row in board:
for item in row:
string+=item
print string
def find_start(lines):
coord=[]
for rowIndex in range(len(lines)):
row=lines[rowIndex]
for moveIndex in range(len(row)):
if row[moveIndex]=="s":
coord+=[rowIndex]
coord+=[moveIndex]
return coord
def find_end(lines):
coord=[]
for rowIndex in range(len(lines)):
row=lines[rowIndex]
for moveIndex in range(len(row)):
if row[moveIndex]=="e":
coord+=[rowIndex]
coord+=[moveIndex]
return coord
def gen_board(fileName):
fileObj=open(fileName, "r")
fileLines=fileObj.readlines()
fileObj.close()
boardList=[]
for line in fileLines:
lineList=list(line)
boardList+=[lineList]
return boardList
def place_player(board, row, move):
board[row][move]="@"
return board
def validate_move(board, row, move):
if board[row][move]==" ":
return True
elif board[row][move]=="#":
return False
elif board[row][move]=="e":
return True
else: return True
def tryForward(board, row, move, dir):
if dir=="s":
return validate_move(board, row+1, move)
elif dir=="w":
return validate_move(board, row, move-1)
elif dir=="n":
return validate_move(board, row-1, move)
elif dir=="e":
return validate_move(board, row, move+1)
def goForward(board, row, move, dir):
for rowIndex in range(len(board)):
row2=board[rowIndex]
for moveIndex in range(len(row2)):
if row2[moveIndex]=="@":
board[rowIndex][moveIndex]=" "
if dir=="s":
board[row+1][move]="@"
return row+1, move
elif dir=="w":
board[row][move-1]="@"
return row, move-1
elif dir=="n":
board[row-1][move]="@"
return row-1, move
elif dir=="e":
board[row][move+1]="@"
return row, move+1
def turnRight(dir):
if dir=="s":
return "w"
elif dir=="w":
return "n"
elif dir=="n":
return "e"
elif dir=="e":
return "s"
def turnLeft(dir):
if dir=="s":
return "e"
elif dir=="w":
return "s"
elif dir=="n":
return "w"
elif dir=="e":
return "n"
def findMoves(board, row, move, dMoves, bMoves, tmpMoves=[]):
moves=[]
if validate_move(board, row+1, move) and [row+1, move] not in dMoves and [row+1, move] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row+1, move]]
if validate_move(board, row-1, move) and [row-1, move] not in dMoves and [row-1, move] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row-1, move]]
if validate_move(board, row, move+1) and [row, move+1] not in dMoves and [row, move+1] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row, move+1]]
if validate_move(board, row, move-1) and [row, move-1] not in dMoves and [row, move-1] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row, move-1]]
return moves
def scanner(board, row, move, dMoves, bMoves):
tmpMoves=[]
moves=findMoves(board, row, move, dMoves, bMoves)
if len(moves)==0:
return False
elif len(moves)>1:
return True
for i in range(5):
moves=findMoves(board, row, move, dMoves, bMoves, tmpMoves)
if board[row][move]=="e":
return True
elif len(moves)==0:
return False
elif len(moves)>1:
return True
coord=moves[0]
row=coord[0]
move=coord[1]
tmpMoves+=[[row, move]]
return True
def dumbAi(board, row, move, end):
dir="s"
coord=[row, move]
count=0
while coord!=end:
dir=turnRight(dir)
if tryForward(board, row, move, dir):
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
else:
dir=turnLeft(dir)
if tryForward(board, row, move, dir):
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
else:
dir=turnLeft(dir)
if tryForward(board, row, move, dir):
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
else:
dir=turnLeft(dir)
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
os.system("cls")
print_board(board)
print count
coord=[row, move]
sleep(.02)
def smartAi(board, row, move, end):
doneMoves=[]
badMoves=[]
startCoord=[row, move]
moveCoord=[row, move]
count=0
while moveCoord!=end:
moves=findMoves(board, row, move, doneMoves, badMoves)
if len(doneMoves)==0:
doneMoves+=[startCoord]
if len(moves)==0:
badMoves+=[moveCoord]
if len(doneMoves)==0:
doneMoves+=[startCoord]
moveCoord=doneMoves.pop()
row=moveCoord[0]
move=moveCoord[1]
else:
rand=randint(0,len(moves)-1)
moveCoord=moves[rand]
row=moveCoord[0]
move=moveCoord[1]
scan=scanner(board, row, move, doneMoves, badMoves)
if not scan:
badMoves+=[moveCoord]
moveCoord=doneMoves.pop()
row=moveCoord[0]
move=moveCoord[1]
else:
doneMoves+=[moveCoord]
for rowIndex in range(len(board)):
row2=board[rowIndex]
for moveIndex in range(len(row2)):
if row2[moveIndex]=="@":
board[rowIndex][moveIndex]=" "
board[row][move]="@"
sleep(.02)
os.system("cls")
print_board(board)
count+=1
print count
board=gen_board("maze.txt")
board[1][1]="s"
board[19][77]="e"
start=find_start(board)
row=start[0]
move=start[1]
end=find_end(board)
board[row][move]="@"
#dumbAi(board, row, move, end)
smartAi(board, row, move, end)
###############################################################################
#s# # # # # # # # #
# # # ####### # # ####### # ############### ### # # # # ############# # ### # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # ####### # ##### # ### # # # ######### # # ##### # ### # ### #######
# # # # # # # # # # # # # # # # # # # # #
# ####### # # ######### ##### ####### ##### ####### ##### # # # # # ### # ### #
# # # # # # # # # # # # # # # # # # #
# # ### # ######### ########### ####### # ##### # ### # # ##### ##### ##### # #
# # # # # # # # # # # # # # # # # #
# ### ######### # ### ####### ########### # ####### # # ### ### # # # # ##### #
# # # # # # # # # # # # # # # # # # # # # #
### # # # # # ##### # # ### ### ##### # ######### # ##### # # # # ##### ### # #
# # # # # # # # # # # # # # # # # # # #
# ### # ### ### ##### ### ####### # ##### ##### ####### ##### # # # ######### #
# # # # # # # # # # # # # # # # #
# ####### ### # ####### ##### # # ######### ##### ### ### ############### # # #
# # # # # # # # # # # # # # # # # # # # #
# # ### ### # ### ### ### # ### # # # # # ##### ### ### ### ######### # ##### #
# # # # # # # # # # # e#
###############################################################################
from random import randint
from time import sleep
import os
def print_board(board):
string=""
for row in board:
for item in row:
string+=item
print string
def find_start(lines):
coord=[]
for rowIndex in range(len(lines)):
row=lines[rowIndex]
for moveIndex in range(len(row)):
if row[moveIndex]=="s":
coord+=[rowIndex]
coord+=[moveIndex]
return coord
def find_end(lines):
coord=[]
for rowIndex in range(len(lines)):
row=lines[rowIndex]
for moveIndex in range(len(row)):
if row[moveIndex]=="e":
coord+=[rowIndex]
coord+=[moveIndex]
return coord
def gen_board(fileName):
fileObj=open(fileName, "r")
fileLines=fileObj.readlines()
fileObj.close()
boardList=[]
for line in fileLines:
lineList=list(line)
boardList+=[lineList]
return boardList
def place_player(board, row, move):
board[row][move]="@"
return board
def validate_move(board, row, move):
if board[row][move]==" ":
return True
elif board[row][move]=="#":
return False
elif board[row][move]=="e":
return True
else: return True
def tryForward(board, row, move, dir):
if dir=="s":
return validate_move(board, row+1, move)
elif dir=="w":
return validate_move(board, row, move-1)
elif dir=="n":
return validate_move(board, row-1, move)
elif dir=="e":
return validate_move(board, row, move+1)
def goForward(board, row, move, dir):
for rowIndex in range(len(board)):
row2=board[rowIndex]
for moveIndex in range(len(row2)):
if row2[moveIndex]=="@":
board[rowIndex][moveIndex]=" "
if dir=="s":
board[row+1][move]="@"
return row+1, move
elif dir=="w":
board[row][move-1]="@"
return row, move-1
elif dir=="n":
board[row-1][move]="@"
return row-1, move
elif dir=="e":
board[row][move+1]="@"
return row, move+1
def turnRight(dir):
if dir=="s":
return "w"
elif dir=="w":
return "n"
elif dir=="n":
return "e"
elif dir=="e":
return "s"
def turnLeft(dir):
if dir=="s":
return "e"
elif dir=="w":
return "s"
elif dir=="n":
return "w"
elif dir=="e":
return "n"
def findMoves(board, row, move, dMoves, bMoves, tmpMoves=[]):
moves=[]
if validate_move(board, row+1, move) and [row+1, move] not in dMoves and [row+1, move] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row+1, move]]
if validate_move(board, row-1, move) and [row-1, move] not in dMoves and [row-1, move] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row-1, move]]
if validate_move(board, row, move+1) and [row, move+1] not in dMoves and [row, move+1] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row, move+1]]
if validate_move(board, row, move-1) and [row, move-1] not in dMoves and [row, move-1] not in bMoves and [row+1, move] not in tmpMoves:
moves+=[[row, move-1]]
return moves
def scanner(board, row, move, dMoves, bMoves):
tmpMoves=[]
moves=findMoves(board, row, move, dMoves, bMoves)
if len(moves)==0:
return False
elif len(moves)>1:
return True
for i in range(5):
moves=findMoves(board, row, move, dMoves, bMoves, tmpMoves)
if board[row][move]=="e":
return True
elif len(moves)==0:
return False
elif len(moves)>1:
return True
coord=moves[0]
row=coord[0]
move=coord[1]
tmpMoves+=[[row, move]]
return True
def dumbAi(board, row, move, end):
dir="s"
coord=[row, move]
count=0
while coord!=end:
dir=turnRight(dir)
if tryForward(board, row, move, dir):
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
else:
dir=turnLeft(dir)
if tryForward(board, row, move, dir):
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
else:
dir=turnLeft(dir)
if tryForward(board, row, move, dir):
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
else:
dir=turnLeft(dir)
coord=goForward(board, row, move, dir)
row=coord[0]
move=coord[1]
count+=1
os.system("cls")
print_board(board)
print count
coord=[row, move]
sleep(.02)
def smartAi(board, row, move, end):
doneMoves=[]
badMoves=[]
startCoord=[row, move]
moveCoord=[row, move]
count=0
while moveCoord!=end:
moves=findMoves(board, row, move, doneMoves, badMoves)
if len(doneMoves)==0:
doneMoves+=[startCoord]
if len(moves)==0:
badMoves+=[moveCoord]
if len(doneMoves)==0:
doneMoves+=[startCoord]
moveCoord=doneMoves.pop()
row=moveCoord[0]
move=moveCoord[1]
else:
rand=randint(0,len(moves)-1)
moveCoord=moves[rand]
row=moveCoord[0]
move=moveCoord[1]
scan=scanner(board, row, move, doneMoves, badMoves)
if not scan:
badMoves+=[moveCoord]
moveCoord=doneMoves.pop()
row=moveCoord[0]
move=moveCoord[1]
else:
doneMoves+=[moveCoord]
for rowIndex in range(len(board)):
row2=board[rowIndex]
for moveIndex in range(len(row2)):
if row2[moveIndex]=="@":
board[rowIndex][moveIndex]=" "
board[row][move]="@"
sleep(.02)
os.system("cls")
print_board(board)
count+=1
print count
board=gen_board("maze.txt")
board[1][1]="s"
board[19][77]="e"
start=find_start(board)
row=start[0]
move=start[1]
end=find_end(board)
board[row][move]="@"
#dumbAi(board, row, move, end)
smartAi(board, row, move, end)
###############################################################################
#s# # # # # # # # #
# # # ####### # # ####### # ############### ### # # # # ############# # ### # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # ####### # ##### # ### # # # ######### # # ##### # ### # ### #######
# # # # # # # # # # # # # # # # # # # # #
# ####### # # ######### ##### ####### ##### ####### ##### # # # # # ### # ### #
# # # # # # # # # # # # # # # # # # #
# # ### # ######### ########### ####### # ##### # ### # # ##### ##### ##### # #
# # # # # # # # # # # # # # # # # #
# ### ######### # ### ####### ########### # ####### # # ### ### # # # # ##### #
# # # # # # # # # # # # # # # # # # # # # #
### # # # # # ##### # # ### ### ##### # ######### # ##### # # # # ##### ### # #
# # # # # # # # # # # # # # # # # # # #
# ### # ### ### ##### ### ####### # ##### ##### ####### ##### # # # ######### #
# # # # # # # # # # # # # # # # #
# ####### ### # ####### ##### # # ######### ##### ### ### ############### # # #
# # # # # # # # # # # # # # # # # # # # #
# # ### ### # ### ### ### # ### # # # # # ##### ### ### ### ######### # ##### #
# # # # # # # # # # # e#
###############################################################################

from os import system
from os import name
def clear():
if (os.name == "posix"):
os.system("clear")
elif os.name in ("nt", "ce", "dos"):
os.system("cls")
else:
print "\n"*125
)
Copyright © 2026, NextGenUpdate.
All Rights Reserved.