Staredit Network > Forums > Technology & Computers > Topic: C++ 2D array help
C++ 2D array help
This topic is locked. You can no longer write replies here.
Mar 23 2008, 10:14 pm
By: Falkoner  

Mar 23 2008, 10:14 pm Falkoner Post #1



Okay, I have a working 2D array, that I can move around on using the aswd keys, but I can't make collision detection, here's the code that allows me to move around:

Code
#include <iostream.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>

int mx=5;
int my=4;
int i, j;
char direction;
char keypress;
char matrix[100][100];


void gotoxy(short x, short y)
{
     HANDLE hConsoleOutput;
     COORD Cursor_an_Pos = {x,y};
     hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}



void loadgrid()
{    
    for(i=0; i<10; i++)
    {
           for(j=0; j<10; j++)
           {
            strcpy(matrix[0],"     ----     ");
            strcpy(matrix[1],"    |    |    ");
            strcpy(matrix[2],"    |    |    ");
            strcpy(matrix[3]," ---      --- ");
            strcpy(matrix[4],"|            |");
            strcpy(matrix[5],"|            |");
            strcpy(matrix[6]," ---      --- ");
            strcpy(matrix[7],"    |    |    ");
            strcpy(matrix[8],"    |    |    ");
            strcpy(matrix[9],"     ----     ");

           }
     }
}



void printgrid()
{
     for (int i=0; i<30; i++)
     {
           for ( int j=0; j<30; j++)
           {
                 printf("%c", matrix[i][j]);
           }
           printf("\n");
     }
}




int main()
{

system("COLOR 0B");
loadgrid();
printgrid();

gotoxy(mx,my);

printf("v");


while (mx>0 && my>0)
{
     loadgrid();

     if (kbhit())
     {
           keypress=getch();

           if ((keypress == 'd') || (keypress == 'a') || (keypress == 'w') || (keypress == 's') || (keypress == 'S') || (keypress == 'D') || (keypress == 'A') || (keypress == 'W'))
            {
                 direction = keypress;
                 if (direction == 's' || direction == 'S')
                 {
                        gotoxy(mx,my);
                        printf(" ");
                        my++;
                        gotoxy(mx,my);
                        printf("v");
                 }
                 if (direction == 'w' || direction == 'W')
                 {
                    gotoxy(mx,my);
                    printf(" ");
                    my--;
                    gotoxy(mx,my);
                    printf("^");
                 }
                 if (direction == 'a' || direction == 'A')
                 {
                    gotoxy(mx,my);
                    printf(" ");
                    mx--;
                    gotoxy(mx,my);
                    printf("<");
                 }
                 if (direction == 'd' || direction == 'D')
                 {
                    gotoxy(mx,my);
                    printf(" ");
                    mx++;
                    gotoxy(mx,my);
                    printf(">");
                }

                 direction = ' ';
            }
     }
}

return 0;
}


Now, I tried to have something look ahead to the space they are walking to, but for some reason that is seeing invisible walls or something... Here's the code I tried:

Code
#include <iostream.h>
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>

int mx=5;
int my=4;
int i, j;
char direction;
char keypress;
char matrix[100][100];


void gotoxy(short x, short y)
{
     HANDLE hConsoleOutput;
     COORD Cursor_an_Pos = {x,y};
     hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleCursorPosition(hConsoleOutput, Cursor_an_Pos);
}



void loadgrid()
{    
    for(i=0; i<10; i++)
    {
           for(j=0; j<10; j++)
           {
            strcpy(matrix[0],"     ----     ");
            strcpy(matrix[1],"    |    |    ");
            strcpy(matrix[2],"    |    |    ");
            strcpy(matrix[3]," ---      --- ");
            strcpy(matrix[4],"|            |");
            strcpy(matrix[5],"|            |");
            strcpy(matrix[6]," ---      --- ");
            strcpy(matrix[7],"    |    |    ");
            strcpy(matrix[8],"    |    |    ");
            strcpy(matrix[9],"     ----     ");

           }
     }
}



void printgrid()
{
     for (int i=0; i<30; i++)
     {
           for ( int j=0; j<30; j++)
           {
                 printf("%c", matrix[i][j]);
           }
           printf("\n");
     }
}




int main()
{

system("COLOR 0B");
loadgrid();
printgrid();

gotoxy(mx,my);

printf("v");


while (mx>0 && my>0)
{
     loadgrid();

     if (kbhit())
     {
           keypress=getch();
           if ((keypress == 'd') || (keypress == 'a') || (keypress == 'w') || (keypress == 's') || (keypress == 'S') || (keypress == 'D') || (keypress == 'A') || (keypress == 'W'))
            {
                 direction = keypress;
                 if (direction == 's' || direction == 'S')
                 {
                    if(matrix[mx][(my + 2)] == ' ')
                    {
                        gotoxy(mx,my);
                        printf(" ");
                        my++;
                        gotoxy(mx,my);
                        printf("v");
                    }
                 }
                 if (direction == 'w' || direction == 'W')
                 {
                    if(matrix[mx][(my - 2)] == ' ')
                    {
                    gotoxy(mx,my);
                    printf(" ");
                    my--;
                    gotoxy(mx,my);
                    printf("^");
                    }
                 }
                 if (direction == 'a' || direction == 'A')
                 {
                    if(matrix[(mx - 2)][my] == ' ')
                    {
                    gotoxy(mx,my);
                    printf(" ");
                    mx--;
                    gotoxy(mx,my);
                    printf("<");
                    }
                 }
                 if (direction == 'd' || direction == 'D')
                 {
                    if(matrix[(mx + 2)][my] == ' ')
                    {
                    gotoxy(mx,my);
                    printf(" ");
                    mx++;
                    gotoxy(mx,my);
                    printf(">");
                    }
                 }

                 direction = ' ';
            }
     }
}

return 0;
}


Help meh plz!

Post has been edited 1 time(s), last time on Mar 23 2008, 10:52 pm by Falkoner.



None.

Mar 23 2008, 10:39 pm Doodle77 Post #2



what's the
Code
cout <<  endl  << matrix[mx - 1][my];
about?



None.

Mar 23 2008, 10:52 pm Falkoner Post #3



Oh, sorry, left my debugging code in there :P



None.

Mar 24 2008, 1:02 pm Falkoner Post #4



Anyone? Is this really that difficult?



None.

Mar 24 2008, 10:23 pm Falkoner Post #5



Nevermind, I figured it out, the x and y are switched, it's normal on the screen, but in the matrix it goes y x, not x y, so it all works now..



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[02:13 pm]
Vrael -- pee poo sibling
[2026-6-28. : 7:00 pm]
Symmetry -- poo poo papa
[2026-6-28. : 2:46 pm]
lil-Inferno -- pee pee child
[2026-6-27. : 6:10 pm]
Ultraviolet -- sweet summer child
[2026-6-26. : 10:31 am]
NudeRaider -- blessed innocent soul knows nothing of the strife we had before EUDs were discovered :teehee:
[2026-6-23. : 3:29 am]
DarkenedFantasies -- Probably just didn't care. For example, at some point before release, they've updated the graphics of some of the Protoss buildings (Forge, CyberCore, Citadel, Observatory, Arbiter Tribunal), but instead of properly re-rendering them with edited 3D models, they did crappy copy-paste jobs on the rendered graphics.
[2026-6-22. : 8:35 pm]
Ultraviolet -- :wob:
[2026-6-21. : 11:38 pm]
Symmetry -- :wob:
[2026-6-21. : 4:56 am]
Ultraviolet -- I suppose we'll likely never know, but my guess would be that they already saw it operating successfully and there was no monetary incentive to finish the original work. And the dev cycle in old school Blizzard was so hectic, it's possible it just got forgotten about after the original game got released. Plus there's an element of existing MPQ files that were packaged with the original discs becoming outdated if they updated it. And it's not like they remade the original MPQs, they just made new ones for BW specifically
[2026-6-21. : 4:26 am]
Oh_Man -- so that makes me think maybe the theory they are unfinished is not true and its a deliberate design decision, coz why not finish them wen ur making brood war?
Please log in to shout.


Members Online: Vrael, NudeRaider