From the looks of it, somewhere a H or G value is not being calculated correctly, still looking through it to see exactly how your doing everything.
For now, here is an A* function I wrote for the Snake Competition to control the mouse that you can look at and maybe get some ideas from. (it is a bit messy, sorry).
// Mouse A Star ///////////////////////////////////////////
FUNCTION Mouse_AStar(StartX, StartY, TargetX, TargetY)
// Setup AStar Variables //
DIM List[705] AS AStar_Type
FOR i = 0 TO 705
List[i].OnList = 0
List[i].X = 0
List[i].Y = 0
List[i].ParentX = 0
List[i].ParentY = 0
List[i].G = 0
List[i].H = 0
List[i].F = 0
NEXT i
DIM IDGrid[31, 21] AS INTEGER
FOR y = 0 TO 21
FOR x = 0 TO 31
IDGrid[x, y] = 0
NEXT x
NEXT y
PathFound = 0
Current = 0
OpenCount = 0
ClosedCount = 0
FreeID = 1
LowScore = 0
BlockedUp = 0
BlockedRight = 0
BlockedDown = 0
BlockedLeft = 0
// Setup Start Location //
List[0].OnList = 1
List[0].X = StartX
List[0].Y = StartY
List[0].ParentX = StartX
List[0].ParentY = StartY
List[0].G = 0
List[0].H = Get_Best_H(StartX, StartY, TargetX, TargetY)
List[0].F = (List[0].G + List[0].H)
IDGrid[StartX, StartY] = FreeID
OpenCount = OpenCount + 1
FreeID = FreeID + 1
WHILE PathFound = 0
BlockedUp = 0
BlockedRight = 0
BlockedDown = 0
BlockedLeft = 0
FOR i = 0 TO 7
curx = List[Current].X
cury = List[Current].Y
checkx = Nodes[curx, cury, i].X
checky = Nodes[curx, cury, i].Y
// Check to see if will be cutting a corner //
NoCornerCut = 0
SELECT i
CASE 1:
upx = Nodes[curx, cury, 0].X
upy = Nodes[curx, cury, 0].Y
rightx = Nodes[curx, cury, 2].X
righty = Nodes[curx, cury, 2].Y
IF Grid[upx, upy] > 0 OR Grid[rightx, righty] > 0 THEN NoCornerCut = 1
ENDCASE
CASE 3:
downx = Nodes[curx, cury, 4].X
downy = Nodes[curx, cury, 4].Y
rightx = Nodes[curx, cury, 2].X
righty = Nodes[curx, cury, 2].Y
IF Grid[downx, downy] > 0 OR Grid[rightx, righty] > 0 THEN NoCornerCut = 1
ENDCASE
CASE 5:
downpx = Nodes[curx, cury, 4].X
downy = Nodes[curx, cury, 4].Y
leftx = Nodes[curx, cury, 6].X
lefty = Nodes[curx, cury, 6].Y
IF Grid[downx, downy] > 0 OR Grid[leftx, lefty] > 0 THEN NoCornerCut = 1
ENDCASE
CASE 7:
upx = Nodes[curx, cury, 0].X
upy = Nodes[curx, cury, 0].Y
leftx = Nodes[curx, cury, 6].X
lefty = Nodes[curx, cury, 6].Y
IF Grid[upx, upy] > 0 OR Grid[leftx, lefty] > 0 THEN NoCornerCut = 1
ENDCASE
ENDSELECT
// Check if already on list, if so see if this is a better path. //
IF IDGrid[checkx, checky] > 0 AND NoCornerCut = 0
tID = IDGrid[checkx, checky]
IF i = 0 OR i = 2 OR i = 4 OR i = 6
tg = List[Current].G + 10
ELSE
tg = List[Current].G + 14
ENDIF
th = Get_Best_H(checkx, checky, TargetX, TargetY)
tf = (tg + th)
IF List[tID].F > tf
List[tID].ParentX = List[Current].X
List[tID].ParentY = List[Current].Y
List[tID].G = tg
List[tID].H = th
List[tID].F = tf
ENDIF
ENDIF
// Check if walkable, if so add to open list. //
IF Grid[checkx, checky] = 0 AND NoCornerCut = 0 AND IDGrid[checkx, checky] = 0
IDGrid[checkx, checky] = FreeID
List[FreeID].OnList = 1
List[FreeID].X = checkx
List[FreeID].Y = checky
List[FreeID].ParentX = curx
List[FreeID].ParentY = cury
IF i = 0 OR i = 2 OR i = 4 OR i = 6
List[FreeID].G = List[Current].G + 10
ELSE
List[FreeID].G = List[Current].G + 14
ENDIF
List[FreeID].H = Get_Best_H(checkx, checky, TargetX, TargetY)
List[FreeID].F = (List[FreeID].G + List[FreeID].H)
OpenCount = OpenCount + 1
FreeID = FreeID + 1
ENDIF
NEXT i
// Done Checking Current, Add to Closed List //
List[Current].Onlist = 2
OpenCount = OpenCount - 1
ClosedCount = ClosedCount + 1
// Get Lowest Score //
LowScore = 100000
FOR i = 0 TO FreeID
IF List[i].OnList = 1
IF List[i].F < LowScore
LowScore = List[i].F
Current = i
ENDIF
ENDIF
NEXT i
IF List[Current].X = TargetX AND List[Current].Y = TargetY THEN PathFound = 1
IF OpenCount = 0 AND PathFound = 0 THEN PathFound = -1
IF FreeID > 705 THEN PathFound = -1
ENDWHILE
// No Path Found, Snake Must be Protecting //
IF PathFound = -1 AND GetSpriteCollision(MouseSprite, FruitSprite) = 0
Score = Score + 250
SetTextString(2, "+250")
SetTextPosition(2, GetSpriteX(FruitSprite), GetSpriteY(FruitSprite))
SetTextColor(2, 255, 255, 0, 255)
IF Player2_State > 0
Player2_Score = Player2_Score + 250
SetTextString(4, "+250")
SetTextPosition(4, GetSpriteX(FruitSprite), GetSpriteY(FruitSprite))
SetTextColor(4, 255, 255, 0, 255)
ENDIF
// Add Some Particles //
IF Player2_State = 0
SetParticlesPosition(2, GetSpriteXByOffset(FruitSprite), GetSpriteYByOffset(FruitSprite))
ResetParticleCount(2)
ELSE
SetParticlesPosition(1, GetSpriteXByOffset(FruitSprite)-10, GetSpriteYByOffset(FruitSprite)-10)
ResetParticleCount(1)
SetParticlesPosition(2, GetSpriteXByOffset(FruitSprite)+10, GetSpriteYByOffset(FruitSprite)+10)
ResetParticleCount(2)
ENDIF
// Re-Locate Fruit //
Find_New_Spot(FruitSprite)
SetSpriteImage(FruitSprite, RANDOM(100, 104))
SetSpriteColorAlpha(FruitSprite, 0)
gx = (GetSpriteX(FruitSprite) / 16) + 1
gy = (GetSpriteY(FruitSprite) / 16) + 1
Grid[gx, gy] = 1
IF Player2_State > 0
Msg = CreateNetworkMessage()
Code = 503
ID = GetSpriteImageID(FruitSprite)
X# = GetSpriteX(FruitSprite)
Y# = GetSpriteY(FruitSprite)
AddNetworkMessageInteger(Msg, Code)
AddNetworkMessageInteger(Msg, ID)
AddNetworkMessageFloat(Msg, X#)
AddNetworkMessageFloat(Msg, Y#)
SendNetworkMessage(NetworkID, 0, Msg)
ENDIF
PlaySound(1)
// Reset Mouse Pathfind //
MouseOldX = -5 // Reset Reset Pathfinding after Collision
MouseOldY = -5
GOTO ExitAStar
ENDIF
// Trace Path back to Start //
IF PathFound = 1
StartFound = 0
i = 0 // Break Counter, Get a lock up from time to time in a infinate loop. added this to break.
tID = IDGrid[TargetX, TargetY]
WHILE StartFound = 0
IF List[tID].ParentX = StartX AND List[tID].ParentY = StartY
gx = List[tID].X
gy = List[tID].Y
StartFound = 1
ELSE
gx = List[tID].ParentX
gy = List[tID].ParentY
ENDIF
tID = IDGrid[gx, gy]
//IF tID < 0 OR tID > 704 THEN EXIT
i = i + 1
IF i > 705 THEN EXIT
ENDWHILE
// Re-Angle Mouse to Follow path to Fruit //
IF gx = StartX AND gy < StartY THEN SetSpriteAngle(MouseSprite, 0)
IF gx > StartX AND gy < StartY THEN SetSpriteAngle(MouseSprite, 45)
IF gx > StartX AND gy = StartY THEN SetSpriteAngle(MouseSprite, 90)
IF gx > StartX AND gy > StartY THEN SetSpriteAngle(MouseSprite, 135)
IF gx = StartX AND gy > StartY THEN SetSpriteAngle(MouseSprite, 180)
IF gx < StartX AND gy > StartY THEN SetSpriteAngle(MouseSprite, 225)
IF gx < StartX AND gy = StartY THEN SetSpriteAngle(MouseSprite, 270)
IF gx < StartX AND gy < StartY THEN SetSpriteAngle(MouseSprite, 315)
// Correct Angle if need to world wrap //
DirX = 0
DirY = 0
IF StartX <= 1 AND gx >= 30 THEN DirX = -1
IF StartX >= 30 AND gx <= 1 THEN DirX = 1
IF StartY <= 1 AND gy >= 20 THEN DirY = -1
IF StartY >= 20 AND gy <= 1 THEN DirY = 1
IF DirX = 0 AND DirY = -1 THEN SetSpriteAngle(MouseSprite, 0)
IF DirX = 1 AND DirY = -1 THEN SetSpriteAngle(MouseSprite, 45)
IF DirX = 1 AND DirY = 0 THEN SetSpriteAngle(MouseSprite, 90)
IF DirX = 1 AND DirY = 1 THEN SetSpriteAngle(MouseSprite, 135)
IF DirX = 0 AND DirY = 1 THEN SetSpriteAngle(MouseSprite, 180)
IF DirX = -1 AND DirY = 1 THEN SetSpriteAngle(MouseSprite, 225)
IF DirX = -1 AND DirY = 0 THEN SetSpriteAngle(MouseSprite, 270)
IF DirX = -1 AND DirY = -1 THEN SetSpriteAngle(MouseSprite, 315)
ENDIF
// Draw Debug Info //
IF Path_Find_Debug = 1
i = 2000
FOR y = 0 TO 21
FOR x = 0 TO 31
IF GetSpriteExists(i) = 0
CreateSprite(i, 0)
SetSpriteSize(i, 16, 16)
SetSpritePosition(i, (x * 16) - 16, (y * 16) - 16)
SetSpriteDepth(i, 2000)
ENDIF
SetSpriteColor(i, 255, 255, 255, 128)
i = i + 1
NEXT x
NEXT y
SpriteNum = 2000
FOR y = 0 TO 21
FOR x = 0 TO 31
i = IDGrid[x, y]
IF List[i].OnList = 0 THEN SetSpriteColor(SpriteNum, 255, 255, 255, 128)
IF List[i].OnList = 1 THEN SetSpriteColor(SpriteNum, 128, 128, 255, 128)
IF List[i].OnList = 2 THEN SetSpriteColor(SpriteNum, 64, 64, 255, 128)
IF i = 0 THEN SetSpriteColor(SpriteNum, 255, 255, 255, 128)
IF i = 1 THEN SetSpriteColor(SpriteNum, 255, 0, 0, 128)
SpriteNum = SpriteNum + 1
NEXT x
NEXT y
// Draw Mouse Path //
IF PathFound = 1
StartFound = 0
i = 0 // Break Counter, Get a lock up from time to time in a infinate loop. added this to break.
tID = IDGrid[TargetX, TargetY]
WHILE StartFound = 0
IF List[tID].ParentX = StartX AND List[tID].ParentY = StartY
gx = List[tID].X
gy = List[tID].Y
s = (2000 + ((32 * gy)+ gx))
SetSpriteColor(s, 0, 0, 0, 128)
StartFound = 1
ELSE
gx = List[tID].ParentX
gy = List[tID].ParentY
s = (2000 + ((32 * gy)+ gx))
SetSpriteColor(s, 0, 0, 0, 128)
ENDIF
tID = IDGrid[gx, gy]
i = i + 1
IF i > 705 THEN EXIT
ENDWHILE
ENDIF
s = (2000 + ((32 * StartY) + StartX))
SetSpriteColor(s, 255, 0, 255, 128)
s = (2000 + ((32 * TargetY) + TargetX))
SetSpriteColor(s, 255, 0, 255, 128)
ENDIF
ExitAStar:
ENDFUNCTION
///////////////////////////////////////////////////////////
// Get Best H Value for Path Find /////////////////////////
//
// Needed to find a way to calculate an H value with
// consideration for the world wrap effect. This is best
// solution I could think of.
//
FUNCTION Get_Best_H(CurX, CurY, TargetX, TargetY)
// H values //
BestH = 10000
TestH = 0
// High and Low X //
HighX = 0
LowX = 0
IF CurX < TargetX
LowX = CurX
HighX = TargetX
ELSE
LowX = TargetX
HighX = CurX
ENDIF
// High and Low Y //
HighY = 0
LowY = 0
If CurY < TargetY
LowY = CurY
HighY = TargetY
Else
LowY = TargetY
HighY = CurY
ENDIF
// Simple Manhattan Distance //
TestH = 10 * (ABS(CurX - TargetX) + ABS(CurY - TargetY))
IF TestH <= BestH THEN BestH = TestH
// Manhattan Distance with World Wrap Calculated only on X //
TestX = ((32 - HighX) + LowX)
TestH = 10 * ((TestX) + ABS(CurY - TargetY))
IF TestH <= BestH THEN BestH = TestH
// Manhattan Distance with World Wrap Calculated only on Y //
TestY = ((22 - HighY) + LowY)
TestH = 10 * (ABS(CurX - TargetX) + (TestY))
IF TestH <= BestH THEN BestH = TestH
// Manhattan Distance with World Wrap Calculated on X and Y //
TestH = 10 * ((TestX) + (TestY))
IF TestH <= BestH THEN BestH = TestH
ENDFUNCTION BestH
///////////////////////////////////////////////////////////