Greedy Snake C language code

Source: Internet
Author: User
Tags abs clear screen rand
greedy snake C language code

Greedy Snake C language code manual greedy snake Intelligent greedy snake

1. Manual Greedy Snake

* The faster the snake runs, the quicker it can run.////////* Author: sgafpz*/#include <stdio.h> #include <windows.h> #include <stdlib.h> #include < math.h>//#include <unistd.h> #include <conio.h> #include <string.h> #include <time.h> 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, 0123 respectively indicates the top and bottom, the same as//int distance[4] = {9999, 9999, 9999, 9999};
int dy[4] = {0, 1, 0,-1};
int dx[4] = {-1, 0, 1, 0};  int sum = 5;
Snake total length int over = 0;   
int speed;  int Foodx, foody;

Food coordinates char MAP[17][17];  struct Snake {int x, y;  body coordinates int dir;

direction (only the direction of the snake Head is useful)} a[100];
    void Ini () {//Initialize 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 < i++) {//Initialize snake head and snake body data a[i].dir = 0; A[i]. x = 0;
    A[I].Y = 0; } a[0].x = 1;  A[0].Y = 1; In the upper left corner of the map, a snake a[1].x = 1 with a length of 5 is set.
    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 < i++) {//Set map for (j = 0; J <; J + +) {Map[i][j] = ' * ';
        (i = 1; i < i++) {for (j = 1; J < + 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 Snake head for (i = 0; i < sum-1 i++) {//Set 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 < i++) {//Display interface for (j = 0; J <; 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 done after the snake is dead while (1) {Char ch = _getch ();
                if (ch = = 113) {//input ' Q ' end return;
                    else if (ch = = 114) {//Enter ' R ' to restart INI ();
                Break  } system ("CLS"); Clear screen for (i = 0; i < i++) {//re-display interface for (j = 0; J <; J +) {printf ("%c
            ", Map[i][j]);
        printf ("\ n"); }//calculate ();
    Calculate and record the snake head and Food distance}} void food () {//Generate food int x, y;  while (1) {x = (int) (* rand ()/(Rand_max + 1.0));
        Randomly produces a set of food coordinates y = (int) (rand ()/(Rand_max + 1.0));
            if (map[x][y] = = ') {//If the space is the place where the food is generated map[x][y] = ' O ';  FOODX = x;
            Record food coordinates//foody = y;
        Break
    }} void Move () {//Snake moves int i, x, y; int t = Sum;  T record current snake total length check (); Check before moving a step in the current direction if (t = = sum) {//No Apple for (i = 0; i < sum-1; i++) {if (i = 0) {/
                /snake tail coordinates into a space, the snake tail coordinates into a former snake body coordinates map[a[i].x][a[i].y] = ';
                a[i].x = A[i + 1].x;
            A[I].Y = A[i + 1].y;
                else {//each of the body coordinates becomes 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 in the current direction a lattice A[sum-1].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 head on the map to ' H '} else {//eat apple (sum + 1) map[a[sum-2].x][a[sum-2].y] = ' x ';  Set the character of the original snake head coordinates 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].y = a[sum-2].y + Dy[a[sum-2].dir] of the original snake head moving one lattice along the current direction; 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 head on the map to ' H ' food ();
        }/*for (i = 0; i < 4; i++) {//record the direction of walking 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;  If you can go, set the value of the corresponding direction to 1} else {if (x!= a[0].x | | y!= a[0].y) {movable[i] = 0;
            Can not go on the corresponding direction of the value set to 0} else {movable[i] = 1;
    }}*/} void Check () {//Check for death or eat food int x, y, I, J;  x = a[sum-1].x + Dx[a[sum-1].dir];
    Record the coordinates of the snake head 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 on the map is ' * ' or ' x ', the death if (x!= a[0].x | | y!= a[0].y) {//Snake tail excepted 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 < n i++) {for (j = 0; J <; J +) {printf ("%c", Map[i][j]);
            printf ("\ n");
            printf ("Input ' R ' to Restart\ninput ' Q ' to quit\n");
        over = 1;  Or else if (map[x][y] = = ' O ') {//eat Apple sum++; Snake body total length plus 1 speed = ((600-sum *) >100)? (600-sum * 20): 100;  Speed up} void Turn () {//Turn if (_kbhit ()) {char dir = _getch ();
        Read Input key switch (dir) {//change direction case 119:a[sum-1].dir = (A[sum-1].dir = = 2)? 2:0; Case 100:a[sum-1].dir = (A[sum-1].dir = = 3)? 3:1;
        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)? 1:3;
        Break
    The/*void Calculate () {//}} calculates and records the distance between the head and the food int i = 0, x, y; for (i = 0; i < 4; i++) {if(Movable[i] = = 1)
            {//If the direction is available, record the distance from the food after taking a step along that direction x = a[sum-1].x + dx[i];
            y = a[sum-1].y + dy[i];
        Distance[i] = ABS (foodx-x) +abs (foody-y);
        else {//If you can't go, set the distance to 9999 distance[i] = 9999;
    }}*/int main () {printf ("' W ' s '" a ' "d ') control up and down, the faster the snake runs ~~~\n");
    printf ("Start \ n" by any key);
    char ch = _getch ();
    System ("CLS");
    INI ();
    Show ();
return 0;
 }
2. Intelligent greedy Snake

Although it is intelligent, it may not be as intelligent as you think.
The basic idea is to follow the direction of the upper, right, down and left order, so that the way forward in this direction can be close to food, the progress of the obstacles will automatically bypass, but can not avoid the snake head surrounded by the situation.

* The faster the snake runs, the quicker it can be run///////////* Author: sgafpz*/#include <stdio.h> #include <windows.h> #include <stdlib.h> # Include <math.h>//#include <unistd.h> #include <conio.h> #include <string.h> #include <
time.h> 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, 0123 respectively representing the top and bottom, the same as int distance[4] = {9999, 9999, 9999, 9999};
int dy[4] = {0, 1, 0,-1};
int dx[4] = {-1, 0, 1, 0};  int sum = 5;
Snake total length int over = 0;   
int speed;  int Foodx, foody;

Food coordinates char MAP[17][17];  struct Snake {int x, y;  body coordinates int dir;

direction (only the direction of the snake Head is useful)} a[100];
    void Ini () {//Initialize 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 < i++) {//Initialize snake head and snake body data a[i].dir = 0; A[i]. x = 0;
    A[I].Y = 0; } a[0].x = 1;  A[0].Y = 1; In the upper left corner of the map, a snake a[1].x = 1 with a length of 5 is set.
    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 < i++) {//Set map for (j = 0; J <; J + +) {Map[i][j] = ' * ';
        (i = 1; i < i++) {for (j = 1; J < + 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 Snake head for (i = 0; i < sum-1 i++) {//Set 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 < i++) {//Display interface for (j = 0; J <; 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 done after the snake is dead while (1) {Char ch = _getch ();
                if (ch = = 113) {//input ' Q ' end return;
                    else if (ch = = 114) {//Enter ' R ' to restart INI ();
                Break  } system ("CLS"); Clear screen for (i = 0; i < i++) {//re-display interface for (j = 0; J <; J +) {printf ("%c
            ", Map[i][j]);
        printf ("\ n"); } calculate ();
    Calculate and record the snake head and Food distance}} void food () {//Generate food int x, y;  while (1) {x = (int) (* rand ()/(Rand_max + 1.0));
        Randomly produces a set of food coordinates y = (int) (rand ()/(Rand_max + 1.0));
            if (map[x][y] = = ') {//If the space is the place where the food is generated map[x][y] = ' O ';  FOODX = x;
            Record food coordinates foody = y;
        Break
    }} void Move () {//Snake moves int i, x, y;  int t = sum; T recordCurrent Snake total length check (); Check before moving a step in the current direction if (t = = sum) {//No Apple for (i = 0; i < sum-1; i++) {if (i = 0) {/
                /snake tail coordinates into a space, the snake tail coordinates into a former snake body coordinates map[a[i].x][a[i].y] = ';
                a[i].x = A[i + 1].x;
            A[I].Y = A[i + 1].y;
                else {//each of the body coordinates becomes 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 in the current direction a lattice A[sum-1].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 head on the map to ' H '} else {//eat apple (sum + 1) map[a[sum-2].x][a[sum-2].y] = ' x ';  Set the character of the original snake head coordinates 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 of the original snake's head moving one grid along the current direction a[sum-1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.