Advertisement
Valleri

Chess checking for invalid chessboard

Aug 25th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.58 KB | None | 0 0
  1. $field = "R1-H1-B1-K1-Q1-B1-H1-R1/P1-P1- -P1-P1- -P1-P1/ - -P1- - - - - /- - - - -P1- - /- - - - - - - /-P2- - - - - -H2/P2- -P2-P2-P2-P2-P2-P2/R2-H2-B2-K2-Q2-B2- -R2";
  2.  
  3. $rows = explode('/', $field);
  4.  
  5. $rows = array_filter($rows, function($val) {
  6.     $valid_figures = array('R1', 'B1', 'Q1', 'K1', 'P1', 'H1', '', ' ', 'R2', 'B2', 'Q2', 'K2', 'P2', 'H2');
  7.     $figures_in_field = explode('-', $val);
  8.     $diff = array_diff(array_diff($figures_in_field, $valid_figures), $valid_figures);
  9.     if(count($figures_in_field) != 8 || !count($diff) == 0) { return false; } else { return true; }
  10. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement