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
|
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAX_SIZE = 7;
const char SPACE = '.';
const char player = 'P', trap = 'T', goal = 'X';
char createDungeon(char myArray[][MAX_SIZE], int rows, int cols, char space)
{
int trapCount = 0;
int NUM_TRAP = 3;
int RandRow = rand() % 7; //generates a random number between 0 and 7
int RandCol = rand() % 7; //generates a random number between 0 and 7
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
myArray[i][j] = space; //Here is the dungeon
}
}
//while (trapCount < NUM_TRAP)
// {
// if (myArray[RandRow][RandCol] == '.')
// {
myArray[RandRow][RandCol] = trap; //Spawns three random traps
cout << "Trap: " << "[" << RandRow << "]" << "[" << RandCol << "]" << endl;
// trapCount++;
// }
//}
myArray[1][1] = player;// Spawns a player start
myArray[6][6] = goal; //Spawns a treasure
//return myArray;
}
/////Dungeon has been created, leave function alone, now updateDungeon() will keep you updated///
////////////////////////////////////////////////////////////////////////////////////////
void displayDungeon(char myArray[][MAX_SIZE], int rows, int cols, char space) //Output visual Dungeon
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << myArray[i][j] << " ";
}
cout << endl;
}
}
int getPositionX(char myArray[][MAX_SIZE], int rows, int cols) //Reads from createDungeon() to get Players X position(index)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (myArray[i][j] == player)
{
return i; //Return X index number
}
}
}
}
int getPositionY(char myArray[][MAX_SIZE], int rows, int cols) //Reads from createDungeon() to get Players Y position(index)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (myArray[i][j] == player)
{
return j; //return Y index number
}
}
}
}
void getMove(char myArray[][MAX_SIZE], int posY, int posX, char space)
{
//Reads from getPositionY() and getPositionX() and adds or subtracts from those and uses those numbers
// as new index's for Player.
char MOVE;
cout << "Decision: ";
cin >> MOVE;
while (MOVE != 'u' && MOVE != 'd' && MOVE != 'l' && MOVE != 'r')
{
cout << "Error, try again: " << endl;
cin >> MOVE;
}
if (MOVE == 'u')
{
myArray[posX][posY] = space;
myArray[posY - 1][posX] = player; //Ex: index[1][1] Now equals index[0][1] So player has moved Upwards on the Dungeon
cout << "Your move: " << "'" << MOVE << "'" << endl;
}
else if (MOVE == 'd')
{
myArray[posX][posY] = space;
myArray[posY + 1][posX] = player;
cout << "Your move: " << "'" << MOVE << "'" << endl;
}
else if (MOVE == 'l')
{
myArray[posX][posY] = space;
myArray[posY][posX - 1] = player;
cout << "Your move: " << "'" << MOVE << "'" << endl;
}
else if (MOVE == 'r')
{
myArray[posX][posY] = space;
myArray[posY][posX + 1] = player;
cout << "Your move: " << "'" << MOVE << "'" << endl;
}
cout << "Player now at Index: " << "[" << posY << "]" << "[" << posX << "]" << endl;
}
void updateDungeon(char myArray[][MAX_SIZE], int rows, int cols, int space, int posY, int posX) //Updates Player position onto a new dungeon
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
myArray[i][j];
}
myArray[posY][posX] = player; //New player position from getX() and getY()
}
}
bool checkMove(char myArray[][MAX_SIZE], int posY, int posX) //Checks if getPositionY() and getX() are same as trap's index
{
if (myArray[posY][posX] == trap)
{
cout << "You've landed on a trap! Game OVER..." << endl;
return true;
}
if (myArray[posY][posX] == goal)
{
cout << "You've captured the treasure! Good game!" << endl;
return true;
}
cout << "Check Win: false" << endl;
return false;
}
int main()
{
srand ( time(NULL) ); //initialize the random seed
char DUNGEON[MAX_SIZE][MAX_SIZE] = {};
//Initializing////////////////////Dungeon/////////////////////////////
createDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);////
displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);//
//
Start:
//Ask for move
getMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), SPACE);
//Update Dungeon
updateDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE, getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE));
checkMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE));
displayDungeon(DUNGEON, MAX_SIZE, MAX_SIZE, SPACE);
if (checkMove(DUNGEON, getPositionY(DUNGEON, MAX_SIZE, MAX_SIZE), getPositionX(DUNGEON, MAX_SIZE, MAX_SIZE)) == false)
{
goto Start;
}
}
|