Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
authorised move in php
Browse files Browse the repository at this point in the history
  • Loading branch information
ntaffore committed Jan 21, 2018
1 parent cdd0a7b commit 2d98252
Showing 1 changed file with 178 additions and 8 deletions.
186 changes: 178 additions & 8 deletions tablut.game.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,191 @@ public function move(string $fromDiscId, string $toSquareId)
$toX = (int) $toSquarePos[1];
$toY = (int) $toSquarePos[2];

// pawn player identification
$srcPawnFromDb = $this->dbPawnAt($fromX, $fromY);
$pawnPlayerId = (int) $srcPawnFromDb['board_player'];
$pawnIsKing = $srcPawnFromDb['board_king'] ? "'1'" : 'NULL';
if (self::getActivePlayerId() != $pawnPlayerId) {
throw new feException("This pawn belongs to your opponent: pawnPlayerId=$pawnPlayerId | pawnIsKing=$pawnIsKing");

This comment has been minimized.

Copy link
@Lucas-C

Lucas-C Jan 21, 2018

Owner

Pourquoi pas laisser cette vérif ?

$pawnIsOnWall = $srcPawnFromDb['board_wall'] ? true : false ;

// ------------------
// reject play
// ------------------

$RejectMove = false;

This comment has been minimized.

Copy link
@Lucas-C

Lucas-C Jan 21, 2018

Owner

~ variable qui commence par une majuscule

// reject if a pawn is present
$dstSquareFromDb = self::DbQuery("SELECT board_player, board_wall FROM board WHERE board_x = $toX AND board_y = $toY")->fetch_assoc();

This comment has been minimized.

Copy link
@Lucas-C

Lucas-C Jan 21, 2018

Owner

Autant utiliser dbPawnAt comme avant ce commit, ça permet de mieux factoriser le code non ?

if ($dstSquareFromDb['board_player'] != NULL) {
throw new feException("Cannot move onto another pawn");
}

$dstSquareFromDb = $this->dbPawnAt($toX, $toY);
if ($dstSquareFromDb['board_wall'] != null) {
throw new feException("Cannot move onto a wall");
// reject diagonal move
if ($toX != $fromX && $toY != $fromY ) {
throw new feException("Cannot move on diagonal");

This comment has been minimized.

Copy link
@Lucas-C

Lucas-C Jan 21, 2018

Owner

👍

}
if ($dstSquareFromDb['board_player'] != null) {
throw new feException("Cannot move onto another pawn");

// throw an exception if is not in same column without pawn or wall between the disc to the final position
if ( $toX == $fromX ) {

This comment has been minimized.

Copy link
@Lucas-C

Lucas-C Jan 21, 2018

Owner

Il n'y aura pas moyen de factoriser ce code pour éviter la répétition ?
Genre avec un tableau, comme dans findEatenPawns

if ( $fromY < $toY ) {
$dbres_asc = self::DbQuery("SELECT board_y posY, board_wall wall_present, board_player player_present FROM board WHERE board_x = $toX ORDER BY board_y ASC");
// Loop on each position between the start position to the end position and verify that no wall and no pawn
// specific case for down of the wall
while ($Column = mysql_fetch_assoc ($dbres_asc) ) {
if ( $fromY < $Column['posY'] && $Column['posY'] <= $toY ) {
///////////////////
// dbg
//self::notifyAllPlayers('moveposible', "test", array(
//'column' => $Column,
//'IsWall' => $pawnIsOnWall,
//'king' => $pawnIsKing,
//'player' => $pawnPlayerId,
//'egal' => $pawnIsOnWall == true
//));
// End DBG
///////////////////
if( $pawnIsOnWall ){
///////////////////
// dbg
//self::notifyAllPlayers('moveposible2', "test", array(
//'IsWall' => $pawnIsOnWall
//));
//// End DBG
///////////////////
if ($Column['wall_present'] == null ) {
$pawnIsOnWall = false;
}
if ($Column['player_present'] != null ) {
$RejectMove = true;
}

} else {
if ($Column['wall_present'] != null || $Column['player_present'] != null ) {
$RejectMove = true;
///////////////////
// dbg
//self::notifyAllPlayers('Error', "test", array(
// 'Xmove' => true,
// 'wall_present' => $Column['wall_present'],
// 'player_p' => $Column['player_present']
// ));
// End DBG
///////////////////
}
}
}

}
} else {
$dbres_desc = self::DbQuery("SELECT board_y posY, board_wall wall_present, board_player player_present FROM board WHERE board_x = $toX ORDER BY board_y DESC");
// Loop on each position between the start position to the end position and verify that no wall and no pawn
// specific case for down of the wall
while ($Column = mysql_fetch_assoc($dbres_desc) ) {
if ( $Column['posY'] < $fromY && $Column['posY'] >= $toY ) {
///////////////////
// dbg
//self::notifyAllPlayers('moveposible', "test", array(
//'column' => $Column,
//'IsWall' => $pawnIsOnWall,
//'king' => $pawnIsKing,
//'player' => $pawnPlayerId,
//'egal' => $pawnIsOnWall == "1"
//));
// End DBG
///////////////////
if( $pawnIsOnWall ){
///////////////////
// dbg
//self::notifyAllPlayers('moveposible2', "test", array(
//'IsWall' => $pawnIsOnWall
//));
// End DBG
///////////////////
if ($Column['wall_present'] == null ) {
$pawnIsOnWall = false;
}
if ($Column['player_present'] != null ) {
$RejectMove = true;
}

} else {
if ($Column['wall_present'] != null || $Column['player_present'] != null) {
$RejectMove = true;
///////////////////
// dbg
//self::notifyAllPlayers('Error', "test", array(
// 'Xmove2' => true,
// 'wall_present' => $Column['wall_present'],
// 'player_p' => $Column['player_present']
// ));
// End DBG
///////////////////
}
}
}
}
}
} else {
// $toY == $fromY
if ( $fromX < $toX ) {
// Loop on each position between the start position to the end position and verify that no wall and no pawn
// specific case for down of the wall
$dbres_asc = self::DbQuery("SELECT board_X posX, board_wall wall_present, board_player player_present FROM board WHERE board_y = $toY ORDER BY board_y ASC");
while ($row = mysql_fetch_assoc ($dbres_asc) ) {
if ( $fromX < $row['posX'] && $row['posX'] <= $toX ) {
if( $pawnIsOnWall ){
if ($row['wall_present'] == null ) {
$pawnIsOnWall = false;
}
if ($row['player_present'] != null ) {
$RejectMove = true;
}

} else {
if ($row['wall_present'] != null || $row['player_present'] != null ) {
$RejectMove = true;
}
}
}

}
} else {
// Loop on each position between the start position to the end position and verify that no wall and no pawn
// specific case for down of the wall
$dbres_desc = self::DbQuery("SELECT board_X posX, board_wall wall_present, board_player player_present FROM board WHERE board_y = $toY ORDER BY board_y DESC");
while ($row = mysql_fetch_assoc($dbres_desc) ) {
if ( $row['posX'] < $fromX && $row['posX'] >= $toX ) {
if( $pawnIsOnWall ){
if ($row['wall_present'] == null ) {
$pawnIsOnWall = false;
}
if ($row['player_present'] != null ) {
$RejectMove = true;
}

} else {
if ($row['wall_present'] != null || $row['player_present'] != null) {
$RejectMove = true;
}
}
}
}
}
}


// throw an exception if is not in same row without pawn or wall between the disc to the final position
if ($RejectMove) {
///////////////////
// dbg
//self::notifyAllPlayers('Error', "test", array(
// 'invalid move' => true,
// 'reject' => $RejectMove
// ));
// end dbg
///////////////////
throw new feException("Cannot move");

This comment has been minimized.

Copy link
@Lucas-C

Lucas-C Jan 21, 2018

Owner

Ce serait pas plus simple de remplacer tous les $rejectMove = true par ce throw new feException ?
Comme ça la variable $rejectMove ne serait plus nécessaire

}


self::DbQuery("UPDATE board SET board_player=NULL, board_king=NULL WHERE board_x = $fromX AND board_y = $fromY");
self::DbQuery("UPDATE board SET board_player='$pawnPlayerId', board_king=$pawnIsKing WHERE board_x = $toX AND board_y = $toY");

Expand Down

0 comments on commit 2d98252

Please sign in to comment.