0

How do I represent each snake with a different integer in a 2d array ? (java)

I am currently in the beginner steps of making an AI snake agent for a snake game. I have created a playground which is represented by zeros. Every time I add a snake to the playground I would like it to be represented by a different integer. I have been thinking about using forloops to replace the zero and increment the snake number, though I am not sure how to go about doing it. Any help will be appreciated. I thank you in advance

19th Aug 2017, 12:10 PM
Noobie
Noobie - avatar
2 Answers
+ 3
Make the snake have an xPos, yPos. In the 2d arrays representing the 'board', you can simply do something along the lines of: array[xPos][yPos] = 1; // or just increment, although if you want everything to be ones and zeros I'd suggest just doing this. But before setting the new position, you must reset the old one. (If the snake is moving) So: array[xPos][yPos] = 0; xPos = curXPos; yPos = curYPos; array[xPos][yPos] = 1; Where curPos was just the position of the snake at that time. This is assuming the snake is of size 1, which is fine for starting out.
19th Aug 2017, 3:36 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
share your code or link of it
19th Aug 2017, 1:30 PM
Mayur Chaudhari
Mayur Chaudhari - avatar