PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/class/iShudan.class.php

http://ishudan.googlecode.com/
PHP | 961 lines | 756 code | 145 blank | 60 comment | 277 complexity | 90e3f60ec3da8de592e45ef7d05cff92 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------
  4. |
  5. | iShudan.class.php (php 4.x)
  6. |
  7. | by Benjam Welker
  8. | http://www.iohelix.net
  9. | based on works by
  10. | http://www.sourceforge.com
  11. |
  12. +---------------------------------------------------------------------------
  13. |
  14. | > PHP Go module
  15. | > Date started: 2007-01-13
  16. | > Last edited: 2007-01-13
  17. |
  18. | > Module Version Number: 0.8
  19. |
  20. | $Id$
  21. +---------------------------------------------------------------------------
  22. */
  23. class iShudan
  24. {
  25. var $board; // the board array
  26. var $oldboard; // the old board array, for ko testing purposes
  27. var $empty_board; // the empty board for display purposes
  28. var $boardsize; // the size of the board
  29. var $handicap; // the black player's handicap
  30. var $color; // the color of the current move
  31. var $turn; // (true / false) player's turn ?
  32. var $history; // the history array
  33. var $move; // the current move
  34. var $moves; // the previous moves array
  35. var $move_count; // the number of moves performed
  36. var $moved_stone_x; // the moved stone's x position
  37. var $moved_stone_y; // the moved stone's y position
  38. var $black_prisoners; // the black prisoners
  39. var $white_prisoners; // the white prisoners
  40. var $passes; // the number of consecutive passes
  41. var $resign; // whether or not the player resigned
  42. var $dummy; // ???
  43. var $visited; // the list of visited points
  44. var $stones_visited; // the list of visited stones
  45. var $freedoms_visited; // the list of visited freedoms
  46. var $removed; // the list of removed stones
  47. var $owner; // the owner of a group with freedoms
  48. var $freedom; // the number of freedoms in a group
  49. var $error; // any errors that may be encountered
  50. function iShudan( )
  51. {
  52. // constructor
  53. }
  54. function set_board($board)
  55. {
  56. $this->board = $board;
  57. }
  58. function get_board( )
  59. {
  60. return $this->board;
  61. }
  62. function set_boardsize($boardsize)
  63. {
  64. // make sure the boarsize is valid
  65. if ((19 < $boardsize) || (9 > $boardsize) || (1 == ($boardsize % 2)))
  66. {
  67. $this->error = 'Incorrect boardsize given, boardsize must be an odd number between 9 and 19, inclusive.';
  68. return false;
  69. }
  70. $this->boardsize = $boardsize;
  71. return true;
  72. }
  73. function set_handicap($handicap)
  74. {
  75. // make sure the handicap is valid
  76. if ((9 < $handicap) || (2 > $handicap))
  77. {
  78. $this->error = 'Incorrect handicap given, handicap must be between 2 and 9, inclusive.';
  79. return false;
  80. }
  81. $this->handicap = $handicap;
  82. return true;
  83. }
  84. function set_moves($moves)
  85. {
  86. $this->moves = $moves;
  87. $this->move_count = count($this->moves);
  88. }
  89. // plays the moves array to the board, used to recreate board from moves array
  90. function play_moves($until = null)
  91. {
  92. $this->black_prisoners = 0;
  93. $this->white_prisoners = 0;
  94. $this->passes = 0;
  95. $this->resign = false;
  96. $this->history = array( );
  97. $until = is_null($until) ? $this->move_count + 1 : $until;
  98. if ($this->handicap > 0)
  99. {
  100. $this->board = $this->set_handicap_stones( );
  101. }
  102. for ($i = 0; $i < $this->move_count; ++$i)
  103. {
  104. for ($j = 0; $j < strlen($this->moves[$i]); $j = $j + 3)
  105. {
  106. list($this->color, $y, $x) = stringtocord(substr($this->moves[$i], $j, 3));
  107. if ('e' == $this->color)
  108. {
  109. if ($until > $i)
  110. {
  111. $this->insert_empty($y, $x);
  112. }
  113. ++$this->white_prisoners;
  114. }
  115. else if ('r' == $this->color)
  116. {
  117. if ($until > $i)
  118. {
  119. $this->insert_empty($y, $x);
  120. }
  121. ++$this->black_prisoners;
  122. }
  123. else if ((23 == $y) && (23 == $x))
  124. {
  125. if (($i == ($this->move_count - 1)) && ($until == ($this->move_count + 1)) || ($i == ($until - 1)))
  126. {
  127. $this->moved_stone_x = 0;
  128. $this->moved_stone_y = 0;
  129. }
  130. if ('b' == $this->color)
  131. {
  132. $cprint = 'Black';
  133. }
  134. if ('w' == $this->color)
  135. {
  136. $cprint = 'White';
  137. }
  138. ++$this->passes;
  139. $this->history[$i] = 'P';
  140. }
  141. else if ((25 == $y) && (25 == $x))
  142. {
  143. if (($i == ($this->move_count - 1)) && ($until == ($this->move_count + 1)) || ($i == ($until - 1)))
  144. {
  145. $this->moved_stone_x = 0;
  146. $this->moved_stone_y = 0;
  147. }
  148. if ('b' == $this->color)
  149. {
  150. $cprint = 'Black';
  151. $this->resign = 1;
  152. }
  153. if ('w' == $this->color)
  154. {
  155. $cprint = 'White';
  156. $this->resign = 2;
  157. }
  158. $this->passes = 0;
  159. $this->history[$i] = 'R';
  160. }
  161. else
  162. {
  163. if ((($i == ($this->move_count - 1)) && ($until == ($this->move_count + 1))) || ($i < $until))
  164. {
  165. $this->moved_stone_x = $x;
  166. $this->moved_stone_y = $y;
  167. }
  168. if ($until > $i)
  169. {
  170. $this->board[$y][$x] = $this->color;
  171. }
  172. if ('b' == $this->color)
  173. {
  174. $cprint = 'Black';
  175. }
  176. if ('w' == $this->color)
  177. {
  178. $cprint = 'White';
  179. }
  180. $this->history[$i] = strtolower(xtoalt($x)) . ($this->boardsize - $y);
  181. $this->passes = 0;
  182. }
  183. }
  184. if ($i == ($this->move_count - 2))
  185. {
  186. $this->oldboard = $this->board;
  187. }
  188. }
  189. # return array(
  190. # $board ,
  191. # $oldboard ,
  192. # $passes ,
  193. # $resign ,
  194. # $bprisoners ,
  195. # $wprisoners ,
  196. # $mstonex ,
  197. # $mstoney ,
  198. # $history ,
  199. # $countm ,
  200. # );
  201. }
  202. function set_handicap_stones( )
  203. {
  204. if (2 <= $this->handicap)
  205. {
  206. if ($this->boardsize >= 13)
  207. {
  208. $this->board[3][$this->boardsize - 4] = 'b';
  209. }
  210. if ($size_of_the_board < 13)
  211. {
  212. $this->board[2][$this->boardsize - 3] = 'b';
  213. }
  214. if ($this->boardsize >= 13)
  215. {
  216. $this->board[$this->boardsize - 4][3] = 'b';
  217. }
  218. if ($this->boardsize < 13)
  219. {
  220. $this->board[$this->boardsize - 3][2] = 'b';
  221. }
  222. }
  223. if (3 <= $this->handicap)
  224. {
  225. if ($this->boardsize >= 13)
  226. {
  227. $this->board[$this->boardsize - 4][$this->boardsize - 4] = 'b';
  228. }
  229. if ($this->boardsize < 13)
  230. {
  231. $this->board[$this->boardsize - 3][$this->boardsize - 3] = 'b';
  232. }
  233. }
  234. if (4 <= $this->handicap)
  235. {
  236. if ($this->boardsize >= 13)
  237. {
  238. $this->board[3][3] = 'b';
  239. }
  240. if ($this->boardsize < 13)
  241. {
  242. $this->board[2][2] = 'b';
  243. }
  244. }
  245. if (5 <= $this->handicap)
  246. {
  247. if ($this->boardsize >= 13)
  248. {
  249. $this->board[ceil($this->boardsize / 2) - 1][$this->boardsize - 4] = 'b';
  250. }
  251. if ($this->boardsize < 13 && $this->boardsize > 7)
  252. {
  253. $this->board[ceil($this->boardsize / 2) - 1][$this->boardsize - 3] = 'b';
  254. }
  255. }
  256. if (6 <= $this->handicap)
  257. {
  258. if ($this->boardsize >= 13)
  259. {
  260. $this->board[ceil($this->boardsize / 2) - 1][3] = 'b';
  261. }
  262. if ($this->boardsize < 13 && $this->boardsize > 7)
  263. {
  264. $this->board[ceil($this->boardsize / 2) - 1][2] = 'b';
  265. }
  266. }
  267. if (7 <= $this->handicap)
  268. {
  269. if ($this->boardsize >= 13)
  270. {
  271. $this->board[3][ceil($this->boardsize / 2) - 1] = 'b';
  272. }
  273. if ($this->boardsize < 13 && $this->boardsize > 7)
  274. {
  275. $this->board[2][ceil($this->boardsize / 2) - 1] = 'b';
  276. }
  277. }
  278. if (8 <= $this->handicap)
  279. {
  280. if ($this->boardsize >= 13)
  281. {
  282. $this->board[$this->boardsize - 4][ceil($this->boardsize / 2) - 1] = 'b';
  283. }
  284. if ($this->boardsize < 13 && $this->boardsize > 7)
  285. {
  286. $this->board[$this->boardsize - 3][ceil($this->boardsize / 2) - 1] = 'b';
  287. }
  288. }
  289. if (9 == $this->handicap)
  290. {
  291. if ($this->boardsize >= 13)
  292. {
  293. $this->board[ceil($this->boardsize / 2) - 1][ceil($this->boardsize / 2) - 1] = 'b';
  294. }
  295. if ($this->boardsize < 13 && $this->boardsize > 7)
  296. {
  297. $this->board[ceil($this->boardsize / 2) - 1][ceil($this->boardsize / 2) - 1] = 'b';
  298. }
  299. }
  300. }
  301. // insert an empty field on the given position into the board array
  302. function insert_empty($y, $x)
  303. {
  304. if ($y == 0)
  305. {
  306. if ($x == 0)
  307. {
  308. $stone = 'lt';
  309. }
  310. else if ($x == $this->boardsize - 1)
  311. {
  312. $stone = 'rt';
  313. }
  314. else
  315. {
  316. $stone = 'mt';
  317. }
  318. }
  319. else if ($y == $this->boardsize - 1)
  320. {
  321. if ($x == 0)
  322. {
  323. $stone = 'lb';
  324. }
  325. else if ($x == $this->boardsize - 1)
  326. {
  327. $stone = 'rb';
  328. }
  329. else
  330. {
  331. $stone = 'mb';
  332. }
  333. }
  334. else
  335. {
  336. if ($x == 0)
  337. {
  338. $stone = 'lm';
  339. }
  340. else if ($x == $this->boardsize - 1)
  341. {
  342. $stone = 'rm';
  343. }
  344. else
  345. {
  346. if ($this->boardsize >= 13 && ($y == 3 && ($x == 3 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 4)))
  347. {
  348. $stone = 'ko';
  349. }
  350. else if ($this->boardsize >= 13 && ($y == ceil($this->boardsize / 2) - 1 && ($x == 3 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 4)))
  351. {
  352. $stone = 'ko';
  353. }
  354. else if ($this->boardsize >= 13 && ($y == $this->boardsize - 4 && ($x == 3 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 4)))
  355. {
  356. $stone = 'ko';
  357. }
  358. else if ($this->boardsize < 13 && $this->boardsize > 7 && ($y == 2 && ($x == 2 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 3)))
  359. {
  360. $stone = 'ko';
  361. }
  362. else if ($this->boardsize < 13 && $this->boardsize > 7 && ($y == ceil($this->boardsize / 2) - 1 && ($x == 2 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 3)))
  363. {
  364. $stone = 'ko';
  365. }
  366. else if ($this->boardsize < 13 && $this->boardsize > 7 && ($y == $this->boardsize - 3 && ($x == 2 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 3)))
  367. {
  368. $stone = 'ko';
  369. }
  370. else if ($this->boardsize == 7 && ($y == 2 && ($x == 2 || $x == $this->boardsize - 3)))
  371. {
  372. $stone = 'ko';
  373. }
  374. else if ($this->boardsize == 7 && ($y == $this->boardsize - 3 && ($x == 2 || $x == $this->boardsize - 3)))
  375. {
  376. $stone = 'ko';
  377. }
  378. else
  379. {
  380. $stone = 'mm';
  381. }
  382. }
  383. }
  384. $this->board[$y][$x] = $stone;
  385. }
  386. // do a move a user requested, take prisoners
  387. function do_move($y, $x)
  388. {
  389. $this->board[$y][$x] = $this->color;
  390. if ($this->color == 'b')
  391. {
  392. $this->dummy = $this->group_runs_out_of_liberties($y, $x, true, 'w');
  393. }
  394. else if ($color == 'w')
  395. {
  396. $this->dummy = $this->group_runs_out_of_liberties($y, $x, true, 'b');
  397. }
  398. else
  399. {
  400. $this->error = 'Given color attribute is not known.';
  401. }
  402. }
  403. // return true if the move is ko and therfore not allowed, else return false
  404. function is_ko($y, $x)
  405. {
  406. $backup = $this->board;
  407. $this->do_move($y, $x);
  408. for ($i = 0; $i < $this->boardsize; ++$i)
  409. {
  410. for ($j = 0; $j < $this->boardsize; ++$j)
  411. {
  412. if ($this->board[$i][$j] == $this->oldboard[$i][$j])
  413. {
  414. $ko = true;
  415. }
  416. else
  417. {
  418. $ko = false;
  419. break;
  420. }
  421. }
  422. if ($ko == false)
  423. {
  424. break;
  425. }
  426. }
  427. $this->board = $backup;
  428. return $ko;
  429. }
  430. // return true if the point on the board is a free space
  431. function is_freedom($y, $x)
  432. {
  433. return ((('b' != $this->board[$y][$x]) && ('w' != $this->board[$y][$x])) ? true : false);
  434. }
  435. // return true if the move is not a suicide, this function does not check for ko se the is_ko function for this
  436. function is_legal_move($y, $x)
  437. {
  438. if (('b' != $this->board[$y][$x]) && ('w' != $this->board[$y][$x]))
  439. {
  440. $backup = $this->board[$y][$x];
  441. if ($this->color == 'b')
  442. {
  443. $this->board[$y][$x] = 'b';
  444. if ($this->freedom_of_group($y, $x) < 1)
  445. {
  446. $this->board[$y][$x] = 'b';
  447. if ( ! $this->group_runs_out_of_liberties($y, $x, false, 'w'))
  448. {
  449. $this->board[$y][$x] = $backup;
  450. return false;
  451. }
  452. else
  453. {
  454. $this->board[$y][$x] = $backup;
  455. return true;
  456. }
  457. }
  458. $this->board[$y][$x] = $backup;
  459. return true;
  460. }
  461. else if ($this->color == 'w')
  462. {
  463. $this->board[$y][$x] = 'w';
  464. if ($this->freedom_of_group($y, $x) < 1)
  465. {
  466. if ( ! $this->group_runs_out_of_liberties($y, $x, false, 'b'))
  467. {
  468. $this->board[$y][$x] = $backup;
  469. return false;
  470. }
  471. else
  472. {
  473. $this->board[$y][$x] = $backup;
  474. return true;
  475. }
  476. }
  477. $this->board[$y][$x] = $backup;
  478. return true;
  479. }
  480. else
  481. {
  482. $this->error = 'Given color attribute is not known.';
  483. return false;
  484. }
  485. }
  486. }
  487. function is_move_possible($y, $x)
  488. {
  489. return ( ! $this->is_ko($y, $x) && $this->is_legal_move($y, $x));
  490. }
  491. // delete the stones last checked by another function
  492. function delete_current_visited( )
  493. {
  494. $pos_to_delete = strtok($this->visited, ',');
  495. while (strlen($pos_to_delete) >= 1)
  496. {
  497. if (strlen($pos_to_delete) >= 1)
  498. {
  499. if (intval($pos_to_delete) >= 100)
  500. {
  501. $y = floor(intval($pos_to_delete) / 100);
  502. }
  503. else
  504. {
  505. $y = 0;
  506. }
  507. $x = intval($pos_to_delete) - ($y * 100);
  508. if ($color == 'b')
  509. {
  510. $rcolor = 'e';
  511. }
  512. else if ($color == 'w')
  513. {
  514. $rcolor = 'r';
  515. }
  516. else
  517. {
  518. echo 'Error';
  519. die;
  520. }
  521. if ( ! strstr($this->removed, $rcolor . chr($y + 97) . chr($x + 97)))
  522. {
  523. $this->removed = $this->removed . $rcolor . chr($y + 97) . chr($x + 97);
  524. }
  525. $this->board[$y][$x] = $hits->insert_empty($y, $x);
  526. }
  527. $pos_to_delete = strtok(',');
  528. }
  529. }
  530. // return all board positions which are connected by lines to the current one
  531. function is_connected_to($y, $x)
  532. {
  533. $connected = array( );
  534. if ($y == 0)
  535. {
  536. if ($x == 0)
  537. {
  538. $connected[0] = $y + 1;
  539. $connected[1] = $x;
  540. $connected[2] = $y;
  541. $connected[3] = $x + 1;
  542. }
  543. else if ($x == $this->boardsize - 1)
  544. {
  545. $connected[0] = $y + 1;
  546. $connected[1] = $x;
  547. $connected[2] = $y;
  548. $connected[3] = $x - 1;
  549. }
  550. else
  551. {
  552. $connected[0] = $y;
  553. $connected[1] = $x - 1;
  554. $connected[2] = $y;
  555. $connected[3] = $x + 1;
  556. $connected[4] = $y + 1;
  557. $connected[5] = $x;
  558. }
  559. }
  560. else if ($y == $this->boardsize - 1)
  561. {
  562. if ($x == 0)
  563. {
  564. $connected[0] = $y - 1;
  565. $connected[1] = $x;
  566. $connected[2] = $y;
  567. $connected[3] = $x + 1;
  568. }
  569. else if ($x == $this->boardsize - 1)
  570. {
  571. $connected[0] = $y - 1;
  572. $connected[1] = $x;
  573. $connected[2] = $y;
  574. $connected[3] = $x - 1;
  575. }
  576. else
  577. {
  578. $connected[0] = $y;
  579. $connected[1] = $x - 1;
  580. $connected[2] = $y;
  581. $connected[3] = $x + 1;
  582. $connected[4] = $y - 1;
  583. $connected[5] = $x;
  584. }
  585. }
  586. else
  587. {
  588. if ($x == 0)
  589. {
  590. $connected[0] = $y - 1;
  591. $connected[1] = $x;
  592. $connected[2] = $y + 1;
  593. $connected[3] = $x;
  594. $connected[4] = $y;
  595. $connected[5] = $x + 1;
  596. }
  597. else if ($x == $this->boardsize - 1)
  598. {
  599. $connected[0] = $y - 1;
  600. $connected[1] = $x;
  601. $connected[2] = $y + 1;
  602. $connected[3] = $x;
  603. $connected[4] = $y;
  604. $connected[5] = $x - 1;
  605. }
  606. else
  607. {
  608. $connected[0] = $y + 1;
  609. $connected[1] = $x;
  610. $connected[2] = $y - 1;
  611. $connected[3] = $x;
  612. $connected[4] = $y;
  613. $connected[5] = $x + 1;
  614. $connected[6] = $y;
  615. $connected[7] = $x - 1;
  616. }
  617. }
  618. return $connected;
  619. }
  620. // return true if group is dead
  621. function group_runs_out_of_liberties($y, $x, $delete_dead_stones)
  622. {
  623. $they_are_dead_jim = false;
  624. $counter = 0;
  625. $to_check = $this->is_connected_to($y, $x);
  626. foreach ($to_check as $number => $coord)
  627. {
  628. if ($counter % 2 == 0)
  629. {
  630. if ($this->board[$to_check[$counter]][$to_check[$counter + 1]] == $this->color)
  631. {
  632. if ($this->freedom_of_group($to_check[$counter], $to_check[$counter + 1]) < 1)
  633. {
  634. if ($delete_dead_stones)
  635. {
  636. $this->delete_current_visited($this->color);
  637. }
  638. $they_are_dead_jim = true;
  639. }
  640. }
  641. }
  642. ++$counter;
  643. }
  644. return $they_are_dead_jim;
  645. }
  646. // returns the owner and the number of freedoms grouped together
  647. function group_of_freedoms($y, $x)
  648. {
  649. $this->visited = ',';
  650. $this->stones_visited = ',';
  651. $to_visit_string = ',' . strval(($y * 100) + $x) . ',';
  652. $this->owner = '';
  653. $this->freedom = 0;
  654. while (strlen($to_visit_string) > 2)
  655. {
  656. $cord_to_visit = strtok(substr($to_visit_string, 1), ',');
  657. if (intval($cord_to_visit) >= 100)
  658. {
  659. $y = floor(intval($cord_to_visit) / 100);
  660. }
  661. else
  662. {
  663. $y = 0;
  664. }
  665. $x = intval($cord_to_visit) - ($y * 100);
  666. $to_visit_string = str_replace(',' . $cord_to_visit . ',', ',', $to_visit_string);
  667. $counter = 0;
  668. $to_check = is_connected_to($y, $x);
  669. foreach ($to_check as $number => $coord)
  670. {
  671. if ($counter % 2 == 0)
  672. {
  673. if ( ! strstr($this->stones_visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
  674. {
  675. if ( ! $this->is_freedom($to_check[$counter], $to_check[$counter + 1]))
  676. {
  677. if ('' != $this->owner)
  678. {
  679. if ($this->owner != $this->board[$to_check[$counter]][$to_check[$counter + 1]])
  680. {
  681. $this->owner = 'f';
  682. }
  683. }
  684. else
  685. {
  686. $this->owner = $this->board[$to_check[$counter]][$to_check[$counter + 1]];
  687. }
  688. $this->stones_visited .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
  689. }
  690. }
  691. if ( ! strstr($this->visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
  692. {
  693. if ($this->is_freedom($to_check[$counter], $to_check[$counter + 1]))
  694. {
  695. ++$this->freedom;
  696. $to_visit_string .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
  697. }
  698. }
  699. }
  700. ++$counter;
  701. }
  702. $this->visited .= strval(($y * 100) + $x) . ',';
  703. }
  704. # return array(
  705. # $owner ,
  706. # $freedom ,
  707. # );
  708. }
  709. // how many liberties does a group of stones have
  710. function freedom_of_group($y, $x)
  711. {
  712. $this->visited = ',';
  713. $this->freedoms_visited = ',';
  714. $to_visit_string = ',' . strval(($y * 100) + $x) . ',';
  715. $this->freedom = 0;
  716. while (strlen($to_visit_string) > 2)
  717. {
  718. $cord_to_visit = strtok(substr($to_visit_string, 1), ',');
  719. if (intval($cord_to_visit) >= 100)
  720. {
  721. $y = floor(intval($cord_to_visit) / 100);
  722. }
  723. else
  724. {
  725. $y = 0;
  726. }
  727. $x = intval($cord_to_visit) - ($y * 100);
  728. $to_visit_string = str_replace(',' . $cord_to_visit . ',', ',', $to_visit_string);
  729. $counter = 0;
  730. $to_check = $this->is_connected_to($y, $x);
  731. foreach ($to_check as $number => $coord)
  732. {
  733. if ($counter % 2 == 0)
  734. {
  735. if ( ! strstr($this->freedoms_visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
  736. {
  737. if ($this->is_freedom($to_check[$counter], $to_check[$counter + 1]))
  738. {
  739. ++$this->freedom;
  740. $this->freedoms_visited .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
  741. }
  742. }
  743. if ( ! strstr($this->visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
  744. {
  745. if ($this->board[$to_check[$counter]][$to_check[$counter + 1]] == $this->color)
  746. {
  747. $to_visit_string .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
  748. }
  749. }
  750. }
  751. ++$counter;
  752. }
  753. $this->visited .= strval(($y * 100) + $x) . ',';
  754. }
  755. return $this->freedom;
  756. }
  757. // creates an empty board to start with
  758. function create_empty_board( )
  759. {
  760. $this->board = array( );
  761. for ($y = 0; $y < $this->boardsize; ++$y)
  762. {
  763. for ($x = 0; $x < $this->boardsize; ++$x)
  764. {
  765. $this->board[$y][$x] = $this->insert_empty($y, $x);
  766. }
  767. }
  768. }
  769. function get_empty_board( )
  770. {
  771. // save the current board
  772. $temp_board = $this->get_board( );
  773. // create a new empty board
  774. $this->create_empty_board( );
  775. // save the new empty board
  776. $return_board = $this->board;
  777. // replace the original board
  778. $this->set_board($temp_board);
  779. // return the empty board to the script
  780. return $return_board;
  781. }
  782. function get_moved_stone_x( )
  783. {
  784. return $this->moved_stone_x;
  785. }
  786. function get_moved_stone_y( )
  787. {
  788. return $this->moved_stone_y;
  789. }
  790. } // end of iShudan class
  791. // other non-class but required functions
  792. // translate numeric coordinates to alphanumeric coordinates
  793. function xtoalt($xcord)
  794. {
  795. if ($xcord < 8)
  796. {
  797. return chr(65 + $xcord);
  798. }
  799. else
  800. {
  801. return chr(66 + $xcord);
  802. }
  803. }
  804. // translates alphanumeric coordinates to numeric coordinates
  805. function stringtocord($costring)
  806. {
  807. $color = substr($costring, 0, 1);
  808. $y = ord(substr($costring, 1, 1)) - 97;
  809. $x = ord(substr($costring, 2, 1)) - 97;
  810. return array(
  811. $color ,
  812. $y ,
  813. $x ,
  814. );
  815. }
  816. ?>