How do I testfor 3 in a row

0 favourites
  • 3 posts
  • So I'm making tic tac toe and cannot figure out how to know if there is 3 of the X's or O's in a row. That's it.

  • Fuzzywuzzy

    Hi,

    Since there are only 8 possible ways of winning a tic-tac-toe game (3 verticals, 3 horizontals and 2 diagonals), I would use a 3 by 3 array to check the value of each square. Here is an example of a simple tic-tac-toe game:

    https://www.dropbox.com/s/5q2aulhuxnsbe ... .capx?dl=0

    There are many different ways to go about this, but to me, this was the simplest. I hope this helps.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Although I love an array, you can do this without, using an animation that runs through those 8 configurations. Here's how:

    Make sure your Xs and Os have collision boundaries less than half the size of one game square

    Make a new sprite, WinStateChecker, and give it eight animation frames

    Make each animation frame a line which corresponds to a win-state (as Simone says, 3 vertical, 3 horizontal and two diagonal)

    Set the collision boundaries of the sprite to match the lines of the animation frames

    After each turn, set the WinStateChecker's frame number to 0 then:

    Sprite: Gamepiece - (your Xs and Os)

    Instance Variable: Gamepiece.Player - (a number variable which indicates the owner of the piece)

    Local Variable: CurrentPlayer - (a number variable telling which player played the last piece)

    If Gamepiece overlapping WinStateChecker

    CurrentPlayer = Gamepiece.Player

    Gamepiece.PickedCount = 3

    A positive check on those conditions should indicate a win-state.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)