Below is a simple pathfinding Algorithm that I have coded after designing. It is meant to take the position of a point or 'Unit', and given the Computer position, will, calculate the difference in X values and Y values, and travel the shortest difference first (due to the game, being a top down turn based game where units need to line up for attacks).
boardArray = [[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]]
# 0 shows an empty space, G is the goal, and C is the computer
boardArray[3][6] = "G"
boardArray[7][7] = "C"
def printBoard():
for r in boardArray:
for c in r:
print(c,end = " ")
print()
printBoard()
row = 0
for i in boardArray:
column = 0
for k in i:
if k == "G":
print(row, column)
goalpos = [row, column]
if k == "C":
print(row, column)
computerpos = [row, column]
column = column + 1
row = row + 1
def updatePositions(currentx, currenty, valrow, valcolumn):
print(currentx)
print(currenty)
boardArray[valrow][valcolumn] = 0
boardArray[currentx][currenty] = "C"
valrow = currentx
valcolumn = currenty
printBoard()
return valrow, valcolumn
def findPath(outscoperow, outscopecolumn):
DifferenceX = computerpos[0] - goalpos[0]
DifferenceY = computerpos[1] - goalpos[1]
CurrentCompX = computerpos[0]
CurrentCompY = computerpos[1] + 1
TemporaryX = DifferenceX
TemporaryY = DifferenceY
if DifferenceX < 0:
DifferenceX = DifferenceX *(-1)
if DifferenceY < 0:
DifferenceY = DifferenceY *(-1)
pathfinding = True
while pathfinding:
if DifferenceX < DifferenceY:
if TemporaryX > 0:
CurrentCompX = CurrentCompX - 1
outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
DifferenceX = DifferenceX - 1
elif TemporaryX < 0:
CurrentCompX = CurrentCompX + 1
outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
DifferenceX = DifferenceX - 1
elif DifferenceX > DifferenceY:
if TemporaryY > 0:
CurrentCompY = CurrentCompY - 1
outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
DifferenceY = DifferenceY - 1
elif TemporaryY < 0:
CurrentCompY = CurrentCompY + 1
outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
DifferenceY = DifferenceY - 1
if DifferenceX == 0 and DifferenceY == 0:
pathfinding = False
findPath(row, column)
As the code runs, it is supposed to actually move the computer position in real time, printing out the 2D Array as it goes, however, when altering the positions, the interpreter will also state that the current coordinates are outside the array's index (or list because Python is weird).
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 G 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 C
3 6
7 7
7
7
Traceback (most recent call last):
File "C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py", line 88, in <module>
findPath(row, column)
File "C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py", line 77, in findPath
outscoperow, outscopecolumn = updatePositions(CurrentCompX, CurrentCompY, outscoperow, outscopecolumn)
File "C:/Users/Cameron/Desktop/Basic RPG/pathfindingTest.py", line 44, in updatePositions
boardArray[valrow][valcolumn] = 0
IndexError: list index out of range
If anyone could offer a helping hand that would be most useful.
findPath
is receiving 8 and 8 as arguments, and in python, the arrays starts with 0.
You may need to rethink the logic where you generate row
and column
, or just do a -1
when receiving them