1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
// PacMan.cpp : main project file.
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
void instructions();
void playGame();
void initialize(char[][17]);
void printBoard(char[][17]);
//moving object class functions, class excluded for simplicity
bool isWall(char[][17],int,int);
void move_up(char[][17],char,int,int);
void move_down(char[][17],char,int,int);
void move_left(char[][17],char,int,int);
void move_right(char[][17],char,int,int);
bool didCollide(char);
int main()
{
int menuChoice = 9;
do
{
cout<<"Welcoe to the Pac-Man game!\n";
cout<<"To read the rules, enter 1.\n";
cout<<"To play the game, enter 2.\n";
cout<<"To exit, enter 0.\n";
cin>>menuChoice;
if(!cin)
{
cin.clear();
cin.ignore(200, '\n');
cout<<"\n\nInvalid input found, game restarting.\n\n\n";
menuChoice = 9;
}
switch(menuChoice)
{
case 1:
instructions();
break;
case 2:
playGame();
break;
case 0:
break;
}
}while(menuChoice != 0);
return 0;
}
void instructions()
{
cout<<"\nPac man, represented by P, has only one goal and that is\n";
cout<<"to eat all the dots. Ghosts will be chasing you along the way\n";
cout<<"trying to stop you (B - blinky, C - clyde, I - inky, K - pinky),\n";
cout<<"don't run into them!. You can also eat a power dot (+) which makes\n";
cout<<"the ghosts edible for Pac man. Use w, a, s, d, to move.\n";
cout<<"Good luck and happy eating!\n\n";
}
void initialize(char board[][17])
{
int row, col;
for(row = 0; row < 17; row++)
for(col = 0; col < 17; col++)
board[row][col] = '*';
//create walls and borders
for(row = 0; row < 17; row ++)
{
board[row][0] = 'X';
board[row][16] = 'X';
board[0][row] = 'X';
board[16][row] = 'X';
}
board[1][1] = '+'; board[1][15] = '+'; board[15][1] = '+', board[15][15] = '+';
board[1][5] = 'X'; board[1][11] = 'X'; board[2][2] = 'X';
board[2][3] = 'X'; board[2][5] = 'X'; board[2][7] = 'X';
board[2][8] = 'X'; board[2][9] = 'X'; board[2][11] = 'X';
board[2][13] = 'X'; board[2][14] = 'X'; board[3][2] = 'X';
board[3][3] = 'X'; board[3][13] = 'X'; board[3][14] = 'X';
board[4][4] = 'X'; board[4][6] = 'X'; board[4][7] = 'X';
board[4][8] = 'X'; board[4][9] = 'X'; board[4][10] = 'X';
board[4][12] = 'X'; board[5][2] = 'X'; board[5][14] = 'X';
board[6][2] = 'X'; board[6][4] = 'X'; board[6][6] = 'X';
board[6][8] = 'X'; board[6][10] = 'X'; board[6][12] = 'X';
board[6][14] = 'X'; board[7][2] = 'X'; board[7][4] = 'X';
board[7][6] = 'X'; board[7][10] = 'X'; board[7][12] = 'X';
board[7][14] = 'X'; board[8][2] = 'X'; board[8][4] = 'X';
board[8][6] = 'X'; board[8][8] = '@'; board[8][10] = 'X';
board[8][12] = 'X'; board[8][14] = 'X'; board[9][2] = 'X';
board[9][4] = 'X'; board[9][6] = 'X'; board[9][10] = 'X';
board[9][12] = 'X'; board[9][14] = 'X'; board[11][1] = 'X';
board[11][3] = 'X'; board[11][5] = 'X'; board[11][6] = 'X';
board[11][7] = 'X'; board[11][8] = 'X'; board[11][9] = 'X';
board[11][10] = 'X'; board[11][11] = 'X'; board[11][13] = 'X';
board[11][15] = 'X'; board[12][1] = 'X'; board[12][3] = 'X';
board[12][13] = 'X'; board[12][15] = 'X'; board[13][5] = 'X';
board[13][6] = 'X'; board[13][7] = 'X'; board[13][9] = 'X';
board[13][10] = 'X'; board[13][11] = 'X'; board[14][2] = 'X';
board[14][3] = 'X'; board[14][7] = 'X'; board[14][9] = 'X';
board[14][13] = 'X'; board[14][14] = 'X'; board[15][5] = 'X';
board[15][11] = 'X';
}
void printBoard(char board[][17])
{
int row, col;
cout<<endl;
for(row = 0; row < 17; row++)
{
for(col = 0; col < 17; col++)
{
cout<<setw(2)<<board[row][col];
}
cout<<endl;
}
cout<<endl;
}
//moving object class functions, class excluded for simplicity
bool isWall(char board[][17], int row, int col)
{
bool wall = false;
if(board[row][col] == 'X')
wall = true;
return wall;
}
void move_up(char board[][17], char object, int row, int col)
{
board[row][col] = ' ';
board[row--][col] = object;
}
void move_down(char board[][17], char object, int row, int col)
{
board[row][col] = ' ';
board[row++][col] = object;
}
void move_left(char board[][17], char object, int row, int col)
{
board[row][col] = ' ';
board[row][col--] = object;
}
void move_right(char board[][17], char object, int row, int col)
{
board[row][col] = ' ';
board[row][col++] = object;
}
bool didCollide(char object)
{
if(object == 'P')
return true;
}
//main game loop functions
void playGame()
{
//win loss variables player location
int dotCount = 147, livesLeft = 2, Prow = 12, Pcol = 8;
//blinky location clyde location
int Brow = 7, Bcol = 8, Crow = 7, Ccol = 8;
//inky location pinky location
int Irow = 7, Icol = 8, Krow = 7, Kcol = 8;
char board[17][17];
char input;
bool wall;
char blinky = 'B', clyde = 'C', inky = 'I', pinky = 'K', player = 'P';
//blinky chases, pinky ambushes, inky is unpredictable, clyde is slow
initialize(board);
board[12][8] = 'P';
board[7][8] = 'B';
//main game loop
do
{
cin.clear();
cin.ignore(200, '\n');
printBoard(board);
cout<<"Next move: ";
cin>>input;
cout<<endl;
switch(input)
{
case 'a':
wall = isWall(board,Prow,Pcol-1);
if(!wall)
{
move_left(board,' ',Prow, Pcol);
Pcol--;
move_left(board,'P',Prow, Pcol);
}
break;
case 'w':
wall = isWall(board,Prow-1,Pcol);
if(!wall)
{
move_up(board,' ',Prow, Pcol);
Prow--;
move_up(board,'P',Prow, Pcol);
}
break;
case 's':
wall = isWall(board,Prow+1,Pcol);
if(!wall)
{
move_down(board,' ',Prow, Pcol);
Prow++;
move_down(board,'P',Prow, Pcol);
}
break;
case 'd':
wall = isWall(board,Prow,Pcol+1);
if(!wall)
{
move_right(board,' ',Prow, Pcol);
Pcol++;
move_right(board,'P',Prow, Pcol);
}
break;
}
printBoard(board);
}while((dotCount > 0) && (livesLeft > 0));
}
|