4 * Description: The TicTacToe game application
6 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8 use Friendica\Core\Addon;
9 use Friendica\Core\Hook;
10 use Friendica\Core\L10n;
12 function tictac_install() {
13 Hook::register('app_menu', 'addon/tictac/tictac.php', 'tictac_app_menu');
16 function tictac_uninstall() {
17 Hook::unregister('app_menu', 'addon/tictac/tictac.php', 'tictac_app_menu');
21 function tictac_app_menu($a,&$b) {
22 $b['app_menu'][] = '<div class="app-title"><a href="tictac">' . DI::l10n()->t('Three Dimensional Tic-Tac-Toe') . '</a></div>';
26 function tictac_module() {
34 function tictac_content(&$a) {
39 $handicap = $a->argv[1];
40 $mefirst = $a->argv[2];
45 $yours .= $_POST['move'];
47 elseif($a->argc > 1) {
48 $handicap = $a->argv[1];
55 $o .= '<h3>' . DI::l10n()->t('3D Tic-Tac-Toe') . '</h3><br />';
57 $t = new tictac($dimen,$handicap,$mefirst,$yours,$mine);
60 $o .= '<a href="tictac">' . DI::l10n()->t('New game') . '</a><br />';
61 $o .= '<a href="tictac/1">' . DI::l10n()->t('New game with handicap') . '</a><br />';
62 $o .= '<p>' . DI::l10n()->t('Three dimensional tic-tac-toe is just like the traditional game except that it is played on multiple levels simultaneously. ');
63 $o .= DI::l10n()->t('In this case there are three levels. You win by getting three in a row on any level, as well as up, down, and diagonally across the different levels.');
65 $o .= DI::l10n()->t('The handicap game disables the center position on the middle level because the player claiming this square often has an unfair advantage.');
74 private $first_move = true;
75 private $handicap = 0;
78 private $winning_play;
82 private $crosses = ['011','101','110','112','121','211'];
85 '001','010','011','012','021',
86 '101','110','111','112','121',
87 '201','210','211','212','221');
91 '000','002','020','022',
92 '200','202','220','222'];
95 ['000','001','002','010','011','012','020','021','022'], // horiz 1
96 ['100','101','102','110','111','112','120','121','122'], // 2
97 ['200','201','202','210','211','212','220','221','222'], // 3
98 ['000','010','020','100','110','120','200','210','220'], // vert left
99 ['000','001','002','100','101','102','200','201','202'], // vert top
100 ['002','012','022','102','112','122','202','212','222'], // vert right
101 ['020','021','022','120','121','122','220','221','222'], // vert bot
102 ['010','011','012','110','111','112','210','211','212'], // left vertx
103 ['001','011','021','101','111','221','201','211','221'], // top vertx
104 ['000','001','002','110','111','112','220','221','222'], // diag top
105 ['020','021','022','110','111','112','200','201','202'], // diag bot
106 ['000','010','020','101','111','121','202','212','222'], // diag left
107 ['002','012','022','101','111','121','200','210','220'], // diag right
108 ['002','011','020','102','111','120','202','211','220'], // diag x
109 ['000','011','022','100','111','122','200','211','222'] // diag x
115 ['000','001','002'], // board 0 winners - left corner across
116 ['000','010','020'], // down
117 ['000','011','022'], // diag
118 ['001','011','021'], // middle-top down
119 ['010','011','012'], // middle-left across
120 ['002','011','020'], // right-top diag
121 ['002','012','022'], // right-top down
122 ['020','021','022'], // bottom-left across
123 ['100','101','102'], // board 1 winners
131 ['200','201','202'], // board 2 winners
139 ['000','100','200'], // top-left corner 3d
143 ['001','101','201'], // top-middle 3d
145 ['002','102','202'], // top-right corner 3d
149 ['010','110','210'], // left-middle 3d
151 ['011','111','211'], // middle-middle 3d
152 ['012','112','212'], // right-middle 3d
154 ['020','120','220'], // bottom-left corner 3d
158 ['021','121','221'], // bottom-middle 3d
160 ['022','122','222'], // bottom-right corner 3d
167 function __construct($dimen,$handicap,$mefirst,$yours,$mine) {
169 $this->handicap = (($handicap) ? 1 : 0);
170 $this->mefirst = (($mefirst) ? 1 : 0);
171 $this->yours = str_replace('XXX','',$yours);
173 $this->you = $this->parse_moves('you');
174 $this->me = $this->parse_moves('me');
177 $this->first_move = false;
182 if($this->first_move) {
184 $o .= '<div class="error-message">' . DI::l10n()->t('You go first...') . '</div><br />';
186 $o .= $this->draw_board();
189 $o .= '<div class="error-message">' . DI::l10n()->t('I\'m going first this time...') . ' </div><br />';
194 if($this->check_youwin()) {
195 $o .= '<div class="error-message">' . DI::l10n()->t('You won!') . '</div><br />';
196 $o .= $this->draw_board();
200 if($this->fullboard())
201 $o .= '<div class="error-message">' . DI::l10n()->t('"Cat" game!') . '</div><br />';
203 $move = $this->winning_move();
205 $this->mine .= $move;
206 $this->me = $this->parse_moves('me');
209 $move = $this->defensive_move();
211 $this->mine .= $move;
212 $this->me = $this->parse_moves('me');
215 $move = $this->offensive_move();
217 $this->mine .= $move;
218 $this->me = $this->parse_moves('me');
223 if($this->check_iwon())
224 $o .= '<div class="error-message">' . DI::l10n()->t('I won!') . '</div><br />';
225 if($this->fullboard())
226 $o .= '<div class="error-message">' . DI::l10n()->t('"Cat" game!') . '</div><br />';
227 $o .= $this->draw_board();
231 function parse_moves($player) {
237 while(strlen($str)) {
238 $ret[] = substr($str,0,3);
239 $str = substr($str,3);
245 function check_youwin() {
246 for($x = 0; $x < count($this->winner); $x ++) {
247 if(in_array($this->winner[$x][0],$this->you) && in_array($this->winner[$x][1],$this->you) && in_array($this->winner[$x][2],$this->you)) {
248 $this->winning_play = $this->winner[$x];
254 function check_iwon() {
255 for($x = 0; $x < count($this->winner); $x ++) {
256 if(in_array($this->winner[$x][0],$this->me) && in_array($this->winner[$x][1],$this->me) && in_array($this->winner[$x][2],$this->me)) {
257 $this->winning_play = $this->winner[$x];
263 function defensive_move() {
265 for($x = 0; $x < count($this->winner); $x ++) {
266 if(($this->handicap) && in_array('111',$this->winner[$x]))
268 if(in_array($this->winner[$x][0],$this->you) && in_array($this->winner[$x][1],$this->you) && (! in_array($this->winner[$x][2],$this->me)))
269 return($this->winner[$x][2]);
270 if(in_array($this->winner[$x][0],$this->you) && in_array($this->winner[$x][2],$this->you) && (! in_array($this->winner[$x][1],$this->me)))
271 return($this->winner[$x][1]);
272 if(in_array($this->winner[$x][1],$this->you) && in_array($this->winner[$x][2],$this->you) && (! in_array($this->winner[$x][0],$this->me)))
273 return($this->winner[$x][0]);
278 function winning_move() {
280 for($x = 0; $x < count($this->winner); $x ++) {
281 if(($this->handicap) && in_array('111',$this->winner[$x]))
283 if(in_array($this->winner[$x][0],$this->me) && in_array($this->winner[$x][1],$this->me) && (! in_array($this->winner[$x][2],$this->you)))
284 return($this->winner[$x][2]);
285 if(in_array($this->winner[$x][0],$this->me) && in_array($this->winner[$x][2],$this->me) && (! in_array($this->winner[$x][1],$this->you)))
286 return($this->winner[$x][1]);
287 if(in_array($this->winner[$x][1],$this->me) && in_array($this->winner[$x][2],$this->me) && (! in_array($this->winner[$x][0],$this->you)))
288 return($this->winner[$x][0]);
293 function offensive_move() {
295 shuffle($this->planes);
296 shuffle($this->winner);
297 shuffle($this->corners);
298 shuffle($this->crosses);
300 if(! count($this->me)) {
301 if($this->handicap) {
302 $p = $this->uncontested_plane();
303 foreach($this->corners as $c)
305 && (! $this->is_yours($c)) && (! $this->is_mine($c)))
309 if((! $this->marked_yours(1,1,1)) && (! $this->marked_mine(1,1,1)))
311 $p = $this->uncontested_plane();
312 foreach($this->crosses as $c)
314 && (! $this->is_yours($c)) && (! $this->is_mine($c)))
319 if($this->handicap) {
320 if(count($this->me) >= 1) {
321 if(count($this->get_corners($this->me)) == 1) {
322 if(in_array($this->me[0],$this->corners)) {
323 $p = $this->my_best_plane();
324 foreach($this->winner as $w) {
325 if((in_array($w[0],$this->you))
326 || (in_array($w[1],$this->you))
327 || (in_array($w[2],$this->you)))
329 if(in_array($w[0],$this->corners)
330 && in_array($w[2],$this->corners)
331 && in_array($w[0],$p) && in_array($w[2],$p)) {
332 if($this->me[0] == $w[0])
334 elseif($this->me[0] == $w[2])
341 $r = $this->get_corners($this->me);
344 foreach($this->winner as $w) {
345 if(in_array('111',$w))
347 if(($r[0] == $w[0]) || ($r[0] == $w[2]))
349 if(($r[1] == $w[0]) || ($r[1] == $w[2]))
352 if(count($w1) && count($w2)) {
355 if((in_array($a[0],$this->you))
356 || (in_array($a[1],$this->you))
357 || (in_array($a[2],$this->you))
358 || (in_array($b[0],$this->you))
359 || (in_array($b[1],$this->you))
360 || (in_array($b[2],$this->you)))
362 if(($a[0] == $b[0]) && ! $this->is_mine($a[0])) {
365 elseif(($a[2] == $b[2]) && ! $this->is_mine($a[2])) {
376 //&& (count($this->me) == 1) && (count($this->you) == 1)
377 // && in_array($this->you[0],$this->corners)
378 // && $this->is_neighbor($this->me[0],$this->you[0])) {
380 // Yuck. You foiled my plan. Since you obviously aren't playing to win,
381 // I'll try again. You may keep me busy for a few rounds, but I'm
382 // gonna' get you eventually.
384 // $p = $this->uncontested_plane();
385 // foreach($this->crosses as $c)
386 // if(in_array($c,$p))
392 // find all the winners containing my points.
394 foreach($this->winner as $w)
395 foreach($this->me as $m)
396 if((in_array($m,$w)) && (! in_array($w,$mywinners)))
399 // find all the rules where my points are in the center.
401 if(count($mywinners)) {
402 foreach($mywinners as $w) {
403 foreach($this->me as $m) {
404 if(($m == $w[1]) && ($this->uncontested_winner($w))
405 && (! in_array($w,$trythese)))
412 for($p = 0; $p < count($this->planes); $p ++) {
413 if($this->handicap && in_array('111',$this->planes[$p]))
415 foreach($this->me as $m)
416 if((in_array($m,$this->planes[$p]))
417 && (! in_array($this->planes[$p],$myplanes)))
418 $myplanes[] = $this->planes[$p];
422 // find all winners which share an endpoint, and which are uncontested
424 if(count($trythese) && count($myplanes)) {
425 foreach($trythese as $t) {
426 foreach($this->winner as $w) {
427 if(! $this->uncontested_winner($w))
429 if((in_array($t[0],$w)) || (in_array($t[2],$w))) {
430 foreach($myplanes as $p)
431 if(in_array($w[0],$p) && in_array($w[1],$p) && in_array($w[2],$p) && ($w[1] != $this->me[0]))
432 if(! in_array($w,$candidates))
439 // Find out if we are about to force a win.
440 // Looking for two winning vectors with a common endpoint
441 // and where we own the middle of both - we are now going to
442 // grab the endpoint. The game isn't yet over but we've already won.
444 if(count($candidates)) {
445 foreach($candidates as $c) {
446 if(in_array($c[1],$this->me)) {
448 foreach($trythese as $t)
451 elseif($t[2] == $c[2])
456 // find opponents planes
458 for($p = 0; $p < count($this->planes); $p ++) {
459 if($this->handicap && in_array('111',$this->planes[$p]))
461 if(in_array($this->you[0],$this->planes[$p]))
462 $yourplanes[] = $this->planes[$p];
465 shuffle($this->winner);
466 foreach($candidates as $c) {
468 // We now have a list of winning strategy vectors for our second point
469 // Pick one that will force you into defensive mode.
470 // Pick a point close to you so we don't risk giving you two
471 // in a row when you block us. That would force *us* into
478 if(count($this->you) == 1) {
479 foreach($this->winner as $w) {
480 if(in_array($this->me[0], $w) && in_array($c[1],$w)
481 && $this->uncontested_winner($w)
482 && $this->is_neighbor($this->you[0],$c[1])) {
489 // You're somewhere else entirely or have made more than one move
490 // - any strategy vector which puts you on the defense will have to do
492 foreach($candidates as $c) {
493 foreach($this->winner as $w) {
494 if(in_array($this->me[0], $w) && in_array($c[1],$w)
495 && $this->uncontested_winner($w)) {
502 // worst case scenario, no strategy we can play,
503 // just find an empty space and take it
505 for($x = 0; $x < $this->dimen; $x ++)
506 for($y = 0; $y < $this->dimen; $y ++)
507 for($z = 0; $z < $this->dimen; $z ++)
508 if((! $this->marked_yours($x,$y,$z))
509 && (! $this->marked_mine($x,$y,$z))) {
510 if($this->handicap && $x == 1 && $y == 1 && $z == 1)
512 return(sprintf("%d%d%d",$x,$y,$z));
518 function marked_yours($x,$y,$z) {
519 $str = sprintf("%d%d%d",$x,$y,$z);
520 if(in_array($str,$this->you))
525 function marked_mine($x,$y,$z) {
526 $str = sprintf("%d%d%d",$x,$y,$z);
527 if(in_array($str,$this->me))
532 function is_yours($str) {
533 if(in_array($str,$this->you))
538 function is_mine($str) {
539 if(in_array($str,$this->me))
544 function get_corners($a) {
548 if(in_array($b,$this->corners))
553 function uncontested_winner($w) {
554 if($this->handicap && in_array('111',$w))
557 if(count($this->you)) {
558 foreach($this->you as $you)
559 if(in_array($you,$w))
562 return (($contested) ? false : true);
566 function is_neighbor($p1,$p2) {
567 list($x1,$y1,$z1) = sscanf($p1, "%1d%1d%1d");
568 list($x2,$y2,$z2) = sscanf($p2, "%1d%1d%1d");
570 if((($x1 == $x2) || ($x1 == $x2+1) || ($x1 == $x2-1)) &&
571 (($y1 == $y2) || ($y1 == $y2+1) || ($y1 == $y2-1)) &&
572 (($z1 == $z2) || ($z1 == $z2+1) || ($z1 == $z2-1)))
578 function my_best_plane() {
581 shuffle($this->planes);
582 for($p = 0; $p < count($this->planes); $p ++ ) {
584 if($this->handicap && in_array('111',$this->planes[$p]))
586 if(! in_array($this->me[0],$this->planes[$p]))
588 foreach($this->you as $m) {
589 if(in_array($m,$this->planes[$p]))
593 return($this->planes[$p]);
595 $second_choice = $this->planes[$p];
597 return $second_choice;
606 function uncontested_plane() {
608 shuffle($this->planes);
611 for($p = 0; $p < count($pl); $p ++ ) {
612 if($this->handicap && in_array('111',$pl[$p]))
614 foreach($this->you as $m) {
615 if(in_array($m,$pl[$p]))
628 function fullboard() {
632 function draw_board() {
633 if(! strlen($this->yours))
634 $this->yours = 'XXX';
635 $o .= "<form action=\"tictac/{$this->handicap}/{$this->mefirst}/{$this->dimen}/{$this->yours}/{$this->mine}\" method=\"post\" />";
636 for($x = 0; $x < $this->dimen; $x ++) {
638 for($y = 0; $y < $this->dimen; $y ++) {
640 for($z = 0; $z < $this->dimen; $z ++) {
641 $s = sprintf("%d%d%d",$x,$y,$z);
642 $winner = ((is_array($this->winning_play) && in_array($s,$this->winning_play)) ? " color: #FF0000; " : "");
643 $bordertop = (($y != 0) ? " border-top: 2px solid #000;" : "");
644 $borderleft = (($z != 0) ? " border-left: 2px solid #000;" : "");
645 if($this->handicap && $x == 1 && $y == 1 && $z == 1)
646 $o .= "<td style=\"width: 25px; height: 25px; $bordertop $borderleft\" align=\"center\"> </td>";
647 elseif($this->marked_yours($x,$y,$z))
648 $o .= "<td style=\"width: 25px; height: 25px; $bordertop $borderleft $winner\" align=\"center\">X</td>";
649 elseif($this->marked_mine($x,$y,$z))
650 $o .= "<td style=\"width: 25px; height: 25px; $bordertop $borderleft $winner\" align=\"center\">O</td>";
652 $val = sprintf("%d%d%d",$x,$y,$z);
653 $o .= "<td style=\"width: 25px; height: 25px; $bordertop $borderleft\" align=\"center\"><input type=\"checkbox\" name=\"move\" value=\"$val\" onclick=\"this.form.submit();\" /></td>";
658 $o .= '</table><br />';