Hi all,
I've been working on this exercise from the beginners exercise and I'm stuck. Here's the 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.
And I wrote this 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <string>
int main()
{
int posX=0, posY=0, skip=0, i, k;
char dungeon[6][10];
std::string input;
for (i=0;i<=5;i++)
{
for (k=0;k<=9;k++)
{
dungeon[i][k] ='O';
}
}
std::cout << "Hello, welcome to the dungeon game created by xbili" << std::endl;
std::cout << "I hope you'll enjoy! To move simply press left, right, up or down." << std::endl;
std::cout << "Avoid the traps denoted by the 'Y' sign!" << std::endl;
std::cout << "Get to the finishing point denoted by the 'x' sign!" << std::endl;
std::cout << "Your character is represented by the 'T' sign." << std::endl;
std::cout << "Enjoy!!!\n\n\n\n\n\n\n" << std::endl;
char player = 'T';
char trap = 'Y';
char finish = 'X';
dungeon[0][0] = player;
dungeon[2][5] = trap;
dungeon[3][8] = trap;
dungeon[4][1] = trap;
dungeon[5][9] = finish;
for (i=0;i<=5;i++)
{
for (k=0;k<=9;k++)
{
std::cout << dungeon[i][k];
}
std::cout << std::endl;
}
do
{
do
{
std::cout << "Where do you want to move? Up, down, left or right?";
std::cin >> input;
if (input!="up" && input!="down" && input!="right" && input!="left")
{
std::cout << "That is not a valid input, try again.\n";
}
}
while (input!="up" && input!="down" && input!="right" && input!="left");
if (input == "up")
{
for (k=0;k<=9;k++)
{
if (dungeon[0][k]==player)
{
std::cout << "You cannot move up from there! \n";
skip = 1;
}
}
if (skip == 0)
{
dungeon[posY][posX] = 'O';
dungeon[posY - 1][posX] = player;
}
}
if (input == "down")
{
for (k=0;k<=9;k++)
{
if (dungeon[6][k]==player)
{
std::cout << "You cannot move down from there! \n";
skip=1;
}
}
if (skip == 0)
{
dungeon[posY][posX] = 'O';
dungeon[posY+1][posX] = player;
}
}
if (input == "left")
{
for (i=0;i<=9;i++)
{
if (dungeon[i][0]==player)
{
std::cout << "You cannot move left from there!\n";
skip=1;
}
}
if (skip==0)
{
dungeon[posY][posX] = 'O';
dungeon[posY][posX-1] = player;
}
}
if (input == "right")
{
for (i=0;i<=9;i++)
{
if(dungeon[i][9]==player)
{
std::cout << "You cannot move right from there!\n";
skip=1;
}
}
if (skip==0)
{
dungeon[posY][posX] = 'O';
dungeon [posY][posX+1] = player;
}
}
if (dungeon[2][5]==player || dungeon[3][8]==player || dungeon[4][1]==player)
{
std::cout << "It's a trap!";
break;
}
for (i=0;i<=5;i++)
{
for (k=0;k<=9;k++)
{
std::cout << dungeon[i][k];
}
std::cout << std::endl;
}
skip = 0;
}
while (dungeon[5][9]!=player);
if (dungeon[5][9]==player)
{
std::cout << "Congratulations! You have escaped the maze.\n";
}
std::cout << "Thanks for playing!\n";
return 0;
}
|
My problem here is that the program will execute just once. i.e. It will make my player move in that direction by one step, and then afterwards nothing would happen no matter the input.
Any help would be greatly appreciated. Thanks!