-3

So I got a little maze game over here, kept very simple. But how is it possible, that I can call a function or do something else when the Player "O" reached a specific point inside the maze ? Let's say he gets to the lower right corner, immediately after him arriving there the screen shall be cleared for example.

       #include<iostream>
        #include<windows.h>
        #include<conio.h>

        using namespace std;



        char map[20][40] = {
         "#######################################",
start -->"#O         X    XXXXX             XX  #",
         "#          X      XX        XXXX   XXX#",
         "#  XXXXXXXXXXXXX  XXXXXXXX    XX      #",
         "#  XX        XXX              XXXXX   #",
         "#  XXXX      XXX  XXXX   X    XX  X   #",
         "#     X     XXXX  X  X   X    XX  X   #",
         "#  XX X     X     X  X   X    XX  X X #",
         "#  XX X     X     X  X   XX       X X #",
         "#  XX X           X  XX  XXXXXXX    X #",
         "#   X          XXXXX           XXXXXX #",
         "#X  X    X     X   X  X     XXXXX   X #",
         "#X  X    XXX   X   X  X  XXXXX  X   X #",
         "#X  X      X   X      XXXX      X   X #",
         "#   X  XX  X   XXXX   X     XX  X   X #",
         "#  XXXXXX  X      X   X    XX         #",
         "#          X          X    XXXX X XXXX#",
         "#         XXXXXXXX    X    X    X    z#",  //<--where the O arrives
         "#######################################"

        };

        int x = 1, y = 1;
        bool game_running = true;

        int main()
        {
            while(game_running == true)
                 {
            HANDLE hStdout;
            COORD destCoord;
            hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

            //position cursor at start of window
            destCoord.X = 0;
            destCoord.Y = 0;
            SetConsoleCursorPosition(hStdout, destCoord);
            Sleep(100);

                  for(int display = 0; display < 20; display++)
                     {
                       cout<<map[display]<<endl;
                     }

                    if(GetAsyncKeyState(VK_DOWN))
                      {
                       int y2 = y + 1;
                       if(map[y2][x] == ' ')
                         {
                          map[y][x] = ' ';
                          y++;
                          map[y2][x] = 'O';
                          }
                      }

                 if(GetAsyncKeyState(VK_UP))
                    {
                     int y2 = y - 1;
                     if(map[y2][x] == ' ')
                       {
                        map[y][x] = ' ';
                        y--;
                        map[y2][x] = 'O';
                       }
                    }

                  if(GetAsyncKeyState(VK_RIGHT))
                    {
                     int x2 = x + 1;
                     if(map[y][x2] == ' ')
                       {
                        map[y][x] = ' ';
                        x++;
                        map[y][x] = 'O';
                       }
                    }

    if(GetAsyncKeyState(VK_LEFT))
                {
                 int x2 = x - 1;
                 if(map[y][x2] == ' ')
                   {
                    map[y][x] = ' ';
                    x--;
                    map[y][x] = 'O';
                   }
                }

               if(GetAsyncKeyState(VK_ESCAPE))
                 {
                  game_running = false;
                 }

             }
             system("pause>nul");
             cout<<"GAME OVER"<<endl;

        return 0;
    }
Intercase
  • 1
  • 3
  • 1
    Well you know specific coordinates from `x` and `y` don't you? – πάντα ῥεῖ Oct 12 '16 at 13:26
  • I am sorry for me being not able to solve this my self. I am a beginner so I don't see why my question maybe is nonsense or stupid. Sorry. I just don't know what to do with the coordinates. – Intercase Oct 12 '16 at 13:30
  • Your code will be *hugely* simpler to read if you write a function `move(int delta_x, int delta y)`. Then each of those tests becomes (eg) `if (GetAsyncKeystate(VK_RIGHT) { move(+1, 0); }` – Martin Bonner supports Monica Oct 12 '16 at 13:31
  • Something along `if(x == 19 && y == 39) function_call();`. I'd recommend you to read a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) before coming here and asking. – πάντα ῥεῖ Oct 12 '16 at 13:33
  • Oh ok I will do so, I thought as a beginner I can ask something as well ? – Intercase Oct 12 '16 at 13:36
  • @Intercase _" I thought as a beginner I can ask something as well ?"_ You can of course. But also beginners need to ask questions within the quality policies we have here. You can inform yourself about these in the Help Center. – πάντα ῥεῖ Oct 12 '16 at 13:42

1 Answers1

1

An OO way

  1. Make a class that contains the position of your person
  2. put getters & setters for getting the coords
  3. make a subclass interface, and a setter for this interface (this is known as the Observer pattern)
  4. When the setter for the coords is called, call the function on your interface.
  5. Have another class implement this interface.

The functional way

Call a function each time you change x or y, and then farm it out to more interesting functions depending on their position.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30