Passage à PHP 8 bis

Fermé
Max747 Messages postés 258 Date d'inscription   Statut Membre Dernière intervention   -  
jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,
Revoici le script dans son intégralité jusqu'à la ligne 362.
<?php
//console.log("chessgame.inc.php : OK");
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
//  Author: Dave Lerner <http://Dave-L.com>                                  //
//  ------------------------------------------------------------------------ //
//  Adapted from Online Chess Club (OCC) version 1.0.10, which was written   //
//  by Michael Speck <http://lgames.sf.net> and published under the GNU      //
//  General Public License.                                                  //
//  ------------------------------------------------------------------------ //

/**
 * class ChessGame
 *
 * @package chess
 * @subpackage game
*/
define("gsRunning"   , 0);
define("gsMate"      , 1);
define("gsStalemate" , 2);
define("gsCheck"     , 3);


define('_MD_CHESS_ERROR',             'Error');
define('_MD_CHESS_FENBAD_LENGTH',          'invalid length');
define('_MD_CHESS_FENBAD_FIELD_COUNT',     'wrong number of fields');
define('_MD_CHESS_FENBAD_PP_INVALID',      'piece placement invalid');
define('_MD_CHESS_FENBAD_AC_INVALID',      'active color invalid');
define('_MD_CHESS_FENBAD_CA_INVALID',      'castling availability invalid');
define('_MD_CHESS_FENBAD_EP_INVALID',      'en passant target_square invalid');
define('_MD_CHESS_FENBAD_HC_INVALID',      'halfmove clock invalid');
define('_MD_CHESS_FENBAD_FN_INVALID',      'fullmove number invalid');
define('_MD_CHESS_FENBAD_MATERIAL',        'insufficient mating material');
define('_MD_CHESS_FENBAD_IN_CHECK',        'player to move cannot have opponent in check');
define('_MD_CHESS_FENBAD_CA_INCONSISTENT', 'castling availability inconsistent with piece placement');
define('_MD_CHESS_FENBAD_EP_COLOR',        'en passant target square wrong color');
define('_MD_CHESS_FENBAD_EP_NO_PAWN',      'en passant target square for nonexistent pawn');

/**
 * The purpose of this class is to handle chess moves.
 *
 * An instantiation of this class comprises the data essential for handling chess
 * moves in a specific game, and provides the requisite methods.
 *
 * - Input:      Game state and proposed move.
 * - Processing: Check the legality of the move, and update the game state if the move is legal.
 * - Output:     Indication of the move's legality, and the (possibly) updated game state.
 *
 * In addition to the above, there are utility methods for converting between Standard Algebraic
 * Notation (SAN) and a notation similar to Long Algebraic Notation.
 *
 * @package chess
 * @subpackage game
 */
class ChessGame {

	/**
		* Indicates whether object is valid.
		*
		* If empty string (''), indicates this is a valid object; otherwise contains an error message.
	 * Should be checked after creating an instance of this class.
		*
		* @var string $error
		*/
	var $error;

	 /**
		* gamestate
		*
	 * The game state is represented as an array with the following elements:
	 *
	 *  - 'fen_piece_placement'
	 *  - 'fen_active_color'
	 *  - 'fen_castling_availability'
	 *  - 'fen_en_passant_target_square'
	 *  - 'fen_halfmove_clock'
	 *  - 'fen_fullmove_number'
	 *  - 'pgn_result'
	 *  - 'pgn_fen'
	 *  - 'pgn_movetext'
	 *
	 * The elements prefixed with 'fen_' are standard Forsyth-Edwards Notation (FEN) elements,
	 * and the elements prefixed with 'pgn_' are standard Portable Game Notation (PGN) elements.
	 *
	 * Each element is a string.
		*
		* @var array $gamestate
		*/
	var $gamestate;

	 /**
		* board
		*
	 * A 64-element array, constructed from fen_piece_placement, is used for handling moves.
	 * Its indices are related to the standard tile coordinates as follows:
	 *
		* <pre>
	 * 8 | 56 57 58 59 60 61 62 63
	 * 7 | 48 49 50 51 52 53 54 55
	 * 6 | 40 41 42 43 44 45 46 47
	 * 5 | 32 33 34 35 36 37 38 39
	 * 4 | 24 25 26 27 28 29 30 31
	 * 3 | 16 17 18 19 20 21 22 23
	 * 2 |  8  9 10 11 12 13 14 15
	 * 1 |  0  1  2  3  4  5  6  7
	 *    ------------------------
	 *      a  b  c  d  e  f  g  h
		* </pre>
	 *
	 * For example, $board[17] is tile b3 and $board[55] is tile h7.
		*
		* @var array $board
		*/
	var $board;

	 /**
		* for auto-completion of moves
		* @var string $ac_move
		*/
	var $ac_move;

	 /**
		* array of white's pieces
		* @var array $w_figures
		*/
	var $w_figures;

	 /**
		* array of black's pieces
		* @var array $b_figures
		*/
	var $b_figures;


	 /**
	 * updated by handleMove, not used now but might be used in future
		* @var string $last_move
		*/
	var $last_move;

	 /**
	 * updated by handleMove, not used now but might be used in future
		* @var string $captured_piece
		*/
	var $captured_piece;

// --------------
// PUBLIC METHODS
// --------------

/**
 * constructor
 *
 * If a failure occurs, $this->error is set to a string containing an error message;
 * otherwise $this->error is set to an empty string.
 *
 * Example:
 * <pre>
 *    $chessgame = new ChessGame($fen);
 *    if (!empty($chessgame->error)) {
 *      echo "'$fen' invalid: $chessgame->error\n";
 *    }
 * </pre>
 *
 * @param mixed $param  If $param is an array, an existing game is loaded using $param as the nine-element gamestate array described above.
 * If $param is a non-empty string, a new game is created using $param as a FEN setup position.
 * Otherwise, a new game is created using the standard starting position.
 */
function __construct($param = null)
{
	// for now
	$this->browsing_mode  = 0;

	if (is_array($param)) {

		$this->gamestate = $param;
		$this->error     = '';

	} elseif (is_string($param) and !empty($param)) {

		$this->error = $this->init_gamestate($param);

	} else {

		$this->init_gamestate();
		$this->error = '';
	}
}

/**
 * Handle a move.
 *
 * @param string $move
 * @return array A two-element array:
 *  - $move_performed: true if the move was performed and the game state has been updated, false otherwise
 *  - $move_result_text: text message
 */
function move($move)
{
	empty($this->error) or trigger_error(_MD_CHESS_ERROR, E_USER_ERROR);
	return $this->handleMove($move);
}

/**
 * get game state
 *
 * @return array
 */
 //echo "Bonjour";
 //echo "<br/>";
function gamestate()
{
	empty($this->error) or trigger_error(_MD_CHESS_ERROR, E_USER_ERROR);
	return $this->gamestate;
}

/*if (empty($this->error) === false) 
{
     trigger_error(_MD_JEU_ERROR, E_USER_ERROR);
}
*/

/*$x = $this->gamestate;
echo $x;
echo "<br/>";
exit ("Stop");*/

// ----------------------------------------------------------------
// PRIVATE METHODS - intended for use only by methods of this class
// ----------------------------------------------------------------

/**
 * Initialize gamestate for a new game.
 *
 * If a non-empty string $fen is provided, the game is initialized using $fen as a FEN setup position.
 * Otherwise the game is initialized using the standard starting position.
 *
 * @param  string  $fen
 * @return string  empty string on success, or error message on failure
 *
 * @access private
 */
function init_gamestate($fen = null)
{
	$this->gamestate = array();

	if (!empty($fen)) {
		$setup = true;
	} else {
		$setup = false;
		$fen   = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
	}

	// check that data is not unreasonably short or long
	if (strlen($fen) < 23 or strlen($fen) > 100) {
		return _MD_CHESS_FENBAD_LENGTH; // invalid length
	}

	$fen_data = explode(' ', $fen);

	if (count($fen_data) != 6) {
		return _MD_CHESS_FENBAD_FIELD_COUNT; // wrong number of fields
	}

	$this->gamestate['fen_piece_placement']          = $fen_data[0];
	$this->gamestate['fen_active_color']             = $fen_data[1];
	$this->gamestate['fen_castling_availability']    = $fen_data[2];
	$this->gamestate['fen_en_passant_target_square'] = $fen_data[3];
	$this->gamestate['fen_halfmove_clock']           = $fen_data[4];
	$this->gamestate['fen_fullmove_number']          = $fen_data[5];
	$this->gamestate['pgn_fen']                      = $setup ? $fen : null;
	$this->gamestate['pgn_result']                   = '*';
	$this->gamestate['pgn_movetext']                 = '*';
var_dump ($this->gamestate['fen_en_passant_target_square']);
	if (!$this->fen_piece_placement_to_board())
	{
		return _MD_CHESS_FENBAD_PP_INVALID; // piece_placement invalid
	}
	elseif ($this->gamestate['fen_active_color'] != 'w' and $this->gamestate['fen_active_color'] != 'b')
	{
		return _MD_CHESS_FENBAD_AC_INVALID; // active_color invalid
	}
	// Since fen_piece_placement_to_board() checked $fen for the correct number of fields above, $castling_availability is non-empty.
	elseif ($this->gamestate['fen_castling_availability'] != '-' and !preg_match('/^(?:K\w?)?(?:Q\w?)?(?:k\w?)?(?:q\w?)?$/', $this->gamestate['fen_castling_availability']))
	{
		return _MD_CHESS_FENBAD_CA_INVALID; // castling_availability invalid
	}
	elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and !preg_match('/^[a-h][36]$/', $this->gamestate['fen_en_passant_target_square']))
	{
		return _MD_CHESS_FENBAD_EP_INVALID; // en_passant_target_square invalid
	}
	elseif (!preg_match('/^\d{0,4}$/', $this->gamestate['fen_halfmove_clock']))
	{
		return _MD_CHESS_FENBAD_HC_INVALID; // halfmove_clock invalid
	}
	elseif (!preg_match('/^\d{0,4}$/', $this->gamestate['fen_fullmove_number']) or $this->gamestate['fen_fullmove_number'] < 1)
	{
		return _MD_CHESS_FENBAD_FN_INVALID; // fullmove_number invalid
	}
	elseif ($this->insufficient_mating_material())
	{
		return _MD_CHESS_FENBAD_MATERIAL; // insufficient mating material
	}
	elseif (($this->gamestate['fen_active_color'] == 'w' and $this->kingIsUnderAttack('b', 'w'))
		or   ($this->gamestate['fen_active_color'] == 'b' and $this->kingIsUnderAttack('w', 'b')))
	{
		return _MD_CHESS_FENBAD_IN_CHECK; // player to move cannot have opponent in check
	}
	elseif ((strstr($this->gamestate['fen_castling_availability'], 'K') and ($this->board[ 4] != 'wK' or $this->board[ 7] != 'wR'))
		or   (strstr($this->gamestate['fen_castling_availability'], 'Q') and ($this->board[ 4] != 'wK' or $this->board[ 0] != 'wR'))
		or   (strstr($this->gamestate['fen_castling_availability'], 'k') and ($this->board[60] != 'bK' or $this->board[63] != 'bR'))
		or   (strstr($this->gamestate['fen_castling_availability'], 'q') and ($this->board[60] != 'bK' or $this->board[56] != 'bR')))
	{
		return _MD_CHESS_FENBAD_CA_INCONSISTENT; // castling availability inconsistent with piece placement
	}
	elseif ($this->gamestate['fen_en_passant_target_square'] != '-'  && (
              ($this->gamestate['fen_en_passant_target_square'][1] == 3 && $this->gamestate['fen_active_color'] != 'b')
		||  ($this->gamestate['fen_en_passant_target_square'][1] == 6 && $this->gamestate['fen_active_color'] != 'w')
		))
	{
		return _MD_CHESS_FENBAD_EP_COLOR; // en passant target square wrong color
	}
	//elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 3
		//and $this->board[$this->boardCoordToIndex($this->gamestate['fen_en_passant_target_square']{0} . '4')] != 'wP')
	elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square'][1] == 3
		and $this->board[$this->boardCoordToIndex($this->gamestate['fen_en_passant_target_square'][0] . '4')] != 'wP')	
	{
		return _MD_CHESS_FENBAD_EP_NO_PAWN; // en passant target square for nonexistent pawn
	}
	//elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 6
		//and $this->board[$this->boardCoordToIndex($this->gamestate['fen_en_passant_target_square']{0} . '5')] != 'bP')
	elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square'][1] == 6
		and $this->board[$this->boardCoordToIndex($this->gamestate['fen_en_passant_target_square'][0] . '5')] != 'bP')	
	{
		return _MD_CHESS_FENBAD_EP_NO_PAWN; // en passant target square for nonexistent pawn
	}

	#echo "In " . __CLASS__ . '::' . __FUNCTION__ . "\n";#*#DEBUG#
	#var_dump('gamestate', $this->gamestate);#*#DEBUG#

	// save the current FEN to the board
	$this->fen_piece_placement_to_board( );
	return ''; // successful
}

Et le message d'erreur au format texte:
Fatal error: Array and string offset access syntax with curly braces is no longer supported in C:\wamp64\www\echecs\includes\chessgame.inc.php on line 554


Configuration: Windows / Firefox 93.0
A voir également:

1 réponse

jordane45 Messages postés 38486 Date d'inscription   Statut Modérateur Dernière intervention   4 752
 
Pas besoin de créer une nouvelle discussion !!!
Et puis....
Revoici le script dans son intégralité jusqu'à la ligne 362.

ça ne va pas nous être très utile ...alors que le message d'erreur parle de la ligne554

0