Hi, I'm new to these forums and new to C++. I started about a week ago and was looking for practice problems online when I ran into a thread from this forum with the following problem:
Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
Make a program that outputs a simple grid based gameboard to the screen using either numbers or characters.
i.e.
. . . . . . . . . .
. G . . . . . . . .
. . . . . . T . . .
. . . . . . . . . .
. . . . T . . . . .
. . . . . . T . . .
. . . . . . . . . X
or
0 0 0 0 0 0 0 0 0 0
0 5 0 0 6 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 7 0 0 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 0 0 4
Allow the user (marked by G in the example) to move either up, down, left, or right each turn. If the player steps on a trap then they lose. If the make it to the treasure 'X' then they win.
I wrote up the following code for it:
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
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
char player = 'G', trap = 'T', goal = 'X';
char grid[7][10];
int i, j, posX = 1, posY = 1, skip = 0;
for (i = 0; i < 7; ++i)
{
for (j = 0; j < 10; ++j)
grid[i][j] = '.';
}
grid[1][1] = player;
grid[2][6] = trap;
grid[4][4] = trap;
grid[5][6] = trap;
grid[6][9] = goal;
cout << "Welcome to Dungeon Crawl!\n";
cout << "\nYou(G) need to get to the exit of the dungeon(X)";
cout << " without hitting any of the traps(T)!\n";
cout << "\nBest of luck to you!!\n";
for (i = 0; i < 7; ++i)
{
for (j = 0; j < 10; ++j)
cout << grid[i][j];
cout << endl;
}
while (grid[6][9] != player)
{
do
{
cout << "\nIn which direction do you wanna move?\n";
cout << "(up, down, left, right or quit):";
cin >> input;
if (input != "up" || input != "down" || input != "left" || input != "right" || input != "quit")
cout << "\nThat's not a valid input. Try again.\n";
} while (input != "up" || input != "down" || input != "left" || input != "right" || input != "quit");
if (input == "up")
{
for (j = 0; j < 10; ++j)
{
if (grid[0][j] == player)
{
cout << "You cannot move up from that position!\n\n";
skip = 1;
}
}
if (skip == 0)
{
grid[posY][posX] = '.';
grid[posY - 1][posX] = player;
}
}
if (input == "down")
{
for (j = 0; j < 9; ++j)
{
if (grid[6][j] == player)
{
cout << "You cannot move down from that position!\n\n";
skip = 1;
}
}
if (skip == 0)
{
grid[posY][posX] = '.';
grid[posY + 1][posX] = player;
}
}
if (input == "left")
{
for (i = 0; i < 7; ++i)
{
if (grid[i][0] == player)
{
cout << "You cannot move left from that position!\n\n";
skip = 1;
}
}
if (skip == 0)
{
grid[posY][posX] = '.';
grid[posY][posX - 1] = player;
}
}
if (input == "right")
{
for (i = 0; i < 6; ++i)
{
if (grid[i][9] == player)
{
cout << "You cannot move right from that position!\n\n";
skip = 1;
}
}
if (skip == 0)
{
grid[posY][posX] = '.';
grid[posY][posX + 1] = player;
}
}
if (input == "quit")
break;
if (grid[2][6] == player || grid[4][4] == player || grid[5][6] == player)
cout << "It's a trap!";
break;
for (i = 0; i < 7; ++i)
{
for (j = 0; j < 10; ++j)
cout << grid[i][j];
cout << endl;
}
skip = 0;
}
if (grid[6][9] == 'G')
cout << "You've escaped the dungeon!!";
cout << "\n\nThanks for playing!";
return 0;
}
|
A lot of this can probably be shortened, but with my limited knowledge, it was the best I can do.
The problem I am having with this is that when I run it, no matter what my input for direction is, it just keeps looping the section with the input and it tells me that it is an invalid input. Any help with this would be really appreciated. Thank you!