Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Easy Snake! Why is it easy? Because you have to hit enter to move the snake!
- //TRY ME
- //
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- const int ROWLENGTH=28, SELECTIONS=1, EsL=17; //EASYSNAKE_BOX_SIDE_LENGTH must be odd
- struct twoints {int x; int y;} coords[EsL*EsL];
- int display_menu(), display_menu_input(), movesnake(int& currentcoordinate), counter=-1;
- void objective(), easysnake(), header(), cls(), easysnakeheader(), easysnakestartscreen(), easysnakedirections(), easysnake_start_coordinates(), printeasysnake();
- int main()
- {
- srand (time(NULL));
- do
- {
- int subprogram=display_menu();
- switch(subprogram)
- {
- case 1:
- easysnake();
- break;
- }
- cls();
- } while(1);
- return 0;
- }
- void easysnake()
- {
- cls();
- for (int its=0;its<EsL*EsL;its++)
- {
- coords[its].x=0;
- coords[its].y=0;
- }
- easysnakestartscreen();
- easysnake_start_coordinates();
- int currentcoordinate=static_cast<int>((EsL*EsL)/2)+1;
- do
- {
- objective();
- printeasysnake();
- int quit=movesnake(currentcoordinate);
- if (quit==1)
- break;
- }while(1);
- counter=0;
- return;
- }
- int movesnake(int ¤tcoordinate)
- {
- char wasd;
- int tempcoord;
- tempcoord=currentcoordinate;
- coords[currentcoordinate].x=0;
- int validity;
- do
- {
- cin >> wasd;
- if (wasd=='0')
- return 1;
- if (wasd=='a' || wasd == 'A')
- {
- if (tempcoord%EsL==0)
- return 1;
- currentcoordinate=tempcoord-1;
- coords[currentcoordinate].x=1;
- if (coords[currentcoordinate].y>0)
- return 1;
- coords[currentcoordinate].y=counter+2;
- validity=0;
- if (currentcoordinate<0)
- return 1;
- }
- else if (wasd=='d' || wasd == 'D')
- {
- currentcoordinate=tempcoord+1;
- coords[currentcoordinate].x=1;
- if (coords[currentcoordinate].y>0)
- return 1;
- coords[currentcoordinate].y=counter+2;
- validity=0;
- if (currentcoordinate>289)
- return 1;
- if (currentcoordinate%EsL==0)
- return 1;
- }
- else if (wasd=='w' || wasd == 'W')
- {
- if (tempcoord<17)
- return 1;
- currentcoordinate=tempcoord-EsL;
- coords[currentcoordinate].x=1;
- if (coords[currentcoordinate].y>0)
- return 1;
- coords[currentcoordinate].y=counter+2;
- validity=0;
- }
- else if (wasd=='s' || wasd == 'S')
- {
- currentcoordinate=tempcoord+EsL;
- coords[currentcoordinate].x=1;
- if (coords[currentcoordinate].y>0)
- return 1;
- coords[currentcoordinate].y=counter+2;
- validity=0;
- if (currentcoordinate>289)
- return 1;
- }
- else if (wasd=='0')
- return 1;
- else
- {
- cout << "Invalid input. Enter w, a, s or d. Enter 0 to quit.\n";
- validity=1;
- }
- }while(validity!=0);
- for (int its=0; its<=EsL*EsL; its++)
- if (coords[its].y>0)
- coords[its].y--;
- cls();
- }
- void objective()
- {
- for (int its=0; its<EsL*EsL;its++)
- if (coords[its].x==2)
- return;
- int randomint=1;
- do
- {
- randomint=rand()%(EsL*EsL);
- if (coords[randomint].x==1)
- randomint=1;
- }while(randomint==1);
- coords[randomint].x=2;
- counter++;
- return;
- }
- void printeasysnake()
- {
- easysnakeheader();
- char ch=254;
- cout << ch;
- for (int its=1; its<=EsL*EsL;its++)
- {
- if (coords[its-1].y>=1&&!(coords[its-1].x==1))
- cout << "o";
- if (!(coords[its-1].x)&&coords[its-1].y==0)
- cout << " ";
- else if (coords[its-1].x==1)
- cout << "0";
- else if (coords[its-1].x==2)
- cout << "*";
- if (its==EsL*EsL)
- cout << ch << endl;
- else if (its==EsL)
- {
- cout << ch;
- if (counter>=1)
- cout << " P";
- cout << "\t[ *\'s eaten: ]"<< endl << ch;
- }
- else if (its==EsL*2)
- {
- cout << ch;
- if (counter>=2)
- cout << " R";
- cout <<"\t[" << setw(8) << counter << " ]" << endl << ch;
- }
- else if (its==EsL*3)
- {
- cout << ch ;
- if (counter >=3)
- cout << " O";
- cout << endl << ch;
- }
- else if (its==EsL*4)
- {
- cout << ch ;
- if (counter >=4)
- cout << " G";
- cout << endl << ch;
- }
- else if (its==EsL*5)
- {
- cout << ch ;
- if (counter >=5)
- cout << " R";
- cout << endl << ch;
- }
- else if (its==EsL*6)
- {
- cout << ch ;
- if (counter >=6)
- cout << " A";
- cout << endl << ch;
- }
- else if (its==EsL*7)
- {
- cout << ch ;
- if (counter >=7)
- cout << " M";
- cout << endl << ch;
- }
- else if (its==EsL*8)
- {
- cout << ch ;
- if (counter >=8)
- cout << " M";
- cout << endl << ch;
- }
- else if (its==EsL*9)
- {
- cout << ch ;
- if (counter >=9)
- cout << " I";
- cout << endl << ch;
- }
- else if (its==EsL*10)
- {
- cout << ch ;
- if (counter >=10)
- cout << " N";
- cout << endl << ch;
- }
- else if (its==EsL*11)
- {
- cout << ch ;
- if (counter >=11)
- cout << " G";
- cout << endl << ch;
- }
- else if (its==EsL*12)
- {
- cout << ch ;
- if (counter >=12)
- cout << " ";
- cout << endl << ch;
- }
- else if (its==EsL*13)
- {
- cout << ch ;
- if (counter >=13)
- cout << " I";
- cout << endl << ch;
- }
- else if (its==EsL*14)
- {
- cout << ch ;
- if (counter >=14)
- cout << " N";
- cout << endl << ch;
- }
- else if (its==EsL*15)
- cout << ch << endl << ch;
- else if (its==EsL*16)
- {
- cout << ch ;
- if (counter >=16)
- cout << " C++";
- cout << endl << ch;
- }
- else if (its==EsL*17)
- cout << ch << endl << ch;
- else if (its%EsL==0 && its!=0)
- cout << ch << endl << ch;
- }
- easysnakeheader();
- }
- void easysnake_start_coordinates()
- {
- coords[static_cast<int>((EsL*EsL)/2)+1].x=1;
- coords[static_cast<int>((EsL*EsL)/2)+1].y=1;
- }
- void easysnakedirections()
- {
- cout << "You control the snake. \nUse WASD to control the snake.\nGuide the snake to the objectives!\nEnter 0 to quit.\n";
- }
- void easysnakestartscreen()
- {
- easysnakedirections();
- easysnakeheader();
- for (int iterations=0; iterations<EsL;iterations++)
- {
- char ch=254;
- cout << ch;
- for (int iterations=0; iterations<EsL;iterations++)
- {
- cout << " ";
- }
- cout << ch << endl;
- }
- easysnakeheader();
- char pause;
- cout << "Enter any key to continue\n";
- cin >> pause;
- }
- void easysnakeheader()
- {
- char ch=254;
- for (int iterations=0;iterations<EsL+2; iterations++)
- cout << ch;
- cout << endl;
- }
- void cls()
- {
- for (int iterations=0;iterations<=20;iterations++)
- cout << endl;
- }
- int display_menu()
- {
- header();
- cout << "Enter a menu choice.\n";
- cout << "\t1: Joe's Easy Snake\n";
- cout << "\t" << SELECTIONS+1 <<": EXIT\n";
- header();
- int selection=display_menu_input();
- return selection;
- }
- int display_menu_input()
- {
- char input;
- int inputasint;
- cin >> input;
- inputasint=input;
- while (inputasint<49 || inputasint>49+SELECTIONS)
- {
- cout << "Bad input; enter between 1 and " << SELECTIONS+1 << endl;
- cin >> input;
- inputasint=input;
- }
- if (inputasint==49+SELECTIONS)
- exit (EXIT_SUCCESS);
- return inputasint-48;
- }int validity;
- void header()
- {
- for (int iterations=0; iterations<ROWLENGTH; iterations++)
- cout << "=";
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment