Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <string>
- #include <conio.h>
- #include <time.h>
- #include <cstdlib>
- using namespace std;
- HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
- // Keyboard input
- #define KEY_W 119
- #define KEY_A 97
- #define KEY_S 115
- #define KEY_D 100
- //--------------------
- // Variables
- int const columns=10,rows=10,maxgoals=(columns+rows)/10; // Game Parameters
- int goals;
- int grid[columns][rows];
- int X,Y; // Grid Counters
- int PX,PY; // Player Coordinates
- int GX[maxgoals],GY[maxgoals]; // Goal Coordinates
- int i;
- int wall,WX,WY,LR; // Wall Variables
- int DX[rows/2],DY[rows/2],d; // Door Variables
- void walls(); // Function for generating walls. (declaration)
- void draw() // Function for drawing the board
- {
- cout<<endl;
- for ( Y=0 ; Y<rows ; Y++ )
- {
- for ( X=0 ; X<columns ; X++ )
- {
- cout<<" ";
- if ( grid [Y][X] == 0) // Empty Spots
- {
- if ( ( grid [Y+1][X] == grid [PY][PX] ) || ( grid [Y-1][X] == grid [PY][PX] ) || ( grid [Y][X+1] == grid [PY][PX] ) || ( grid [Y][X-1] == grid [PY][PX] ) )
- SetConsoleTextAttribute(h, 7);
- else
- SetConsoleTextAttribute(h, 0);
- cout<<".";
- }
- else if ( grid [Y][X] == 1) // The Player
- {
- SetConsoleTextAttribute(h, 11);
- cout<<"@";
- SetConsoleTextAttribute(h, 10);
- }
- else if ( grid [Y][X] == 2) // Goal
- {
- if ( ( grid [Y+1][X] == grid [PY][PX] ) || ( grid [Y-1][X] == grid [PY][PX] ) || ( grid [Y][X+1] == grid [PY][PX] ) || ( grid [Y][X-1] == grid [PY][PX] ) )
- SetConsoleTextAttribute(h, 14);
- else
- SetConsoleTextAttribute(h, 0);
- cout<<"$";
- SetConsoleTextAttribute(h, 10);
- }
- else if ( grid [Y][X] == 3) // Walls
- {
- SetConsoleTextAttribute(h, 8);
- cout<<"+";
- SetConsoleTextAttribute(h, 10);
- }
- else if ( grid [Y][X] == 4) // Ladder
- cout<<"H";
- else if ( grid [Y][X] == 5) // Closed Door
- {
- SetConsoleTextAttribute(h, 4);
- cout<<"X";
- SetConsoleTextAttribute(h, 10);
- }
- else if ( grid [Y][X] == 6) // Open Door
- {
- SetConsoleTextAttribute(h, 4);
- cout<<"/";
- SetConsoleTextAttribute(h, 10);
- }
- }
- cout<<endl;
- }
- SetConsoleTextAttribute(h, 15);
- cout<<"\n You have to collect "<<goals<<" more!"<<endl;
- }
- int main()
- {
- SetConsoleTextAttribute(h, 10);
- walls();
- srand(time(NULL));
- do
- {
- Y=rand()%rows;
- X=rand()%3;
- }
- while (grid[Y][X]!=0);
- grid[Y][X]=1;
- PX=X;
- PY=Y;
- while ( goals < maxgoals )
- {
- do
- {
- Y=rand()%rows;
- X=rand()%columns;
- }
- while (grid[Y][X]!=0);
- grid[Y][X]=2;
- GX[goals]=X;
- GY[goals]=Y;
- goals++;
- }
- _sleep(800);
- system("cls");
- while ( goals > 0 )
- {
- draw();
- cout<<"\n You are the ";
- SetConsoleTextAttribute(h, 12);
- cout<<"'@'";
- SetConsoleTextAttribute(h, 15);
- cout<<", you are supposed to collect the goals signed with ";
- SetConsoleTextAttribute(h, 14);
- cout<<"'$'"<<endl;
- SetConsoleTextAttribute(h, 15);
- cout<<"\n Which way do you want to go? (w for up, a for left, s for Down and d for right)"<<endl;
- char key=getch();
- int value=key;
- switch (value)
- {
- case KEY_W:
- if ( ((PY-1) != (rows-rows-1)) && ( grid[PY-1][PX]!=3) )
- {
- if (grid[PY-1][PX]!=5)
- {
- if (grid[PY][PX]==grid[DY[d]][DX[d]])
- grid[PY][PX]=6;
- else
- grid[PY][PX]=0;
- grid[PY-1][PX]=1;
- PY-=1;
- }
- else if (grid[PY-1][PX]==5)
- grid[PY-1][PX]=6;
- }
- break;
- case KEY_A:
- if ( ((PX-1) != (columns-columns-1)) && ( grid[PY][PX-1]!=3) )
- {
- if (grid[PY][PX-1]!=5)
- {
- if (grid[PY][PX]==grid[DY[d]][DX[d]])
- grid[PY][PX]=6;
- else
- grid[PY][PX]=0;
- grid[PY][PX-1]=1;
- PX-=1;
- }
- else if (grid[PY][PX-1]==5)
- grid[PY][PX-1]=6;
- }
- break;
- case KEY_S:
- if ( ((PY+1) != (rows)) && ( grid[PY+1][PX]!=3) )
- {
- if (grid[PY+1][PX]!=5)
- {
- if (grid[PY][PX]==grid[DY[d]][DX[d]])
- grid[PY][PX]=6;
- else
- grid[PY][PX]=0;
- grid[PY+1][PX]=1;
- PY+=1;
- }
- else if (grid[PY+1][PX]==5)
- grid[PY+1][PX]=6;
- }
- break;
- case KEY_D:
- if ( ((PX+1) != (columns)) && ( grid[PY][PX+1]!=3) )
- {
- if (grid[PY][PX+1]!=5)
- {
- if (grid[PY][PX]==grid[DY[d]][DX[d]])
- grid[PY][PX]=6;
- else
- grid[PY][PX]=0;
- grid[PY][PX+1]=1;
- PX+=1;
- }
- else if (grid[PY][PX+1]==5)
- grid[PY][PX+1]=6;
- }
- break;
- default:
- cout<<" Invalid input. Only lowercase w,a,s and d inputs are valid. Press any key to continue.";
- _getch();
- break;
- }
- while (i<maxgoals)
- {
- if ( ( PX == GX[i] ) && ( PY == GY[i] ) )
- {
- GX[i]=0;
- GY[i]=0;
- goals--;
- }
- i++;
- }
- i=0;
- system("cls");
- }
- cout<<" You win! Congratulations!"<<endl;
- _getch();
- return 0;
- }
- void walls() // Function for generating walls . (definition)
- {
- srand(time(NULL));
- WX=rand()%(columns-7)+(columns/5+1);
- grid[WY][WX]=3;
- grid[WY+1][WX]=3;
- grid[WY+2][WX]=3;
- WY+=3;
- wall+=3;
- while (wall <= rows)
- {
- if (WX >= (columns-3))
- LR=rand()%(2)+(-1);
- else if (WX <= 3)
- LR=rand()%(3);
- else
- LR=rand()%(3)+(-1);
- if (wall==columns)
- LR=0;
- if (LR==0)
- {
- grid[WY][WX]=3;
- DY[d]=WY-1;
- DX[d]=WX;
- d++;
- grid[WY+1][WX]=3;
- WY+=2;
- wall+=2;
- }
- else if (LR==(-1))
- {
- grid[WY-1][WX-1]=3;
- DY[d]=WY;
- DX[d]=WX-1;
- d++;
- grid[WY][WX-1]=3;
- grid[WY+1][WX-1]=3;
- WX-=1;
- WY+=2;
- wall+=2;
- }
- else if (LR==(1))
- {
- grid[WY-1][WX+1]=3;
- DY[d]=WY;
- DX[d]=WX+1;
- d++;
- grid[WY][WX+1]=3;
- grid[WY+1][WX+1]=3;
- WX+=1;
- WY+=2;
- wall+=2;
- }
- }
- d=rand()%((rows/2)-1);
- grid[DY[d]][DX[d]]=5;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement