Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   need some help with this damn code...rage... (http://www.go4expert.com/forums/help-damn-coderage-t15704/)

cpulocksmith 2Jan2009 16:05

need some help with this damn code...rage...
 
hay xpi0t0s, or anyone else who decides to tell me stuff. i took a bit of a break from this code and i cam back, i reformatted so i lost all the stuff i had done but no matter. i still remember it all. (i am vary good at remembering numbers) but i still ahve the same problem as before, the stupid encounter thing xpi0t0s was trying to tell me about. here is my code so far, i think it if good enough for a demo of what you were trying to tell me. please if it would not be too much trouble could you xpi0t0s, or anyone else who cares edit in the proper way to make it when you go south || 's' of the starting location message pops up saying "monster" or something... using the monster array at the top.
Code:

#include <iostream>

using namespace std;

int main(){


int monster[3]={0,0,1};
char dir;
int placement;
//end veriables

placement =0;

do{
if (placement == 0){
cout<<"you are in the starting location...move!"<<endl<<"north or south? "<<endl;
cin>>dir;

if (dir == 'n'){
placement = 1;

}

else if (dir == 's'){
placement = 2;
}

}

if (placement == 1){
cout<<"you are in the place north of the start"<<endl<<"you can go south"<<endl;
cin>>dir;

if (dir == 's'){
placement = 0;
}

}

if (placement == 2){
cout<<"you are south of the start"<<endl<<"you can go north"<<endl;
cin>>dir;

if (dir == 'n'){
placement = 0;
}
}
 
}while (dir > -1);

cout<<monster[0]<<monster[1]<<monster[2]<<endl;
cin>>a;
cout<<monster[a]<<endl;
}

(i really hope this post makes sence i have not had any sleep in three days and i am kinda out of it. and i can never rember how to spell "xpi0t0s".)

xpi0t0s 4Jan2009 17:44

Re: need some help with this damn code...rage...
 
Sorry, it is a bit of a cryptic username. I got sick of other people nicking the good names I thought of, saxman, jazzman, even jazman with one z got nicked, so I thought why don't I start using an old password instead. So far I've never seen anyone else use xpi0t0s, probably because nobody can figure out what the heck it means.

If you're going to use the fairly simple construct of "if (placement==1) { } if (placement==2) { } ..." then you don't need the monster array at all; just bung all the code in the "if placement==2" clause. The point of the arrays is that it gets you out of writing all the duplicate code you would have to write if you stick to the simple construct, and gives you additional flexibility in that monsters can move from location to location as they might in "real" life.

So just do something like:
Code:

if (placement==2)
{
  cout<<"description, including that of a monster";
  // fight code
}

If you search the site for my other posts then you'll find the more generic code I posted previously.

Oh, and keep backups!!! I use Peters Backup; it's simple and runs daily in Scheduled Tasks. Backup selectively though, otherwise you'll end up using tons of DVDR's.

cpulocksmith 5Jan2009 08:46

Re: need some help with this damn code...rage...
 
does your name mean christ, in greek? i think.

and thank man, i think i have found a different way of doing this, please tell me if there is a horrible flaw or doing this is just stupid.

Code:

#include <iostream>

using namespace std;

int main(){

int encounterTrap;
int encounterMonster;
int trap[4]={0,1,0,0};
int monster[4]={0,0,1,0};
char dir;
int placement;
//end variables

//also remember that the whole just saying trap or monster thing is just to debug and test this crap. i will change it when i go moke the real game.
placement =0;
cout<<endl<<endl<<endl<<"you stand befor a large pond. you have no memery of what has happened or how you have come to find yourself here. you are wearing a plain cotton robe. your hair and cloths are damp as if you may have been in the pond some time ago."<<endl;

do{
if (placement == 0){
cout<<endl<<"you stand near the pond and look around you see two paths leading off through the forest one leading north the other leading south. witch way would you like to go?"<<endl;

encounterTrap=trap[placement];
if (encounterTrap == 1){
cout<<endl<<"******"<<endl<<"*Trap*"<<endl<<"******"<<endl;
}


encounterMonster=monster[placement];
if (encounterMonster == 1){
cout<<endl<<"*********"<<endl<<"*monster*"<<endl<<"*********"<<endl;
}

cin>>dir;

if (dir == 'n'){
placement = 1;

}

else if (dir == 's'){
placement = 2;
}

}




if (placement == 1){
cout<<endl<<"you walk north for a short distance befor you find yourself standing infront of an unscalable cliff face, "<<endl<<"you can go south"<<endl;

encounterTrap=trap[placement];
if (encounterTrap == 1){
cout<<endl<<"******"<<endl<<"*Trap*"<<endl<<"******"<<endl;
}


encounterMonster=monster[placement];
if (encounterMonster == 1){
cout<<endl<<"*********"<<endl<<"*monster*"<<endl<<"*********"<<endl;
}


cin>>dir;

if (dir == 's'){
placement = 0;
}

}


if (placement == 2){
cout<<endl<<"you start down the path leading south into the forest. the forest is dark, darker then it should be. you feel cold, the deeper in you go the colder you feel. "<<endl<<"you can go north or keep going south."<<endl;

encounterTrap=trap[placement];
if (encounterTrap == 1){
cout<<endl<<"******"<<endl<<"*Trap*"<<endl<<"******"<<endl;
}


encounterMonster=monster[placement];
if (encounterMonster == 1){
cout<<endl<<"*********"<<endl<<"*monster*"<<endl<<"*********"<<endl;
}

cin>>dir;

if (dir == 'n'){
placement = 0;
}

if (dir == 's'){
placement = 3;
}
}


if (placement == 3){
cout<<endl<<"you keep walking south into the dark forest, you can now see faint clouds form of your breath as you breath. your damp hair is begining to stiffen. "<<endl<<"you can go north or keep going south."<<endl;

encounterTrap=trap[placement];
if (encounterTrap == 1){
cout<<endl<<"******"<<endl<<"*Trap*"<<endl<<"******"<<endl;
}
encounterMonster=monster[placement];
if (encounterMonster == 1){
cout<<endl<<"*********"<<endl<<"*monster*"<<endl<<"*********"<<endl;
}

cin>>dir;

if (dir == 'n'){
placement = 2;
}

if (dir == 's'){
placement = 4;
}

}




if (placement == 4){
cout<<endl<<"you turn a bend in the trail to see a small clearing, the clearing looks as if it had been cleared by fire not long ago. there is a man standing in the middle of the clearing. he is speaking in a strang tounge that is unknown to you. he wears a large black cloak the skull of some grotesk looking animal over his face. he has a rapiar at his side."<<endl;

encounterTrap=trap[placement];
if (encounterTrap == 1){
cout<<endl<<"******"<<endl<<"*Trap*"<<endl<<"******"<<endl;
}


encounterMonster=monster[placement];
if (encounterMonster == 1){
cout<<endl<<"*********"<<endl<<"*monster*"<<endl<<"*********"<<endl;
}

cin>>dir;

if (dir == 'n'){
placement = 3;

}

else if (dir == 'e'){
placement = 5;
}

}



 
}while (dir > -1);

}


xpi0t0s 5Jan2009 13:43

Re: need some help with this damn code...rage...
 
Yes, that's right. Christ in Greek, take the English letters that most closely resemble the Greek letters then replace a couple of letters with digits (because the Unix system at the time insisted on at least one digit in passwords). (I'm a follower of Jesus Christ)

Well, you're still using the "if placement==n" approach and lots of duplicate code; the trap and monster stuff is repeated within each "if" block.

There's nothing wrong with this if the program is going to stay small and is just for experimenting, maybe if you stay under 20 nodes or so you'll have no problem.

But if you're planning to develop this into a large game with hundreds of nodes then this is going to lead to a massive program that is impossible to keep track of and debug - there'll be lots of duplicate code, and if you want to change the behaviour (maybe process monsters before traps) then you'll have to do that for each if block. Hence my previous suggestion of describing the game in data rather than in software, and making the software generic.


All times are GMT +5.5. The time now is 04:14.