chessboard.php:
Codice PHP:
<?php

include("chess.php");

class 
Chessboard {

    public 
$chessboard;
    public 
$board;
    public 
$turn;
    public 
$input;
    public 
$output;

    public function 
__construct ($input) {
        
$this->chessboard=array();
        
$this->board=array();
        
$this->turn="";
        
$this->input=$input;
        
$this->output="";
    } 
//__construct
    
    
public function mount () {
        for (
$i=0;$i<8;$i++)
            for (
$j=0;$j<8;$j++) {
                
$piece=substr($this->input,$i*8+$j,$i*8+$j+3);
                switch (
$piece[0]) {
                    case 
'E': {
                        
$this->chessboard[$i][$j]=new EmptyBox($j,$i);
                        break;
                    } 
//case
                    
case 'P': {
                        switch (
$piece[1]) {
                            case 
'W': {
                                switch (
$piece[2]) {
                                    case 
'T': {
                                        
$this->chessboard[$i][$j]=new Pawn($j,$i,"WHITE",true);
                                        break;
                                    } 
//case
                                    
case 'F': {
                                        
$this->chessboard[$i][$j]=new Pawn($j,$i,"WHITE",false);
                                        break;
                                    } 
//case
                                
//switch
                            
//case
                            
case 'B': {
                                switch (
$piece[2]) {
                                    case 
'T': {
                                        
$this->chessboard[$i][$j]=new Pawn($j,$i,"BLACK",true);
                                        break;
                                    } 
//case
                                    
case 'F': {
                                        
$this->chessboard[$i][$j]=new Pawn($j,$i,"BLACK",false);
                                        break;
                                    } 
//case
                                
//switch                            
                            
//case
                        
//switch
                    
//case
                    
case 'N': {
                        switch (
$piece[1]) {
                            case 
'W': {
                                
$this->chessboard[$i][$j]=new Knight($j,$i,"WHITE");
                                break;
                            } 
//case
                            
case 'B': {
                                
$this->chessboard[$i][$j]=new Knight($j,$i,"BLACK");
                                break;
                            } 
//case
                        
//switch
                    
//case
                    
case 'B': {
                        switch (
$piece[1]) {
                            case 
'W': {
                                
$this->chessboard[$i][$j]=new Bishop($j,$i,"WHITE");
                                break;
                            } 
//case
                            
case 'B': {
                                
$this->chessboard[$i][$j]=new Bishop($j,$i,"BLACK");
                                break;
                            } 
//case
                        
//switch
                    
//case
                    
case 'R': {
                        switch (
$piece[1]) {
                            case 
'W': {
                                switch (
$piece[2]) {
                                    case 
'T': {
                                        
$this->chessboard[$i][$j]=new Rook($j,$i,"WHITE",true);
                                        break;
                                    } 
//case
                                    
case 'F': {
                                        
$this->chessboard[$i][$j]=new Rook($j,$i,"WHITE",false);
                                        break;
                                    } 
//case
                                
//switch
                            
//case
                            
case 'B': {
                                switch (
$piece[2]) {
                                    case 
'T': {
                                        
$this->chessboard[$i][$j]=new Rook($j,$i,"BLACK",true);
                                        break;
                                    } 
//case
                                    
case 'F': {
                                        
$this->chessboard[$i][$j]=new Rook($j,$i,"BLACK",false);
                                        break;
                                    } 
//case
                                
//switch                            
                            
//case
                        
//switch
                    
//case
                    
case 'Q': {
                        switch (
$piece[1]) {
                            case 
'W': {
                                
$this->chessboard[$i][$j]=new Queen($j,$i,"WHITE");
                                break;
                            } 
//case
                            
case 'B': {
                                
$this->chessboard[$i][$j]=new Queen($j,$i,"BLACK");
                                break;
                            } 
//case
                        
//switch
                    
//case
                    
case 'K': {
                        switch (
$piece[1]) {
                            case 
'W': {
                                switch (
$piece[2]) {
                                    case 
'T': {
                                        
$this->chessboard[$i][$j]=new King($j,$i,"WHITE",true);
                                        break;
                                    } 
//case
                                    
case 'F': {
                                        
$this->chessboard[$i][$j]=new King($j,$i,"WHITE",false);
                                        break;
                                    } 
//case
                                
//switch
                            
//case
                            
case 'B': {
                                switch (
$piece[2]) {
                                    case 
'T': {
                                        
$this->chessboard[$i][$j]=new King($j,$i,"BLACK",true);
                                        break;
                                    } 
//case
                                    
case 'F': {
                                        
$this->chessboard[$i][$j]=new King($j,$i,"BLACK",false);
                                        break;
                                    } 
//case
                                
//switch                            
                            
//case
                        
//switch
                    
//case                    
                
//switch                
            
//for
        
switch ($this->input[192]) {
            case 
'W': {
                
$this->turn="WHITE";
                break;
            } 
//case
            
case 'B': {
                
$this->turn="BLACK";
                break;
            } 
//case
        
//switch
    
//mount

//Chessboard

?>
test.php:
Codice PHP:
<?php

include("chessboard.php");

$stringa="RBTNBFBBFQBFKBTBBFNBFRBTPBFPBFPBFPBFPBFPBFPBFPBFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFEFFPWFPWFPWFPWFPWFPWFPWFPWFRWTNWFBWFQWFKWTBWFNWFRWTW";
$oggetto=new Chessboard($stringa);
$oggetto->mount();
echo 
$oggetto->chessboard[0][0]->getRank();
echo 
$oggetto->chessboard[0][1]->getRank();
        
?>
L'output dovrebbe essere (secondo le mie previsioni) "RookKnight" in quanto la getRank() restituisce il tipo del pezzo, tuttavia come output ottengo:
codice:
King 
Notice: Undefined offset: 1 
Fatal error: Call to a member function getRank() on a non-object
Qual'è il problema?