# How do I make a gomoku game?

Get help using Construct 2

### » Tue Jul 18, 2017 3:42 am

I have a 5*5 board, When I click the first time, make a blue ball, click the second, make a yellow ball , how to check when there are 3 blue or yellow ball in a row (vertically, horizontally, or diagonally)?
B
13
S
1
Posts: 11
Reputation: 547

### » Tue Jul 18, 2017 9:26 am

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.
The LEVEL GENERATOR TOOLKIT

24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 254
Reputation: 2,332

### » Wed Jul 19, 2017 3:10 am

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?
B
13
S
1
Posts: 11
Reputation: 547

### » Fri Jul 21, 2017 10:06 am

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:

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"`

The LEVEL GENERATOR TOOLKIT

24 functions dedicated to producing randomly generated terrain for all kinds of games.
B
25
S
7
Posts: 254
Reputation: 2,332