Location: PHPKode > scripts > Some Chess > some-chess/gameFunc.php
<?php
//		Some Chess, a PHP multi-player chess server.
//		Copyright (C) 2006 Jon Link
function newGame($wID,$bID,$gameFuncStr){
	//$setup		= array('a8'=>'rd','b8'=>'nd','c8'=>'bd','d8'=>'qd','e8'=>'kd','f8'=>'bd','g8'=>'nd','h8'=>'rd','a7'=>'pd','b7'=>'pd','c7'=>'pd','d7'=>'pd','e7'=>'pd','f7'=>'pd','g7'=>'pd','h7'=>'pd','a1'=>'rl','b1'=>'nl','c1'=>'bl','d1'=>'ql','e1'=>'kl','f1'=>'bl','g1'=>'nl','h1'=>'rl','a2'=>'pl','b2'=>'pl','c2'=>'pl','d2'=>'pl','e2'=>'pl','f2'=>'pl','g2'=>'pl','h2'=>'pl');
	//$castle		= array('k'=>1,'a'=>1,'h'=>1);
	if(!$wID || !$bID){
		return '<div class="error">'.$gameFuncStr[18].'</div>';
	}else{
		$setup			= 'a:32:{s:2:"a8";s:2:"rd";s:2:"b8";s:2:"nd";s:2:"c8";s:2:"bd";s:2:"d8";s:2:"qd";s:2:"e8";s:2:"kd";s:2:"f8";s:2:"bd";s:2:"g8";s:2:"nd";s:2:"h8";s:2:"rd";s:2:"a7";s:2:"pd";s:2:"b7";s:2:"pd";s:2:"c7";s:2:"pd";s:2:"d7";s:2:"pd";s:2:"e7";s:2:"pd";s:2:"f7";s:2:"pd";s:2:"g7";s:2:"pd";s:2:"h7";s:2:"pd";s:2:"a1";s:2:"rl";s:2:"b1";s:2:"nl";s:2:"c1";s:2:"bl";s:2:"d1";s:2:"ql";s:2:"e1";s:2:"kl";s:2:"f1";s:2:"bl";s:2:"g1";s:2:"nl";s:2:"h1";s:2:"rl";s:2:"a2";s:2:"pl";s:2:"b2";s:2:"pl";s:2:"c2";s:2:"pl";s:2:"d2";s:2:"pl";s:2:"e2";s:2:"pl";s:2:"f2";s:2:"pl";s:2:"g2";s:2:"pl";s:2:"h2";s:2:"pl";}';
		$castle			= 'a:3:{s:1:"k";i:1;s:1:"a";i:1;s:1:"h";i:1;}';
		$queryNewGame	= 'INSERT INTO games (whitePlayerID,blackPlayerID,setup,bCastle,wCastle) VALUES ("'.$wID.'","'.$bID.'",\''.$setup.'\',\''.$castle.'\',\''.$castle.'\')';
		mysql_query($queryNewGame) or die('<div class=error>'.errorDBStr.'game</div>');
		return '<div class="message">'.$gameFuncStr[17].'</div>';
	}
}
function mated($id,$gameID,$matedStr){
	$queryGames		= 'UPDATE games SET winner="'.$id.'" WHERE gameID="'.$gameID.'"';
	$resultGames	= mysql_query($queryGames) or die('<div class=error>'.errorDBStr.' games</div>');
	if(deleteChat == true) killchat($gameID);
	movesToPGN($gameID);
}
function resign($id,$gameID,$gameFuncStr){
	$queryGame		= 'SELECT blackPlayerID,whitePlayerID FROM games WHERE gameID="'.$gameID.'" LIMIT 1';
	$resultGame		= mysql_query($queryGame) or die('<div class="error">'.errorDBStr.'games</div>');	
	if($id == mysql_result($resultGame,0,'whitePlayerID')){$oppID = mysql_result($resultGame,0,'blackPlayerID');}else{$oppID = mysql_result($resultGame,0,'whitePlayerID');}
	$queryGames		= 'UPDATE games SET winner="'.$oppID.'" WHERE gameID="'.$gameID.'"';
	$resultGames	= mysql_query($queryGames) or die('<div class="error">'.errorDBStr.'games</div>');	
	if(deleteChat == true)killchat($gameID);
	movesToPGN($gameID);
	return '<div class="message">'.$gameFuncStr[1].'</div>';
}
function draw($id,$gameID,$gameFuncStr){
	$queryGame		= 'SELECT reqDraw,blackPlayerID,whitePlayerID FROM games WHERE gameID="'.$gameID.'" LIMIT 1';
	$resultGame		= mysql_query($queryGame) or die('<div class="error">'.errorDBStr.'games</div>');	
	if($id != mysql_result($resultGame,0,'blackPlayerID')){ $oppID = mysql_result($resultGame,0,'blackPlayerID'); }else{ $oppID = mysql_result($resultGame,0,'whitePlayerID'); }
	$draw 			= mysql_result($resultGame,0,'reqDraw');
	if(!$draw){
		$queryGames		= 'UPDATE games SET reqDraw="'.$id.'" WHERE gameID="'.$gameID.'"';
		$resultGames	= mysql_query($queryGames) or die('<div class="error">'.errorDBStr.'games</div>');
		return '<div class="message">'.$gameFuncStr[2].'</div>';
	}elseif($draw != $id){
		$queryGames		= 'UPDATE games SET winner="D" WHERE gameID="'.$gameID.'"';
		$resultGames	= mysql_query($queryGames) or die('<div class="error">'.errorDBStr.'games</div>');	
		if(deleteChat == true) killchat($gameID);
		movesToPGN($gameID);
		return '<div class="message">'.$gameFuncStr[3].'</div>';
	}else{
		return '<div class="message">'.$gameFuncStr[4].'</div>';
	}
}
function undo($id,$gameID,$gameFuncStr){
	$queryGame		= 'SELECT * FROM games WHERE gameID="'.$gameID.'" LIMIT 1';
	$resultGame		= mysql_query($queryGame) or die('<div class="error">'.errorDBStr.'games</div>');	
	if($id != mysql_result($resultGame,0,'blackPlayerID')){$oppID = mysql_result($resultGame,0,'blackPlayerID');}else{$oppID = mysql_result($resultGame,0,'whitePlayerID');}
	$undo 			= mysql_result($resultGame,0,'reqUndo');
	if(!$undo){
		$queryGames		= 'UPDATE games SET reqUndo="'.$id.'" WHERE gameID="'.$gameID.'"';
		$resultGames	= mysql_query($queryGames) or die('<div class="error">'.errorDBStr.'games</div>');
		return '<div class="message">'.$gameFuncStr[14].'</div>';
	}elseif($undo != $id){
		$nextMoveNum 	= mysql_result($resultGame,0,'nextMoveNum');
		$lastMove 		= unserialize(mysql_result($resultGame,0,'lastMove'));
		$nextTurnColor 	= mysql_result($resultGame,0,'nextTurnColor');		
		$undoneNTC		= ($nextTurnColor == 'white')? 'black' : 'white';
		$undoneMoNum 	= ($undoneNTC == 'black')? $nextMoveNum-1 : $nextMoveNum;
		$canCastle 		= unserialize(mysql_result($resultGame,0,$undoneNTC{0}.'Castle'));
		$queryMoves		= 'SELECT * FROM moves WHERE gameID="'.$gameID.'" ORDER BY moveNum DESC LIMIT 1';
		$resultMoves	= mysql_query($queryMoves) or die('<div class="error">'.errorDBStr.'move</div>');
		$LaMove['move']	= mysql_result($resultMoves,0,$nextTurnColor.'Move');
		$undoneLaMove	= serialize($LaMove);
		$pgn			= movesToPGN($gameID,1);	
		$movedPiece		= $lastMove['move']{0};
		if($lastMove['move']{0} == 'O'){
			$canCastle['k'] = 1;
			$canCastle['km'] = null;
		}elseif($movedPiece == 'K' || $movedPiece == 'R'){
			if($movedPiece == 'K' && $canCastle['km'] == $nextMoveNum){
				$canCastle['k'] = 1;
				$canCastle['km'] = null;
			}elseif($movedPiece == 'R' && $canCastle['am'] == $nextMoveNum){
				$canCastle['a'] = 1;
				$canCastle['am'] = null;
			}elseif($movedPiece == 'R' && $canCastle['hm'] == $nextMoveNum){
				$canCastle['h'] = 1;
				$canCastle['hm'] = null;				
			}
		}
		$unCanCastle	= serialize($canCastle);
		$playerMove		= ($undoneNTC == 'white')? '2' : '1';
		$undoneLocat	= serialize(parsePGN($pgn,($nextMoveNum -1),$playerMove));
		if($undoneNTC == 'black'){
			$queryUnMoves	= 'UPDATE moves SET '.$undoneNTC.'Move="" WHERE gameID="'.$gameID.'" AND moveNum="'.$undoneMoNum.'"';
		}else{
			$queryUnMoves	= 'DELETE FROM moves WHERE gameID="'.$gameID.'" AND moveNum="'.$undoneMoNum.'"';
		}
		mysql_query($queryUnMoves) or die('<div class="error">'.errorDBStr.'moves</div>'.$queryUnGames);	
		$queryUnGames	= 'UPDATE games SET reqUndo="", lastMove=\''.$undoneLaMove.'\', nextTurnColor="'.$undoneNTC.'", nextMoveNum="'.$undoneMoNum.'", setup=\''.$undoneLocat.'\', '.$undoneNTC{0}.'Castle=\''.$unCanCastle.'\' WHERE gameID="'.$gameID.'"';
		mysql_query($queryUnGames) or die('<div class="error">'.errorDBStr.'games</div>'.$queryUnGames);	
		return '<div class="message">'.$gameFuncStr[13].'</div>';
	}else{
		return '<div class="message">'.$gameFuncStr[14].'</div>';
	}
}
function ended($id,$gameID,$gameFuncStr){
	$queryGame		= 'SELECT * FROM games WHERE gameID="'.$gameID.'" LIMIT 1';
	$resultGame		= mysql_query($queryGame) or die('<div class="error">'.errorDBStr.'games</div>');	
	$nextMoveNum	= mysql_result($resultGame,0,'nextMoveNum');
	if($id == mysql_result($resultGame,0,'blackPlayerID')){ 
		$oppID 			= mysql_result($resultGame,0,'whitePlayerID'); 
		$playerColor 	= 'black'; 
	}else{ 
		$oppID 			= mysql_result($resultGame,0,'blackPlayerID'); 
		$playerColor 	= 'white'; 
	}
	if(mysql_result($resultGame,0,'nextTurnColor') == $playerColor && $nextMoveNum > 2){
		$queryGames		= 'UPDATE games SET winner="'.$oppID.'" WHERE gameID="'.$gameID.'"';
		$finished		= '<div class="message">'.$gameFuncStr[5].'</div>';
	}elseif(mysql_result($resultGame,0,'nextTurnColor') != $playerColor && $nextMoveNum > 2){
		$queryGames		= 'UPDATE games SET winner="'.$id.'" WHERE gameID="'.$gameID.'"';
		$finished		= '<div class="message">'.$gameFuncStr[6].'</div>';
	}else{
		$queryGames		= 'UPDATE games SET winner="X" WHERE gameID="'.$gameID.'"';
		$finished		= '<div class="message">'.$gameFuncStr[7].'</div>';
	}
	$resultGames	= mysql_query($queryGames) or die('<div class="error">'.errorDBStr.'games</div>');	
	if(deleteChat == true) killchat($gameID);
	movesToPGN($gameID);
	return $finished;
}
function killChat($gameID){
	$gameID			= $gameID;
	$queryKill 		= 'DELETE FROM chat WHERE gameID="'.$gameID.'"';
	mysql_query($queryKill) or die('<div class="error">'.errorDBStr.'player</div>');
}
function movesToPGN($gameID,$justConvert=false){
	$gameID			= $gameID;
	$queryGames		= 'SELECT * FROM games WHERE gameID="'.$gameID.'" LIMIT 1';
	$resultGames	= mysql_query($queryGames) or die('<div class="error">'.errorDBStr.'games</div>');
	$winner	 		= mysql_result($resultGames,0,'winner');
	$wID	 		= mysql_result($resultGames,0,'whitePlayerID');
	$bID	 		= mysql_result($resultGames,0,'blackPlayerID');
	$gameDate 		= mysql_result($resultGames,0,'gameDate');
	$pgnDate		= substr($gameDate,0,4).'.'.(substr($gameDate,4,2)).'.'.(substr($gameDate,6,2));
	if($winner == $wID){$result='1-0';}elseif($winner == $bID){$result='0-1';}elseif($winner='D'){$result='1/2-1/2';}else{$result='*';}
	$queryPlayers	= 'SELECT * FROM players WHERE id="'.$bID.'" OR id="'.$wID.'" LIMIT 2';
	$resultPlayers	= mysql_query($queryPlayers) or die('<div class="error">'.errorDBStr.'players</div>');
	$names[mysql_result($resultPlayers,0,'id')]	= mysql_result($resultPlayers,0,'name');
	$names[mysql_result($resultPlayers,1,'id')]	= mysql_result($resultPlayers,1,'name');
	$queryMoves		= 'SELECT * FROM moves WHERE gameID="'.$gameID.'" ORDER BY moveNum ASC';
	$resultMoves	= mysql_query($queryMoves) or die('<div class="error">'.errorDBStr.'moves</div>');
	$movesNum		= mysql_num_rows($resultMoves);
	$pgnHead 		='[Event "Some Chess Game"]
[Site "Some Chess"]
[Date "'.$pgnDate.'"]
[Round ""]
[White "'.$names[$wID].'"]
[Black "'.$names[$bID].'"]
[Result "'.$result.'"]
[Board	"'.$gameID.'"]
';
	for($i=0;$i<$movesNum;++$i) $pgnMoves .= mysql_result($resultMoves,$i,'moveNum').'.'.(mysql_result($resultMoves,$i,'whiteMove')).' '.(mysql_result($resultMoves,$i,'blackMove')).' ';
	$players	= $names[$wID].' Vs. '.$names[$bID];
	$pgn 		= $pgnHead.$pgnMoves;
	if(!$justConvert){
		$queryPGN	= 'INSERT INTO complete (gameID,players,result,pgn) VALUES ("'.$gameID.'","'.$players.'","'.$result.'",\''.$pgn.'\')';
		mysql_query($queryPGN) or die('<div class="error">'.errorDBStr.'player</div>'.$queryPGN);
	}
	if(deleteMoves && !$justConvert){
		$killMoves 	= 'DELETE FROM moves WHERE gameID="'.$gameID.'"';
		mysql_query($killMoves) or die('<div class="error">'.errorDBStr.'player</div>');
	}
	return $pgn;
}
function parsePGN($pgn,$movesNum=null,$playerMove=2,$toMoves=null,$newGame=null){
	$setup	= array('a8'=>'rd','b8'=>'nd','c8'=>'bd','d8'=>'qd','e8'=>'kd','f8'=>'bd','g8'=>'nd','h8'=>'rd','a7'=>'pd','b7'=>'pd','c7'=>'pd','d7'=>'pd','e7'=>'pd','f7'=>'pd','g7'=>'pd','h7'=>'pd','a1'=>'rl','b1'=>'nl','c1'=>'bl','d1'=>'ql','e1'=>'kl','f1'=>'bl','g1'=>'nl','h1'=>'rl','a2'=>'pl','b2'=>'pl','c2'=>'pl','d2'=>'pl','e2'=>'pl','f2'=>'pl','g2'=>'pl','h2'=>'pl');
	$pgn	= trim(str_replace('\r\n',' ',$pgn));
	$pgn	= stripslashes($pgn);
	if($newGame){
		$white	= preg_replace('/[\.\D\S\W]*\[white "/i','',$pgn);
		$white	= preg_replace('/"\][\.\D\S\W]*/','',$white);			
		$black	= preg_replace('/[\.\D\S\W]*\[black "/i','',$pgn);
		$black	= preg_replace('/"\][\.\D\S\W]*/','',$black);
		$date	= preg_replace('/[\.\D\S\W]*\[date "/i','',$pgn);
		$date	= preg_replace('/"\][\.\D\S\W]*/','',$date);
		if(strpos('board',$pgn)){
			$importID = preg_replace('/[\.\D\S\W]*\[board "/i','',$pgn);
			$importID = preg_replace('/"\][\.\D\S\W]*/','',$importID);

			$queryGames = 'SELECT * FROM games WHERE gameID="'.$importID.'"'; 
			$resultGames= mysql_query($queryGames) or die('<div class="error">'.errorDBStr.'games (pp1)</div>');
			if(mysql_num_rows($queryGames) == 1) $inDB = true;
		}
		
		$queryPlayers 		= 'SELECT * FROM players WHERE name="'.$black.'"'; 
		$resultPlayers 		= mysql_query($queryPlayers) or die('<div class="error">'.errorDBStr.'players (pp2)</div>');		
		if(mysql_num_rows($resultPlayers) == 1){
			$blackID		= mysql_result($resultPlayers,0,'id');
		}else{
			$makePlayers 	= 'INSERT INTO players (name,invitedBy) VALUES ("'.$black.'","X")';
			mysql_query($makePlayers)or die('<div class="error">'.errorDBStr.'players (pp3)</div>');
			$blackID		= mysql_insert_id();
		}		
		$queryPlayers 		= 'SELECT * FROM players WHERE name="'.$white.'"'; 
		$resultPlayers 		= mysql_query($queryPlayers) or die('<div class="error">'.errorDBStr.'players (pp4)</div>');
		if(mysql_num_rows($resultPlayers) == 1){
			$whiteID		= mysql_result($resultPlayers,0,'id');
		}else{
			$makePlayers 	= 'INSERT INTO players (name,invitedBy) VALUES ("'.$white.'","X")';
			mysql_query($makePlayers)or die('<div class="error">'.errorDBStr.'players (pp5)</div>');
			$whiteID		= mysql_insert_id();
		}
		if(!$inDB){
			$setupDB	 		= serialize($setup);
			$queryGame	 		= 'INSERT INTO games (whitePlayerID,blackPlayerID,nextMoveNum,nextTurnColor,setup) VALUES ("'.$whiteID.'","'.$blackID.'","1","white",\''.$setupDB.'\')';
			mysql_query($queryGame)or die('<div class="error">'.$queryGame.' '.errorDBStr.'games (pp6)</div>');
			$gameID				= mysql_insert_id();
		}else{
			$gameID = $importID;
		}
	}
	$moves 	= trim(preg_replace('/\[.*\]/','',$pgn));
	$moves	= preg_split('/\d*\./',$moves);
	if(!$movesNum) $movesNum = count($moves);
	$playerMovesNum = 2;
	for($i=1;$i<=$movesNum;++$i){
		$moveSet 	= explode(' ',$moves[$i]);
		if($toMoves && !empty($moveSet[0])){
			$queryMove	= 'INSERT INTO moves (gameID,moveNum,whiteMove,blackMove) VALUES ("'.$gameID.'","'.$i.'","'.$moveSet[0].'","'.$moveSet[1].'")';
			mysql_query($queryMove) or die('<div class="error">'.errorDBStr.'moves (pp7)</div>');
		}
		if($i>$movesNum-1)$playerMovesNum = $playerMove;		
		for($x=0;$x<$playerMovesNum;++$x){
			unset($oldSpot,$newSpot,$specific);
			$move 			= $moveSet[$x];			
			$pieceColor 	= ($x == 0)? 'l' : 'd';
			if(strpos($move,'K') !== false){
				$piece 						= 'k'.$pieceColor;
				$oldSQ						= array_search($piece,$setup);
				$setup[$oldSQ] 				= null;
				$setup[substr($move,-2)] = $piece;
 			}elseif($move == 'O-O'){
				$piece 	= 'k'.$pieceColor;
				if($pieceColor == 'd'){
					$setup['e8'] = null;
					$setup['h8'] = null;
					$setup['f8'] = 'rd';
					$setup['g8'] = $piece;
				}else{
					$setup['e1'] = null;
					$setup['h1'] = null;
					$setup['f1'] = 'rl';
					$setup['g1'] = $piece;
				}
			}elseif($move == 'O-O-O'){
				$piece 	= 'k'.$pieceColor;
				if($pieceColor == 'd'){
					$setup['e8'] = null;
					$setup['a8'] = null;
					$setup['d8'] = 'rd';
					$setup['c8'] = $piece;
				}else{
					$setup['e1'] = null;
					$setup['a1'] = null;
					$setup['d1'] = 'rl';
					$setup['c1'] = $piece;
				}
 			}elseif(strpos($move,'Q') !== false){
 				$piece = 'q'.$pieceColor;
				$oldSQ				= array_search($piece,$setup);
				$setup[$oldSQ] 		= null;
				$newSpot			= str_replace('+','',$move);
				$newSpot			= str_replace('#','',$newSpot);
				$newSpot			= substr($newSpot,-2);
				$setup[$newSpot]	= $piece;
			}elseif(strpos($move,'B') !== false){
				$piece = 'b'.$pieceColor;			
				$newSpot	= str_replace('+','',$move);
				$newSpot	= str_replace('#','',$newSpot);
				$newSpot	= str_replace('x','',$newSpot);	
				if(strlen($newSpot)>3) $specific = $newSpot{1};
				$newSpot	= substr($newSpot,-2);
				$pieceArray	= array_keys($setup,$piece);
				$arrayCount	= count($pieceArray);				
				for($n=0;$n<$arrayCount;++$n){
					$square	= $pieceArray[$n];
					if($specific){
						if($square{0} == $specific || $square{1} == $specific) $oldSpot	= $square;
					}elseif(!$oldSpot){
						if(abs(ord($newSpot{0})-ord($square{0})) == abs($newSpot{1}-$square{1})){
							if($newSpot{0} > $square{0} && $newSpot{1} > $square{1}){
								$gapSize = abs($newSpot{1} - $square{1});
								$direc	 = 'ur';
							}elseif($newSpot{0} < $square{0} && $newSpot{1} < $square{1}){
								$gapSize = abs($newSpot{1} - $square{1});
								$direc	 = 'dl';								
							}elseif($newSpot{0} > $square{0} && $newSpot{1} < $square{1}){
								$gapSize = abs($newSpot{1} - $square{1});
								$direc	 = 'dr';								
							}elseif($newSpot{0} < $square{0} && $newSpot{1} > $square{1}){
								$gapSize = abs($newSpot{1} - $square{1});
								$direc	 = 'ul';								
							}
							unset($blocked);
							for($r=1;$r<$gapSize;++$r){
								if($direc == 'ur'){
									$lookSq	= chr(ord($newSpot{0})-$r).($newSpot{1}-$r);
								}elseif($direc == 'dl'){
									$lookSq	= chr(ord($newSpot{0})+$r).($newSpot{1}+$r);
								}elseif($direc == 'dr'){
									$lookSq	= chr(ord($newSpot{0})-$r).($newSpot{1}+$r);
								}elseif($direc == 'ul'){
									$lookSq	= chr(ord($newSpot{0})+$r).($newSpot{1}-$r);
								}
								if($setup[$lookSq] != '') $blocked = true;
							}
							if(!$blocked) $oldSpot = $square;
						}
					}
				}	
				$setup[$newSpot]	= $piece;
				$setup[$oldSpot]	= null;
 			}elseif(strpos($move,'N') !== false){
				$piece = 'n'.$pieceColor;
				$newSpot	= str_replace('+','',$move);
				$newSpot	= str_replace('#','',$newSpot);
				$newSpot	= str_replace('x','',$newSpot);	
				if(strlen($newSpot)>3) $specific = $newSpot{1};
				$newSpot	= substr($newSpot,-2);							
				$posArray[0]	= chr(ord($newSpot{0}) - 2).($newSpot{1} + 1);
				$posArray[1]	= chr(ord($newSpot{0}) - 2).($newSpot{1} - 1);
				$posArray[2]	= chr(ord($newSpot{0}) + 2).($newSpot{1} + 1);
				$posArray[3]	= chr(ord($newSpot{0}) + 2).($newSpot{1} - 1);
				$posArray[4]	= chr(ord($newSpot{0}) - 1).($newSpot{1} + 2);
				$posArray[5]	= chr(ord($newSpot{0}) - 1).($newSpot{1} - 2);
				$posArray[6]	= chr(ord($newSpot{0}) + 1).($newSpot{1} + 2);
				$posArray[7]	= chr(ord($newSpot{0}) + 1).($newSpot{1} - 2);
				for($n=0;$n<8;++$n){
					$square 	= $posArray[$n];
					$spotPiece	= $setup[$square];
					if($spotPiece == $piece){
						if($specific){
							if(strpos($square, $specific) !== false) $oldSpot = $square;
						}else{
							$oldSpot = $square;
						}
					}
				}				
				$setup[$newSpot]	= $piece;
				$setup[$oldSpot]	= null;
 			}elseif(strpos($move,'R') !== false){
				$piece = 'r'.$pieceColor;
				$newSpot	= str_replace('+','',$move);
				$newSpot	= str_replace('#','',$newSpot);
				$newSpot	= str_replace('x','',$newSpot);	
				if(strlen($newSpot)>3) $specific = $newSpot{1};
				$newSpot	= substr($newSpot,-2);
				$pieceArray	= array_keys($setup,$piece);
				$arrayCount	= count($pieceArray);
				for($n=0;$n<$arrayCount;++$n){
					$square	= $pieceArray[$n];
					if($square{0} == $newSpot{0} || $square{1} == $newSpot{1}){
						if($specific){
							if($square{0} == $specific || $square{1} == $specific) $oldSpot	= $square;
						}elseif(!$oldSpot){
							if($newSpot{0} == $square{0}){
								$gap 	 = $newSpot{1} - $square{1};
								$gapSize = abs($gap);
								$direc	 = 'file';
							}else{
								$gap 	 = ord($newSpot{0}) - ord($square{0});
								$gapSize = abs($gap);
								$direc	 = 'row';								
							}
							unset($blocked);
							for($r=1;$r<$gapSize;++$r){
								if($direc == 'file'){
									if($gap<0){
										$lookSq	= $newSpot{0}.($newSpot{1}+$r);
									}else{
										$lookSq	= $newSpot{0}.($newSpot{1}-$r);
									}
								}else{
									if($gap<0){
										$lookSq	= chr(ord($newSpot{0})+$r).$newSpot{1};
									}else{
										$lookSq	= chr(ord($newSpot{0})-$r).$newSpot{1};
									}
								}
								if($setup[$lookSq] != '') $blocked = true;
							}
							if(!$blocked) $oldSpot	= $square;
						}
					}
				}	
				$setup[$newSpot]	= $piece;
				$setup[$oldSpot]	= null;
			}elseif($move){
				$piece 		= 'p'.$pieceColor;
				if(strpos($move,'x')){
					if($pieceColor == 'l'){
						$setup[$move{2}.$move{3}]	= $piece;
						$setup[$move{0}.($move{3}-1)]	= null; 
					}else{
						$setup[$move{2}.$move{3}]	= $piece;
						$setup[$move{0}.($move{3}+1)]	= null; 
					}
				}else{
					if($pieceColor == 'l'){
						$square	= $move{0}.($move{1}-1);
						if($setup[$square] == $piece){
							$oldSpot	= $square;
						}else{
							$oldSpot	= $move{0}.($move{1}-2);				
						}
					}else{
						$square	= $move{0}.($move{1}+1);
						if($setup[$square] == $piece){
							$oldSpot	= $square;
						}else{
							$oldSpot	= $move{0}.($move{1}+2);				
						}
					}
					$setup[$move] = $piece;
					$setup[$oldSpot]	= null;
				}
			}			
		}
	}
	if($newGame){	
		$setup 		= serialize($setup);
		$queryGame 	= 'UPDATE games SET setup=\''.$setup.'\' WHERE gameID="'.$gameID.'"';
		mysql_query($queryGame) or die('<div class="error">'.errorDBStr.'games (pp8)</div>');
	}
	return $setup;
}
?>
Return current item: Some Chess