Page 1 of 1
Missing function in Gomoku tutorial
Posted: 23 August 2022, 06:31
by IndianaScones
In the last section of the Gomoku tutorial, titled "Implement rules and end of game conditions", there is the section:
add the appropriate code in .game.php before proceeding to the next state, using a checkForWin() function implemented separately for clarity. If the game has been won, we set the score, send a score update notification to the client side, and set the 'end_of_game' global variable to 1 as a flag signaling that the game has ended.
Nowhere in the tutorial does it show the code for checkForWin(). I can try writing one myself but the point of the tutorials seems to be to present all of the code necessary. I'm not sure if I'm ready yet to implement the check. Can anybody share the relevant function?
I'm not even sure where to start really, whether it should be written in game.php or in the js file.
Re: Missing function in Gomoku tutorial
Posted: 23 August 2022, 21:04
by tchobello
Hi ! Here are the functions you are looking for.
Code: Select all
function checkForWin( $coord_x, $coord_y, $color )
{
// Get intersections in the same row
$sql = "SELECT
id, coord_x, coord_y, stone_color
FROM
intersection
WHERE
coord_y = $coord_y
ORDER BY
coord_x
";
$intersections = self::getCollectionFromDb( $sql );
if ( $this->checkFiveInARow( $intersections, $coord_x, $coord_y, $color ) ) {
return true;
}
// Get intersections in the same column
$sql = "SELECT
id, coord_x, coord_y, stone_color
FROM
intersection
WHERE
coord_x = $coord_x
ORDER BY
coord_y
";
$intersections = self::getCollectionFromDb( $sql );
if ( $this->checkFiveInARow( $intersections, $coord_x, $coord_y, $color ) ) {
return true;
}
// Get intersections in the same top to bottom diagonal
$sql = "SELECT
id, coord_x, coord_y, stone_color
FROM
intersection
WHERE
( cast(coord_x as signed ) - cast( coord_y as signed) ) = $coord_x - $coord_y
ORDER BY
coord_x
";
$intersections = self::getCollectionFromDb( $sql );
if ( $this->checkFiveInARow( $intersections, $coord_x, $coord_y, $color ) ) {
return true;
}
// Get intersections in the same bottom to top diagonal
$sql = "SELECT
id, coord_x, coord_y, stone_color
FROM
intersection
WHERE
coord_x + coord_y = $coord_x + $coord_y
ORDER BY
coord_x
";
$intersections = self::getCollectionFromDb( $sql );
if ( $this->checkFiveInARow( $intersections, $coord_x, $coord_y, $color ) ) {
return true;
}
return false;
}
function checkFiveInARow( $intersections, $coord_x, $coord_y, $color )
{
// If we find a consecutive set of exactly 5 stones of the same color including this one, it's done!
// Gomoku+ (Caro) variant: the set must not be blocked on either side.
$set = array();
$stoneInSet = false;
$leftBlocked = false;
$rightBlocked = false;
foreach ($intersections as $intersection) {
if ($intersection['stone_color'] == $color) {
if ($set == null) {
// Create set
$set = array();
}
// Add to set
$set[] = $intersection;
// Set flag if current stone is in the set
if ($intersection['coord_x'] == $coord_x && $intersection['coord_y'] == $coord_y) {
$stoneInSet = true;
}
} else {
if (! $stoneInSet) {
$leftBlocked = ($intersection['stone_color'] != null);
// Reset set and go on looping
$set = null;
} else {
$rightBlocked = ($intersection['stone_color'] != null);
// We have the complete set containing the current stone -> break
break;
}
}
}
if ($stoneInSet && $set != null && count($set) == 5) {
if (self::getGameStateValue('game_variant') == VARIANT_GOMOKU_STANDARD) {
return true;
}
if (self::getGameStateValue('game_variant') == VARIANT_GOMOKU_PLUS && ! $leftBlocked && ! $rightBlocked) {
return true;
}
}
return false;
}
Re: Missing function in Gomoku tutorial
Posted: 24 August 2022, 00:34
by IndianaScones
tchobello wrote: ↑23 August 2022, 21:04
Hi ! Here are the functions you are looking for.
This is wonderful, thank you.