Here's the code:
This is the output when I compile:
Sketch uses 13086 bytes (42%) of program storage space. Maximum is 30720 bytes. Global variables use 1456 bytes (71%) of dynamic memory, leaving 592 bytes for local variables. Maximum is 2048 bytes.
Everything runs smoothly until getNextFood(), Line 194. I try to make a LinkedList of integers from 0 to 255 (since I am using a 16x16 LED grid). However, my list only fills up to about 39, then the rest are set to zero. I suspected that I was out of memory, so I tried optimizing a few things and was able to get to 42. I changed all the ints to bytes in the getNextFood function and got up to 61, but that broke my program somehow (inf loop).
Any suggestions on how to fix this? If I can get this list to fit, the program is basically done, I have nothing else to add assuming it works as intended.
Also, why I am running out of RAM in the first place? Can someone explain which parts of the program are gobbling up those bytes during runtime (because I would expect the integer list to take up around 512 bytes leaving 80 bytes left, so why I am getting cut off around 40 integers or 80 bytes?)
Edit1: People have pointed out that linked lists take up a lot of space. I am using this library because the way the program is designed, I needed to make an array that could change size. I needed to be able to add and remove elements. Maybe there is a better way to do it, I don't know. I thought about making a single 16x16 array for the whole thing and using a different number for each type of piece. I don't know how I would do the snake movement in that case though, because each piece is "connected" to the one in front of it. If anyone knows of a similar library that would save space I'd be happy to try it. Also there might be a way for me to avoid using the tempSnake list.
Edit2: Per Paul's suggestion, I rewrote the code using a single 16x16 array to represent the entire game board, removing the need for LinkedList. However, it seems I am still falling short, unless there is something else wrong with my code. The program resets when I try to generate the food tiles. https://pastebin.com/ZU4hX477