public static function check($input) { if (strlen($input) > 0) { $list = self::genList($input, false); } else { $list = self::genList("AA1121314151617181122232425262728217273747576777871828384858687888000000", false); } $cnt = 0; for ($i = 0; $i < count($list); $i++) { if (self::kingKillable($list[$i]) == false) { $cnt++; if ($cnt > 1) { return "Moves Available\n"; } else { $rec = explode(',', $list[$i]); $oneboard = $rec[3]; } } } if ($cnt == 1) { return "One:" . $oneboard . "\n"; } $board = new ChessBoard(); $board->loadString($input); $board->changeTurn(); $revboard = "Test,00,00," . $board->toString(); if (self::kingKillable($revboard)) { return "Checkmate\n"; } else { return "Stalemate\n"; } }
public function generatePreList() { $temp = new ChessBoard(); $aboard = new ChessBoard(); $moves = array(); $output = array(); $moves[] = array(1, -1); $moves[] = array(1, 0); $moves[] = array(1, 1); $moves[] = array(-1, 1); $moves[] = array(-1, -1); $moves[] = array(-1, 0); $moves[] = array(0, 1); $moves[] = array(0, -1); foreach ($moves as $move) { $output = array_merge($output, $this->addBoard($this->x, $this->y, $move)); } //the rest is castling conditions if ($this->typecode == ChessBoard::$WHITEKING) { //white king castling to the left if ($this->x == 5 && $this->y == 1 && $this->board->get(4, 1) == -1 && $this->board->get(3, 1) == -1 && $this->board->get(2, 1) == -1 && $this->board->get(1, 1) == ChessBoard::$WHITEROOK && !($this->board->castleData() & 64) && !($this->board->castleData() & 32)) { $aboard->copy($this->board); $aboard->changeTurn(); if ($this->recurse && !kingKillable("k,1,1," + $aboard->toString()) && !leftCastleThroughCheck($this->board)) { $temp->copy($this->board); $temp->set(1, 1, -1); $temp->set(3, 1, ChessBoard::$WHITEKING); $temp->set(4, 1, ChessBoard::$WHITEROOK); $temp->set(5, 1, -1); $temp->setLastMove(5, 1, 3, 1); $temp->changeTurn(); $temp->checkKingRookHome(); $output[] = $temp->toString(); } } // white king castling to the right if ($this->x == 5 && $this->y == 1 && $this->board->get(6, 1) == -1 && $this->board->get(7, 1) == -1 && $this->board->get(8, 1) == ChessBoard::$WHITEROOK && !($this->board->castleData() & 64) && !($this->board->castleData() & 16)) { $aboard->copy($this->board); $aboard->changeTurn(); if ($this->recurse && !kingKillable("k,1,1," + $aboard->toString()) && !rightCastleThroughCheck($this->board)) { $temp->copy($this->board); $temp->set(8, 1, -1); $temp->set(7, 1, ChessBoard::$WHITEKING); $temp->set(6, 1, ChessBoard::$WHITEROOK); $temp->set(5, 1, -1); $temp->setLastMove(5, 1, 7, 1); $temp->changeTurn(); $temp->checkKingRookHome(); $output[] = $temp->toString(); } } } if ($this->typecode == ChessBoard::$BLACKKING) { // black king castling to the left if ($this->x == 5 && $this->y == 8 && $this->board->get(4, 8) == -1 && $this->board->get(3, 8) == -1 && $this->board->get(2, 8) == -1 && $this->board->get(1, 8) == ChessBoard::$BLACKROOK && !($this->board->castleData() & 4) && !($this->board->castleData() & 2)) { $aboard->copy($this->board); $aboard->changeTurn(); if ($this->recurse && !kingKillable("k,1,1," + $aboard->toString()) && !leftCastleThroughCheck($this->board)) { $temp->copy($this->board); $temp->set(1, 8, -1); $temp->set(3, 8, ChessBoard::$BLACKKING); $temp->set(4, 8, ChessBoard::$BLACKROOK); $temp->set(5, 8, -1); $temp->setLastMove(5, 8, 3, 8); $temp->changeTurn(); $temp->checkKingRookHome(); $output[] = $temp->toString(); } } // black king castling to the right if ($this->x == 5 && $this->y == 8 && $this->board->get(6, 8) == -1 && $this->board->get(7, 8) == -1 && $this->board->get(8, 8) == ChessBoard::$BLACKROOK && !($this->board->castleData() & 4) && !($this->board->castleData() & 1)) { $aboard->copy($this->board); $aboard->changeTurn(); if ($this->recurse && !kingKillable("k,1,1," + $aboard->toString()) && !rightCastleThroughCheck($this->board)) { $temp->copy($this->board); $temp->set(8, 8, -1); $temp->set(7, 8, ChessBoard::$BLACKKING); $temp->set(6, 8, ChessBoard::$BLACKROOK); $temp->set(5, 8, -1); $temp->setLastMove(5, 8, 7, 8); $temp->changeTurn(); $temp->checkKingRookHome(); $output[] = $temp->toString(); } } } return $output; }