All pastes #460698 Raw Edit

phpChess (Objects)

public php v1 · immutable
#460698 ·published 2007-04-27 06:58 UTC
rendered paste body
phpChess :: (Daemon) barryd.it@gmail.com<?php/*  This class is to create and manage a single chess board object with PHP 5/6 OOP*/class chess {  protected $board = array();  public function board($pieces = array()) {    /*     * Populate the Chess Board w/ spaces. (NULL will not work properally without using "if" statments... in other words its pointless)     */    if (!empty($pieces) && empty($this->board)) {      foreach (range('a', 'h') as $file) {        foreach (range('1', '8') as $rank) {          $this->board["$file$rank"] = "\x20";        }      }      $this->board = array_merge($this->board, $pieces);    }    else {      return $this->board;    }  }  public function evaluate() {  }  public function __construct() {    /*     * Populate the Chess Board w/ associated characters.     */    $this->board(    /*     * Figurine Algebraic Notation     */      array(        'a1' => "\xe2\x99\x96", 'b1' => "\xe2\x99\x98", 'c1' => "\xe2\x99\x97", 'd1' => "\xe2\x99\x95", 'e1' => "\xe2\x99\x94", 'f1' => "\xe2\x99\x97", 'g1' => "\xe2\x99\x98", 'h1' => "\xe2\x99\x96", 'a2' => "\xe2\x99\x99", 'b2' => "\xe2\x99\x99", 'c2' => "\xe2\x99\x99", 'd2' => "\xe2\x99\x99", 'e2' => "\xe2\x99\x99", 'f2' => "\xe2\x99\x99", 'g2' => "\xe2\x99\x99", 'h2' => "\xe2\x99\x99", 'a7' => "\xe2\x99\x99", 'b7' => "\xe2\x99\x99", 'c7' => "\xe2\x99\x99", 'd7' => "\xe2\x99\x99", 'e7' => "\xe2\x99\x99", 'f7' => "\xe2\x99\x99", 'g7' => "\xe2\x99\x99", 'h7' => "\xe2\x99\x99", 'a8' => "\xe2\x99\x9c", 'b8' => "\xe2\x99\x9e", 'c8' => "\xe2\x99\x9d", 'd8' => "\xe2\x99\x9b", 'e8' => "\xe2\x99\x9a", 'f8' => "\xe2\x99\x9d", 'g8' => "\xe2\x99\x9e", 'h8' => "\xe2\x99\x9c"      )    );  }  public function main() {  }  public function move($string) {  }}/*White&#9812; (king) ("\xe2\x99\x94")&#9813; (queen) ("\xe2\x99\x95")&#9814; (rook) ("\xe2\x99\x96")&#9815; (bishop) ("\xe2\x99\x97")&#9816; (knight) ("\xe2\x99\x98")&#9817; (pawn) ("\xe2\x99\x99")Black&#9818; (king) ("\xe2\x99\x9a")&#9819; (queen) ("\xe2\x99\x9b")&#9820; (rook) ("\xe2\x99\x9c")&#9821; (bishop) ("\xe2\x99\x9d")&#9822; (knight) ("\xe2\x99\x9e")&#9823; (pawn) ("\xe2\x99\x9f")*/?><?php  /***************************************************************************   *   Copyright (C) 2006 by Barry Dick <d43m0n@shaw.ca>                     *   *                                                                         *   *   This program is free software; you can redistribute it and/or modify  *   *   it under the terms of the GNU General Public License as published by  *   *   the Free Software Foundation; either version 2 of the License, or     *   *   (at your option) any later version.                                   *   *                                                                         *   *   This program is distributed in the hope that it will be useful,       *   *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *   *   GNU General Public License for more details.                          *   *                                                                         *   *   You should have received a copy of the GNU General Public License     *   *   along with this program; if not, write to the                         *   *   Free Software Foundation, Inc.,                                       *   *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *   ***************************************************************************/class player extends chess {  public $player = array('stdin' => NULL);  public function stdin() {    $this->player['stdin']['read'][] = STDIN;    stream_select($this->player['stdin']['read'], $this->player['stdin']['write'] = NULL, $this->player['stdin']['except'] = NULL, $this->player['stdin']['tv'] = 0);    if (count($this->player['stdin']['read'])) {      $this->player['stdin']['buffer'] = @fread(STDIN, 1500);      switch($this->player['stdin']['buffer']) {        case "\x0A":          return "\x0A";          break;        default:          $this->player['stdin']['buffer'] = str_replace(array("\r", "\n"), "", $this->player['stdin']['buffer']);          return $this->player['stdin']['buffer'];          break;       }    }    unset($this->player['stdin']);  }  function command($string) {    switch($string) {      case "test":        return 'Testing your computer for virus scanner.';        break;      case "quit":        global $config;        $config['run'] = false;        return "Shutting down";        break;      default:        break;    }  }  public function chessboard() {    $chessboard = $this->board();    return <<<END                   Files         a   b   c   d   e   f   g   h       +---+---+---+---+---+---+---+---+     1 |\033[37m \033[0m{$chessboard['a1']}\033[37m \033[0m|\033[40m\033[37m \033[0m{$chessboard['b1']}\033[40m\033[37m \033[0m| {$chessboard['c1']} | {$chessboard['d1']} | {$chessboard['e1']} | {$chessboard['f1']} | {$chessboard['g1']} | {$chessboard['h1']} | 1       +---+---+---+---+---+---+---+---+     2 | {$chessboard['a2']} | {$chessboard['b2']} | {$chessboard['c2']} | {$chessboard['d2']} | {$chessboard['e2']} | {$chessboard['f2']} | {$chessboard['g2']} | {$chessboard['h2']} | 2       +---+---+---+---+---+---+---+---+     3 | {$chessboard['a3']} | {$chessboard['b3']} | {$chessboard['c3']} | {$chessboard['d3']} | {$chessboard['e3']} | {$chessboard['f3']} | {$chessboard['g3']} | {$chessboard['h3']} | 3 R     +---+---+---+---+---+---+---+---+ a   4 | {$chessboard['a4']} | {$chessboard['b4']} | {$chessboard['c4']} | {$chessboard['d4']} | {$chessboard['e4']} | {$chessboard['f4']} | {$chessboard['g4']} | {$chessboard['h4']} | 4 n     +---+---+---+---+---+---+---+---+ k   5 | {$chessboard['a5']} | {$chessboard['b5']} | {$chessboard['c5']} | {$chessboard['d5']} | {$chessboard['e5']} | {$chessboard['f5']} | {$chessboard['g5']} | {$chessboard['h5']} | 5 s     +---+---+---+---+---+---+---+---+     6 | {$chessboard['a6']} | {$chessboard['b6']} | {$chessboard['c6']} | {$chessboard['d6']} | {$chessboard['e6']} | {$chessboard['f6']} | {$chessboard['g6']} | {$chessboard['h6']} | 6       +---+---+---+---+---+---+---+---+     7 | {$chessboard['a7']} | {$chessboard['b7']} | {$chessboard['c7']} | {$chessboard['d7']} | {$chessboard['e7']} | {$chessboard['f7']} | {$chessboard['g7']} | {$chessboard['h7']} | 7       +---+---+---+---+---+---+---+---+     8 | {$chessboard['a8']} | {$chessboard['b8']} | {$chessboard['c8']} | {$chessboard['d8']} | {$chessboard['e8']} | {$chessboard['f8']} | {$chessboard['g8']} | {$chessboard['h8']} | 8       +---+---+---+---+---+---+---+---+         a   b   c   d   e   f   g   hEND;  }}?><?php/*http://en.wikipedia.org/wiki/Chesshttp://en.wikipedia.org/wiki/Algebraic_chess_notation >http://en.wikipedia.org/wiki/Descriptive_chess_notationFédération Internationale des Échecs (World Chess Federation)http://en.wikipedia.org/wiki/FIDEhttp://en.wikipedia.org/wiki/Chess_problemhttp://en.wikipedia.org/wiki/Chess_problem_terminologyhttp://en.wikipedia.org/wiki/Chessboard$chessboard = array();foreach (range('a', 'h') as $file) {  foreach (range('1', '8') as $rank) {    $chessboard[$file$rank] = '';  }}print_r($chessboard);http://en.wikipedia.org/wiki/Fairy_chess_piece16 Pieces White -> King(1), Queen(1), Rook(2), Bishop(2), Knight(2), Pawn(8) Black -> King(1), Queen(1), Rook(2), Bishop(2), Knight(2), Pawn(8)                   Files         a   b   c   d   e   f   g   h       +---+---+---+---+---+---+---+---+     1 | X | X | X | X | X | X | X | X | 1       +---+---+---+---+---+---+---+---+     2 | X | X | X | X | X | X | X | X | 2       +---+---+---+---+---+---+---+---+     3 |   |   |   |   |   |   |   |   | 3 R     +---+---+---+---+---+---+---+---+ a   4 |   |   |   |   |   |   |   |   | 4 n     +---+---+---+---+---+---+---+---+ k   5 |   |   |   |   |   |   |   |   | 5 s     +---+---+---+---+---+---+---+---+     6 |   |   |   |   |   |   |   |   | 6       +---+---+---+---+---+---+---+---+     7 | X | X | X | X | X | X | X | X | 7       +---+---+---+---+---+---+---+---+     8 | X | X | X | X | X | X | X | X | 8       +---+---+---+---+---+---+---+---+         a   b   c   d   e   f   g   hhttp://www.rubyquiz.com/quiz35.html*/include('class.chess.php');include('class.player.php');$chess = new chess;$player = new player;while(1) {  echo "\x1b\x5b\x58\x1b\x5b\x32\x4a";  echo $player->chessboard();  usleep(900);  $tmp['stream'] = $player->stdin();  if ($tmp['stream'] != NULL) {    if ($tmp['stream'] == "\n") {      echo 'command # ';    }    else {      if (($tmp['buffer'] = $player->command($tmp['stream']))) {        echo $tmp['buffer'] . "\n";      }      echo 'root@localhost:~ # ';    }  }exit();}?>