# Detection Algorithms

So i’m trying to make a basic game, so i decided to make connect 4. It turns out that it is not that basic because i don’t know how do make a detection algorithm. If anybody could help me or tell me if its even possible That would help me a lot.

You could take a look at how iplay88keys implemented recursion in their [GAME] Shape Fill, that might get you started on the right track

I was looking at your game and I saw that for the Shapes you use the item Tiles. does that mean i would need to make the Game Peaces for my game item tiles?

Items and sprites can both have scripts attached to them. I started with sprites for the shapes, but I found that the player character could not walk over them (which is how I handle user selection), so I switched them to items.

1 Like

Since i’m not trying to “mimic” a tile just detect it, would i need to make them items instead of environment tiles?

also i’m not sure how it’ll run but if i make each peace an item and have each one detect a win for its play many that’ll work?

The "mimic" aspect was to have each tile implement the recursive algorithm without having to copy it manually between the tiles. I'd still use items and mimic the scripts if you want to use similar recursive logic to what I have implemented.

I'd have the board or game PulpScript handle checking for win conditions after each piece is played. The board would then kick off the recursive algorithm (probably emanating outward from where the last piece was played). Where my recursive algorithm only checks each tile on the four sides, you could write one to check diagonals as well.

i've been messing around with it for a bit. I'm still very new to coding and I don't really understand how I would go about doing this.

In my "board" PulpScript, I begin the recursive call starting with the upper left cell on the game board, then recursively call each surrounding cell if those cells are within the bounds of the game board and have not yet been marked as visited.

So, for your implementation, you could start with the piece that was just placed and then recursively call outward counting the number of connecting pieces that contain the same player's color, stopping if you hit an empty cell or the other player's color. If opposite directions have connecting pieces that add up to the winning number, then the player whose piece was just played won.

Another option could be to have the board or game PulpScript scan each row/column/diagonal for winning states after each piece is played. This wouldn't require recursion and would probably be easier to implement as a result.

I’m still pushing through… I decided to test every win possibility based off of the last piece.

i’m just working out the bugs🪲 . (its almost finished)