For any one learning about arrays and maps
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: For any one learning about arrays and maps

  1. #1
    Join Date
    Jul 2009
    Posts
    4

    Talking Fixed* For any one learning about arrays and maps

    Fixed*

    ive been working on this since my last post but i added a monster('=') to it so far
    monster('=') moves and takes off hp when you go 1 space in any vertical or horisontal from it.
    also added score for how steps you take before you losse

    Code:
    // made by marcus wiseman
    
    #include <iostream>
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    //        y  x
    char Map[10][20] = { "###################",
                         "#@                #",
                         "#   =        =    #",
                         "#                 #",
                         "#        =        #",
                         "#                 #",
                         "#   =        =    #",
                         "#                 #",
                         "###################" };
                         
    int move;
    int PlayerX;
    int PlayerY;
    int MonsterX;
    int MonsterY;
    int HP = 100;
    int SCORE = 0;
    int main()
    {
        int exit = 0;
        while (exit == 0)
        {
              system("cls");
              for (int i = 0; i < 10; i++)
              {
                  cout << Map[i] << endl;
              }
              cout << "HP = " << HP << endl;
              cout << "Score = " << SCORE << endl;
              cout << "Press Q to quit"  << endl; 
              cout << "A,S,D,W To Move Around"  << endl; 
                      if (HP <= 0)
                      {
                             system("cls");
                             cout << "  You Have Died  " << endl;
                             Sleep(3000);
                             cout << "Press Any Key To Restart" << endl;
                             system("pause > nul");
                             HP = 100;
                             SCORE = 0; 
                             main();
                      }
              char key = getch();
              
              for (int y = 0; y < 10; y++)
              {
                  for (int x = 0; x < 20; x++)
                  {
                      
                      switch(Map[y][x])
                      {
                                       case '@':
                                            {
                                                if (key == 'a') 
                                                {
                                                int x2 = (x - 1); 
                                                      switch(Map[y][x2])
                                                       {
                                                                       case ' ':
                                                                            {
                                                                              Map[y][x] = ' ';
                                                                              x -= 1;
                                                                              SCORE++;
                                                                              Map[y][x2] = '@';
                                                                            }break;
                                                                       case '#':
                                                                            {
                                                                            }break;
                                                                                            
                                                       }
                                                }
                                                
                                                if (key == 'd') 
                                                {
                                                int x2 = (x + 1); 
                                                      switch(Map[y][x2])
                                                       {
                                                                       case ' ':
                                                                            {
                                                                              Map[y][x] = ' ';
                                                                              x += 1;
                                                                              SCORE++;
                                                                              Map[y][x2] = '@';
                                                                            }break;
                                                                       case '#':
                                                                            {
                                                                            }break;
                                                                                            
                                                       }
                                                }
                                                
                                                if (key == 's') 
                                                {
                                                int y2 = (y + 1); 
                                                      switch(Map[y2][x])
                                                       {
                                                                       case ' ':
                                                                            {
                                                                              Map[y][x] = ' ';
                                                                              y += 1;
                                                                              SCORE++;
                                                                              Map[y2][x] = '@';
                                                                            }break;
                                                                       case '#':
                                                                            {
                                                                            }break;
                                                                                            
                                                       }
                                                }
                                                
                                                if (key == 'w') 
                                                {
                                                int y2 = (y - 1); 
                                                      switch(Map[y2][x])
                                                       {
                                                                       case ' ':
                                                                            {
                                                                              Map[y][x] = ' ';
                                                                              y -= 1;
                                                                              SCORE++;
                                                                              Map[y2][x] = '@';
                                                                            }break;
                                                                       case '#':
                                                                            {
                                                                            }break;
                                                                                            
                                                       }
                                                }
                                                if (key == 'q') 
                                                {
                                                        exit = 1;
                                                }
                                                PlayerX = x;
                                                PlayerY = y;
                                            }break;
                                            case '=':
                                                 {
                                                     move = rand()%4 + 1;
                                                     if (move == 1)
                                                     {
                                                        int x2 = (x - 1);
                                                        switch(Map[y][x2])
                                                        {
                                                                        case ' ':
                                                                             {
                                                                                Map[y][x] = ' ';
                                                                                x -= 1;
                                                                                Map[y][x2] = '=';
                                                                             }break;
                                                                        case '#':
                                                                             {
                                                                             }break;            
                                                        }
                                                     }
                                                     if (move == 2)
                                                     {
                                                        int x2 = (x + 1);
                                                        switch(Map[y][x2])
                                                        {
                                                                        case ' ':
                                                                             {
                                                                                Map[y][x] = ' ';
                                                                                x += 1;
                                                                                Map[y][x2] = '=';
                                                                             }break;
                                                                        case '#':
                                                                             {
                                                                             }break;            
                                                        }
                                                     }
                                                     if (move == 3)
                                                     {
                                                        int y2 = (y - 1);
                                                        switch(Map[y2][x])
                                                        {
                                                                        case ' ':
                                                                             {
                                                                                Map[y][x] = ' ';
                                                                                y -= 1;
                                                                                Map[y2][x] = '=';
                                                                             }break;
                                                                        case '#':
                                                                             {
                                                                             }break;            
                                                        }
                                                     }
                                                     if (move == 4)
                                                     {
                                                        int y2 = (y + 1);
                                                        switch(Map[y2][x])
                                                        {
                                                                        case ' ':
                                                                             {
                                                                                Map[y][x] = ' ';
                                                                                y += 1;
                                                                                Map[y2][x] = '=';
                                                                             }break;
                                                                        case '#':
                                                                             {
                                                                             }break;            
                                                        }
                                                     }
                                                MonsterX = x;
                                                MonsterY = y;
                                                if (PlayerX == (x - 1))
                                                {
                                                   if (PlayerY == y)
                                                   {
                                                      HP -= 10;       
                                                   }
                                                }
                                                if (PlayerX == (x + 1))
                                                   {
                                                   if (PlayerY == y)
                                                   {
                                                      HP -= 10;        
                                                   }
                                                }
                                                if (PlayerY == (y - 1))
                                                   {
                                                   if (PlayerX == x)
                                                   {
                                                      HP -= 10;        
                                                   }
                                                }
                                                if (PlayerY == (y + 1))
                                                   {
                                                   if (PlayerX == x)
                                                   {
                                                      HP -= 10;          
                                                   }
                                                }
                                                 }break;
                                            
                                            
                      }
                      
                  }
              }
        }
        return 0;
    }
    Last edited by madmarky; July 17th, 2009 at 05:26 PM.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: For any one learning about arrays and maps

    Quote Originally Posted by madmarky View Post
    i have tested this and it works...
    Doesn't mean it's even half way correct.

    Your array dimensions are wrong and the control flow in your program is complete chaos (GLoop and GLoadMap are calling each other in infinite recursion).

    What's your motivation for posting this?
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jul 2009
    Posts
    4

    Re: For any one learning about arrays and maps

    i donno i made this ages ago but i donno about "compete chaos" it works rather well
    and the dimensions are not wrong.
    if you still think they are say why
    and the loops arnt infinite. in GLoop it waits for key input jumps to GLoadMap

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: For any one learning about arrays and maps

    Quote Originally Posted by madmarky View Post
    i donno i made this ages ago but i donno about "compete chaos" it works rather well
    and the dimensions are not wrong.
    if you still think they are say why
    and the loops arnt infinite. in GLoop it waits for key input jumps to GLoadMap
    He's right. GLoop calls GlobalMap which calls GLoop forever. That's just bad form and uncontrolled recursion.

    Why is Map 20 x 200? [20][200] means exactly that, not 20 x 10.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: For any one learning about arrays and maps

    Quote Originally Posted by madmarky
    i donno i made this ages ago but i donno about "compete chaos" it works rather well
    A program can work well for the time being and yet be very difficult to maintain because it is in "complete chaos". You would do well to stop the use of global variables and to break up GLoadMap() into smaller functions that do one thing and do it well. You probably also should rename GLoop to something more descriptive, and avoid the mutual recursion while you are at it, e.g., by using a loop instead.

    Quote Originally Posted by madmarky
    and the loops arnt infinite. in GLoop it waits for key input jumps to GLoadMap
    That just means that the infinite mutual recursion is punctuated by waits for input.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jul 2009
    Posts
    4

    Re: For any one learning about arrays and maps

    well im not a pro at c++ , it worked fine so thought it was fine
    i dident even no what uncontrolled recursion is.

  7. #7
    Join Date
    Nov 2006
    Posts
    1,611

    Re: For any one learning about arrays and maps

    Well, at the tail of a rough start, let me say this:

    ...welcome to the board,

    anyway.

    I hope you won't be too discouraged at participating.

    We are generally a friendly group most of the time
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  8. #8
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: For any one learning about arrays and maps

    Quote Originally Posted by JVene View Post
    We are generally a friendly group most of the time
    I didn't mean to be unfriendly. But the original post did not state a question, neither was it a reply to a question, it simply stated "this code works" so I thought it to be necessary to state that there is a big difference between correct code and code that works when tested.

    @madmarky: You edited your original post, which means, you removed the context of all the answers in this thread so far. Please in the future post a reply with your corrections (if you want to receive further comments on your code, that is). Editing is meant for doing small corrections (spelling errors, small errors in the code) but not to replace a post with an entirely new one.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  9. #9
    Join Date
    Jul 2009
    Posts
    4

    Smile Re: For any one learning about arrays and maps

    ok sorry i dident no

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center