C language code
C language code
Snake C language code manual snake intelligent snake
1. Manually greedy for snakes
/* The faster the snake runs * // * Author: SGAFPZ */# include
# Include
# Include
# Include
// # Include
# Include
# Include
# Include
Void food (); void show (); void move (); void turn (); void check (); void ini (); // void calculate (); // int movable [4] = {0, 1, 1, 0}; // The subscript of the array indicates the direction, and 0123 indicates the top, bottom, and bottom, respectively, the same below // int distance [4] = {9999,999 9, 9999,999 9}; int dy [4] = {0, 1, 0,-1 }; int dx [4] = {-1, 0, 1, 0}; int sum = 5; // total snake length int over = 0; int speed; // int foodx, foody; // The coordinates of the food char map [17] [17]; struct snake {int x, y; // The body coordinate int dir; // direction (only the direction of the snake header is useful)} A [100]; void ini () {// initialization interface speed = 500; over = 0; sum = 5; // movable [0] = 0; movable [1] = 1; movable [2] = 1; movable [3] = 0; // distance [0] = 9999; distance [1] = 9999; distance [2] = 9999; distance [3] = 9999; int I, j; for (I = 0; I <100; I ++) {// initialize the data of the snake header and the snake body A [I]. dir = 0; A [I]. x = 0; A [I]. y = 0;} A [0]. x = 1; A [0]. y = 1; // set A snake with A length of 5 in the upper left corner of the map. A [1]. x = 1; A [1]. y = 2; A [2]. x = 1; A [2]. y = 3; A [3]. x = 1; A [3]. y = 4; A [4]. x = 1; A [4]. y = 5; A [4]. dir = 1; srand (time (0); for (I = 0; I <17; I ++) {// set map for (j = 0; j <17; j ++) {map [I] [j] = '*' ;}} for (I = 1; I <16; I ++) {for (j = 1; j <16; j ++) {map [I] [j] = '';}} // map [6] [5] = '*'; map [6] [6] = '*'; map [6] [7] = '*'; // map [7] [5] = '*'; map [7] [7] = '*'; map [A [4]. x] [A [4]. y] = 'H'; // set the snake header for (I = 0; I <sum-1; I ++) {// sets the snake body map [A [I]. x] [A [I]. y] = 'X';} food (); // calculate ();} void show () {// display interface int I, j, X, y; for (I = 0; I <17; I ++) {// display interface for (j = 0; j <17; j ++) {printf ("% c", map [I] [j]);} printf ("\ n");} while (1) {Sleep (speed ); // interface refresh speed turn (); move (); if (over) {// set the operation that can be performed after the snake dies while (1) {char ch = _ getch (); if (ch = 113) {// enter 'q' to end return;} else if (ch = 114) {// enter 'R' to restart ini (); break ;}} system ("cls "); // Clear screen for (I = 0; I <17; I ++) {// re-display the interface for (j = 0; j <17; j ++) {printf ("% c", map [I] [j]);} printf ("\ n");} // calculate (); // calculate and record the distance between the Snake Head and the food} void food () {// generate int x, y; while (1) {x = (int) (15 * rand ()/(RAND_MAX + 1.0); // randomly generates a group of food coordinates y = (int) (15 * rand () /(RAND_MAX + 1.0); if (map [x] [y] = '') {// if it is a space, the food map [x] [y] = 'O' is generated here; // foodx = x; // records the coordinates of the food. // foody = y; break ;}} void move () {// Snake moves int I, x, y; int t = sum; // t records the total length of the current snake check (); // check if (t = sum) after moving one step in the current direction before moving {// I = 0; I <sum-1; I ++) {if (I = 0) {// turn the coordinates of the snake tail into spaces and convert the coordinates of the snake tail into the coordinates of the previous snake body map [A [I]. x] [A [I]. y] = ''; A [I]. x = A [I + 1]. x; A [I]. y = A [I + 1]. y;} else {// The coordinates of each snake body are changed to the coordinates of the previous snake body A [I]. x = A [I + 1]. x; A [I]. y = A [I + 1]. y;} map [A [I]. x] [A [I]. y] = 'X'; // set the character at the coordinates of the snake body on the map to 'X'} A [sum-1]. x = A [sum-1]. x + dx [A [sum-1]. dir]; // The Snake head moves A [sum-1] In the current direction. y = A [sum-1]. y + dy [A [sum-1]. dir]; map [A [sum-1]. x] [A [sum-1]. y] = 'H'; // set the character at the coordinates of the snake header on the map to 'H'} else {// you can eat the apple (sum will add 1) map [A [sum-2]. x] [A [sum-2]. y] = 'X'; // set the character at the coordinates of the original snake header on the map to 'X' A [sum-1]. x = A [sum-2]. x + dx [A [sum-2]. dir]; // The coordinates of the new snake head are the coordinates A [sum-1] after the original snake head moves A grid along the current direction. y = A [sum-2]. y + dy [A [sum-2]. dir]; A [sum-1]. dir = A [sum-2]. dir; // the direction of the new snake head is the direction of the original Snake Head Map [A [sum-1]. x] [A [sum-1]. y] = 'H'; // set the character at the coordinates of the snake header on the map to 'H' food ();}/* for (I = 0; I <4; I ++) {// record the direction x = A [sum-1]. x + dx [I]; y = A [sum-1]. y + dy [I]; if (map [x] [y] = ''| map [x] [y] = 'O ') {movable [I] = 1; // set the value in the corresponding direction to 1} else {if (x! = A [0]. x | y! = A [0]. y) {movable [I] = 0; // set the value in the corresponding direction to 0} else {movable [I] = 1 ;}} if the value cannot go ;}}} * //} void check () {// check whether it is dead or whether int x, y, I, j; x = A [sum-1] is eaten. x + dx [A [sum-1]. dir]; // records the coordinates of the snake header after moving A grid in the current direction y = A [sum-1]. y + dy [A [sum-1]. dir]; if (map [x] [y] = '*' | map [x] [y] = 'X ') {// if the character at the coordinate on the map is '*' or 'X', it will die if (X! = A [0]. x | y! = A [0]. y) {// except for snake tails, map [8] [4] = 'G'; map [8] [5] = 'a '; map [8] [6] = 'M'; map [8] [7] = 'E '; // output "game over" map [8] [9] = 'O'; map [8] [10] = 'V '; map [8] [11] = 'E'; map [8] [12] = 'R'; map [8] [8] = ''; system ("cls"); for (I = 0; I <17; I ++) {for (j = 0; j <17; j ++) {printf ("% c", map [I] [j]);} printf ("\ n ");} printf ("Input 'R' to restart \ nInput 'q' to quit \ n"); over = 1 ;}} else if (map [x] [y] = 'O '){// Eat apple sum ++; // The total length of the snake body plus 1 speed = (600-sum * 20)> 100 )? (600-sum * 20): 100; // speed up} void turn () {// turn if (_ kbhit () {char dir = _ getch (); // read the Input key switch (dir) {// change the direction case 119: A [sum-1]. dir = (A [sum-1]. dir = 2 )?; Break; case 100: A [sum-1]. dir = (A [sum-1]. dir = 3 )?; Break; case 115: A [sum-1]. dir = (A [sum-1]. dir = 0 )? 0: 2; break; case 97: A [sum-1]. dir = (A [sum-1]. dir = 1 )?; Break ;}}/ * void calculate () {// calculate and record the distance between the Snake Head and the food int I = 0, x, y; for (I = 0; I <4; I ++) {if (movable [I] = 1) {// if this direction can be performed, record the distance from the food after taking A step in this direction x = A [sum-1]. x + dx [I]; y = A [sum-1]. y + dy [I]; distance [I] = abs (foodx-x) + abs (foody-y );} else {// set the distance to 9999 distance [I] = 9999;} */int main () {printf ("'W''s 'A 'd 'D 'control up and down. \ n the faster the snake runs ~~~ \ N "); printf (" start by any key \ n "); char ch = _ getch (); system (" cls "); ini (); show (); return 0 ;}
2. Intelligent snake
Although intelligent, it may not be as intelligent as you think.
The basic idea is to followTop, right, bottom, left
The sequential search direction allows you to move in this direction closer to food. obstacles encountered during the forward process will be automatically bypassed, but the situation that the snake head is surrounded by a snake cannot be avoided.
/* The faster the snake runs * // * the snake can search for food by itself * // * Author: SGAFPZ */# include
# Include
# Include
# Include
// # Include
# Include
# Include
# Include
Void food (); void show (); void move (); void turn (); void check (); void ini (); void calculate (); int movable [4] = {0, 1, 1, 0}; // The subscript of the array indicates the direction, and 0123 indicates the top, bottom, and bottom, respectively. The same int distance [4] = {9999,999 9, 9999,999 9}; int dy [4] = {0, 1, 0,-1}; int dx [4] = {-1, 0, 1, 0 }; int sum = 5; // total length of the snake int over = 0; int speed; int foodx, foody; // The coordinates of the food char map [17] [17]; struct snake {int x, y; // body coordinate int dir; // direction (only the direction of the snake header is useful)} A [100 ]; Void ini () {// initialization interface speed = 500; over = 0; sum = 5; movable [0] = 0; movable [1] = 1; movable [2] = 1; movable [3] = 0; distance [0] = 9999; distance [1] = 9999; distance [2] = 9999; distance [3] = 9999; int I, j; for (I = 0; I <100; I ++) {// initialize the data of the snake header and the snake body A [I]. dir = 0; A [I]. x = 0; A [I]. y = 0;} A [0]. x = 1; A [0]. y = 1; // set A snake with A length of 5 in the upper left corner of the map. A [1]. x = 1; A [1]. y = 2; A [2]. x = 1; A [2]. y = 3; A [3]. x = 1; A [3]. y = 4; A [4]. x = 1; A [4]. y = 5; A [4]. dir = 1; srand (time (0); for (I = 0; I <17; I ++) {// set map for (j = 0; j <17; j ++) {map [I] [j] = '*' ;}} for (I = 1; I <16; I ++) {for (j = 1; j <16; j ++) {map [I] [j] = '';}} // map [6] [5] = '*'; map [6] [6] = '*'; map [6] [7] = '*'; // map [7] [5] = '*'; map [7] [7] = '*'; map [A [4]. x] [A [4]. y] = 'H'; // set the snake header for (I = 0; I <sum-1; I ++) {// sets the snake body map [A [I]. x] [A [I]. y] = 'X';} food (); calculate ();} void show () {// display interface int I, j, X, y; for (I = 0; I <17; I ++) {// display interface for (j = 0; j <17; j ++) {printf ("% c ", map [I] [j]);} printf ("\ n");} while (1) {Sleep (speed); // interface refresh speed turn (); move (); if (over) {// operation that can be performed after the snake dies while (1) {char ch = _ getch (); if (ch = 113) {// enter 'q' to end return;} else if (ch = 114) {// enter 'R' to start ini (); break ;}}} system ("cls"); // clear screen for (I = 0; I <17; I ++) {// re-display the interface for (j = 0; j <17; j ++) {printf ("% c ", map [I] [j]);} printf ("\ n");} calculate (); // calculate and record the distance between the Snake Head and the food} void food () {// generate int x, y; while (1) {x = (int) (15 * rand ()/(RAND_MAX + 1.0 )); // randomly generate a group of food coordinates y = (int) (15 * rand ()/(RAND_MAX + 1.0 )); if (map [x] [y] = '') {// if it is a space, the food map [x] [y] = 'O' is generated here '; foodx = x; // record the food coordinate foody = y; break ;}} void move () {// snake moves int I, x, y; Int t = sum; // t record the total length of the current snake check (); // check the situation after moving one step in the current direction before moving if (t = sum) {// do not eat apple for (I = 0; I <sum-1; I ++) {if (I = 0) {// space at the coordinates of the end of the snake, map the coordinates of the snake tail to the coordinates of the previous snake body [A [I]. x] [A [I]. y] = ''; A [I]. x = A [I + 1]. x; A [I]. y = A [I + 1]. y;} else {// The coordinates of each snake body are changed to the coordinates of the previous snake body A [I]. x = A [I + 1]. x; A [I]. y = A [I + 1]. y;} map [A [I]. x] [A [I]. y] = 'X'; // set the character at the coordinates of the snake body on the map to 'X'} A [sum-1]. x = A [sum-1]. x + dx [A [sum-1]. dir]; // The snake head moves A [sum-1] In the current direction. y = A [sum-1]. y + dy [A [sum-1]. dir]; map [A [sum-1]. x] [A [sum-1]. y] = 'H'; // set the character at the coordinates of the snake header on the map to 'H'} else {// you can eat the apple (sum will add 1) map [A [sum-2]. x] [A [sum-2]. y] = 'X'; // set the character at the coordinates of the original snake header on the map to 'X' A [sum-1]. x = A [sum-2]. x + dx [A [sum-2]. dir]; // The coordinates of the new snake head are the coordinates A [sum-1] after the original snake head moves A grid along the current direction. y = A [sum-2]. y + dy [A [sum-2]. dir]; A [sum-1]. dir = A [sum-2]. dir; // map [A [sum-1]. x] [[Sum-1]. y] = 'H'; // set the character at the coordinates of the snake header on the map to 'H' food ();} for (I = 0; I <4; I ++) {// record the direction x = A [sum-1]. x + dx [I]; y = A [sum-1]. y + dy [I]; if (map [x] [y] = ''| map [x] [y] = 'O ') {movable [I] = 1; // set the value in the corresponding direction to 1} else {if (x! = A [0]. x | y! = A [0]. y) {movable [I] = 0; // set the value in the corresponding direction to 0} else {movable [I] = 1 ;}} if the value cannot go ;}}}} void check () {// check whether the food is dead or int x, y, I, j; x = A [sum-1]. x + dx [A [sum-1]. dir]; // records the coordinates of the snake header after moving A grid in the current direction y = A [sum-1]. y + dy [A [sum-1]. dir]; if (map [x] [y] = '*' | map [x] [y] = 'X ') {// if the character at the coordinate on the map is '*' or 'X', it will die if (X! = A [0]. x | y! = A [0]. y) {// except for snake tails, map [8] [4] = 'G'; map [8] [5] = 'a '; map [8] [6] = 'M'; map [8] [7] = 'E '; // output "game over" map [8] [9] = 'O'; map [8] [10] = 'V '; map [8] [11] = 'E'; map [8] [12] = 'R'; map [8] [8] = ''; system ("cls"); for (I = 0; I <17; I ++) {for (j = 0; j <17; j ++) {printf ("% c", map [I] [j]);} printf ("\ n ");} printf ("Input 'R' to restart \ nInput 'q' to quit \ n"); over = 1 ;}} else if (map [x] [y] = 'O '){// Eat apple sum ++; // The total length of the snake body plus 1 speed = (600-sum * 20)> 100 )? (600-sum * 20): 100; // speed up} void turn () {// turn int I, k = 0; for (I = 1; I <4; I ++) {// find the shortest direction from the food after taking a step. if (distance [k]> distance [I]) {k = I ;}} switch (k) {// change the direction of the snake header to this direction. case 0: A [sum-1]. dir = (A [sum-1]. dir = 2 )? 2: 0; break; case 1: A [sum-1]. dir = (A [sum-1]. dir = 3 )? 3:1; break; case 2: A [sum-1]. dir = (A [sum-1]. dir = 0 )? 0: 2; break; case 3: A [sum-1]. dir = (A [sum-1]. dir = 1 )?; Break ;}} void calculate () {// calculate and record the distance between the Snake Head and the food int I = 0, x, y; for (I = 0; I <4; I ++) {if (movable [I] = 1) {// if this direction can go, record the distance from the food after taking A step in this direction x = A [sum-1]. x + dx [I]; y = A [sum-1]. y + dy [I]; distance [I] = abs (foodx-x) + abs (foody-y );} else {// set the distance to 9999 distance [I] = 9999; }}} int main () {printf ("You only need to watch it quietly run \ n"); printf ("press any key to start \ n"); char ch = _ getch (); system ("cls"); ini (); show (); return 0 ;}