4 posts
• Page **1** of **1**

To do this you'll want loops, an an array will make it easy too. Add an array to your project with a Width of 5, a Height of 5, and a Depth of 1. The array can be seen as a 'spreadsheet' where you store the result of each turn. When a player places a piece, fill the appropriate cell in the array with the appropriate value. For example:

Running this at the end of each turn will make it much easier to check for a win-state - which is what we check for next. There are a lot of ways to do this, but here's what I can think of:

This seems pretty long-winded but it's actually a single event, which is nice. The 'For each element (xy)' action means the event will be run for every cell in the Array's x,y dimension. The first of the two values to compare is a long 'or' expression - each vertical line '|' character can be read as 'or'. The conditions in the expression create strings that represent the orthogonal lines through the current cell, and compare them to strings that define a win-state, eg: "blueblueblue" or "yellowyellowyellow". Because they are part of an 'or' expression, if any of the conditions in this expression are met, the output of the expression will be "1" - so the second value with which to compare the first should just be "1".

Place whatever actions go in your win condition into this action - good luck! What a fun little problem.

- Code: Select all
`The YELLOW player's turn, they have placed a piece at X:`

.....

.YYX.

..B..

..B..

.....

Array > Set at x,y > X: 3, Y: 1, Value: "yellow"

Running this at the end of each turn will make it much easier to check for a win-state - which is what we check for next. There are a lot of ways to do this, but here's what I can think of:

- Code: Select all
`Array > For each element (xy)`

System > Compare two values >

First Value: Array.At( Array.CurX - 1 , Array.CurY )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY ) = "yellowyellowyellow" | Array.At( Array.CurX , Array.CurY + 1 )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX , Array.CurY - 1 ) = "yellowyellowyellow" | Array.At( Array.CurX - 1 , Array.CurY )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY ) = "blueblueblue" | Array.At( Array.CurX , Array.CurY + 1 )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX , Array.CurY - 1 ) = "blueblueblue"

Second Value: 1

This seems pretty long-winded but it's actually a single event, which is nice. The 'For each element (xy)' action means the event will be run for every cell in the Array's x,y dimension. The first of the two values to compare is a long 'or' expression - each vertical line '|' character can be read as 'or'. The conditions in the expression create strings that represent the orthogonal lines through the current cell, and compare them to strings that define a win-state, eg: "blueblueblue" or "yellowyellowyellow". Because they are part of an 'or' expression, if any of the conditions in this expression are met, the output of the expression will be "1" - so the second value with which to compare the first should just be "1".

Place whatever actions go in your win condition into this action - good luck! What a fun little problem.

<iframe frameborder="0" src="https://itch.io/embed/146124?linkback=true&dark=true" width="552" height="167"></iframe>

**Posts:**254**Reputation:**2,389

B

26
S

7
mrtumbles wrote:To do this you'll want loops, an an array will make it easy too. Add an array to your project with a Width of 5, a Height of 5, and a Depth of 1. The array can be seen as a 'spreadsheet' where you store the result of each turn. When a player places a piece, fill the appropriate cell in the array with the appropriate value. For example:

- Code: Select all
`The YELLOW player's turn, they have placed a piece at X:`

.....

.YYX.

..B..

..B..

.....

Array > Set at x,y > X: 3, Y: 1, Value: "yellow"

Running this at the end of each turn will make it much easier to check for a win-state - which is what we check for next. There are a lot of ways to do this, but here's what I can think of:

- Code: Select all
`Array > For each element (xy)`

System > Compare two values >

First Value: Array.At( Array.CurX - 1 , Array.CurY )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY ) = "yellowyellowyellow" | Array.At( Array.CurX , Array.CurY + 1 )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX , Array.CurY - 1 ) = "yellowyellowyellow" | Array.At( Array.CurX - 1 , Array.CurY )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY ) = "blueblueblue" | Array.At( Array.CurX , Array.CurY + 1 )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX , Array.CurY - 1 ) = "blueblueblue"

Second Value: 1

This seems pretty long-winded but it's actually a single event, which is nice. The 'For each element (xy)' action means the event will be run for every cell in the Array's x,y dimension. The first of the two values to compare is a long 'or' expression - each vertical line '|' character can be read as 'or'. The conditions in the expression create strings that represent the orthogonal lines through the current cell, and compare them to strings that define a win-state, eg: "blueblueblue" or "yellowyellowyellow". Because they are part of an 'or' expression, if any of the conditions in this expression are met, the output of the expression will be "1" - so the second value with which to compare the first should just be "1".

Place whatever actions go in your win condition into this action - good luck! What a fun little problem.

Thanks,

I did it but it only checked horizontally and vertically, but how to check in diagonally?

**Posts:**11**Reputation:**547

B

13
S

1
Congratulations! To check diagonally you'll need to add to that 'OR' condition (I didn't realise diagonals counted in gomoku!). You see how it currently checks at X/Y +/-1, like so:

Note these lines check orthogonally. For example, X-1, X, X+1 describes a horizontal line. You'll have to add new checks to the OR expression which describe diagonal lines. Here's a starter, a diagonal line running top-left to bottom-right:

- Code: Select all
`Array.At( Array.CurX - 1 , Array.CurY )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY ) = "yellowyellowyellow"`

Note these lines check orthogonally. For example, X-1, X, X+1 describes a horizontal line. You'll have to add new checks to the OR expression which describe diagonal lines. Here's a starter, a diagonal line running top-left to bottom-right:

- Code: Select all
`Array.At( Array.CurX - 1 , Array.CurY - 1 )&Array.At( Array.CurX , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY + 1 ) = "yellowyellowyellow"`

<iframe frameborder="0" src="https://itch.io/embed/146124?linkback=true&dark=true" width="552" height="167"></iframe>

**Posts:**254**Reputation:**2,389

B

26
S

7
4 posts
• Page **1** of **1**