Search Unity

How do you detect four objects in a line

Discussion in 'Scripting' started by herbie, Jun 8, 2013.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Hi,

    Somewhere in my 2D game there is some kind of checkerboard with colored stones. You can move these stones with the mouse in horizonal and vertical direction. When you have four stones in a line (horizontal, vertical or diagonal), you get points.
    How do you detect if four stones are in a line?

    For example, how would you make the four-in-a-line detection of this game in Unity? http://www.mathsisfun.com/games/connect4.html

    Can somebody push me in the right direction?
     
  2. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    That example places onbects in a grid-like fasion. All the objects will be in a multi dimentional array and for each time an object has been placed you will have to run a set of checks, checking each object individually. Those checks might be divided into vertical check, horizontal check and diagonal check.

    For example you might start with the first object and do a for lop that check the four items to the right. As soon as one of the items does not match, the loop stops and move on to the next item, same process until you find one loop that runs through all four items and you have a match. Hope this is clear enough?
     
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Ok, I think that's the way to do it.
    Thanks!