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

Threaded View

  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.

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