PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/include/go.inc.php

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