How do I make a gomoku game?

0 favourites
  • 4 posts
From the Asset Store
five golem elements Sprites Sheet.Best for enemy or villain game characters.
  • 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)?

  • 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:

    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" [/code:2s1fo3nl]
    
    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:2s1fo3nl]
    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
    [/code:2s1fo3nl]
    
    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.
  • 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:

    > 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" [/code:3o94uufb]
    
    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:3o94uufb]
    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
    [/code:3o94uufb]
    
    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?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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:

    Array.At( Array.CurX - 1 , Array.CurY )&Array.At( Array.CurX  , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY ) = "yellowyellowyellow"[/code:162de7n3]
    
    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:162de7n3]Array.At( Array.CurX - 1 , Array.CurY - 1 )&Array.At( Array.CurX  , Array.CurY )&Array.At( Array.CurX + 1 , Array.CurY + 1 ) = "yellowyellowyellow"[/code:162de7n3]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)