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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
#include <iostream>
#include <stdio.h>
#include <stdlib.h>//rand srand getchar()
#include <time.h>//time NUZLL for rand
#include <string>//used in 1 line..
using namespace std;
const int ROWS = 4;
const int COLM = 10;
const char DEFAULT = '.';
const char Trap = 'T';
const char Treasure = 'X';
const char Mob = 'M';
const char Player = 'P';
//Function to fill gameboard with default
void InitializeBoard(char GameBoard[][COLM]);
//function to randomly place traps, pass to check function (posx posy symbol)
void PlaceGameObjects(char GameBoard[][COLM], int TrapCount, int &PlayerRow, int &MobRow, int &MobColm);
//print gameboard
void PrintBoard(char GameBoard[][COLM]);
void MovePlayer(char GameBoard[][COLM], int &PlayerRow, int &PlayerColm);//Pass by reference so that we dont have to call func twice
//Check if character moved into tile that will end game.
void MoveMob(char GameBoard[][COLM], int &MobRow, int &MobColm);
bool CheckGameState(char GameBoard[][COLM], int PlayerRow, int PlayerColm);
int main()
{
srand(time(NULL));//Seed random
char GameBoard[ROWS][COLM];
int TrapCount = 3;
int PlayerRow, PlayerColm;
int MobRow, MobColm;
PlayerRow = 0;//none of these are used until initialized.. worth having?
PlayerColm = 0;
MobRow = 0;
MobColm = 0;
InitializeBoard(GameBoard);
PlaceGameObjects(GameBoard, TrapCount, PlayerRow, MobRow, MobColm);
GameBoard[PlayerRow][PlayerColm] = Player; //character on first column.
//cout << string( 100, '\n' );//clear screen forces it to start at the bottom of the console from the getgo
do
{
PrintBoard(GameBoard);
MovePlayer(GameBoard,PlayerRow,PlayerColm);
MoveMob(GameBoard,MobRow, MobColm);
//cout << string( 100, '\n' );//clear screen
}while(CheckGameState(GameBoard, PlayerRow, PlayerColm) == true);
return 0;
}
//Function to fill gameboard with default
void InitializeBoard(char GameBoard[][COLM])
{
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLM; j++)
{
GameBoard[i][j] = DEFAULT;
}
}
}
//function to randomly place traps, pass to check function (posx posy symbol)
void PlaceGameObjects(char GameBoard[][COLM], int TrapCount, int &PlayerRow, int &MobRow, int &MobColm)
{
//Traps will not spawn on starting row of character or treasure
int RandomRow, RandomColm;
for(int i = 0; i < TrapCount; i++)
{
RandomRow = rand() % ROWS;
RandomColm = rand() % COLM;
//check if gameboard position not default - if not taken then fill position
if(RandomColm == 0 || RandomColm == 9 || GameBoard[RandomRow][RandomColm] != DEFAULT)//Prevent traps from appearing on column Character and Treasure spawns
{
//!= DEFAULT prevents Traps from being on top of each other.
i--;
continue;//Skips Below and loops
}
GameBoard[RandomRow][RandomColm] = Trap;
}
//place monster
bool IsMobPlaced = false;
while(IsMobPlaced == false)
{
RandomRow = RandomRow = rand() % ROWS;//out of if statement so we dont need another random above while loop
RandomColm = RandomColm = rand() % COLM;
if(GameBoard[RandomRow][RandomColm] == Trap || RandomColm == 0 || RandomColm == 9)
{
//Want it to random again, so we loop and dont set mob placed so it stays false
}
else
{
GameBoard[RandomRow][RandomColm] = Mob;
MobRow = RandomRow;
MobColm = RandomColm;
IsMobPlaced = true;
}
}
//No way for player or treasure to overlap anything so i do not run a check for them.
RandomRow = rand() % ROWS;
GameBoard[RandomRow][COLM-1] = Treasure;//Treasure on last column
RandomRow = rand() % ROWS;
PlayerRow = RandomRow;//Passes random row for Player to start on, Not assigned here since used and modified in main
}
//print gameboard
void PrintBoard(char GameBoard[][COLM])
{
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLM; j++)
{
cout << GameBoard[i][j];
}
cout << endl;
}
}
void MovePlayer(char GameBoard[][COLM], int &PlayerRow, int &PlayerColm)//Pass by reference so that we dont have to call func twice
{
GameBoard[PlayerRow][PlayerColm] = DEFAULT; //Clears current position on board
char UserInput;
UserInput = getchar();
switch(UserInput)
{
case 'w':
PlayerRow--;//Character position Y - 1 Starts at 0 goes upwards as we go down so negative moves up
if(PlayerRow < 0)
{
cout << "You hit a wall \n";
PlayerRow++;
}
break;
case 'a':
PlayerColm--;
if(PlayerColm < 0)
{
PlayerColm--;
cout << "You hit a wall \n";
}
break;
case 's':
PlayerRow++;
if(PlayerRow > 3)
{
cout << "You hit a wall \n";
PlayerRow--;
}
break;
case 'd':
PlayerColm++;
if(PlayerColm > 9)
{
PlayerColm--;
cout << "You hit a wall \n";
}
break;
}
UserInput = getchar();//Grabs newline character so we dont double loop
GameBoard[PlayerRow][PlayerColm] = Player;//sets new position
}
void MoveMob(char GameBoard[][COLM], int &MobRow, int &MobColm)
{
GameBoard[MobRow][MobColm] = DEFAULT;//Clears current position on board
int RandomNum;
RandomNum = rand() % 4 + 1;
switch(RandomNum)
{
case 1:
MobRow--;//Character position Y - 1 Starts at 0 goes upwards as we go down so negative moves up
if(MobRow < 0 || GameBoard[MobRow][MobColm] == Trap)
{
MobRow++;
}
break;
case 2:
MobColm--;
if(MobColm < 0 || GameBoard[MobRow][MobColm] == Trap)
{
MobColm--;
}
break;
case 3:
MobRow++;
if(MobRow > 3 || GameBoard[MobRow][MobColm] == Trap)
{
MobRow--;
}
break;
case 4:
MobColm++;
if(MobColm > 9 || GameBoard[MobRow][MobColm] == Trap)
{
MobColm--;
}
break;
}
GameBoard[MobRow][MobColm] = Mob;//set new mob position
}
//Check if character moved into tile that will end game.
bool CheckGameState(char GameBoard[][COLM], int PlayerRow, int PlayerColm)
{
string c;
if(GameBoard[PlayerRow][PlayerColm] == Trap || GameBoard[PlayerRow][PlayerColm] == Mob)
{
//Lose
cout << "You were killed by a ";//a smooth criminal
c = (GameBoard[PlayerRow][PlayerColm] == Trap) ? "Trap" : "Monster";
cout << c;
return false;
}
else if(GameBoard[PlayerRow][PlayerColm] == Treasure)
{
//Win
cout << "You reached the treasure! You Win! \n";
return false;
}
else
{
//continue playing
return true;
}
}
|