# Include <iostream> # include <windows. h> # include <stdlib. h> # include <conio. h> # include <time. h> // use the current time as the seed Enum dir {up, down, left, right}; // Enumeration type Enum dir // wall class fence {public: void initfence (); void outputf (); Public: Char game [20] [20];} f; // defines the object // void fence: lnitfence () {for (INT I = 0; I <20; I ++) for (Int J = 0; j <20; j ++) {if (I = 0 | I = 19 | j = 0 | j = 19) game [I] [J] = '*'; else game [I] [J] = '';} // display Box void fence: outputf () {for (INT I = 0; I <20; I ++) {for (Int J = 0; j <20; j ++) cout <game [I] [J] <''; cout <Endl ;}// class snakenode {PRIVATE: int X, Y; snake kenode * Prior, * Next; public: void add_head (int x, int y) int get_x (); int get_y (); void delete_tail ();} * head = NULL, * tail = NULL; // Insert the header node void snail kenode: add_head (int x, int y) {snail kenode * q = new snail kenode; q-> X = X; q-> Y = y; q-> next = head; q-> prior = NULL; If (head) Head-> prior = Q; head = Q; If (! Tail) tail = head; F. game [x] [Y] = '*'; // The F object can be defined when the fence class is defined; and The fnce class is defined before the snkenode class} int snkenode: get_x () {return X;} int snkenode: get_y () {return y;} // Delete the End Node void snkenode: delete_tail () {snkenode * P = tail; F. game [tail-> get_x ()] [tail-> get_y ()] = ''; // set '*' in the coordinates of the End Node to a space; if (tail = head) tail = head = NULL; else {tail = tail-> prior; tail = next = NULL;} Delete P;} class move {public: dir point; // enumerated variable point: Control direction int F Ood_x; int food_y; public: void moving (); void change_point (char); // change the direction; void get_food () ;}; void move: Moving () {int A, B; A = head-> get_x (); // obtain the horizontal coordinate of the header Node B = head-> get_y (); // obtain the vertical coordinate switch (point) of the header Node) {Case up: -- A; break; case down: ++ A; break; case left: -- B; break; case right: ++ B; break ;} if (A = 19 | B = 19 | A = 0 | B = 0 | game [a] [B] = '*') // determine whether to hit the wall {cout <"gameover !!! "<Endl; exit (0);} if (a = food_x & B = food_y) // eat food {head-> add_head (A, B ); get_food;} else {head-> add_head (a, B); // Insert the head node head-> delete_tail (); // Delete the End Node} void move :: change_point (char keydown) {Switch (keydown) {Case 'W': Point = up; break; Case 's': Point = down; break; Case 'A ': point = left; break; Case 'D': Point = right; break ;}} void move: get_food () {srand (unsigned INT) Time (null )); // seed (program run Row time) food_x = rand () % 18 + 1; food-y = rand () % 18 + 1; F. game [food_x] [food_y] = '*';} int main () {cout <"using 'W, A, S, D' to control direction !!! \ N "; // draw box and snake; move m; F. lnitfence (); head-> add_head (4, 3); head-> add_head (4, 4); head-> add_head (4, 5); M. get_food (); F. outputf (); While (ture) {char keydown = getch (); // getch () returns M. chan ge_point (keydown); While (! Kbhit () // determines whether a key is missing {system ("CLS"); // The Screen clearing function; M. moving (); F. outputf (); sleep (200) ;}} return 0 ;}
Error display:
This is what I saw on the Internet. It took more than half an hour to figure out the source image, but there are still a lot of problems. The online stuff is really untrustworthy. It seems that most of them are in C language, if you have time, please help me change it to C ++, find out the error and correct it. Thank you. If you are a newbie, please take care of it...
The code of the greedy Snake game cannot be run. Please help me modify it!