C language instances: Trapped smart snakes, instance smart snakes
After writing a snake, I found it interesting to do such a simple thing in C language. Although dalao seems a little unskillful, it still feels pretty good.
First, use the code written on the snake, and then add a code that controls the movement of the snake.
There have been many situations, such as snakes eating and eating the walls, or eating themselves. What's more, sometimes it turns around. In particular, when an extra obstacle is set, the snake often enters the endless circle of turns.
So how can we make the snake automatically seek the path? Here we need to know the coordinates of the Snake Head and the food. By calculating the next position that the snake head will reach, which is closer to the food, we can get the moving direction. So how can we avoid hitting a wall or your own body? You only need to use an if statement.
In fact, this algorithm is quite low-level, and snakes often do ...... Make some moths. But it's okay for me to turn this one into a little white.
Some of the beginning of the Code is saved first.
Int len = 5; int life = 0; int food = 0; int Fx = 0, Fy = 0; // The int Hx = 1, Hy = 5; // The coordinates of the snake header int X [snail ke_max_length] = {, 1}; // sankeint Y [snail ke_max_length] = {1, 2, 3, 4, 5 }; // snail kechar map [12] [12] = {"**************", "* XXXXH *","**", "**","**","**","**","**","**","**","**", "*************",}; void snail kemove (int x, int y) {char ch; int I; if (X [len-1]-X [len-2] =-x & Y [len-1] = = Y [len-2]) | (Y [len-1]-Y [len-2] =-y & X [len-1] = X [len-2]) {return;} // if the direction of the keyboard input is the opposite to that of the snake header, then nothing happens. if (map [X [len-1] + x] [Y [len-1] + y] = WALL_CELL | map [X [len- 1] + x] [Y [len-1] + y] = snail ke_body) {life ++ ;} // if you encounter a wall or yourself, it will die. if (map [X [len-1] + x] [Y [len-1] + y] = snail ke_food) {map [X [len-1] [Y [len-1] = snail ke_body; map [X [len-1] + x] [Y [len-1] + y] = snail ke_head; le N ++; X [len-1] = X [len-2] + x; Y [len-1] = Y [len-2] + y; hx = X [len-1]; Hy = Y [len-1]; food --;} // else {map [X [0] [Y [0] = BLANK_CELL; for (I = 0; I <len-1; I ++) {X [I] = X [I + 1]; Y [I] = Y [I + 1];} X [len-1] + = x; Y [len-1] + = y; Hx = X [len-1]; Hy = Y [len-1]; map [Hx] [Hy] = snke_head; for (I = 0; I <len-1; I ++) {map [X [I] [Y [I] = snail ke_body ;}} // food not met} void put_mo Ney (void) {if (food = 0) {srand (time (NULL); Fx = rand () % 9 + 1; Fy = rand () % 9 + 1; if (map [Fx] [Fy] = BLANK_CELL) {map [Fx] [Fy] = snail ke_food; food ++ ;}}} void output (void) {int I, j; for (I = 0; I <12; I ++) {for (j = 0; j <12; j ++) printf ("% c", map [I] [j]); printf ("\ n") ;}} void gameover (void) {printf ("GAME OVER !!! "); Return;} char whereGoNext (int Hx, int Hy, int Fx, int Fy) {int I; int temp = 0; int min = 10000; char moveble [4] = "WASD"; int distance [4] = {0, 0, 0, 0 }; distance [0] = abs (Fx-(Hx-1) + abs (Fy-Hy); distance [1] = abs (Fx-Hx) + abs (Fy-(Hy-1); distance [2] = abs (Fx-(Hx + 1) + abs (Fy-Hy ); distance [3] = abs (Fx-Hx) + abs (Fy-(Hy + 1 )); if (map [Hx-1] [Hy] = '*' | map [Hx-1] [Hy] = = 'X') distance [0] = 9999; if (map [Hx] [Hy-1] = '*' | map [Hx] [Hy-1] = 'X') distance [1] = 9999; if (map [Hx + 1] [Hy] = '*' | map [Hx + 1] [Hy] = 'X') distance [2] = 9999; if (map [Hx] [Hy + 1] = '*' | map [Hx] [Hy + 1] = 'X') distance [3] = 9999; // printf ("% d \ n", distance [0], distance [1], distance [2], distance [3]); // for (I = 0; I <4; I ++) {if (min> distance [I]) {temp = I; min = d Istance [I];} else continue;} printf ("% d \ n", min); return moveble [temp];} int main () {put_money (); output (); while (1) {put_money (); char ch = whereGoNext (Hx, Hy, Fx, Fy ); // printf ("% d", Hx, Hy, Fx, Fy); printf ("% c", whereGoNext (Hx, Hy, Fx, fy); // Sleep (100); switch (ch) {case 'A': snkemove (0,-1); break; case's ': snkemove (1, 0); break; case 'D': snail kemove (0, 1); break; case 'W ': Snail kemove (-1, 0); break;} system ("cls"); if (life! = 0) {gameover (); break;} if (len = 20) {printf ("you win !!! "); Break;} output ();}}'
Some printf statements in the Code are used to check whether there are logical computing errors in the amount.
Finally, with my adjustments, the snake will finally eat. Although it is basically difficult to eat a few items.
But occasionally, 20 of them will be eaten.
Oh, by the way, a sleep () function can be used here to prevent the snake from running properly. Run on Dev cpp and find that when s is lower case, the number in the brackets is in seconds, and the unit in the upper case is changed to milliseconds.
This is a simple intelligence (zhizhang) snake.