# C / C + + Programming Notes: C language greedy Snake source code console (1), will move the kind of Oh!

Time：2020-10-29

A few days ago, a classmate added my QQ to chat privately. I said that their teacher arranged a greedy snake. He didn’t know how to write it, so he came to me to solve it. I gave him a brief explanation of the train of thought and some difficulties. After that, he was able to complete the project independently! Considering that more students may have the problem of greedy snake, I will write the solution today when I have time. I will teach you to write a greedy Snake game in several steps. As you may not have finished the C language, this tutorial only involves arrays and functions and other knowledge points. ## 1、 Drawing a map

First, we use the macro definition to define two constants, the height (H) and the width (W) of the map

#defineH 20

#defineW 20

The advantage of this method is that if you change the size of the map in the future, you don’t need to change the written code, just modify the macro definition. It saves time and avoids bugs

Next, we define a two-dimensional array. Each value of the two-dimensional array corresponds to each point in the plane, so that we can easily customize the map. You can set up obstacles or something. Then we use the array a [H] [w] to store the map,If a [i] [J] = = 0, it means that row I + 1 and column j + 1 are empty. If a [i] [J] = = 1, it means that this point is an obstacle

Then we use the following code to make the edge of the array equal to 1. (the int global variable defaults to 0). This is just the simplest map. If you have other ideas, you can make your own map.

The map is done, the rest is to draw the map. We also write the process of drawing maps as a function, which makes the code structure clearer and easier to find bugs. Drawing a map is simple, that is, traversing the entire array, and then outputting a space where the value is 0, and outputting a symbol where the value is 1. According to my own preference, I use ා. Then we find that the length and width are not suitable. We can modify the initial values of H and W.

#defineH 23

#defineW 75

And see if it looks better than before. See the benefits of macro definition. Well, so far, we’ve learned to draw maps.

This is just the simplest map. By modifying the map array, we can make many different maps.

The complete code for the first step is attached below.  ## 2、 Drawing snakes

The body of a snake is just some points. We can store the coordinates of these points in a two-dimensional array.

First declare a two-dimensional array large enough. Although I don’t think you can play that long.

int s[H*W];

In this way, s   is the I coordinate of the snake head, and s   is the j coordinate of the snake head. S [i]  represents the i-coordinate of the I + 1 point of the snake. S [i] .

Then declare an int variable to store the length of the snake. Then put these initialization operations into the init function above.

Int slength; / / length of snake

Slength = 4; / / let the snake’s initial length be 4

Then give the initial coordinates of the snake Then we draw the snake. I have a function here (Baidu), which can move the cursor to the specified point. Note: to add the header file windows. H

As follows: Then you can draw a map first, and then draw a snake on the map by moving the cursor. OK, let’s see the effect The snake does come out, but the cursor is in the way. Add the following code at the beginning of the init function to hide the cursor

CONSOLE_CURSOR_INFO cursor_info = {1, 0};

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); There is no cursor.

Code so far.   Let’s get here today! Don’t worry, I will share the rest of the snake in the next article!

WeChat official account: C programming language learning base, learning C/C++ programming knowledge, want to be a more excellent programmer, or you learn C/C++ difficult, you can come to the C language C++ zero base programming learning Q circle 9541726787 grow together!

## Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]