Test Setup Failed
Branch master (42270c)
by Michael
03:57
created
class/chessgame.inc.php 1 patch
Braces   +495 added lines, -292 removed lines patch added patch discarded remove patch
@@ -260,8 +260,7 @@  discard block
 block discarded – undo
260 260
 	if (!$this->fen_piece_placement_to_board())
261 261
 	{
262 262
 		return _MD_CHESS_FENBAD_PP_INVALID; // piece_placement invalid
263
-	}
264
-	elseif ($this->gamestate['fen_active_color'] != 'w' and $this->gamestate['fen_active_color'] != 'b')
263
+	} elseif ($this->gamestate['fen_active_color'] != 'w' and $this->gamestate['fen_active_color'] != 'b')
265 264
 	{
266 265
 		return _MD_CHESS_FENBAD_AC_INVALID; // active_color invalid
267 266
 	}
@@ -269,46 +268,37 @@  discard block
 block discarded – undo
269 268
 	elseif ($this->gamestate['fen_castling_availability'] != '-' and !preg_match('/^K?Q?k?q?$/', $this->gamestate['fen_castling_availability']))
270 269
 	{
271 270
 		return _MD_CHESS_FENBAD_CA_INVALID; // castling_availability invalid
272
-	}
273
-	elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and !preg_match('/^[a-h][36]$/', $this->gamestate['fen_en_passant_target_square']))
271
+	} elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and !preg_match('/^[a-h][36]$/', $this->gamestate['fen_en_passant_target_square']))
274 272
 	{
275 273
 		return _MD_CHESS_FENBAD_EP_INVALID; // en_passant_target_square invalid
276
-	}
277
-	elseif (!preg_match('/^\d{0,4}$/', $this->gamestate['fen_halfmove_clock']))
274
+	} elseif (!preg_match('/^\d{0,4}$/', $this->gamestate['fen_halfmove_clock']))
278 275
 	{
279 276
 		return _MD_CHESS_FENBAD_HC_INVALID; // halfmove_clock invalid
280
-	}
281
-	elseif (!preg_match('/^\d{0,4}$/', $this->gamestate['fen_fullmove_number']) or $this->gamestate['fen_fullmove_number'] < 1)
277
+	} elseif (!preg_match('/^\d{0,4}$/', $this->gamestate['fen_fullmove_number']) or $this->gamestate['fen_fullmove_number'] < 1)
282 278
 	{
283 279
 		return _MD_CHESS_FENBAD_FN_INVALID; // fullmove_number invalid
284
-	}
285
-	elseif ($this->insufficient_mating_material())
280
+	} elseif ($this->insufficient_mating_material())
286 281
 	{
287 282
 		return _MD_CHESS_FENBAD_MATERIAL; // insufficient mating material
288
-	}
289
-	elseif (($this->gamestate['fen_active_color'] == 'w' and $this->kingIsUnderAttack('b', 'w'))
283
+	} elseif (($this->gamestate['fen_active_color'] == 'w' and $this->kingIsUnderAttack('b', 'w'))
290 284
 		or   ($this->gamestate['fen_active_color'] == 'b' and $this->kingIsUnderAttack('w', 'b')))
291 285
 	{
292 286
 		return _MD_CHESS_FENBAD_IN_CHECK; // player to move cannot have opponent in check
293
-	}
294
-	elseif ((strstr($this->gamestate['fen_castling_availability'], 'K') and ($this->board[ 4] != 'wK' or $this->board[ 7] != 'wR'))
287
+	} elseif ((strstr($this->gamestate['fen_castling_availability'], 'K') and ($this->board[ 4] != 'wK' or $this->board[ 7] != 'wR'))
295 288
 		or   (strstr($this->gamestate['fen_castling_availability'], 'Q') and ($this->board[ 4] != 'wK' or $this->board[ 0] != 'wR'))
296 289
 		or   (strstr($this->gamestate['fen_castling_availability'], 'k') and ($this->board[60] != 'bK' or $this->board[63] != 'bR'))
297 290
 		or   (strstr($this->gamestate['fen_castling_availability'], 'q') and ($this->board[60] != 'bK' or $this->board[56] != 'bR')))
298 291
 	{
299 292
 		return _MD_CHESS_FENBAD_CA_INCONSISTENT; // castling availability inconsistent with piece placement
300
-	}
301
-	elseif (($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 3 and $this->gamestate['fen_active_color'] != 'b')
293
+	} elseif (($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 3 and $this->gamestate['fen_active_color'] != 'b')
302 294
 		or   ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 6 and $this->gamestate['fen_active_color'] != 'w'))
303 295
 	{
304 296
 		return _MD_CHESS_FENBAD_EP_COLOR; // en passant target square wrong color
305
-	}
306
-	elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 3
297
+	} elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 3
307 298
 		and $this->board[$this->boardCoordToIndex($this->gamestate['fen_en_passant_target_square']{0} . '4')] != 'wP')
308 299
 	{
309 300
 		return _MD_CHESS_FENBAD_EP_NO_PAWN; // en passant target square for nonexistent pawn
310
-	}
311
-	elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 6
301
+	} elseif ($this->gamestate['fen_en_passant_target_square'] != '-' and $this->gamestate['fen_en_passant_target_square']{1} == 6
312 302
 		and $this->board[$this->boardCoordToIndex($this->gamestate['fen_en_passant_target_square']{0} . '5')] != 'bP')
313 303
 	{
314 304
 		return _MD_CHESS_FENBAD_EP_NO_PAWN; // en passant target square for nonexistent pawn
@@ -341,8 +331,9 @@  discard block
 block discarded – undo
341 331
 
342 332
 		#echo "path: $pos: '$this->board[$pos]' "; #*#DEBUG#
343 333
 
344
-		if (!$this->is_empty_tile($pos))
345
-			return 0;
334
+		if (!$this->is_empty_tile($pos)) {
335
+					return 0;
336
+		}
346 337
 	}
347 338
 
348 339
 	return 1;
@@ -421,8 +412,7 @@  discard block
 block discarded – undo
421 412
 				$change += ($dx < $fx) ? -1 : 1;
422 413
 			} elseif ($fx == $dx) {
423 414
 				$change = ($dy < $fy) ? -8 : 8;
424
-			} 
425
-			else {
415
+			} else {
426 416
 				$change = ($dx < $fx) ? -1 : 1;
427 417
 			}
428 418
 			break;
@@ -448,7 +438,9 @@  discard block
 block discarded – undo
448 438
  */
449 439
 function tileIsReachable($fig, $fig_pos, $dest_pos)
450 440
 {
451
-  if ( $fig_pos==$dest_pos) return;
441
+  if ( $fig_pos==$dest_pos) {
442
+  	return;
443
+  }
452 444
   $result = 0;
453 445
   $fy = floor($fig_pos/8); $fx = $fig_pos%8;
454 446
   $dy = floor($dest_pos/8); $dx = $dest_pos%8;
@@ -457,61 +449,103 @@  discard block
 block discarded – undo
457 449
   {
458 450
     /* knight */
459 451
     case 'N':
460
-      if ( abs($fx-$dx)==1 && abs($fy-$dy)==2 )
461
-        $result = 1;
462
-      if ( abs($fy-$dy)==1 && abs($fx-$dx)==2 )
463
-        $result = 1;
452
+      if ( abs($fx-$dx)==1 && abs($fy-$dy)==2 ) {
453
+              $result = 1;
454
+      }
455
+      if ( abs($fy-$dy)==1 && abs($fx-$dx)==2 ) {
456
+              $result = 1;
457
+      }
464 458
       break;
465 459
     /* bishop */
466 460
     case 'B':
467
-      if ( abs($fx-$dx) != abs($fy-$dy) )
468
-        break;
469
-      if ( $dy < $fy ) $change = -8; else $change =  8;
470
-      if ( $dx < $fx ) $change -= 1; else $change += 1;
471
-      if ( $this->pathIsNotBlocked( $fig_pos+$change, $dest_pos, $change ) )
472
-        $result = 1;
461
+      if ( abs($fx-$dx) != abs($fy-$dy) ) {
462
+              break;
463
+      }
464
+      if ( $dy < $fy ) {
465
+      	$change = -8;
466
+      } else {
467
+      	$change =  8;
468
+      }
469
+      if ( $dx < $fx ) {
470
+      	$change -= 1;
471
+      } else {
472
+      	$change += 1;
473
+      }
474
+      if ( $this->pathIsNotBlocked( $fig_pos+$change, $dest_pos, $change ) ) {
475
+              $result = 1;
476
+      }
473 477
       break;
474 478
     /* rook */
475 479
     case 'R':
476
-      if ( $fx!=$dx && $fy!=$dy )
477
-        break;
480
+      if ( $fx!=$dx && $fy!=$dy ) {
481
+              break;
482
+      }
478 483
       if ( $fx==$dx ) 
479 484
       {
480
-        if ( $dy<$fy ) $change = -8; else $change = 8;
485
+        if ( $dy<$fy ) {
486
+        	$change = -8;
487
+        } else {
488
+        	$change = 8;
489
+        }
490
+      } else {
491
+        if ( $dx<$fx ) {
492
+        	$change = -1;
493
+        } else {
494
+        	$change = 1;
495
+        }
481 496
       }
482
-      else {
483
-        if ( $dx<$fx ) $change = -1; else $change = 1;
497
+      if ( $this->pathIsNotBlocked( $fig_pos+$change, $dest_pos, $change ) ) {
498
+              $result = 1;
484 499
       }
485
-      if ( $this->pathIsNotBlocked( $fig_pos+$change, $dest_pos, $change ) )
486
-        $result = 1;
487 500
       break;
488 501
     /* queen */
489 502
     case 'Q':
490
-      if ( abs($fx-$dx) != abs($fy-$dy) && $fx!=$dx && $fy!=$dy )
491
-        break;
503
+      if ( abs($fx-$dx) != abs($fy-$dy) && $fx!=$dx && $fy!=$dy ) {
504
+              break;
505
+      }
492 506
       if ( abs($fx-$dx) == abs($fy-$dy) )
493 507
       {
494
-        if ( $dy < $fy ) $change = -8; else $change =  8;
495
-        if ( $dx < $fx ) $change -= 1; else $change += 1;
496
-      }
497
-      else if ( $fx==$dx ) {
498
-        if ( $dy<$fy ) $change = -8; else $change = 8;
499
-      } 
500
-      else
508
+        if ( $dy < $fy ) {
509
+        	$change = -8;
510
+        } else {
511
+        	$change =  8;
512
+        }
513
+        if ( $dx < $fx ) {
514
+        	$change -= 1;
515
+        } else {
516
+        	$change += 1;
517
+        }
518
+      } else if ( $fx==$dx ) {
519
+        if ( $dy<$fy ) {
520
+        	$change = -8;
521
+        } else {
522
+        	$change = 8;
523
+        }
524
+      } else
501 525
       {
502
-        if ( $dx<$fx ) $change = -1; else $change = 1;
526
+        if ( $dx<$fx ) {
527
+        	$change = -1;
528
+        } else {
529
+        	$change = 1;
530
+        }
531
+      }
532
+      if ( $this->pathIsNotBlocked( $fig_pos+$change, $dest_pos, $change ) ) {
533
+              $result = 1;
503 534
       }
504
-      if ( $this->pathIsNotBlocked( $fig_pos+$change, $dest_pos, $change ) )
505
-        $result = 1;
506 535
       break;
507 536
     /* king */
508 537
     case 'K':
509
-      if ( abs($fx-$dx) > 1 || abs($fy-$dy) > 1 ) break;
538
+      if ( abs($fx-$dx) > 1 || abs($fy-$dy) > 1 ) {
539
+      	break;
540
+      }
510 541
       $kings = 0;
511 542
       $adj_tiles = $this->getAdjTiles( $dest_pos );
512
-      foreach( $adj_tiles as $tile )
513
-        if ($this->board[$tile]{1} == 'K' ) $kings++;
514
-      if ( $kings == 2 ) break;
543
+      foreach( $adj_tiles as $tile ) {
544
+              if ($this->board[$tile]{1} == 'K' ) $kings++;
545
+      }
546
+      if ( $kings == 2 ) {
547
+      	break;
548
+      }
515 549
       $result = 1;
516 550
       break;
517 551
   }
@@ -533,17 +567,20 @@  discard block
 block discarded – undo
533 567
 {
534 568
   if ( $this->board[$fig_pos]{0} == 'w' )
535 569
   {
536
-    if ( ($fig_pos % 8) > 0 && $dest_pos == $fig_pos+7 )
537
-      return 1;
538
-    if ( ($fig_pos % 8) < 7 && $dest_pos == $fig_pos+9 )
539
-      return 1;
540
-  }
541
-  else if ( $this->board[$fig_pos]{0} == 'b' )
570
+    if ( ($fig_pos % 8) > 0 && $dest_pos == $fig_pos+7 ) {
571
+          return 1;
572
+    }
573
+    if ( ($fig_pos % 8) < 7 && $dest_pos == $fig_pos+9 ) {
574
+          return 1;
575
+    }
576
+  } else if ( $this->board[$fig_pos]{0} == 'b' )
542 577
   {
543
-    if ( ($fig_pos % 8) < 7 && $dest_pos == $fig_pos-7 )
544
-      return 1;
545
-    if ( ($fig_pos % 8) > 0 && $dest_pos == $fig_pos-9 )
546
-      return 1;
578
+    if ( ($fig_pos % 8) < 7 && $dest_pos == $fig_pos-7 ) {
579
+          return 1;
580
+    }
581
+    if ( ($fig_pos % 8) > 0 && $dest_pos == $fig_pos-9 ) {
582
+          return 1;
583
+    }
547 584
   }
548 585
   return 0;
549 586
 }
@@ -568,23 +605,28 @@  discard block
 block discarded – undo
568 605
   
569 606
   if ( $this->board[$fig_pos]{0} == 'w' )
570 607
   {
571
-    if ( $fig_pos >= 8 && $fig_pos <= 15 )
572
-      $first_move = 1;
573
-    if ( $dest_pos==$fig_pos+8 )
574
-      return 1;
575
-    if ( $first_move && ( $dest_pos==$fig_pos+16 ) )
576
-    if ($this->is_empty_tile($fig_pos + 8))
608
+    if ( $fig_pos >= 8 && $fig_pos <= 15 ) {
609
+          $first_move = 1;
610
+    }
611
+    if ( $dest_pos==$fig_pos+8 ) {
612
+          return 1;
613
+    }
614
+    if ( $first_move && ( $dest_pos==$fig_pos+16 ) ) {
615
+        if ($this->is_empty_tile($fig_pos + 8))
577 616
       return 1;
578
-  }
579
-  else if ($this->board[$fig_pos]{0} == 'b' )
617
+    }
618
+  } else if ($this->board[$fig_pos]{0} == 'b' )
580 619
   {
581
-    if ( $fig_pos >= 48 && $fig_pos <= 55 )
582
-      $first_move = 1;
583
-    if ( $dest_pos==$fig_pos-8 )
584
-      return 1;
585
-    if ( $first_move && ( $dest_pos==$fig_pos-16 ) )
586
-    if ($this->is_empty_tile($fig_pos - 8))
620
+    if ( $fig_pos >= 48 && $fig_pos <= 55 ) {
621
+          $first_move = 1;
622
+    }
623
+    if ( $dest_pos==$fig_pos-8 ) {
624
+          return 1;
625
+    }
626
+    if ( $first_move && ( $dest_pos==$fig_pos-16 ) ) {
627
+        if ($this->is_empty_tile($fig_pos - 8))
587 628
       return 1;
629
+    }
588 630
   }
589 631
   return 0;
590 632
 }
@@ -601,8 +643,8 @@  discard block
 block discarded – undo
601 643
 function tileIsUnderAttack($opp, $dest_pos)
602 644
 {
603 645
 #var_dump('tileIsUnderAttack, opp', $opp, 'dest_pos', $dest_pos, 'board', $board);#*#DEBUG#
604
-  for ( $i = 0; $i < 64; $i++ )
605
-    if ($this->board[$i]{0} == $opp )
646
+  for ( $i = 0; $i < 64; $i++ ) {
647
+      if ($this->board[$i]{0} == $opp )
606 648
     {
607 649
       if ( ($this->board[$i]{1}=='P' && $this->checkPawnAttack($i,$dest_pos)) ||
608 650
            ($this->board[$i]{1}!='P' && 
@@ -610,6 +652,7 @@  discard block
 block discarded – undo
610 652
       {
611 653
         /*DEBUG: echo "attack test: $i: ",$opp,"P<BR>"; */
612 654
         return 1;
655
+  }
613 656
       }
614 657
     }
615 658
   return 0;
@@ -628,10 +671,11 @@  discard block
 block discarded – undo
628 671
 {
629 672
 
630 673
 #var_dump('kingIsUnderAttack, player', $player, 'opp', $opp, 'board', $board);#*#DEBUG#
631
-  for ( $i = 0; $i < 64; $i++ )
632
-    if ($this->board[$i]{0} == $player && $this->board[$i]{1} == 'K' )
674
+  for ( $i = 0; $i < 64; $i++ ) {
675
+      if ($this->board[$i]{0} == $player && $this->board[$i]{1} == 'K' )
633 676
     {
634 677
       $king_pos = $i;
678
+  }
635 679
       break;
636 680
     }
637 681
   /*DEBUG: echo "$player king is at $king_pos<BR>"; */
@@ -650,10 +694,11 @@  discard block
 block discarded – undo
650 694
  */
651 695
 function isCheckMate($player, $opp)
652 696
 {
653
-  for ( $i = 0; $i < 64; $i++ )
654
-    if ($this->board[$i]{0} == $player && $this->board[$i]{1} == 'K' )
697
+  for ( $i = 0; $i < 64; $i++ ) {
698
+      if ($this->board[$i]{0} == $player && $this->board[$i]{1} == 'K' )
655 699
     {
656 700
       $king_pos = $i;
701
+  }
657 702
       $king_x = $i % 8;
658 703
       $king_y = floor($i/8);
659 704
       break;
@@ -664,8 +709,12 @@  discard block
 block discarded – undo
664 709
   $contents = $this->board[$king_pos]; $this->clear_tile($king_pos);
665 710
   foreach ( $adj_tiles as $dest_pos ) 
666 711
   {
667
-    if ($this->board[$dest_pos]{0} == $player ) continue;
668
-    if ( $this->tileIsUnderAttack($opp,$dest_pos) ) continue;
712
+    if ($this->board[$dest_pos]{0} == $player ) {
713
+    	continue;
714
+    }
715
+    if ( $this->tileIsUnderAttack($opp,$dest_pos) ) {
716
+    	continue;
717
+    }
669 718
     $this->board[$king_pos] = $contents;
670 719
     return 0;
671 720
   }
@@ -675,14 +724,15 @@  discard block
 block discarded – undo
675 724
 
676 725
   /* get all figures that attack the king */
677 726
   $attackers = array(); $count = 0;
678
-  for ( $i = 0; $i < 64; $i++ )
679
-    if ( $this->board[$i]{0} == $opp )
727
+  for ( $i = 0; $i < 64; $i++ ) {
728
+      if ( $this->board[$i]{0} == $opp )
680 729
     {
681 730
       if ( ($this->board[$i]{1}=='P' && $this->checkPawnAttack($i,$king_pos)) ||
682 731
            ($this->board[$i]{1}!='P' && 
683 732
                 $this->tileIsReachable($this->board[$i]{1},$i,$king_pos)) )
684 733
       {
685 734
           $attackers[$count++] = $i;
735
+  }
686 736
       }
687 737
     }
688 738
   /* DEBUG: 
@@ -691,12 +741,14 @@  discard block
 block discarded – undo
691 741
   echo "Attackercount: ",count($attackers), " "; */
692 742
  
693 743
   /* if more than one there is no chance to escape */
694
-  if ( $count > 1 ) return 1;
744
+  if ( $count > 1 ) {
745
+  	return 1;
746
+  }
695 747
 
696 748
   /* check whether attacker can be killed by own figure */
697 749
   $dest_pos = $attackers[0];
698
-  for ( $i = 0; $i < 64; $i++ )
699
-    if ( $this->board[$i]{0} == $player )
750
+  for ( $i = 0; $i < 64; $i++ ) {
751
+      if ( $this->board[$i]{0} == $player )
700 752
     {
701 753
       if ( ($this->board[$i]{1}=='P' && $this->checkPawnAttack($i,$dest_pos)) ||
702 754
            ($this->board[$i]{1}!='P' && $this->board[$i]{1}!='K' &&
@@ -707,12 +759,14 @@  discard block
 block discarded – undo
707 759
       {
708 760
         /* DEBUG: echo "candidate: $i "; */
709 761
         $can_kill_atk = 0;
762
+  }
710 763
         $contents_def = $this->board[$i];
711 764
         $contents_atk = $this->board[$dest_pos];
712 765
         $this->board[$dest_pos] = $this->board[$i];
713 766
         $this->clear_tile($i);
714
-        if ( !$this->tileIsUnderAttack($opp,$king_pos) )
715
-          $can_kill_atk = 1;
767
+        if ( !$this->tileIsUnderAttack($opp,$king_pos) ) {
768
+                  $can_kill_atk = 1;
769
+        }
716 770
         $this->board[$i] = $contents_def;
717 771
         $this->board[$dest_pos] = $contents_atk;
718 772
         if ( $can_kill_atk )
@@ -727,14 +781,17 @@  discard block
 block discarded – undo
727 781
   
728 782
   /* if attacking unit is a knight there
729 783
    * is no way to block the path */
730
-  if ( $this->board[$dest_pos]{1} == 'N' ) return 1;
784
+  if ( $this->board[$dest_pos]{1} == 'N' ) {
785
+  	return 1;
786
+  }
731 787
 
732 788
   /* if enemy is adjacent to king there is no
733 789
    * way either */
734 790
   $dest_x = $dest_pos % 8;
735 791
   $dest_y = floor($dest_pos/8);
736
-  if ( abs($dest_x-$king_x)<=1 && abs($dest_y-$king_y)<=1 )
737
-    return 1;
792
+  if ( abs($dest_x-$king_x)<=1 && abs($dest_y-$king_y)<=1 ) {
793
+      return 1;
794
+  }
738 795
 
739 796
   /* get the list of tiles between king and attacking
740 797
    * unit that can be blocked to stop the attack */
@@ -744,8 +801,8 @@  discard block
 block discarded – undo
744 801
   /* DEBUG: foreach( $path as $tile ) echo "tile: $tile "; */
745 802
   foreach( $path as $pos )
746 803
   {
747
-    for ( $i = 0; $i < 64; $i++ )
748
-      if ( $this->board[$i]{0} == $player )
804
+    for ( $i = 0; $i < 64; $i++ ) {
805
+          if ( $this->board[$i]{0} == $player )
749 806
       {
750 807
         if ( ($this->board[$i]{1}=='P' && $this->checkPawnMove($i,$pos)) ||
751 808
              ($this->board[$i]{1}!='P' && $this->board[$i]{1}!='K' &&
@@ -753,6 +810,7 @@  discard block
 block discarded – undo
753 810
         {
754 811
             /* DEBUG: echo "$i can block "; */
755 812
             return 0;
813
+    }
756 814
         }
757 815
       }
758 816
   }
@@ -772,16 +830,21 @@  discard block
 block discarded – undo
772 830
  */
773 831
 function isStaleMate($player, $opp)
774 832
 {
775
-  for ( $i = 0; $i < 64; $i++ )
776
-    if ($this->board[$i]{0} == $player )
833
+  for ( $i = 0; $i < 64; $i++ ) {
834
+      if ($this->board[$i]{0} == $player )
777 835
       switch ($this->board[$i]{1} )
778 836
       {
779 837
         case 'K':
780 838
           $adj_tiles = $this->getAdjTiles( $i );
839
+  }
781 840
           foreach ( $adj_tiles as $pos ) 
782 841
           {
783
-            if ( $this->board[$pos]{0} == $player ) continue;
784
-            if ( $this->tileIsUnderAttack($opp,$pos) ) continue;
842
+            if ( $this->board[$pos]{0} == $player ) {
843
+            	continue;
844
+            }
845
+            if ( $this->tileIsUnderAttack($opp,$pos) ) {
846
+            	continue;
847
+            }
785 848
             return 0;
786 849
           }
787 850
           /* DEBUG:  echo "King cannot escape by itself! "; */
@@ -789,44 +852,88 @@  discard block
 block discarded – undo
789 852
         case 'P':
790 853
           if ( $player == 'w' )
791 854
           {
792
-            if ($this->is_empty_tile($i + 8)) return 0;
793
-            if ( ($i%8) > 0 && $this->board[$i+7]{0} != $player ) return 0;
794
-            if ( ($i%8) < 7 && $this->board[$i+9]{0} != $player ) return 0;
795
-          }
796
-          else
855
+            if ($this->is_empty_tile($i + 8)) {
856
+            	return 0;
857
+            }
858
+            if ( ($i%8) > 0 && $this->board[$i+7]{0} != $player ) {
859
+            	return 0;
860
+            }
861
+            if ( ($i%8) < 7 && $this->board[$i+9]{0} != $player ) {
862
+            	return 0;
863
+            }
864
+          } else
797 865
           {
798
-            if ($this->is_empty_tile($i - 8)) return 0;
799
-            if ( ($i%8) > 0 && $this->board[$i-9]{0} != $player ) return 0;
800
-            if ( ($i%8) < 7 && $this->board[$i-7]{0} != $player ) return 0;
866
+            if ($this->is_empty_tile($i - 8)) {
867
+            	return 0;
868
+            }
869
+            if ( ($i%8) > 0 && $this->board[$i-9]{0} != $player ) {
870
+            	return 0;
871
+            }
872
+            if ( ($i%8) < 7 && $this->board[$i-7]{0} != $player ) {
873
+            	return 0;
874
+            }
801 875
           }
802 876
           break;
803 877
         case 'B':
804
-          if ( $i-9 >= 0  && $this->board[$i-9]{0} != $player ) return 0;
805
-          if ( $i-7 >= 0  && $this->board[$i-7]{0} != $player ) return 0;
806
-          if ( $i+9 <= 63 && $this->board[$i+9]{0} != $player ) return 0;
807
-          if ( $i+7 <= 63 && $this->board[$i+7]{0} != $player ) return 0;
878
+          if ( $i-9 >= 0  && $this->board[$i-9]{0} != $player ) {
879
+          	return 0;
880
+          }
881
+          if ( $i-7 >= 0  && $this->board[$i-7]{0} != $player ) {
882
+          	return 0;
883
+          }
884
+          if ( $i+9 <= 63 && $this->board[$i+9]{0} != $player ) {
885
+          	return 0;
886
+          }
887
+          if ( $i+7 <= 63 && $this->board[$i+7]{0} != $player ) {
888
+          	return 0;
889
+          }
808 890
           break;
809 891
         case 'R':
810
-          if ( $i-8 >= 0  && $this->board[$i-8]{0} != $player ) return 0;
811
-          if ( $i-1 >= 0  && $this->board[$i-1]{0} != $player ) return 0;
812
-          if ( $i+8 <= 63 && $this->board[$i+8]{0} != $player ) return 0;
813
-          if ( $i+1 <= 63 && $this->board[$i+1]{0} != $player ) return 0;
892
+          if ( $i-8 >= 0  && $this->board[$i-8]{0} != $player ) {
893
+          	return 0;
894
+          }
895
+          if ( $i-1 >= 0  && $this->board[$i-1]{0} != $player ) {
896
+          	return 0;
897
+          }
898
+          if ( $i+8 <= 63 && $this->board[$i+8]{0} != $player ) {
899
+          	return 0;
900
+          }
901
+          if ( $i+1 <= 63 && $this->board[$i+1]{0} != $player ) {
902
+          	return 0;
903
+          }
814 904
           break;
815 905
         case 'Q':
816 906
           $adj_tiles = $this->getAdjTiles( $i );
817
-          foreach ( $adj_tiles as $pos )
818
-            if ( $this->board[$pos]{0} != $player ) 
907
+          foreach ( $adj_tiles as $pos ) {
908
+                      if ( $this->board[$pos]{0} != $player ) 
819 909
               return 0;
910
+          }
820 911
           break;
821 912
         case 'N':
822
-          if ( $i-17 >= 0  && $this->board[$i-17]{0} != $player ) return 0; 
823
-          if ( $i-15 >= 0  && $this->board[$i-15]{0} != $player ) return 0;
824
-          if ( $i-6  >= 0  && $this->board[$i-6]{0}  != $player ) return 0;
825
-          if ( $i+10 <= 63 && $this->board[$i+10]{0} != $player ) return 0;
826
-          if ( $i+17 <= 63 && $this->board[$i+17]{0} != $player ) return 0;
827
-          if ( $i+15 <= 63 && $this->board[$i+15]{0} != $player ) return 0;
828
-          if ( $i+6  <= 63 && $this->board[$i+6]{0}  != $player ) return 0;
829
-          if ( $i-10 >= 0  && $this->board[$i-10]{0} != $player ) return 0;
913
+          if ( $i-17 >= 0  && $this->board[$i-17]{0} != $player ) {
914
+          	return 0;
915
+          }
916
+          if ( $i-15 >= 0  && $this->board[$i-15]{0} != $player ) {
917
+          	return 0;
918
+          }
919
+          if ( $i-6  >= 0  && $this->board[$i-6]{0}  != $player ) {
920
+          	return 0;
921
+          }
922
+          if ( $i+10 <= 63 && $this->board[$i+10]{0} != $player ) {
923
+          	return 0;
924
+          }
925
+          if ( $i+17 <= 63 && $this->board[$i+17]{0} != $player ) {
926
+          	return 0;
927
+          }
928
+          if ( $i+15 <= 63 && $this->board[$i+15]{0} != $player ) {
929
+          	return 0;
930
+          }
931
+          if ( $i+6  <= 63 && $this->board[$i+6]{0}  != $player ) {
932
+          	return 0;
933
+          }
934
+          if ( $i-10 >= 0  && $this->board[$i-10]{0} != $player ) {
935
+          	return 0;
936
+          }
830 937
           break;
831 938
       }
832 939
       
@@ -892,9 +999,10 @@  discard block
 block discarded – undo
892 999
   {
893 1000
     /* full move: a pawn requires a ? in the end
894 1001
      * to automatically choose a queen on last line */
895
-    if ( $move[0] == 'P' )
896
-    if ( $move[strlen($move)-1]<'A' || $move[strlen($move)-1]>'Z' )
1002
+    if ( $move[0] == 'P' ) {
1003
+        if ( $move[strlen($move)-1]<'A' || $move[strlen($move)-1]>'Z' )
897 1004
       $this->ac_move = "$move?";
1005
+    }
898 1006
     return "";
899 1007
   }
900 1008
 
@@ -912,8 +1020,10 @@  discard block
 block discarded – undo
912 1020
   if ($move{strlen($move)-1} == '=') {
913 1021
     $move = substr($move, 0, strlen($move)-1);
914 1022
   }
915
-  if ( $pawn_upg == "P" || $pawn_upg == "K" )
916
-    return _MD_CHESS_MOVE_PAWN_MAY_BECOME; // "A pawn may only become either a knight, a bishop, a rook or a queen!"
1023
+  if ( $pawn_upg == "P" || $pawn_upg == "K" ) {
1024
+      return _MD_CHESS_MOVE_PAWN_MAY_BECOME;
1025
+  }
1026
+  // "A pawn may only become either a knight, a bishop, a rook or a queen!"
917 1027
 
918 1028
   if ( $move[0]>='a' && $move[0]<='h' )
919 1029
   {
@@ -921,21 +1031,23 @@  discard block
 block discarded – undo
921 1031
      * listed above */
922 1032
     if ( strlen($move) == 4 )
923 1033
     {
924
-      if ( $move[1] != 'x' )
925
-        return _MD_CHESS_MOVE_USE_X; // "use x to indicate an attack"
1034
+      if ( $move[1] != 'x' ) {
1035
+              return _MD_CHESS_MOVE_USE_X;
1036
+      }
1037
+      // "use x to indicate an attack"
926 1038
       $dest_x = $move[2];
927 1039
       $dest_y = $move[3];
928 1040
       $src_x  = $move[0];
929
-      if ( $player == 'w' )
930
-        $src_y  = $dest_y-1;
931
-      else
932
-        $src_y  = $dest_y+1;
1041
+      if ( $player == 'w' ) {
1042
+              $src_y  = $dest_y-1;
1043
+      } else {
1044
+              $src_y  = $dest_y+1;
1045
+      }
933 1046
       $this->ac_move = sprintf( "P%s%dx%s%d%s", 
934 1047
                           $src_x,$src_y,$dest_x,$dest_y,
935 1048
                           $pawn_upg );
936 1049
       return "";
937
-    }
938
-    else
1050
+    } else
939 1051
     if (strlen($move) == 2 )
940 1052
     {
941 1053
       $fig = sprintf( "%sP", $player );
@@ -943,16 +1055,26 @@  discard block
 block discarded – undo
943 1055
       {
944 1056
         /* pawn move */
945 1057
         $pos = $this->boardCoordToIndex( $move );
946
-        if ( $pos == 64 ) return $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $move); // "coordinate $move is invalid"
1058
+        if ( $pos == 64 ) {
1059
+        	return $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $move);
1060
+        }
1061
+        // "coordinate $move is invalid"
947 1062
         if ( $player == 'w' )
948 1063
         {
949
-          while( $pos >= 0 && $this->board[$pos] != $fig ) $pos -= 8;
950
-          if ( $pos < 0 ) $not_found = 1;
951
-        }
952
-        else
1064
+          while( $pos >= 0 && $this->board[$pos] != $fig ) {
1065
+          	$pos -= 8;
1066
+          }
1067
+          if ( $pos < 0 ) {
1068
+          	$not_found = 1;
1069
+          }
1070
+        } else
953 1071
         {
954
-          while( $pos <= 63 && $this->board[$pos] != $fig ) $pos += 8;
955
-          if ( $pos > 63 ) $not_found = 1;
1072
+          while( $pos <= 63 && $this->board[$pos] != $fig ) {
1073
+          	$pos += 8;
1074
+          }
1075
+          if ( $pos > 63 ) {
1076
+          	$not_found = 1;
1077
+          }
956 1078
         }
957 1079
         $pos = $this->boardIndexToCoord( $pos );
958 1080
         if ( (isset($not_found) && $not_found) || $pos == "" ) {
@@ -961,127 +1083,155 @@  discard block
 block discarded – undo
961 1083
           $this->ac_move = sprintf( "P%s-%s%s", $pos, $move, $pawn_upg );
962 1084
           return "";
963 1085
         }
964
-      }
965
-      else
1086
+      } else
966 1087
       {
967 1088
         /* notation: [a-h][a-h] for pawn attack no longer allowed 
968 1089
          * except for history browser */
969
-        if ( $this->browsing_mode == 0 )
970
-            return _MD_CHESS_MOVE_USE_NOTATION; // "please use denotation [a-h]x[a-h][1-8] for pawn attacks (see help for more information)"
1090
+        if ( $this->browsing_mode == 0 ) {
1091
+                    return _MD_CHESS_MOVE_USE_NOTATION;
1092
+        }
1093
+        // "please use denotation [a-h]x[a-h][1-8] for pawn attacks (see help for more information)"
971 1094
         /* pawn attack must be only one pawn in column! */
972 1095
         $pawns = 0;
973 1096
         $start = $this->boardCoordToIndex( sprintf( "%s1", $move[0] ) );
974
-        if ( $start == 64 ) return $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $move[0]); // "coordinate $move[0] is invalid"
975
-        for ( $i = 1; $i <= 8; $i++, $start+=8 )
976
-          if ( $this->board[$start] == $fig ) 
1097
+        if ( $start == 64 ) {
1098
+        	return $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $move[0]);
1099
+        }
1100
+        // "coordinate $move[0] is invalid"
1101
+        for ( $i = 1; $i <= 8; $i++, $start+=8 ) {
1102
+                  if ( $this->board[$start] == $fig ) 
977 1103
           {
978 1104
             $pawns++;
1105
+        }
979 1106
             $pawn_line = $i;
980 1107
           }
981
-        if ( $pawns == 0 )
982
-          return $this->move_msg(_MD_CHESS_MOVE_NO_PAWN, $move[0]); // "there is no pawn in column $move[0]"
983
-        else if ( $pawns > 1 )
984
-          return $this->move_msg(_MD_CHESS_MOVE_TWO_PAWNS, $move[0]); // "there is more than one pawn in column $move[0]"
1108
+        if ( $pawns == 0 ) {
1109
+                  return $this->move_msg(_MD_CHESS_MOVE_NO_PAWN, $move[0]);
1110
+        }
1111
+        // "there is no pawn in column $move[0]"
1112
+        else if ( $pawns > 1 ) {
1113
+                  return $this->move_msg(_MD_CHESS_MOVE_TWO_PAWNS, $move[0]);
1114
+        }
1115
+        // "there is more than one pawn in column $move[0]"
985 1116
         else
986 1117
         {
987
-          if ( $player == 'w' )
988
-            $dest_line = $pawn_line+1;
989
-          else
990
-            $dest_line = $pawn_line-1;
1118
+          if ( $player == 'w' ) {
1119
+                      $dest_line = $pawn_line+1;
1120
+          } else {
1121
+                      $dest_line = $pawn_line-1;
1122
+          }
991 1123
           $this->ac_move = sprintf( "P%s%dx%s%d", 
992 1124
                             $move[0],$pawn_line,$move[1],$dest_line );
993 1125
           return "";
994 1126
         }
995 1127
       }
996 1128
     }
997
-  }
998
-  else
1129
+  } else
999 1130
   {
1000 1131
     /* figure move */
1001 1132
     $dest_coord = substr( $move, strlen($move)-2, 2 );
1002 1133
     $action = $move[strlen($move)-3];
1003
-    if ( $action != 'x' ) $action = '-';
1004
-    if ( $player == 'w' ) 
1005
-      $figures = $this->w_figures;
1006
-    else
1007
-      $figures = $this->b_figures;
1134
+    if ( $action != 'x' ) {
1135
+    	$action = '-';
1136
+    }
1137
+    if ( $player == 'w' ) {
1138
+          $figures = $this->w_figures;
1139
+    } else {
1140
+          $figures = $this->b_figures;
1141
+    }
1008 1142
     $fig_count = 0;
1009
-    foreach( $figures as $figure )
1010
-      if ( $figure[0] == $move[0] )
1143
+    foreach( $figures as $figure ) {
1144
+          if ( $figure[0] == $move[0] )
1011 1145
       {
1012 1146
         $fig_count++;
1013
-        if ( $fig_count == 1 )
1014
-          $pos1 = substr( $figure, 1, 2 );
1015
-        else
1016
-          $pos2 = substr( $figure, 1, 2 );
1147
+    }
1148
+        if ( $fig_count == 1 ) {
1149
+                  $pos1 = substr( $figure, 1, 2 );
1150
+        } else {
1151
+                  $pos2 = substr( $figure, 1, 2 );
1152
+        }
1017 1153
       }
1018
-    if ( $fig_count == 0 )
1019
-      return $this->move_msg(_MD_CHESS_MOVE_NO_FIGURE, $move[0], $this->getFullFigureName($move[0])); // sprintf("there is no figure %s = %s", $move[0], $this->getFullFigureName($move[0]))
1154
+    if ( $fig_count == 0 ) {
1155
+          return $this->move_msg(_MD_CHESS_MOVE_NO_FIGURE, $move[0], $this->getFullFigureName($move[0]));
1156
+    }
1157
+    // sprintf("there is no figure %s = %s", $move[0], $this->getFullFigureName($move[0]))
1020 1158
     else
1021 1159
     if ( $fig_count == 1 )
1022 1160
     {
1023 1161
        $this->ac_move = sprintf( "%s%s%s%s",
1024 1162
                       $move[0], $pos1, $action, $dest_coord ); 
1025 1163
        return "";
1026
-    }
1027
-    else
1164
+    } else
1028 1165
     {
1029 1166
       /* two figures which may cause ambiguity */
1030 1167
       $dest_pos = $this->boardCoordToIndex( $dest_coord );
1031
-      if ( $dest_pos == 64 ) 
1032
-        return $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $dest_coord); // "coordinate $dest_coord is invalid"
1168
+      if ( $dest_pos == 64 ) {
1169
+              return $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $dest_coord);
1170
+      }
1171
+      // "coordinate $dest_coord is invalid"
1033 1172
       $fig1_can_reach = $this->tileIsReachable( $move[0], 
1034 1173
                             $this->boardCoordToIndex($pos1), $dest_pos );
1035 1174
       $fig2_can_reach = $this->tileIsReachable( $move[0], 
1036 1175
                             $this->boardCoordToIndex($pos2), $dest_pos );
1037
-      if ( !$fig1_can_reach && !$fig2_can_reach )
1038
-        return $this->move_msg(_MD_CHESS_MOVE_NEITHER_CAN_REACH, $move[0], $this->getFullFigureName($move[0]), $dest_coord); // sprintf("neither of the %s = %s can reach %s", $move[0], $this->getFullFigureName($move[0]), $dest_coord)
1176
+      if ( !$fig1_can_reach && !$fig2_can_reach ) {
1177
+              return $this->move_msg(_MD_CHESS_MOVE_NEITHER_CAN_REACH, $move[0], $this->getFullFigureName($move[0]), $dest_coord);
1178
+      }
1179
+      // sprintf("neither of the %s = %s can reach %s", $move[0], $this->getFullFigureName($move[0]), $dest_coord)
1039 1180
       else
1040 1181
       if ( $fig1_can_reach && $fig2_can_reach )
1041 1182
       {
1042 1183
         /* ambiguity - check whether a hint is given */
1043 1184
         if ( ($action=='-' && strlen($move)==4) ||
1044
-             ($action=='x' && strlen($move)==5) )
1045
-          $hint = $move[1];
1046
-        if ( empty($hint) )
1047
-          return $this->move_msg(_MD_CHESS_MOVE_BOTH_CAN_REACH, $move[0], $this->getFullFigureName($move[0]), $dest_coord); // sprintf("both of the %s = %s can reach %s", $move[0], $this->getFullFigureName($move[0]), $dest_coord)
1185
+             ($action=='x' && strlen($move)==5) ) {
1186
+                  $hint = $move[1];
1187
+        }
1188
+        if ( empty($hint) ) {
1189
+                  return $this->move_msg(_MD_CHESS_MOVE_BOTH_CAN_REACH, $move[0], $this->getFullFigureName($move[0]), $dest_coord);
1190
+        }
1191
+        // sprintf("both of the %s = %s can reach %s", $move[0], $this->getFullFigureName($move[0]), $dest_coord)
1048 1192
         else
1049 1193
         {
1050 1194
           $move_fig1 = 0;
1051 1195
           $move_fig2 = 0;
1052 1196
           if ( $hint>='1' && $hint<='8' )
1053 1197
           {
1054
-            if ( $pos1[1]==$hint && $pos2[1]!=$hint )
1055
-              $move_fig1 = 1;
1056
-            if ( $pos2[1]==$hint && $pos1[1]!=$hint )
1057
-              $move_fig2 = 1;
1058
-          }
1059
-          else
1198
+            if ( $pos1[1]==$hint && $pos2[1]!=$hint ) {
1199
+                          $move_fig1 = 1;
1200
+            }
1201
+            if ( $pos2[1]==$hint && $pos1[1]!=$hint ) {
1202
+                          $move_fig2 = 1;
1203
+            }
1204
+          } else
1060 1205
           {
1061
-            if ( $pos1[0]==$hint && $pos2[0]!=$hint )
1062
-              $move_fig1 = 1;
1063
-            if ( $pos2[0]==$hint && $pos1[0]!=$hint )
1064
-              $move_fig2 = 1;
1206
+            if ( $pos1[0]==$hint && $pos2[0]!=$hint ) {
1207
+                          $move_fig1 = 1;
1208
+            }
1209
+            if ( $pos2[0]==$hint && $pos1[0]!=$hint ) {
1210
+                          $move_fig2 = 1;
1211
+            }
1065 1212
           }
1066
-          if ( !$move_fig1 && !$move_fig2 )
1067
-            return _MD_CHESS_MOVE_AMBIGUOUS; // "ambiguity is not properly resolved"
1068
-          if ( $move_fig1 )
1069
-            $this->ac_move = sprintf( "%s%s%s%s",
1213
+          if ( !$move_fig1 && !$move_fig2 ) {
1214
+                      return _MD_CHESS_MOVE_AMBIGUOUS;
1215
+          }
1216
+          // "ambiguity is not properly resolved"
1217
+          if ( $move_fig1 ) {
1218
+                      $this->ac_move = sprintf( "%s%s%s%s",
1070 1219
                           $move[0], $pos1, $action, $dest_coord );
1071
-          else
1072
-            $this->ac_move = sprintf( "%s%s%s%s",
1220
+          } else {
1221
+                      $this->ac_move = sprintf( "%s%s%s%s",
1073 1222
                           $move[0], $pos2, $action, $dest_coord );
1223
+          }
1074 1224
           return;
1075 1225
         }
1076
-      }
1077
-      else
1226
+      } else
1078 1227
       {
1079
-        if ( $fig1_can_reach )
1080
-          $this->ac_move = sprintf( "%s%s%s%s",
1081
-                        $move[0], $pos1, $action, $dest_coord ); 
1082
-        else
1083
-          $this->ac_move = sprintf( "%s%s%s%s",
1084
-                        $move[0], $pos2, $action, $dest_coord ); 
1228
+        if ( $fig1_can_reach ) {
1229
+                  $this->ac_move = sprintf( "%s%s%s%s",
1230
+                        $move[0], $pos1, $action, $dest_coord );
1231
+        } else {
1232
+                  $this->ac_move = sprintf( "%s%s%s%s",
1233
+                        $move[0], $pos2, $action, $dest_coord );
1234
+        }
1085 1235
         return "";
1086 1236
       }
1087 1237
     }
@@ -1113,22 +1263,23 @@  discard block
 block discarded – undo
1113 1263
   {
1114 1264
     /* skip P anycase. for attacks skip source digit
1115 1265
        and for moves skip source pos and - */
1116
-    if ( $move[3] == '-' )
1117
-      $new_move = substr( $move, 4 );
1118
-    else
1119
-    if ( $move[3] == 'x' )
1120
-      $new_move = sprintf("%s%s", $move[1], substr( $move, 3 ) );
1121
-  }
1122
-  else
1266
+    if ( $move[3] == '-' ) {
1267
+          $new_move = substr( $move, 4 );
1268
+    } else
1269
+    if ( $move[3] == 'x' ) {
1270
+          $new_move = sprintf("%s%s", $move[1], substr( $move, 3 ) );
1271
+    }
1272
+  } else
1123 1273
   {
1124 1274
     /* try to remove the source position and check whether it
1125 1275
      * is a non-ambigious move. if it is add one of the components
1126 1276
      * and check again */
1127
-    if ( $move[3] == '-' )
1128
-      $dest = substr( $move, 4 );
1129
-    else
1130
-    if ( $move[3] == 'x' )
1131
-      $dest = substr( $move, 3 );
1277
+    if ( $move[3] == '-' ) {
1278
+          $dest = substr( $move, 4 );
1279
+    } else
1280
+    if ( $move[3] == 'x' ) {
1281
+          $dest = substr( $move, 3 );
1282
+    }
1132 1283
     $new_move = sprintf("%s%s", $move[0], $dest );
1133 1284
     if ( $this->completeMove($player,$new_move) != "" )
1134 1285
     {
@@ -1138,8 +1289,10 @@  discard block
 block discarded – undo
1138 1289
       {
1139 1290
         /* add other component */
1140 1291
         $new_move = sprintf("%s%s%s", $move[0], $move[2], $dest );
1141
-        if ( $this->completeMove($player,$new_move) != "" )
1142
-           $new_move = $move; /* give up */
1292
+        if ( $this->completeMove($player,$new_move) != "" ) {
1293
+                   $new_move = $move;
1294
+        }
1295
+        /* give up */
1143 1296
       }
1144 1297
     }
1145 1298
   }
@@ -1323,67 +1476,91 @@  discard block
 block discarded – undo
1323 1476
 		$result = _MD_CHESS_MOVE_CASTLED_LONG;
1324 1477
 		$move_handled = 1;
1325 1478
 		$this->last_move = "O-O-O";
1326
-	}
1327
-
1328
-  else
1479
+	} else
1329 1480
   {
1330 1481
     /* [PRNBQK][a-h][1-8][-:x][a-h][1-8][RNBQK] full move */
1331 1482
 
1332 1483
     /* allow short move description by autocompleting to
1333 1484
      * full description */
1334 1485
     $ac_error = $this->completeMove( $cur_player, trim($move) );
1335
-    if ( $ac_error != "" )
1336
-      return(array(false, $ac_error)); // "ERROR: autocomplete: $ac_error"
1337
-    else 
1338
-      $move = $this->ac_move;
1486
+    if ( $ac_error != "" ) {
1487
+          return(array(false, $ac_error));
1488
+    }
1489
+    // "ERROR: autocomplete: $ac_error"
1490
+    else {
1491
+          $move = $this->ac_move;
1492
+    }
1339 1493
     $this->last_move = str_replace( "?", "", $move );
1340 1494
     
1341 1495
     /* a final captial letter may only be N,B,R,Q for the
1342 1496
      * appropiate chessman */
1343 1497
     $c = $move[strlen($move)-1];
1344
-    if ( $c >= 'A' && $c <= 'Z' )
1345
-    if ( $c != 'N' && $c != 'B' && $c != 'R' && $c != 'Q' )
1346
-      return(array(false, _MD_CHESS_MOVE_INVALID_PIECE)); // "ERROR: only N (knight), B (bishop), R (rook) and Q (queen) are valid chessman identifiers"
1498
+    if ( $c >= 'A' && $c <= 'Z' ) {
1499
+        if ( $c != 'N' && $c != 'B' && $c != 'R' && $c != 'Q' )
1500
+      return(array(false, _MD_CHESS_MOVE_INVALID_PIECE));
1501
+    }
1502
+    // "ERROR: only N (knight), B (bishop), R (rook) and Q (queen) are valid chessman identifiers"
1347 1503
     
1348 1504
     /* if it is a full move, try to shorten the history move */
1349
-    if ( strlen( $history_move ) >= 6 )
1350
-      $history_move = 
1505
+    if ( strlen( $history_move ) >= 6 ) {
1506
+          $history_move = 
1351 1507
           $this->convertFullToChessNotation($cur_player,$history_move);
1508
+    }
1352 1509
     /* DEBUG: echo "Move: $move ($history_move)<BR>"; */
1353 1510
     
1354 1511
     /* validate figure and position */
1355 1512
     $fig_type = $move[0];
1356 1513
     $fig_name = $this->getFullFigureName( $fig_type );
1357
-    if ( $fig_name == "empty" )
1358
-      return(array(false, $this->move_msg(_MD_CHESS_MOVE_UNKNOWN_FIGURE, $fig_type))); // "ERROR: Figure $fig_type is unknown!"
1514
+    if ( $fig_name == "empty" ) {
1515
+          return(array(false, $this->move_msg(_MD_CHESS_MOVE_UNKNOWN_FIGURE, $fig_type)));
1516
+    }
1517
+    // "ERROR: Figure $fig_type is unknown!"
1359 1518
     $fig_coord = substr($move,1,2);
1360 1519
     $fig_pos = $this->boardCoordToIndex( $fig_coord );
1361
-    if ( $fig_pos == 64 ) return(array(false, $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $fig_coord))); // "ERROR: $fig_coord is invalid!"
1520
+    if ( $fig_pos == 64 ) {
1521
+    	return(array(false, $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $fig_coord)));
1522
+    }
1523
+    // "ERROR: $fig_coord is invalid!"
1362 1524
     /* DEBUG  echo "fig_type: $fig_type, fig_pos: $fig_pos<BR>"; */
1363
-    if ($this->is_empty_tile($fig_pos))
1364
-      return(array(false, $this->move_msg(_MD_CHESS_MOVE_TILE_EMPTY, $fig_coord))); // "ERROR: Tile $fig_coord is empty."
1365
-    if ( $this->board[$fig_pos]{0} != $cur_player )
1366
-      return(array(false, _MD_CHESS_MOVE_NOT_YOUR_PIECE)); // "ERROR: Figure does not belong to you!"
1367
-    if ( $this->board[$fig_pos]{1} != $fig_type )
1368
-      return(array(false, _MD_CHESS_MOVE_NOEXIST_FIGURE)); // "ERROR: Figure does not exist!"
1525
+    if ($this->is_empty_tile($fig_pos)) {
1526
+          return(array(false, $this->move_msg(_MD_CHESS_MOVE_TILE_EMPTY, $fig_coord)));
1527
+    }
1528
+    // "ERROR: Tile $fig_coord is empty."
1529
+    if ( $this->board[$fig_pos]{0} != $cur_player ) {
1530
+          return(array(false, _MD_CHESS_MOVE_NOT_YOUR_PIECE));
1531
+    }
1532
+    // "ERROR: Figure does not belong to you!"
1533
+    if ( $this->board[$fig_pos]{1} != $fig_type ) {
1534
+          return(array(false, _MD_CHESS_MOVE_NOEXIST_FIGURE));
1535
+    }
1536
+    // "ERROR: Figure does not exist!"
1369 1537
     
1370 1538
     /* get target index */
1371 1539
     $dest_coord = substr($move,4,2);
1372 1540
     $dest_pos = $this->boardCoordToIndex( $dest_coord );
1373
-    if ( $dest_pos == 64 )
1374
-      return(array(false, $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $dest_coord))); // "ERROR: $dest_coord is invalid!"
1375
-    if ( $dest_pos == $fig_pos )
1376
-      return(array(false, _MD_CHESS_MOVE_START_END_SAME));
1541
+    if ( $dest_pos == 64 ) {
1542
+          return(array(false, $this->move_msg(_MD_CHESS_MOVE_COORD_INVALID, $dest_coord)));
1543
+    }
1544
+    // "ERROR: $dest_coord is invalid!"
1545
+    if ( $dest_pos == $fig_pos ) {
1546
+          return(array(false, _MD_CHESS_MOVE_START_END_SAME));
1547
+    }
1377 1548
     /* DEBUG  echo "dest_pos: $dest_pos<BR>"; */
1378 1549
 
1379 1550
     /* get action */
1380 1551
     $action = $move[3];
1381
-    if ( $move[3] == "-" ) 
1382
-      $action = 'M'; /* move */
1383
-    else if ( $move[3] == 'x' )
1384
-      $action = 'A'; /* attack */
1385
-    else
1386
-      return(array(false, $this->move_msg(_MD_CHESS_MOVE_UNKNOWN_ACTION, $action))); // "ERROR: $action is unknown! Please use \"-\" for a move and \"x\" for an attack."
1552
+    if ( $move[3] == "-" ) {
1553
+          $action = 'M';
1554
+    }
1555
+    /* move */
1556
+    else if ( $move[3] == 'x' ) {
1557
+          $action = 'A';
1558
+    }
1559
+    /* attack */
1560
+    else {
1561
+          return(array(false, $this->move_msg(_MD_CHESS_MOVE_UNKNOWN_ACTION, $action)));
1562
+    }
1563
+    // "ERROR: $action is unknown! Please use \"-\" for a move and \"x\" for an attack."
1387 1564
 
1388 1565
     /* if attack an enemy unit must be present on tile
1389 1566
      * and if move then tile must be empty. in both cases
@@ -1395,14 +1572,19 @@  discard block
 block discarded – undo
1395 1572
      * checked here. */
1396 1573
     if ( $fig_type != 'P' )
1397 1574
     {
1398
-        if ( !$this->tileIsReachable( $fig_type, $fig_pos, $dest_pos ) )
1399
-          return(array(false, $this->move_msg(_MD_CHESS_MOVE_OUT_OF_RANGE, $dest_coord, $fig_name, $fig_coord))); // "ERROR: Tile $dest_coord is out of moving range for $fig_name at $fig_coord!"
1400
-    }
1401
-    else {
1402
-      if ( $action == 'M' && !$this->checkPawnMove( $fig_pos, $dest_pos ) )
1403
-          return(array(false, $this->move_msg(_MD_CHESS_MOVE_OUT_OF_RANGE, $dest_coord, $fig_name, $fig_coord))); // "ERROR: Tile $dest_coord is out of moving range for $fig_name at $fig_coord!"
1404
-      if ( $action == 'A' && !$this->checkPawnAttack( $fig_pos, $dest_pos ) )
1405
-          return(array(false, $this->move_msg(_MD_CHESS_MOVE_OUT_OF_RANGE, $dest_coord, $fig_name, $fig_coord))); // "ERROR: Tile $dest_coord is out of attacking range for $fig_name at $fig_coord!"
1575
+        if ( !$this->tileIsReachable( $fig_type, $fig_pos, $dest_pos ) ) {
1576
+                  return(array(false, $this->move_msg(_MD_CHESS_MOVE_OUT_OF_RANGE, $dest_coord, $fig_name, $fig_coord)));
1577
+        }
1578
+        // "ERROR: Tile $dest_coord is out of moving range for $fig_name at $fig_coord!"
1579
+    } else {
1580
+      if ( $action == 'M' && !$this->checkPawnMove( $fig_pos, $dest_pos ) ) {
1581
+                return(array(false, $this->move_msg(_MD_CHESS_MOVE_OUT_OF_RANGE, $dest_coord, $fig_name, $fig_coord)));
1582
+      }
1583
+      // "ERROR: Tile $dest_coord is out of moving range for $fig_name at $fig_coord!"
1584
+      if ( $action == 'A' && !$this->checkPawnAttack( $fig_pos, $dest_pos ) ) {
1585
+                return(array(false, $this->move_msg(_MD_CHESS_MOVE_OUT_OF_RANGE, $dest_coord, $fig_name, $fig_coord)));
1586
+      }
1587
+      // "ERROR: Tile $dest_coord is out of attacking range for $fig_name at $fig_coord!"
1406 1588
     }
1407 1589
      
1408 1590
 	$en_passant_capture_performed = 0; // 1 if en passant captured occurred, else 0
@@ -1433,8 +1615,9 @@  discard block
 block discarded – undo
1433 1615
 
1434 1616
     /* perform move */
1435 1617
     $this->clear_tile($fig_pos);
1436
-    if (!$this->is_empty_tile($dest_pos))
1437
-      $this->piece_captured = sprintf("%s%s",$this->board[$dest_pos],$dest_pos);
1618
+    if (!$this->is_empty_tile($dest_pos)) {
1619
+          $this->piece_captured = sprintf("%s%s",$this->board[$dest_pos],$dest_pos);
1620
+    }
1438 1621
     $this->board[$dest_pos] = "$cur_player$fig_type";
1439 1622
     if ( $en_passant_capture_performed ) {
1440 1623
       /* kill pawn */
@@ -1442,8 +1625,7 @@  discard block
 block discarded – undo
1442 1625
       {
1443 1626
         $this->clear_tile($dest_pos - 8);
1444 1627
         $this->piece_captured = sprintf("bP%s",$dest_pos-8);
1445
-      }
1446
-      else
1628
+      } else
1447 1629
       {
1448 1630
         $this->clear_tile($dest_pos + 8);
1449 1631
         $this->piece_captured = sprintf("wP%s",$dest_pos+8);
@@ -1457,10 +1639,11 @@  discard block
 block discarded – undo
1457 1639
       $this->board[$dest_pos] = $old_dest_tile;
1458 1640
       if ( $en_passant_capture_performed ) {
1459 1641
        // restore pawn that was captured above, since that move is invalid
1460
-        if ( $cur_player == 'w' ) 
1461
-          $this->board[$dest_pos-8] = "bP";
1462
-        else
1463
-          $this->board[$dest_pos+8] = "wP";
1642
+        if ( $cur_player == 'w' ) {
1643
+                  $this->board[$dest_pos-8] = "bP";
1644
+        } else {
1645
+                  $this->board[$dest_pos+8] = "wP";
1646
+        }
1464 1647
       }
1465 1648
       return(array(false, _MD_CHESS_MOVE_IN_CHECK)); // "ERROR: Move is invalid because king would be under attack then."
1466 1649
     }
@@ -1501,10 +1684,11 @@  discard block
 block discarded – undo
1501 1684
 		$this->gamestate['fen_en_passant_target_square'] = '-';
1502 1685
 	}
1503 1686
 
1504
-    if ($action == 'M' )
1505
-      $result = $this->move_msg(_MD_CHESS_MOVE_MOVED, $fig_name, $fig_coord, $dest_coord);
1506
-    else
1507
-      $result = $this->move_msg(_MD_CHESS_MOVE_CAPTURED, $fig_name, $dest_coord, $fig_coord);
1687
+    if ($action == 'M' ) {
1688
+          $result = $this->move_msg(_MD_CHESS_MOVE_MOVED, $fig_name, $fig_coord, $dest_coord);
1689
+    } else {
1690
+          $result = $this->move_msg(_MD_CHESS_MOVE_CAPTURED, $fig_name, $dest_coord, $fig_coord);
1691
+    }
1508 1692
     
1509 1693
     /* if pawn reached last line convert into a queen */
1510 1694
     if ( $fig_type == 'P' )
@@ -1763,8 +1947,10 @@  discard block
 block discarded – undo
1763 1947
     default: return 64; /* erronous coord */
1764 1948
   }
1765 1949
   $y = $coord[1]-1; 
1766
-  if ( $y < 0 || $y > 7 )
1767
-    return 64; /* erronous coord */
1950
+  if ( $y < 0 || $y > 7 ) {
1951
+      return 64;
1952
+  }
1953
+  /* erronous coord */
1768 1954
   $index = $y * 8 + $x;
1769 1955
   //echo "$index | ";
1770 1956
   return $index;
@@ -1781,8 +1967,9 @@  discard block
 block discarded – undo
1781 1967
 function boardIndexToCoord( $index )
1782 1968
 {
1783 1969
   //echo $index," --> ";
1784
-  if ( $index < 0 || $index > 63 )
1785
-    return "";
1970
+  if ( $index < 0 || $index > 63 ) {
1971
+      return "";
1972
+  }
1786 1973
   $y = floor($index/8)+1;
1787 1974
   $x = chr( ($index%8)+97 );
1788 1975
   $coord = "$x$y";
@@ -1825,14 +2012,30 @@  discard block
 block discarded – undo
1825 2012
 
1826 2013
   $x = $fig_pos % 8; $y = floor( $fig_pos / 8 );
1827 2014
   
1828
-  if ( $x > 0 && $y > 0 ) $adj_tiles[$i++] = $fig_pos-9;
1829
-  if (           $y > 0 ) $adj_tiles[$i++] = $fig_pos-8;
1830
-  if ( $x < 7 && $y > 0 ) $adj_tiles[$i++] = $fig_pos-7;
1831
-  if ( $x < 7           ) $adj_tiles[$i++] = $fig_pos+1;
1832
-  if ( $x < 7 && $y < 7 ) $adj_tiles[$i++] = $fig_pos+9;
1833
-  if (           $y < 7 ) $adj_tiles[$i++] = $fig_pos+8;
1834
-  if ( $x > 0 && $y < 7 ) $adj_tiles[$i++] = $fig_pos+7;
1835
-  if ( $x > 0           ) $adj_tiles[$i++] = $fig_pos-1;
2015
+  if ( $x > 0 && $y > 0 ) {
2016
+  	$adj_tiles[$i++] = $fig_pos-9;
2017
+  }
2018
+  if (           $y > 0 ) {
2019
+  	$adj_tiles[$i++] = $fig_pos-8;
2020
+  }
2021
+  if ( $x < 7 && $y > 0 ) {
2022
+  	$adj_tiles[$i++] = $fig_pos-7;
2023
+  }
2024
+  if ( $x < 7           ) {
2025
+  	$adj_tiles[$i++] = $fig_pos+1;
2026
+  }
2027
+  if ( $x < 7 && $y < 7 ) {
2028
+  	$adj_tiles[$i++] = $fig_pos+9;
2029
+  }
2030
+  if (           $y < 7 ) {
2031
+  	$adj_tiles[$i++] = $fig_pos+8;
2032
+  }
2033
+  if ( $x > 0 && $y < 7 ) {
2034
+  	$adj_tiles[$i++] = $fig_pos+7;
2035
+  }
2036
+  if ( $x > 0           ) {
2037
+  	$adj_tiles[$i++] = $fig_pos-1;
2038
+  }
1836 2039
 
1837 2040
   /* DEBUG:  foreach( $adj_tiles as $tile )
1838 2041
     echo "adj: $tile "; */
Please login to merge, or discard this patch.