Thread: Another console game logic error...

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

    Another console game logic error...

    (WinXP Borland C++ Free Command Line Compiler)

    I must be terrible at making walls, I have once again added a wall that doesnt work. I cant find why though, I have repaired two other walls, but this one just doesnt work... The weird thing is, the wall is only non-existant for enemies, and the weirder thing is, only one enemy fits in the wall, and the rest just bounce off after that, until it comes out.

    I'm so lost... This is the weirdest error I have come across... For the last few hours, all my attempts to reformat my code, re-edit my enemy layout, nothing works. (Though, it did help me optimize my code)

    I swear, I would french-kiss a toad to get this to work -,-.

    Well, heres my code with enemies added (Getting bigger):

    Code:
    #include <windows.h>
    #include <iostream>
    #include <time.h>
    #include <stdlib.h>
    #include <conio.h>
    using namespace std;
    
    enum {
     ESC_KEY = 27,
     UP_ARROW = 256+72,
     DOWN_ARROW = 256+80,
     LEFT_ARROW = 256+75,
     RIGHT_ARROW = 256+77
    };
    
    COORD Pos;
    COORD Pos2;
    SHORT grid_x; //Shorts take less room than integers.
    SHORT grid_y; //Y axis grid for main player.
    SHORT Counter; //Universal counter.
    SHORT c2; //A second universal counter.
    SHORT Epos_X[4]; //Enemy position X, totals 5(C++ is 0 based)
    SHORT Epos_Y[4]; //Enemy Position Y.
    byte b1=0; //Boolean 1.
    byte b2=0; //Boolean 2. Bytes are the smallest integer-type value.
    
    void yourturn();
    void enemyturn();
    void GAMEOVER();
    int get_arrow();
    int get_random(int cap);
    
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    HANDLE h2 = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD Color;
    CONSOLE_SCREEN_BUFFER_INFO inf;  
    CONSOLE_CURSOR_INFO Cur;
    
    int main() { //Main thing, sets up everything.
     srand(time(NULL));
     clrscr();
    
     GetConsoleScreenBufferInfo(h2, &inf);
     Color = inf.wAttributes; 
    
     Cur.bVisible = FALSE;
     Cur.dwSize = 1;
    
     SetConsoleTextAttribute(h, BACKGROUND_RED | BACKGROUND_INTENSITY);
     SetConsoleCursorInfo(h, &Cur);
     SetConsoleTitle("Some Weird Game (Esc to exit.)");
    
     Pos2.X = 0;
     Pos2.Y = 0;
     Epos_X[0] = 10;
     Epos_Y[0] = 7;
     Epos_X[1] = 5;
     Epos_Y[1] = 15;
     Epos_X[2] = 13;
     Epos_Y[2] = 8;
     Epos_X[3] = 5;
     Epos_Y[3] = 7;
     Epos_X[4] = 20;
     Epos_Y[4] = 12; 
     grid_x = 10;
     grid_y = 13;
    
     for(Counter=0; Counter < 80; Counter++)
      for(c2=0; c2 < 25; c2++) {
       Pos.X = Counter;
       Pos.Y = c2;
       SetConsoleCursorPosition(h, Pos);
       if(Counter == grid_x && c2 == grid_y)
        cout << "*" << endl;
       else
        cout << " " << endl;
       if(Counter == Epos_X[0] && c2 == Epos_Y[0]) {
        SetConsoleTextAttribute(h, BACKGROUND_GREEN | BACKGROUND_INTENSITY);
        Pos.X = Epos_X[0];
        Pos.Y = Epos_Y[0];
        SetConsoleCursorPosition(h, Pos);
        cout << "@" << endl;
        SetConsoleTextAttribute(h, BACKGROUND_RED | BACKGROUND_INTENSITY);
       }
       SetConsoleCursorPosition(h, Pos2);
      }
    
     SetConsoleCursorPosition(h, Pos2);
    
     for(;;) //63
      yourturn();
    
     clrscr();
     SetConsoleTextAttribute(h2, Color);
    
     return 0;
    }
    
    void yourturn() {
     SHORT buffer;
    
     buffer = get_arrow();
     if(buffer == ESC_KEY) {
      clrscr();
      SetConsoleTextAttribute(h2, Color);
      exit(0);
     }
     if(buffer == DOWN_ARROW)
      if(grid_y == 23)
       cout << "";
      else
       grid_y++;
     if(buffer == UP_ARROW)
      if(grid_y == 1)
       cout << "";
      else
       grid_y--;
     if(buffer == LEFT_ARROW)
      if(grid_x == 1)
       cout << "";
      else
       grid_x--;
     if(buffer == RIGHT_ARROW)
      if(grid_x == 78)
       cout << "";
      else
       grid_x++;
     enemyturn();
    }
    
    void enemyturn() {
     SHORT buffer;
     SHORT sMov;
     SHORT x;
    
     for(x=0; x < 5; x++) {
      sMov = get_random(8);
    
      if(sMov == 1 || sMov == 5)
       if(Epos_X[x] < 77 && b1 == 0)
        Epos_X[x]++;
       else
        b1=1;
     
      if(sMov == 2 || sMov == 6)
       if(Epos_X[x] > 1 && b1 == 1)
        Epos_X[x]--;
       else {
        Epos_X[x]++;
        b1=0;
       }
    
      if(sMov == 3 || sMov == 7)
       if(Epos_Y[x] < 23 && b2 == 0)
        Epos_Y[x]++;
       else
        b2=1;
     
      if(sMov == 4 || sMov == 8)
       if(Epos_Y[x] > 1 && b2 == 1)
        Epos_Y[x]--;
       else {
        Epos_Y[x]++;
        b2=0;
       }
      }
    
     for(Counter=0; Counter < 80; Counter++)
      for(c2=0; c2 < 25; c2++) {
       Pos.X = Counter;
       Pos.Y = c2;
       SetConsoleCursorPosition(h, Pos);
       if(Counter == grid_x && c2 == grid_y)
        cout << "*" << endl;
       else
        cout << " " << endl;
       for(x=0; x < 5; x++) {
        if(Counter == Epos_X[x] && c2 == Epos_Y[x]) {
         SetConsoleTextAttribute(h, BACKGROUND_GREEN | BACKGROUND_INTENSITY);
         Pos.X = Epos_X[x];
         Pos.Y = Epos_Y[x];
         SetConsoleCursorPosition(h, Pos);
         cout << "@" << endl;
         SetConsoleTextAttribute(h, BACKGROUND_RED | BACKGROUND_INTENSITY);
        }
       }
       SetConsoleCursorPosition(h, Pos2);
      }
     for(x=0; x < 5; x++)
      if(Epos_X[x] == grid_x && Epos_Y[x] == grid_y)
       GAMEOVER(); 
    }
    
    void GAMEOVER() {
     clrscr();
     for(c2=0; c2 < 25; c2++)
      for(Counter=0; Counter < 80; Counter++) {
       Pos.X = Counter;
       Pos.Y = c2;
       SetConsoleCursorPosition(h, Pos);
       SetConsoleTextAttribute(h, BACKGROUND_BLUE | FOREGROUND_INTENSITY);
       cout << " " << endl;
       }
      SetConsoleCursorPosition(h, Pos2);
     Sleep(2000);
     clrscr();
     cout << "You have DIED! Sorry, to lazy to make a restart function :P" << endl;
     SetConsoleTextAttribute(h, Color);
     Sleep(2000);
     
     exit(0);
    }
    
    int get_arrow() {
     int ch = getch();
     
     if(ch == 0 || ch == 224)
      ch = 256 + getch();
     return ch;
    }
    
    int get_random(int cap) {
     return (rand() % cap) + 1;
    }
    Sorry to post two of theyse, but I just cant find this damned error...

    THANK YOU VERY MUCH in advance!!!!!!!!!!!!!!!!!!!!!!!!!
    Last edited by Blackroot; 01-07-2006 at 07:23 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    32
    For starters, I believe you're jumping off the end of your array:

    Code:
    SHORT Epos_X[4]; //Enemy position X, totals 5(C++ is 0 based)
    ...
    Epos_X[4] = 20;
    Zero based only means that they are "numbered" starting from zero. If you state Epos_X[4] you will still get 4 elements, i.e. 0, 1, 2, 3.

    That's the only thing I'm going to try and spot at this hour... must... have... sleep...

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah >.>, I was looking in the wrong spot... Weird, how did I call a part of an array that doesnt exist?
    Well, thank you very much! I need to remember that >.>.

    However, it didint fix it :/. I'm looking in the code now to see what else is wrong. Any other ideas?
    Ahh and now the right wall is letting them in >.>. Whyyyyyy??

    I cut off some of the increments, its working now. Thank you for pointing that array error out though!
    Last edited by Blackroot; 01-07-2006 at 09:03 PM.
    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. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM