Github Address: https://github.com/nagger28443/Projects_did_while_learning/tree/master/GreedySnake
Exciting! Just got my first project done. This little project, named as the title said ----"Greedy Snake", is my first project in java.
Here are some problems I met and solved by myself.
1. How to judge the sake is try to eat itself?
The video uses a for loop to traverse the whole snake, which seems not efficient enough to me. I try to solve this problem with a hashmap<pair,bool> and it's definitely no problem.
While doing this, I got familiar with how to use a hashmap and a pair. It's quite different from C++.
2. How to make the snake eat food?
The solution is quite complicated with using several methods. I just did it in the move method with dealing with linkedlist. It's much simpler.
3. If I press the direction button too fast, it generates a bug.
For instance, when I want the forward-left snake to turn back, I try to press up and right very fast. Then the program judge that the snake is eating itself and game over. It's because every time KeyMonitor detects a press the program change the direction of the snake immediately. To solve this problrm, the direction should refresh when the snake move a new step. So I use a variable tmpDir to reserve the last key that the player press. Then assign it to dir int the move method.
THis is it! Sorry for my lame English. I will try to do better.