So I'm trying to write a program that solves the "Dungeon Crawl" problem here: http://ift.tt/1jqXE90. If you are too lazy to read the link (which is entirely understandable), the basic premise of the game is that a player moves on a 10x10 grid and tries to reach treasure while avoiding traps and moving enemies. Because everything is revealed to the player, it's not much of a real game, but I thought it'd be good coding practice. Sadly, I'm rusty on user input and never properly learned how to put an entire program together, which means I'm having serious problems with functions executing properly.
The main function is this:
int main()
{
int turn=0,enemyCount=0;
bool end=false;
srand (time(NULL));
node* world;
world = new node[100];
generateWorld(world);
drawWorld(world);
do {
playerMove(world);
enemiesMoves(world,enemyCount);
drawWorld(world);
end = endCheck(world);
turn++;
} while (end == false);
return 0;
}
I think this is fine as is, because it follows the simple process of gathering moves, updating the world, and then checking to see if an end condition has been reached. The problem is that numerous functions are not working as I would expect them to, and I can't figure out why they are not. This includes user input validation, updating the movement of the enemies, drawing the world, and checking if the game ended. I'll go in order.
This is the playerMove() function:
void playerMove(node* array){
int i,playerLocation,oldLocation;
bool valid=false;
char move;
for (i=0;i<100;i++){
if (array[i].player == true){
playerLocation = i;
oldLocation = playerLocation;
}
}
while(!valid){
if (cin >> move){
if (move=='w' || move=='s' || move=='a' || move=='d')
valid = true;
}
if (move=='w'){
playerLocation = oldLocation-10;
if (playerLocation < 0){
cout << "You can't swim." << endl;
valid = false;
}
}
else if (move=='s'){
playerLocation = oldLocation+10;
if (playerLocation > 99){
cout << "You can't swim." << endl;
valid = false;
}
}
else if (move=='a'){
playerLocation = oldLocation-1;
if (playerLocation % 10 == 9){
cout << "You can't swim." << endl;
valid = false;
}
}
else if (move=='d'){
playerLocation = oldLocation+1;
if (playerLocation % 10 == 0){
cout << "You can't swim." << endl;
valid = false;
}
}
}
array[oldLocation].player = false;
array[playerLocation].player = true;
}
It's supposed to gather player input, do nothing if the player enters a key that isn't WASD (they can only move in the four cardinal directions), reject an input if the player tries to move off the edge of the world, and update the player position. It does successfully do the latter, but it will not catch an invalid input, and if a move fails, it refuses to display the message it is supposed to.
This is the enemiesMoves function:
void enemiesMoves(node* array,int enemyCount){
int i,j=0,n,enemyLocations[enemyCount],oldLocations[enemyCount];
bool valid = false;
for (i=0;j<enemyCount;i++){
if (array[i].enemy==true){
enemyLocations[j] = i;
cout << enemyLocations[j] << endl;
j++;
}
}
for (j=0;j<enemyCount;j++){
oldLocations[j] = enemyLocations[j];
}
for (j=0;j<enemyCount;j++){
while (!valid){
n = rand() % 4 + 1;
if (n = 1){
enemyLocations[j] = oldLocations[j]-10;
if (enemyLocations[j] < 0)
valid = false;
}
else if (n = 2){
enemyLocations[j] = oldLocations[j]+10;
if (enemyLocations[j] > 100)
valid = false;
}
else if (n = 3){
enemyLocations[j] = oldLocations[j]-1;
if (enemyLocations[j] % 10 == 9)
valid = false;
}
else if (n = 1){
enemyLocations[j] = oldLocations[j]+1;
if (enemyLocations[j] % 10 == 0)
valid = false;
}
}
array[enemyLocations[j]].enemy = true;
array[oldLocations[j]].enemy = false;
}
}
It is supposed to move the enemies in a random direction, but reject the movement if the enemy moves off the world. Although I thought I properly copied over the playerMove() code to this, it refuses to update the position of the enemies; they stay in the same place turn after turn.
This is the drawWorld() function:
void drawWorld(node* array){
int i;
for (i=0;i<100;i++){
if (array[i].player==true)
cout << "P";
else if (array[i].trap==true)
cout << "T";
else if (array[i].enemy==true)
cout << "E";
else if (array[i].treasure==true)
cout << "X";
else if (array[i].player==true && array [i].treasure==true)
cout << "W";
else if ((array[i].player==true && array[i].trap==true) || (array[i].player==true && array[i].enemy==true))
cout << "L";
else {
cout << "O";
}
if ((i+1 % 10) == 0){
cout << endl;
}
}
cout << endl << endl;
}
It draws the world fine, except that it won't wrap the text every ten characters as it is plainly told to do. I can't fathom what I'm missing here.
Finally, this is the endCheck() function:
bool endCheck(node* array){
int i;
for (i=0;i<100;i++){
if (array[i].player==true && array[i].treasure==true){
cout << "You found the treasure and now enjoy a life of unmitigated opulence." << endl;
return true;
}
else if (array[i].player==true && array[i].trap==true){
cout << "You fell into a conspicuous trap and became tiger food." << endl;
return true;
}
else if (array[i].player==true && array[i].enemy==true){
cout << "You were captured alive by angry natives and enjoyed as part of their New Year's feast." << endl;
return true;
}
else
return false;
}
}
This is simply not executing at all. I can move the player onto a trap and nothing happens.
These problems are incredibly frustrating because I am simply unable to discern what is wrong with the code. I know the post is long, but if anyone could point out what's wrong it'd be greatly appreciated. Also it should be noted that I searched for answers before posting this, but because I think the problems are intrinsic to my code I was unable to find any usable answers.
Aucun commentaire:
Enregistrer un commentaire