Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cstdio>
- #include <ctime>
- #include <cmath>
- #include <fstream>
- #include <conio.h>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- ifstream fin ("pacman.txt");
- int height, it, j, its=0, endgame=0, pacrow, paccollumn, exitrow, exitcolumn, numofgems=0, collectedgems=0, monsters=0, quit=0, works=0, counter4=0, randommove;
- fin >> height;
- char moves;
- string level;
- string line[33];
- string lines[12];
- int monsterrow[100];
- int monstercolumn[100];
- for ( int i=0; i<32; i++)
- {
- fin >> line[i];
- }
- cout <<"What level do you want? Choose easy, medium, hard, or extreme."<< endl;
- cin >> level;
- if (level=="medium")
- {
- for (int i=0; i<9; i++)
- {
- lines[i]=line[i];
- height=9;
- }
- }
- else if (level=="hard")
- {
- for (int i=9; i<18; i++)
- {
- height=9;
- lines[its]=line[i];
- its++;
- }
- }
- else if (level=="easy")
- {
- for (int i=18; i<23; i++)
- {
- height=5;
- lines[its]=line[i];
- its++;
- }
- }
- else if (level=="extreme")
- {
- for (int i=23; i<32; i++)
- {
- height=9;
- lines[its]=line[i];
- its++;
- }
- }
- for (int i=0; i<height; i++)
- {
- for (int j=0; j<lines[i].length(); j++)
- {
- if (lines[i][j]=='P')
- {
- pacrow=i;
- paccollumn=j;
- }
- if (lines[i][j]=='E')
- {
- exitrow=i;
- exitcolumn=j;
- }
- if (lines[i][j]=='G')
- {
- numofgems++;
- }
- if (lines[i][j]=='M')
- {
- monsterrow[counter4]=i;
- monstercolumn[counter4]=j;
- counter4++;
- }
- }
- }
- cout <<"Collect all the gems and get to the exit without being killed!"<< endl;
- while (endgame==0 && monsters==0)
- {
- if (lines[pacrow][paccollumn]=='E' && collectedgems==numofgems)
- {
- endgame=1;
- break;
- }
- if (lines[pacrow][paccollumn]=='E'&& collectedgems!=numofgems)
- {
- cout <<"You need to get all the gems first. That's a loss for not following instructions."<< endl;
- break;
- }
- if (lines[pacrow][paccollumn]=='G')
- {
- collectedgems++;
- }
- if (lines[pacrow][paccollumn]=='M')
- {
- monsters=1;
- break;
- }
- lines[pacrow][paccollumn]='P';
- //monstermoving
- for (int i=0; i<counter4; i++)
- {
- /*
- cout <<"move M from"<<i<< endl;
- cout <<monsterrow[i]<< endl;
- cout <<monstercolumn[i]<< endl;
- for (int j=0; j<height; j++)
- {
- cout <<lines[j]<< endl;
- }
- cout << "to" <<endl;
- */
- //cout <<"hi"<< endl;
- works=0;
- while (works==0)
- {
- //cout <<"hi"<< endl;
- randommove=rand()%4+1;
- if (randommove==1)
- {
- //cout <<"hi"<< endl;
- if (lines[monsterrow[i]-1][monstercolumn[i]]=='.' || lines[monsterrow[i]-1][monstercolumn[i]]=='P')
- {
- //cout <<"Hi"<< endl;
- works=1;
- lines[monsterrow[i]][monstercolumn[i]]='.';
- lines[monsterrow[i]-1][monstercolumn[i]]='M';
- monsterrow[i]--;
- }
- }
- if (randommove==2)
- {
- if (lines[monsterrow[i]+1][monstercolumn[i]]=='.'|| lines[monsterrow[i]+1][monstercolumn[i]]=='P')
- {
- works=1;
- lines[monsterrow[i]][monstercolumn[i]]='.';
- lines[monsterrow[i]+1][monstercolumn[i]]='M';
- monsterrow[i]++;
- }
- }
- if (randommove==3)
- {
- if (lines[monsterrow[i]][monstercolumn[i]-1]=='.'|| lines[monsterrow[i]][monstercolumn[i]-1]=='P')
- {
- works=1;
- lines[monsterrow[i]][monstercolumn[i]]='.';
- lines[monsterrow[i]][monstercolumn[i]-1]='M';
- monstercolumn[i]--;
- }
- }
- if (randommove==4)
- {
- if (lines[monsterrow[i]][monstercolumn[i]+1]=='.'|| lines[monsterrow[i]][monstercolumn[i]+1]=='P')
- {
- works=1;
- lines[monsterrow[i]][monstercolumn[i]]='.';
- lines[monsterrow[i]][monstercolumn[i]+1]='M';
- monstercolumn[i]++;
- }
- }
- }
- //for (int j=0; j<height; j++)
- //{
- //cout <<lines[j]<< endl;
- //}
- }
- for (int i=0; i<height; i++)
- {
- cout <<lines[i]<< endl;
- }
- if (lines[pacrow][paccollumn]=='M')
- {
- cout <<"Pacman was eaten"<< endl;
- monsters=1;
- break;
- }
- cout <<"Type either w, a, s, d for up, left, down, or right, or q for quit. Warning: an illegal move will allow the monsters to move but not you."<< endl;
- moves=getch();
- if (moves=='w')
- {
- if (lines[pacrow-1][paccollumn]=='.'||lines[pacrow-1][paccollumn]=='E'||lines[pacrow-1][paccollumn]=='M' || lines[pacrow-1][paccollumn]=='G')
- {
- lines[pacrow][paccollumn]='.';
- pacrow--;
- }
- else
- cout <<"Illegal move!"<< endl;
- }
- else if (moves=='a')
- {
- if (lines[pacrow][paccollumn-1]=='.'||lines[pacrow][paccollumn-1]=='E'||lines[pacrow][paccollumn-1]=='M' || lines[pacrow][paccollumn-1]=='G')
- {
- lines[pacrow][paccollumn]='.';
- paccollumn--;
- }
- else
- cout <<"Illegal move!"<< endl;
- }
- else if (moves=='s')
- {
- if (lines[pacrow+1][paccollumn]=='.'||lines[pacrow+1][paccollumn]=='E'||lines[pacrow+1][paccollumn]=='M' || lines[pacrow+1][paccollumn]=='G')
- {
- lines[pacrow][paccollumn]='.';
- pacrow++;
- }
- else
- cout <<"Illegal move!"<< endl;
- }
- else if (moves=='d')
- {
- if (lines[pacrow][paccollumn+1]=='.'||lines[pacrow][paccollumn+1]=='E'||lines[pacrow][paccollumn+1]=='M'|| lines[pacrow][paccollumn+1]=='G')
- {
- lines[pacrow][paccollumn]='.';
- paccollumn++;
- }
- else
- cout <<"Illegal move!"<< endl;
- }
- else if (moves=='q')
- {
- cout <<"Quitters never prosper, but you can quit, I guess."<< endl;
- quit=1;
- break;
- }
- else
- {
- cout <<"Illegal move! Try again"<< endl;
- }
- }
- if (endgame==1)
- cout <<"You beat the game!"<< endl;
- else if (quit==1)
- cout <<"I hope you won't quit next time!"<< endl;
- else
- cout <<"Gameover! You lost."<< endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement