The code of the greedy Snake game cannot be run. Please help me modify it !, Greedy snake

Source: Internet
Author: User
Tags draw box

The code of the greedy Snake game cannot be run. Please help me modify it !, Greedy snake

# 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...


For the code of the greedy snake, it cannot be run in vc60. let me check it.

This program can only run in TC or BC.

The complete code of the C ++-based greedy Snake game can be run

A reference instance can be run successfully in VC ++ 6.0 (there may be problems)

# Include <iostream. h>
# 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}; // enum dir of Enumeration type;
// Wall;
Class Fence {
Public:
Void InitFence ();
Void OutputF ();
Public:
Char game [20] [20];
} F; // define the object;
// Draw a box;
Void Fence: InitFence (){
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;
}
}
// Snake node;
Class snail kenode {
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 a header node;
Void snkenode: 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 Fence 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 (){
Snail kenode * 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-... the remaining full text>

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.