• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
shinypixel

Checking dynamic 2d array diagonally

8 posts in this topic

Edit: Please check my reply below for the current code.

 

I'm not having too much luck with this. Think of Tic Tac Toe, and trying to find if the angle contains all of the same value (such as 1). I'm operating on a dynamic, 2D array.

 
    bool isAngleWithValue(const int xPosFrom, const int yPosFrom,
        const int xPosTo, const int yPosTo, const T &value)
    {
        bool blnGreatSuccess = true;
        int width, height;  

        // ... removed validation code for demostration

        // get width and height  
        width = (xPosTo - xPosFrom);
        height = (yPosTo - yPosFrom);  
        
        for (int y = yPosFrom; y < height; y++)
        {
            for (int x = xPosFrom; x < width; x++)
            {
                if (x == y)
                {
                    if (m_array[y * m_width + x] != value)
                        blnGreatSuccess = false;
                }
            }
        }

        return blnGreatSuccess;
    }

For example, this works:

    Array2D<int> a(3, 3);
    a.fill(0);

    a.get(0, 0) = 1;
    a.get(1, 1) = 1;
    a.get(2, 2) = 1;

    if (a.isAngleWithValue(0, 0, 2, 2, 1))
        cout << "Yes!" << endl;
    else
        cout << "No!" << endl; 

But when I change a.get(2,2) to a.get(1, 2), it's still outputting "Yes!", which is not functioning correctly.

 

I tried researching various sites, but I can't seem to relate it to this problem. A general algorithm can be helpful too for checking diagonally.

Edited by shinypixel
0

Share this post


Link to post
Share on other sites

What your code is doing is basically just checking that any elements on the diagonal of the 2D array that are within the rect (xPosFrom, yPosFrom) to (xPosFrom + width, yPosFrom + height) have the value 'value'. Try printing out what x,y positions actually ever get checked, and you'll see what I mean.

 

What you're looking for is probably a DDA type of algorithm. DDA stands for "digital differential analyzer". It's the kind of algorithm that's used to raster triangles/lines into 2D framebuffers, for example. What you're doing is essentially trying to draw a line across a 2D grid and check all the squares it intersects with, which is very similar to the rasterization problem.

0

Share this post


Link to post
Share on other sites

Thanks for the thoughts. I'm just trying to think of this really simple, like checking for a win in a Tic Tac Toe game (but keeping this as a general array member function).

 

The idea is:

If a line starting from (x,y) is N long, no matter what direction, return true. Else, return false.

 

I modified the function a bit.

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Checks if an angle contains all the same value.
    /// </summary>
    /// <param name = "xPosFrom"> the starting x-position to check for value. </param>
    /// <param name = "yPosFrom"> the starting y-position to check for value. </param>
    /// <param name = "intPlaces"> How many cells to check for value. </param>
    /// <param name = "value"> The value to check for in each cell. </param>
    /// <returns> Returns true on success, else false. </returns>
    ///////////////////////////////////////////////////////////////////////////
    bool isAngleWithValue(const int xPosFrom, const int yPosFrom,
        const int intPlaces, const T &value)
    {
        bool blnGreatSuccess = false;
        int width, height;   
        int xPos, yPos;
        int times = 0;

        // validate array
        if (!isValid())
            return false;  

        // check for first occurance of finding value
        if ((m_array[yPosFrom * m_width + xPosFrom]) == value)
        {
            times++;
        }
        else
        {
            // value doesn't exist in first position. Bail out.
            return false;
        }  

        // TODO: How to check around the first position?

        if (times >= intPlaces)
            blnGreatSuccess = true;

        return blnGreatSuccess;
    }
Edited by shinypixel
0

Share this post


Link to post
Share on other sites

But when I change a.get(2,2) to a.get(1, 2), it's still outputting "Yes!", which is not functioning correctly.
 
I tried researching various sites, but I can't seem to relate it to this problem. A general algorithm can be helpful too for checking diagonally.


You are passing in 0 and 2 as the dimensions.

Your width and height are then (2 - 0) -> 2.

You then loop from i = 0 to i < 2 -> meaning you only check 0 and 1.

So, the value at (2, 2) does not affect your result.
0

Share this post


Link to post
Share on other sites

Yeah, I had to rearrange the algorithm a bit, and got it working finally. Thanks everyone.

Edited by shinypixel
0

Share this post


Link to post
Share on other sites

Yeah, I had to rearrange the algorithm a bit, and got it working finally. Thanks everyone.


Your newer function doesn't even have a loop in it. I cannot understand how that would work either. It's only checking one location.
0

Share this post


Link to post
Share on other sites

Try this.

// use Bresenham-like algorithm to print a line from (y1,x1) to (y2,x2) 
// The difference with Bresenham is that ALL the points of the line are 
//   printed, not only one per x coordinate. 
// Principles of the Bresenham's algorithm (heavily modified) were taken from: 
//   http://www.intranet.ca/~sshah/waste/art7.html 
void useVisionLine (int y1, int x1, int y2, int x2) 
{ 
  int i;               // loop counter 
  int ystep, xstep;    // the step on y and x axis 
  int error;           // the error accumulated during the increment 
  int errorprev;       // *vision the previous value of the error variable 
  int y = y1, x = x1;  // the line points 
  int ddy, ddx;        // compulsory variables: the double values of dy and dx 
  int dx = x2 - x1; 
  int dy = y2 - y1; 
  POINT (y1, x1);  // first point 
  // NB the last point can't be here, because of its previous point (which has to be verified) 
  if (dy < 0){ 
    ystep = -1; 
    dy = -dy; 
  }else 
    ystep = 1; 
  if (dx < 0){ 
    xstep = -1; 
    dx = -dx; 
  }else 
    xstep = 1; 
  ddy = 2 * dy;  // work with double values for full precision 
  ddx = 2 * dx; 
  if (ddx >= ddy){  // first octant (0 <= slope <= 1) 
    // compulsory initialization (even for errorprev, needed when dx==dy) 
    errorprev = error = dx;  // start in the middle of the square 
    for (i=0 ; i < dx ; i++){  // do not use the first point (already done) 
      x += xstep; 
      error += ddy; 
      if (error > ddx){  // increment y if AFTER the middle ( > ) 
        y += ystep; 
        error -= ddx; 
        // three cases (octant == right->right-top for directions below): 
        if (error + errorprev < ddx)  // bottom square also 
          POINT (y-ystep, x); 
        else if (error + errorprev > ddx)  // left square also 
          POINT (y, x-xstep); 
        else{  // corner: bottom and left squares also 
          POINT (y-ystep, x); 
          POINT (y, x-xstep); 
        } 
      } 
      POINT (y, x); 
      errorprev = error; 
    } 
  }else{  // the same as above 
    errorprev = error = dy; 
    for (i=0 ; i < dy ; i++){ 
      y += ystep; 
      error += ddx; 
      if (error > ddy){ 
        x += xstep; 
        error -= ddy; 
        if (error + errorprev < ddy) 
          POINT (y, x-xstep); 
        else if (error + errorprev > ddy) 
          POINT (y-ystep, x); 
        else{ 
          POINT (y, x-xstep); 
          POINT (y-ystep, x); 
        } 
      } 
      POINT (y, x); 
      errorprev = error; 
    } 
  } 
  // assert ((y == y2) && (x == x2));  // the last point (y2,x2) has to be the same with the last point of the algorithm 
} 

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0