ishudan /class/iShudan.class.php

Language PHP Lines 961
MD5 Hash 90e3f60ec3da8de592e45ef7d05cff92 Estimated Cost $20,085 (why?)
Repository http://ishudan.googlecode.com/svn/trunk/ View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
<?php

/*
+---------------------------------------------------------------------------
|
|   iShudan.class.php (php 4.x)
|
|   by Benjam Welker
|   http://www.iohelix.net
|   based on works by
|   http://www.sourceforge.com
|
+---------------------------------------------------------------------------
|
|   > PHP Go module
|   > Date started: 2007-01-13
|   >  Last edited: 2007-01-13
|
|   > Module Version Number: 0.8
|
|   $Id$
+---------------------------------------------------------------------------
*/

class iShudan
{
	var $board; // the board array
	var $oldboard; // the old board array, for ko testing purposes
	var $empty_board; // the empty board for display purposes
	var $boardsize; // the size of the board
	var $handicap; // the black player's handicap
	var $color; // the color of the current move
	var $turn; // (true / false) player's turn ?
	var $history; // the history array
	var $move; // the current move
	var $moves; // the previous moves array
	var $move_count; // the number of moves performed
	var $moved_stone_x; // the moved stone's x position
	var $moved_stone_y; // the moved stone's y position
	var $black_prisoners; // the black prisoners
	var $white_prisoners; // the white prisoners
	var $passes; // the number of consecutive passes
	var $resign; // whether or not the player resigned
	var $dummy; // ???
	var $visited; // the list of visited points
	var $stones_visited; // the list of visited stones
	var $freedoms_visited; // the list of visited freedoms
	var $removed; // the list of removed stones
	var $owner; // the owner of a group with freedoms
	var $freedom; // the number of freedoms in a group
	var $error; // any errors that may be encountered

	function iShudan( )
	{
		// constructor
	}


	function set_board($board)
	{
		$this->board = $board;
	}


	function get_board( )
	{
		return $this->board;
	}


	function set_boardsize($boardsize)
	{
		// make sure the boarsize is valid
		if ((19 < $boardsize) || (9 > $boardsize) || (1 == ($boardsize % 2)))
		{
			$this->error = 'Incorrect boardsize given, boardsize must be an odd number between 9 and 19, inclusive.';
			return false;
		}

		$this->boardsize = $boardsize;

		return true;
	}


	function set_handicap($handicap)
	{
		// make sure the handicap is valid
		if ((9 < $handicap) || (2 > $handicap))
		{
			$this->error = 'Incorrect handicap given, handicap must be between 2 and 9, inclusive.';
			return false;
		}

		$this->handicap = $handicap;

		return true;
	}


	function set_moves($moves)
	{
		$this->moves = $moves;
		$this->move_count = count($this->moves);
	}

	// plays the moves array to the board, used to recreate board from moves array
	function play_moves($until = null)
	{
		$this->black_prisoners = 0;
		$this->white_prisoners = 0;
		$this->passes = 0;
		$this->resign = false;
		$this->history = array( );

		$until = is_null($until) ? $this->move_count + 1 : $until;

		if ($this->handicap > 0)
		{
			$this->board = $this->set_handicap_stones( );
		}

		for ($i = 0; $i < $this->move_count; ++$i)
		{
			for ($j = 0; $j < strlen($this->moves[$i]); $j = $j + 3)
			{
				list($this->color, $y, $x) = stringtocord(substr($this->moves[$i], $j, 3));

				if ('e' == $this->color)
				{
					if ($until > $i)
					{
						$this->insert_empty($y, $x);
					}

					++$this->white_prisoners;
				}
				else if ('r' == $this->color)
				{
					if ($until > $i)
					{
						$this->insert_empty($y, $x);
					}

					++$this->black_prisoners;
				}
				else if ((23 == $y) && (23 == $x))
				{
					if (($i == ($this->move_count - 1)) && ($until == ($this->move_count + 1)) || ($i == ($until - 1)))
					{
						$this->moved_stone_x = 0;
						$this->moved_stone_y = 0;
					}

					if ('b' == $this->color)
					{
						$cprint = 'Black';
					}

					if ('w' == $this->color)
					{
						$cprint = 'White';
					}

					++$this->passes;
					$this->history[$i] = 'P';
				}
				else if ((25 == $y) && (25 == $x))
				{
					if (($i == ($this->move_count - 1)) && ($until == ($this->move_count + 1)) || ($i == ($until - 1)))
					{
						$this->moved_stone_x = 0;
						$this->moved_stone_y = 0;
					}

					if ('b' == $this->color)
					{
						$cprint = 'Black';
						$this->resign = 1;
					}

					if ('w' == $this->color)
					{
						$cprint = 'White';
						$this->resign = 2;
					}

					$this->passes      = 0;
					$this->history[$i] = 'R';
				}
				else
				{
					if ((($i == ($this->move_count - 1)) && ($until == ($this->move_count + 1))) || ($i < $until))
					{
						$this->moved_stone_x = $x;
						$this->moved_stone_y = $y;
					}

					if ($until > $i)
					{
						$this->board[$y][$x] = $this->color;
					}

					if ('b' == $this->color)
					{
						$cprint = 'Black';
					}

					if ('w' == $this->color)
					{
						$cprint = 'White';
					}

					$this->history[$i] = strtolower(xtoalt($x)) . ($this->boardsize - $y);
					$this->passes = 0;
				}
			}

			if ($i == ($this->move_count - 2))
			{
				$this->oldboard = $this->board;
			}
		}

#		return array(
#			$board ,
#			$oldboard ,
#			$passes ,
#			$resign ,
#			$bprisoners ,
#			$wprisoners ,
#			$mstonex ,
#			$mstoney ,
#			$history ,
#			$countm ,
#		);
	}


	function set_handicap_stones( )
	{
		if (2 <= $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[3][$this->boardsize - 4] = 'b';
			}

			if ($size_of_the_board < 13)
			{
				$this->board[2][$this->boardsize - 3] = 'b';
			}

			if ($this->boardsize >= 13)
			{
				$this->board[$this->boardsize - 4][3] = 'b';
			}

			if ($this->boardsize < 13)
			{
				$this->board[$this->boardsize - 3][2] = 'b';
			}
		}

		if (3 <= $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[$this->boardsize - 4][$this->boardsize - 4] = 'b';
			}

			if ($this->boardsize < 13)
			{
				$this->board[$this->boardsize - 3][$this->boardsize - 3] = 'b';
			}
		}

		if (4 <= $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[3][3] = 'b';
			}

			if ($this->boardsize < 13)
			{
				$this->board[2][2] = 'b';
			}
		}

		if (5 <= $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[ceil($this->boardsize / 2) - 1][$this->boardsize - 4] = 'b';
			}

			if ($this->boardsize < 13 && $this->boardsize > 7)
			{
				$this->board[ceil($this->boardsize / 2) - 1][$this->boardsize - 3] = 'b';
			}
		}

		if (6 <= $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[ceil($this->boardsize / 2) - 1][3] = 'b';
			}

			if ($this->boardsize < 13 && $this->boardsize > 7)
			{
				$this->board[ceil($this->boardsize / 2) - 1][2] = 'b';
			}
		}

		if (7 <= $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[3][ceil($this->boardsize / 2) - 1] = 'b';
			}

			if ($this->boardsize < 13 && $this->boardsize > 7)
			{
				$this->board[2][ceil($this->boardsize / 2) - 1] = 'b';
			}
		}

		if (8 <= $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[$this->boardsize - 4][ceil($this->boardsize / 2) - 1] = 'b';
			}

			if ($this->boardsize < 13 && $this->boardsize > 7)
			{
				$this->board[$this->boardsize - 3][ceil($this->boardsize / 2) - 1] = 'b';
			}
		}

		if (9 == $this->handicap)
		{
			if ($this->boardsize >= 13)
			{
				$this->board[ceil($this->boardsize / 2) - 1][ceil($this->boardsize / 2) - 1] = 'b';
			}

			if ($this->boardsize < 13 && $this->boardsize > 7)
			{
				$this->board[ceil($this->boardsize / 2) - 1][ceil($this->boardsize / 2) - 1] = 'b';
			}
		}
	}


	// insert an empty field on the given position into the board array
	function insert_empty($y, $x)
	{
		if ($y == 0)
		{
			if ($x == 0)
			{
				$stone = 'lt';
			}
			else if ($x == $this->boardsize - 1)
			{
				$stone = 'rt';
			}
			else
			{
				$stone = 'mt';
			}
		}
		else if ($y == $this->boardsize - 1)
		{
			if ($x == 0)
			{
				$stone = 'lb';
			}
			else if ($x == $this->boardsize - 1)
			{
				$stone = 'rb';
			}
			else
			{
				$stone = 'mb';
			}
		}
		else
		{
			if ($x == 0)
			{
				$stone = 'lm';
			}
			else if ($x == $this->boardsize - 1)
			{
				$stone = 'rm';
			}
			else
			{
				if ($this->boardsize >= 13 && ($y == 3 && ($x == 3 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 4)))
				{
					$stone = 'ko';
				}
				else if ($this->boardsize >= 13 && ($y == ceil($this->boardsize / 2) - 1 && ($x == 3 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 4)))
				{
					$stone = 'ko';
				}
				else if ($this->boardsize >= 13 && ($y == $this->boardsize - 4 && ($x == 3 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 4)))
				{
					$stone = 'ko';
				}
				else if ($this->boardsize < 13 && $this->boardsize > 7 && ($y == 2 && ($x == 2 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 3)))
				{
					$stone = 'ko';
				}
				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)))
				{
					$stone = 'ko';
				}
				else if ($this->boardsize < 13 && $this->boardsize > 7 && ($y == $this->boardsize - 3 && ($x == 2 || $x == ceil($this->boardsize / 2) - 1 || $x == $this->boardsize - 3)))
				{
					$stone = 'ko';
				}
				else if ($this->boardsize == 7 && ($y == 2 && ($x == 2 || $x == $this->boardsize - 3)))
				{
					$stone = 'ko';
				}
				else if ($this->boardsize == 7 && ($y == $this->boardsize - 3 && ($x == 2 || $x == $this->boardsize - 3)))
				{
					$stone = 'ko';
				}
				else
				{
					$stone = 'mm';
				}
			}
		}

		$this->board[$y][$x] = $stone;
	}


	// do a move a user requested, take prisoners
	function do_move($y, $x)
	{
		$this->board[$y][$x] = $this->color;

		if ($this->color == 'b')
		{
			$this->dummy = $this->group_runs_out_of_liberties($y, $x, true, 'w');
		}
		else if ($color == 'w')
		{
			$this->dummy = $this->group_runs_out_of_liberties($y, $x, true, 'b');
		}
		else
		{
			$this->error = 'Given color attribute is not known.';
		}
	}


	// return true if the move is ko and therfore not allowed, else return false
	function is_ko($y, $x)
	{
		$backup = $this->board;
		$this->do_move($y, $x);

		for ($i = 0; $i < $this->boardsize; ++$i)
		{
			for ($j = 0; $j < $this->boardsize; ++$j)
			{
				if ($this->board[$i][$j] == $this->oldboard[$i][$j])
				{
					$ko = true;
				}
				else
				{
					$ko = false;
					break;
				}
			}

			if ($ko == false)
			{
				break;
			}
		}

		$this->board = $backup;
		return $ko;
	}


	// return true if the point on the board is a free space
	function is_freedom($y, $x)
	{
		return ((('b' != $this->board[$y][$x]) && ('w' != $this->board[$y][$x])) ? true : false);
	}


	// return true if the move is not a suicide, this function does not check for ko se the is_ko function for this
	function is_legal_move($y, $x)
	{
		if (('b' != $this->board[$y][$x]) && ('w' != $this->board[$y][$x]))
		{
			$backup = $this->board[$y][$x];

			if ($this->color == 'b')
			{
				$this->board[$y][$x] = 'b';

				if ($this->freedom_of_group($y, $x) < 1)
				{
					$this->board[$y][$x] = 'b';

					if ( ! $this->group_runs_out_of_liberties($y, $x, false, 'w'))
					{
						$this->board[$y][$x] = $backup;
						return false;
					}
					else
					{
						$this->board[$y][$x] = $backup;
						return true;
					}
				}

				$this->board[$y][$x] = $backup;
				return true;
			}
			else if ($this->color == 'w')
			{
				$this->board[$y][$x] = 'w';

				if ($this->freedom_of_group($y, $x) < 1)
				{
					if ( ! $this->group_runs_out_of_liberties($y, $x, false, 'b'))
					{
						$this->board[$y][$x] = $backup;
						return false;
					}
					else
					{
						$this->board[$y][$x] = $backup;
						return true;
					}
				}

				$this->board[$y][$x] = $backup;
				return true;
			}
			else
			{
				$this->error = 'Given color attribute is not known.';
				return false;
			}
		}
	}



	function is_move_possible($y, $x)
	{
		return ( ! $this->is_ko($y, $x) && $this->is_legal_move($y, $x));
	}


	// delete the stones last checked by another function
	function delete_current_visited( )
	{
		$pos_to_delete = strtok($this->visited, ',');

		while (strlen($pos_to_delete) >= 1)
		{
			if (strlen($pos_to_delete) >= 1)
			{
				if (intval($pos_to_delete) >= 100)
				{
					$y = floor(intval($pos_to_delete) / 100);
				}
				else
				{
					$y = 0;
				}

				$x = intval($pos_to_delete) - ($y * 100);

				if ($color == 'b')
				{
					$rcolor = 'e';
				}
				else if ($color == 'w')
				{
					$rcolor = 'r';
				}
				else
				{
					echo 'Error';
					die;
				}

				if ( ! strstr($this->removed, $rcolor . chr($y + 97) . chr($x + 97)))
				{
					$this->removed = $this->removed . $rcolor . chr($y + 97) . chr($x + 97);
				}

				$this->board[$y][$x] = $hits->insert_empty($y, $x);
			}

			$pos_to_delete = strtok(',');
		}
	}


	// return all board positions which are connected by lines to the current one
	function is_connected_to($y, $x)
	{
		$connected = array( );

		if ($y == 0)
		{
			if ($x == 0)
			{
				$connected[0] = $y + 1;
				$connected[1] = $x;
				$connected[2] = $y;
				$connected[3] = $x + 1;
			}
			else if ($x == $this->boardsize - 1)
			{
				$connected[0] = $y + 1;
				$connected[1] = $x;
				$connected[2] = $y;
				$connected[3] = $x - 1;
			}
			else
			{
				$connected[0] = $y;
				$connected[1] = $x - 1;
				$connected[2] = $y;
				$connected[3] = $x + 1;
				$connected[4] = $y + 1;
				$connected[5] = $x;
			}
		}
		else if ($y == $this->boardsize - 1)
		{
			if ($x == 0)
			{
				$connected[0] = $y - 1;
				$connected[1] = $x;
				$connected[2] = $y;
				$connected[3] = $x + 1;
			}
			else if ($x == $this->boardsize - 1)
			{
				$connected[0] = $y - 1;
				$connected[1] = $x;
				$connected[2] = $y;
				$connected[3] = $x - 1;
			}
			else
			{
				$connected[0] = $y;
				$connected[1] = $x - 1;
				$connected[2] = $y;
				$connected[3] = $x + 1;
				$connected[4] = $y - 1;
				$connected[5] = $x;
			}
		}
		else
		{
			if ($x == 0)
			{
				$connected[0] = $y - 1;
				$connected[1] = $x;
				$connected[2] = $y + 1;
				$connected[3] = $x;
				$connected[4] = $y;
				$connected[5] = $x + 1;
			}
			else if ($x == $this->boardsize - 1)
			{
				$connected[0] = $y - 1;
				$connected[1] = $x;
				$connected[2] = $y + 1;
				$connected[3] = $x;
				$connected[4] = $y;
				$connected[5] = $x - 1;
			}
			else
			{
				$connected[0] = $y + 1;
				$connected[1] = $x;
				$connected[2] = $y - 1;
				$connected[3] = $x;
				$connected[4] = $y;
				$connected[5] = $x + 1;
				$connected[6] = $y;
				$connected[7] = $x - 1;
			}
		}

		return $connected;
	}


	// return true if group is dead
	function group_runs_out_of_liberties($y, $x, $delete_dead_stones)
	{
		$they_are_dead_jim = false;
		$counter           = 0;
		$to_check          = $this->is_connected_to($y, $x);

		foreach ($to_check as $number => $coord)
		{
			if ($counter % 2 == 0)
			{
				if ($this->board[$to_check[$counter]][$to_check[$counter + 1]] == $this->color)
				{
					if ($this->freedom_of_group($to_check[$counter], $to_check[$counter + 1]) < 1)
					{
						if ($delete_dead_stones)
						{
							$this->delete_current_visited($this->color);
						}

						$they_are_dead_jim = true;
					}
				}
			}

			++$counter;
		}

		return $they_are_dead_jim;
	}


	// returns the owner and the number of freedoms grouped together
	function group_of_freedoms($y, $x)
	{
		$this->visited = ',';
		$this->stones_visited = ',';

		$to_visit_string = ',' . strval(($y * 100) + $x) . ',';
		$this->owner           = '';
		$this->freedom         = 0;

		while (strlen($to_visit_string) > 2)
		{
			$cord_to_visit = strtok(substr($to_visit_string, 1), ',');

			if (intval($cord_to_visit) >= 100)
			{
				$y = floor(intval($cord_to_visit) / 100);
			}
			else
			{
				$y = 0;
			}

			$x = intval($cord_to_visit) - ($y * 100);
			$to_visit_string = str_replace(',' . $cord_to_visit . ',', ',', $to_visit_string);
			$counter = 0;
			$to_check = is_connected_to($y, $x);

			foreach ($to_check as $number => $coord)
			{
				if ($counter % 2 == 0)
				{
					if ( ! strstr($this->stones_visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
					{
						if ( ! $this->is_freedom($to_check[$counter], $to_check[$counter + 1]))
						{
							if ('' != $this->owner)
							{
								if ($this->owner != $this->board[$to_check[$counter]][$to_check[$counter + 1]])
								{
									$this->owner = 'f';
								}
							}
							else
							{
								$this->owner = $this->board[$to_check[$counter]][$to_check[$counter + 1]];
							}

							$this->stones_visited .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
						}
					}

					if ( ! strstr($this->visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
					{
						if ($this->is_freedom($to_check[$counter], $to_check[$counter + 1]))
						{
							++$this->freedom;
							$to_visit_string .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
						}
					}
				}

				++$counter;
			}

			$this->visited .= strval(($y * 100) + $x) . ',';
		}

#		return array(
#			$owner ,
#			$freedom ,
#		);
	}


	// how many liberties does a group of stones have
	function freedom_of_group($y, $x)
	{
		$this->visited = ',';
		$this->freedoms_visited = ',';

		$to_visit_string = ',' . strval(($y * 100) + $x) . ',';
		$this->freedom         = 0;

		while (strlen($to_visit_string) > 2)
		{
			$cord_to_visit = strtok(substr($to_visit_string, 1), ',');

			if (intval($cord_to_visit) >= 100)
			{
				$y = floor(intval($cord_to_visit) / 100);
			}
			else
			{
				$y = 0;
			}

			$x = intval($cord_to_visit) - ($y * 100);
			$to_visit_string = str_replace(',' . $cord_to_visit . ',', ',', $to_visit_string);
			$counter = 0;
			$to_check = $this->is_connected_to($y, $x);

			foreach ($to_check as $number => $coord)
			{
				if ($counter % 2 == 0)
				{
					if ( ! strstr($this->freedoms_visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
					{
						if ($this->is_freedom($to_check[$counter], $to_check[$counter + 1]))
						{
							++$this->freedom;
							$this->freedoms_visited .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
						}
					}

					if ( ! strstr($this->visited, ',' . strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ','))
					{
						if ($this->board[$to_check[$counter]][$to_check[$counter + 1]] == $this->color)
						{
							$to_visit_string .= strval((($to_check[$counter]) * 100) + $to_check[$counter + 1]) . ',';
						}
					}
				}

				++$counter;
			}

			$this->visited .= strval(($y * 100) + $x) . ',';
		}

		return $this->freedom;
	}


	// creates an empty board to start with
	function create_empty_board( )
	{
		$this->board = array( );

		for ($y = 0; $y < $this->boardsize; ++$y)
		{
			for ($x = 0; $x < $this->boardsize; ++$x)
			{
				$this->board[$y][$x] = $this->insert_empty($y, $x);
			}
		}
	}


	function get_empty_board( )
	{
		// save the current board
		$temp_board = $this->get_board( );

		// create a new empty board
		$this->create_empty_board( );

		// save the new empty board
		$return_board = $this->board;

		// replace the original board
		$this->set_board($temp_board);

		// return the empty board to the script
		return $return_board;
	}


	function get_moved_stone_x( )
	{
		return $this->moved_stone_x;
	}


	function get_moved_stone_y( )
	{
		return $this->moved_stone_y;
	}



} // end of iShudan class


// other non-class but required functions

// translate numeric coordinates to alphanumeric coordinates
function xtoalt($xcord)
{
	if ($xcord < 8)
	{
		return chr(65 + $xcord);
	}
	else
	{
		return chr(66 + $xcord);
	}
}


// translates alphanumeric coordinates to numeric coordinates
function stringtocord($costring)
{
	$color = substr($costring, 0, 1);
	$y     = ord(substr($costring, 1, 1)) - 97;
	$x     = ord(substr($costring, 2, 1)) - 97;
	return array(
		$color ,
		$y ,
		$x ,
	);
}




?>
Back to Top