Post: [Python] AI Project
04-18-2011, 09:50 PM #1
schnzrs
Do a barrel roll!
(adsbygoogle = window.adsbygoogle || []).push({}); This is an Artificial Intelligence project I did for my computer science class. Just give it a certain type of maze and it will solve it. There are two types of AIs I made. One keeps turning right and moving forward, which will mean it will hit the end at one time. The other one scans around and takes semi-educated guesses on which way to go.

    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)


And here is the maze. Just save it as "maze.txt"
    ###############################################################################
#s# # # # # # # # #
# # # ####### # # ####### # ############### ### # # # # ############# # ### # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # ####### # ##### # ### # # # ######### # # ##### # ### # ### #######
# # # # # # # # # # # # # # # # # # # # #
# ####### # # ######### ##### ####### ##### ####### ##### # # # # # ### # ### #
# # # # # # # # # # # # # # # # # # #
# # ### # ######### ########### ####### # ##### # ### # # ##### ##### ##### # #
# # # # # # # # # # # # # # # # # #
# ### ######### # ### ####### ########### # ####### # # ### ### # # # # ##### #
# # # # # # # # # # # # # # # # # # # # # #
### # # # # # ##### # # ### ### ##### # ######### # ##### # # # # ##### ### # #
# # # # # # # # # # # # # # # # # # # #
# ### # ### ### ##### ### ####### # ##### ##### ####### ##### # # # ######### #
# # # # # # # # # # # # # # # # #
# ####### ### # ####### ##### # # ######### ##### ### ### ############### # # #
# # # # # # # # # # # # # # # # # # # # #
# # ### ### # ### ### ### # ### # # # # # ##### ### ### ### ######### # ##### #
# # # # # # # # # # # e#
###############################################################################
(adsbygoogle = window.adsbygoogle || []).push({});

The following 2 users say thank you to schnzrs for this useful post:

Outlasted Wolf, vSaBoTeuR x
04-18-2011, 09:58 PM #2
vSaBoTeuR x
< ^ > < ^ >
Originally posted by schnzrs View Post
This is an Artificial Intelligence project I did for my computer science class. Just give it a certain type of maze and it will solve it. There are two types of AIs I made. One keeps turning right and moving forward, which will mean it will hit the end at one time. The other one scans around and takes semi-educated guesses on which way to go.

    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)


And here is the maze. Just save it as "maze.txt"
    ###############################################################################
#s# # # # # # # # #
# # # ####### # # ####### # ############### ### # # # # ############# # ### # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # ####### # ##### # ### # # # ######### # # ##### # ### # ### #######
# # # # # # # # # # # # # # # # # # # # #
# ####### # # ######### ##### ####### ##### ####### ##### # # # # # ### # ### #
# # # # # # # # # # # # # # # # # # #
# # ### # ######### ########### ####### # ##### # ### # # ##### ##### ##### # #
# # # # # # # # # # # # # # # # # #
# ### ######### # ### ####### ########### # ####### # # ### ### # # # # ##### #
# # # # # # # # # # # # # # # # # # # # # #
### # # # # # ##### # # ### ### ##### # ######### # ##### # # # # ##### ### # #
# # # # # # # # # # # # # # # # # # # #
# ### # ### ### ##### ### ####### # ##### ##### ####### ##### # # # ######### #
# # # # # # # # # # # # # # # # #
# ####### ### # ####### ##### # # ######### ##### ### ### ############### # # #
# # # # # # # # # # # # # # # # # # # # #
# # ### ### # ### ### ### # ### # # # # # ##### ### ### ### ######### # ##### #
# # # # # # # # # # # e#
###############################################################################


2 Python releases in one evening, on NGU, you don't see that much. :P

Anyway, very nice! I will never program as good as that, lol. But here's a little catch. The system command "CLS" doesn't work on Linux distributions and Mac OS. That's why I have in all my apps where I need screenclears this function:

    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


(I'm not trying to be a smartass :P)

The following user thanked vSaBoTeuR x for this useful post:

Outlasted Wolf
04-20-2011, 12:10 PM #3
Outlasted Wolf
Do a barrel roll!
It's seems great :y: but i dont know how to run it on linux (im on Ubuntu) :(
How do you do it plz?
I'm new to python so could you give me a little help :confused:
Smile

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo