Logic Error...

This is a discussion on Logic Error... within the C++ Programming forums, part of the General Programming Boards category; (WinXP, BORLAND free command line compiler, console project) Ok, I'm making a ladder-type thing in game. The idea is you ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Logic Error...

    (WinXP, BORLAND free command line compiler, console project)
    Ok, I'm making a ladder-type thing in game. The idea is you can climb up '/' and '\'s.

    I'm sorry to ask this, but I have been staring at this code for two days and I'm not getting anywhere...

    Heres the problematic code, and the constants relating to it;

    Code:
    #include "EnemyClass.h"
    
    #define Player '*'
    #define ExitPeice 'O'
    
    #define Blank ' '
    
    #define Wall1 '|' //Basic straight wall.
    
    #define Wall2 '//' //Stairs to the right.
    
    #define Wall3 '\\' //Stairs to the left.
    
    #define Wall4 '>' //Peice pushable right. Kills @, &, $.
    
    #define Wall5 '<' //Peice pushable left. Kills @, &, $.
    
    #define Wall6 '[' //Peice pushable left. For protection.
    #define Wall7 ']' //Peice, pushable right. For protection
    #define Wall8 '-' //A bridge peice, will break when stepped off of.
    #define Wall9 '=' //Normal peice.
    #define Wall10 '^' //Death if fallen onto or stepped on.
    
    #define Up 1
    #define Down 2
    #define Left 3
    #define Right 4
    #define Esc 5
    
    int MoveRight() {
     DWORD reader;
     COORD Pos;
     char Saveme[1];
     char Savemeto[1];
     char Walllist[] = "|][<>-=^\\//";
     char Enemies[3];
     const char W2 = Wall2;
     unsigned int x;
    
     Enemies[0]=Enemy1;
     Enemies[1]=Enemy2;
     Enemies[2]=Enemy3;
     Enemies[3]=Enemy4;
    
     if(Jumping == 1)
      return 0;
     
     Pos.X=HEROX;
     Pos.Y=HEROY;
    
     if(Pos.X >= 79 || Pos.X <= 0)
      return 0;
    
     Pos.X++;
     ReadConsoleOutputCharacter(hOt, &Saveme[0], 1, Pos, &reader);
    
     Pos.Y--;
     
     ReadConsoleOutputCharacter(hOt, &Savemeto[0], 1, Pos, &reader);
    
     Pos.X--;
     Pos.Y++;
    
     for(x=0; x <= 3; x++)
      if(Saveme[0] == Enemies[x])
       Death();
    
     if(Saveme[0] == W2 || Savemeto[0] == W2) {
      for(x=0; x <= (strlen(Walllist)-2); x++)
       if(Saveme[0] == Walllist[x])
        return 0;
      SetConsoleCursorPosition(hOt, Pos);
      cout << Blank;
      Pos.X++;
      Pos.Y--;
      SetConsoleCursorPosition(hOt, Pos);
      CPlayer();
      HEROX=Pos.X;
      HEROY=Pos.Y;
      SetConsoleCursorPosition(hOt, RSP);
      return 1;
     }
    
     for(x=0; x >= strlen(Walllist); x++)
      if(Saveme[0] == Walllist[x])
       return 0;
    
     SetConsoleCursorPosition(hOt, Pos);
     Pos.X++;
     cout << Blank;
     SetConsoleCursorPosition(hOt, Pos);
     CPlayer();
     HEROX=Pos.X;
     HEROY=Pos.Y;
    
     SetConsoleCursorPosition(hOt, RSP);
    
     return 1;
    }
    
    int MoveLeft() {
     DWORD reader;
     COORD Pos;
     char Saveme[1];
     char Savemeto[1];
     char Walllist[] = "|][<>-=^//\\";
     char Enemies[3];
     const char W2 = Wall3;
     unsigned int x;
    
     Enemies[0]=Enemy1;
     Enemies[1]=Enemy2;
     Enemies[2]=Enemy3;
     Enemies[3]=Enemy4;
    
     if(Jumping == 1)
      return 0;
     
     Pos.X=HEROX;
     Pos.Y=HEROY;
    
     if(Pos.X >= 78 || Pos.X <= 1)
      return 0;
    
     Pos.X--;
     ReadConsoleOutputCharacter(hOt, &Saveme[0], 1, Pos, &reader);
    
     Pos.Y--;
     
     ReadConsoleOutputCharacter(hOt, &Savemeto[0], 1, Pos, &reader);
    
     Pos.X++;
     Pos.Y++;
    
     for(x=0; x <= 3; x++)
      if(Saveme[0] == Enemies[x])
       Death();
    
     if(Saveme[0] == W2 || Savemeto[0] == W2) {
      for(x=0; x <= (strlen(Walllist)-2); x++)
       if(Saveme[0] == Walllist[x])
        return 0;
      SetConsoleCursorPosition(hOt, Pos);
      cout << Blank;
      Pos.X--;
      Pos.Y--;
      SetConsoleCursorPosition(hOt, Pos);
      CPlayer();
      HEROX=Pos.X;
      HEROY=Pos.Y;
      SetConsoleCursorPosition(hOt, RSP);
      return 1;
     }
    
     for(x=0; x <= strlen(Walllist); x++)
      if(Saveme[0] == Walllist[x])
       return 0;
    
     SetConsoleCursorPosition(hOt, Pos);
     Pos.X--;
     cout << Blank;
     SetConsoleCursorPosition(hOt, Pos);
     CPlayer();
     HEROX=Pos.X;
     HEROY=Pos.Y;
    
     SetConsoleCursorPosition(hOt, RSP);
    
     return 1;
    }
    
    int Check() {
     int x;
    
     x=IsArrowDown();
    
     if(x==Right) {
      MoveRight();
      return 1;
      }
    
     if(x==Left) {
      MoveLeft();
      return 2;
     }
    
     if(x==Esc)
      return 5;
    
     return 0;
    }
    Thats all the resources that are needed -,-. So, as I said you can climb up the edges of '/' and '\'s. Now, you should not be able to go through walls. However, for the right side you cant climb them at all. For the left, you can climb, but you end up going through walls...

    So I'm wondering, why does it let you go through walls on the left, but not right?

    Thank you very much!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    > #define Wall2 '//' //Stairs to the right.
    Probably because this is a large character constant, which does NOT compare equal to a single '/'

    #define Wall2 '/' //Stairs to the right.


    Code:
     Pos.X++;
     ReadConsoleOutputCharacter(hOt, &Saveme[0], 1, Pos, &reader);
    
     Pos.Y--;
     
     ReadConsoleOutputCharacter(hOt, &Savemeto[0], 1, Pos, &reader);
    
     Pos.X--;
     Pos.Y++;
    You seem to do this a lot - how about a separate function?

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I only call the reading once? (The co-ordinants are different on the left and right) The other part of reading depends on where, but they could be thrown into functions. The problem is I would then need to allocate more memory to that portion of code.

    Wait... '//' Is not the same as '/'? So does that mean "//" = '/'? And does that apply to the continuation character aswell? ('\\')

    Thank you for your reply!

    (When replaced the results where the same)
    Last edited by Blackroot; 01-28-2006 at 01:48 AM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    Code:
    // you can easily combine the first two parameters into one
    void readNextChars ( char *ch1, char *ch2, COORD Pos, int dx, int dy ) {
      DWORD reader;
      pos.X += dx;
      ReadConsoleOutputCharacter(hOt, ch1, 1, Pos, &reader);
      pos.Y += dy;
      ReadConsoleOutputCharacter(hOt, ch2, 1, Pos, &reader);
    }
    
    In MoveRight, call with
        readNextChars ( Saveme, Savemeto, Pos, +1, -1 );
    In MoveLeft, call with
        readNextChars ( Saveme, Savemeto, Pos, -1, -1 );
    And you don't have to do that ++ stuff just to undo whatever you did to get back to the original position.

    > Wait... '//' Is not the same as '/'? So does that mean "//" = '/'?
    You only need the double character when the first one is the escape character \
    So
    / = /
    \\ = \
    // = //
    /a = /a
    \a = <the character which would cause a beep>

    > char Walllist[] = "|][<>-=^//\\";
    See, you have // here as well.
    Coupled with your broken search loop of
    for(x=0; x <= (strlen(Walllist)-2); x++)
    which stops one character short of the whole array, means you're not checking everything.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    for(x=0; x <= (strlen(Walllist)-2); x++)
    The reason for this is to skip checking the '//' and '\' characters. Its not broken :P. (When the -2 is removed, you cant go up ladders at all :P)

    So, thanks to you the left and right duplicate the same behavior, both are climable, but neither detect proporely.

    And the reason for that is, its not detecting proporely period XD. So, I have zero'd it down, its not detecting the walls correctly. But I dont get why, the cycle should work...?

    I did not encase my detection in a function, it would probably be smarter, but I dont call it enough to feel the need to make it into a function. If I do call it more though, I'll probably end up making it a function.

    Oh yes, my revised code, its not to different:

    Code:
    int MoveRight() {
     DWORD reader;
     COORD Pos;
     char Saveme[1];
     char Savemeto[1];
     char Walllist[] = "|][<>-=^\\/";
     char Enemies[3];
     const char W2 = Wall2;
     unsigned int x;
    
     Enemies[0]=Enemy1;
     Enemies[1]=Enemy2;
     Enemies[2]=Enemy3;
     Enemies[3]=Enemy4;
    
     if(Jumping == 1)
      return 0;
     
     Pos.X=HEROX;
     Pos.Y=HEROY;
    
     if(Pos.X >= 79 || Pos.X <= 0)
      return 0;
    
     Pos.X++;
     ReadConsoleOutputCharacter(hOt, &Saveme[0], 1, Pos, &reader);
    
     Pos.Y--;
     
     ReadConsoleOutputCharacter(hOt, &Savemeto[0], 1, Pos, &reader);
    
     Pos.X--;
     Pos.Y++;
    
     for(x=0; x <= 3; x++)
      if(Saveme[0] == Enemies[x])
       Death();
    
     if(Saveme[0] == W2 || Savemeto[0] == W2) {
      for(x=0; x <= (strlen(Walllist)-2); x++)
       if(Saveme[0] == Walllist[x])
        return 0;
      SetConsoleCursorPosition(hOt, Pos);
      cout << Blank;
      Pos.X++;
      Pos.Y--;
      SetConsoleCursorPosition(hOt, Pos);
      CPlayer();
      HEROX=Pos.X;
      HEROY=Pos.Y;
      SetConsoleCursorPosition(hOt, RSP);
      return 1;
     }
    
     for(x=0; x >= strlen(Walllist); x++)
      if(Saveme[0] == Walllist[x])
       return 0;
    
     SetConsoleCursorPosition(hOt, Pos);
     Pos.X++;
     cout << Blank;
     SetConsoleCursorPosition(hOt, Pos);
     CPlayer();
     HEROX=Pos.X;
     HEROY=Pos.Y;
    
     SetConsoleCursorPosition(hOt, RSP);
    
     return 1;
    }
    
    int MoveLeft() {
     DWORD reader;
     COORD Pos;
     char Saveme[1];
     char Savemeto[1];
     char Walllist[] = "|][<>-=^/\\";
     char Enemies[3];
     const char W2 = Wall3;
     unsigned int x;
    
     Enemies[0]=Enemy1;
     Enemies[1]=Enemy2;
     Enemies[2]=Enemy3;
     Enemies[3]=Enemy4;
    
     if(Jumping == 1)
      return 0;
     
     Pos.X=HEROX;
     Pos.Y=HEROY;
    
     if(Pos.X >= 78 || Pos.X <= 1)
      return 0;
    
     Pos.X--;
     ReadConsoleOutputCharacter(hOt, &Saveme[0], 1, Pos, &reader);
    
     Pos.Y--;
     
     ReadConsoleOutputCharacter(hOt, &Savemeto[0], 1, Pos, &reader);
    
     Pos.X++;
     Pos.Y++;
    
     for(x=0; x <= 3; x++)
      if(Saveme[0] == Enemies[x])
       Death();
    
     if(Saveme[0] == W2 || Savemeto[0] == W2) {
      for(x=0; x <= (strlen(Walllist)-2); x++)
       if(Saveme[0] == Walllist[x])
        return 0;
      SetConsoleCursorPosition(hOt, Pos);
      cout << Blank;
      Pos.X--;
      Pos.Y--;
      SetConsoleCursorPosition(hOt, Pos);
      CPlayer();
      HEROX=Pos.X;
      HEROY=Pos.Y;
      SetConsoleCursorPosition(hOt, RSP);
      return 1;
     }
    
     for(x=0; x <= strlen(Walllist); x++)
      if(Saveme[0] == Walllist[x])
       return 0;
    
     SetConsoleCursorPosition(hOt, Pos);
     Pos.X--;
     cout << Blank;
     SetConsoleCursorPosition(hOt, Pos);
     CPlayer();
     HEROX=Pos.X;
     HEROY=Pos.Y;
    
     SetConsoleCursorPosition(hOt, RSP);
    
     return 1;
    }
    
    int Check() {
     int x;
    
     x=IsArrowDown();
    
     if(x==Right) {
      MoveRight();
      return 1;
      }
    
     if(x==Left) {
      MoveLeft();
      return 2;
     }
    
     if(x==Esc)
      return 5;
    
     return 0;
    }
    Ha... I'm probably going to have a headache when I try to multithread this >.<. Lol programming is so damn buggy, and its not even the languages fault -,-.

    Thank you for the help salem!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    Oh I see - your variables are just horribly named - it's not a wall list at all but an object list.

    > I did not encase my detection in a function, it would probably be smarter,
    The length of the function doesn't matter, nor how many times you would end up calling it.

    Functions like
    - isWall
    - isLadder
    Would make the code SO much more readable, that it might be possible to tell whether you're doing something dump simply because it reads wrong, without having to dig through the logic.

  7. #7
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    . I usualy end up naming things randomly because I cant really come up with a name that relates to what the variable is. IsWall would work better, but Walllist is fine (A little trivial though, it does make it sound like its a constant :/). You have to keep in mind I didint actualy intend anyone to read thing code -,-.

    It gets alot more confusing when you dig through the whole code to, I usualy have to reference my own variable names to see what I called them -,-.

    So now I guess my only problem is this line;
    Code:
    for(x=0; x <= strlen(Walllist); x++)
      if(Saveme[0] == Walllist[x])
       return 0;
    Is it cycling wrong?

    Let me think this out...

    Code:
    Psuedocode:
    Start at 0, because C++ is 0 based.
    Check if x is less than or equal to the string length of Walllist.
    Add one to x.
    If the value I want to check, matchs the value of Walllist's character indexed by x's value.
    (This doesnt sound right, I need some way to describe this better -,-) 
    If its true, return 0 to exit the function.
    Ok, that didint really help... Why is this cycling wrong? Or is my string value messed up or...???

    Thank you again for your help!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  4. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21