Click here to Skip to main content
14,218,892 members
Rate this:
Please Sign up or sign in to vote.
I am working on a Snake AI game. I want to use BFS/A* to find the shortest path to the food. I know the coordinates of the food. There are other snakes on the board so I am trying to avoid them. I have the logic but how can I use BFS/A* to find the path? For example my snake's head's coordinate is (40,40) since i can only go left, right and up (no backwards as it is rest of my snake), how can I add these to my queue in BFS/A*? How can the BFS/A* expand? (It is 2D array type of board, nothing fancy). Output example: 1s and 4s are the snakes. 9 is the Food.

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 9 0 0 1 0 0 0 0 4 4 4 4 4 4
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 4
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0

Each turn new coordinates are entered so the snakes movement. Aim is to use BFS/A* to locate food so snake can move towards it. BFS/A* will be used to determine my move. Help will be appreciated.

What I have tried:

I have sorted out the coordinates. I have my board size. So my input will be
(int[][] board Size, target coordinate x, target coordinate y, snake head x, snake head y)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100