0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> on 2 there will be a pit

No, C counts from ZERO, not 1, so int traps[]={0,1,0,0,0,0,0}; puts the bottomless pit at 1, not at 2.
Compare int arr[]={0,1,2,3,4,5} which creates a 6 item array with each element equal to its index.

The game knows that the pit is at 1 because we just lookup traps[pos] where pos is the player's position. If pos is 1 then traps[pos] is 1, otherwise traps[pos]=0. Essentially I'm using the array index as one part of a 1-1 mapping 0->0; 1->1; 2->0; 3->0 etc.

> int traps[]={0,1,0,2,0,0,0}; // 1=bottomless and pit 2= arrow trap

Similarly no, this puts the bottomless pit at 1 and the arrow trap at 3, and the code if an arrow trap just reduces your health by 3 whenever you visit it would be:
Code:
switch (traps[pos])
{
  case 0: // nothing
    break;
  case 1: // bottomless pit
    // .. handle bottomless pit
    break;
  case 2: // arrow trap
    health-=3;
    break;
}

Or:

if (traps[pos]==2) // !1 see below
{  // !2 see below
  health-=3;
}
!1: remember the difference between ASSIGNMENT = and COMPARISON ==. BASIC uses = for both and deduces the meaning from the context, because it doesn't allow an assignment within an expression context, but in C an assignment is a valid expression and so the two different operators are necessary. An assignment as an expression is usually a shortcut; you can shorten:
Code:
c=getchar();
if (c)
{
  // ...
}

to:

if (c=getchar())
{
  // ...
}

Or perhaps:

while (c=getchar())
{
  // process characters until getchar returns 0
}

Without the shortcut you would have the clunky:

while (TRUE)
{
  c=getchar();
  if (!c) break;
  // etc
}
!2: Put an opening brace on its own line; it makes it clearer. I've been programming since 1981 and I still use this form. You'll see this throughout any code I write, e.g.:
Code:
	while (!game_exit)
	{
		// display the description
0
cpulocksmith's Avatar, Join Date: Jul 2008
Skilled contributor
sir, is there anyway you could post a working runnable example of what you are tinking. i think i could get more out of it if i could see it work, and be able to screw around with it a little. i think that would help a lot in my understanding of this.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
I did - see my two posts on Nov 14 at around 10.30.
Or do you mean a working example of something else?
0
cpulocksmith's Avatar, Join Date: Jul 2008
Skilled contributor
that did not work, i tried to compile it but it failed the debugger said.

"[linker error] undefined reference to 'winmain@16'"
"id return 1 exit status"

so it did not work. i use dev-cpp, if that means anything. could it be just that we are using different compilers and your has a little bit of a nich to do stuff a little different then mine does? or somthing like that. i really have no idea.^^,
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Oh, that just means you've compiled it as a Windows program instead of as a DOS program. The entry point for Windows programs is WinMain, and mine of course is a console application. None of the printfs will work if it's a Windows function; you need to use considerably more complicated GUI drawing tools if you want to write it as a Windows program (Hint: you don't).