ive been working on this since my last post but i added a monster('=') to it so far
monster('=') moves and takes off hp when you go 1 space in any vertical or horisontal from it.
also added score for how steps you take before you losse
Code:
// made by marcus wiseman
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
// y x
char Map[10][20] = { "###################",
"#@ #",
"# = = #",
"# #",
"# = #",
"# #",
"# = = #",
"# #",
"###################" };
int move;
int PlayerX;
int PlayerY;
int MonsterX;
int MonsterY;
int HP = 100;
int SCORE = 0;
int main()
{
int exit = 0;
while (exit == 0)
{
system("cls");
for (int i = 0; i < 10; i++)
{
cout << Map[i] << endl;
}
cout << "HP = " << HP << endl;
cout << "Score = " << SCORE << endl;
cout << "Press Q to quit" << endl;
cout << "A,S,D,W To Move Around" << endl;
if (HP <= 0)
{
system("cls");
cout << " You Have Died " << endl;
Sleep(3000);
cout << "Press Any Key To Restart" << endl;
system("pause > nul");
HP = 100;
SCORE = 0;
main();
}
char key = getch();
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 20; x++)
{
switch(Map[y][x])
{
case '@':
{
if (key == 'a')
{
int x2 = (x - 1);
switch(Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x -= 1;
SCORE++;
Map[y][x2] = '@';
}break;
case '#':
{
}break;
}
}
if (key == 'd')
{
int x2 = (x + 1);
switch(Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x += 1;
SCORE++;
Map[y][x2] = '@';
}break;
case '#':
{
}break;
}
}
if (key == 's')
{
int y2 = (y + 1);
switch(Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y += 1;
SCORE++;
Map[y2][x] = '@';
}break;
case '#':
{
}break;
}
}
if (key == 'w')
{
int y2 = (y - 1);
switch(Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y -= 1;
SCORE++;
Map[y2][x] = '@';
}break;
case '#':
{
}break;
}
}
if (key == 'q')
{
exit = 1;
}
PlayerX = x;
PlayerY = y;
}break;
case '=':
{
move = rand()%4 + 1;
if (move == 1)
{
int x2 = (x - 1);
switch(Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x -= 1;
Map[y][x2] = '=';
}break;
case '#':
{
}break;
}
}
if (move == 2)
{
int x2 = (x + 1);
switch(Map[y][x2])
{
case ' ':
{
Map[y][x] = ' ';
x += 1;
Map[y][x2] = '=';
}break;
case '#':
{
}break;
}
}
if (move == 3)
{
int y2 = (y - 1);
switch(Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y -= 1;
Map[y2][x] = '=';
}break;
case '#':
{
}break;
}
}
if (move == 4)
{
int y2 = (y + 1);
switch(Map[y2][x])
{
case ' ':
{
Map[y][x] = ' ';
y += 1;
Map[y2][x] = '=';
}break;
case '#':
{
}break;
}
}
MonsterX = x;
MonsterY = y;
if (PlayerX == (x - 1))
{
if (PlayerY == y)
{
HP -= 10;
}
}
if (PlayerX == (x + 1))
{
if (PlayerY == y)
{
HP -= 10;
}
}
if (PlayerY == (y - 1))
{
if (PlayerX == x)
{
HP -= 10;
}
}
if (PlayerY == (y + 1))
{
if (PlayerX == x)
{
HP -= 10;
}
}
}break;
}
}
}
}
return 0;
}
Last edited by madmarky; July 17th, 2009 at 05:26 PM.
Your array dimensions are wrong and the control flow in your program is complete chaos (GLoop and GLoadMap are calling each other in infinite recursion).
What's your motivation for posting this?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
i donno i made this ages ago but i donno about "compete chaos" it works rather well
and the dimensions are not wrong.
if you still think they are say why
and the loops arnt infinite. in GLoop it waits for key input jumps to GLoadMap
i donno i made this ages ago but i donno about "compete chaos" it works rather well
and the dimensions are not wrong.
if you still think they are say why
and the loops arnt infinite. in GLoop it waits for key input jumps to GLoadMap
He's right. GLoop calls GlobalMap which calls GLoop forever. That's just bad form and uncontrolled recursion.
Why is Map 20 x 200? [20][200] means exactly that, not 20 x 10.
i donno i made this ages ago but i donno about "compete chaos" it works rather well
A program can work well for the time being and yet be very difficult to maintain because it is in "complete chaos". You would do well to stop the use of global variables and to break up GLoadMap() into smaller functions that do one thing and do it well. You probably also should rename GLoop to something more descriptive, and avoid the mutual recursion while you are at it, e.g., by using a loop instead.
Originally Posted by madmarky
and the loops arnt infinite. in GLoop it waits for key input jumps to GLoadMap
That just means that the infinite mutual recursion is punctuated by waits for input.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Well, at the tail of a rough start, let me say this:
...welcome to the board,
anyway.
I hope you won't be too discouraged at participating.
We are generally a friendly group most of the time
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
We are generally a friendly group most of the time
I didn't mean to be unfriendly. But the original post did not state a question, neither was it a reply to a question, it simply stated "this code works" so I thought it to be necessary to state that there is a big difference between correct code and code that works when tested.
@madmarky: You edited your original post, which means, you removed the context of all the answers in this thread so far. Please in the future post a reply with your corrections (if you want to receive further comments on your code, that is). Editing is meant for doing small corrections (spelling errors, small errors in the code) but not to replace a post with an entirely new one.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Bookmarks