added tips to lamer.bash help
[mirrors/Programs.git] / php / skripty / tictactoe.php
1 <?php
2 //Tic-Tac-Toe
3 //<- Harvie 2oo7
4 /*
5 Inspired by WarGames ;D
6 */
7
8 echo("Tic-Tac-Toe (<- Harvie 2oo7)\n");
9 echo("Inspired by WarGames ;D\n");
10
11 //Settings////////////////////////////////////////////////////////
12 //Skin:
13 $cross = "X";
14 $circl = "O";
15 $versep = "|";
16 $horsep = "-";
17 $ttt = " 123456789";
18 //First player:
19 $p = false; //false - O; true - X;
20 //Artificial inteligency:
21 $ai = 3; /* AI (very poor == rand(1,9) ;)
22 Settings:
23 0 - Circle
24 1 - Cross
25 2 - Disabled
26 3 - PCvsPC
27 */
28
29 //Functions//////////////////////////////////////////////////////
30 //
31 function ttt_print($ttt) {
32 $v = $GLOBALS["versep"];
33 $h = $GLOBALS["horsep"];
34
35 echo("\n");
36 echo($ttt[7].$v.$ttt[8].$v.$ttt[9]."\n");
37 echo("$h$h$h$h$h\n");
38 echo($ttt[4].$v.$ttt[5].$v.$ttt[6]."\n");
39 echo("$h$h$h$h$h\n");
40 echo($ttt[1].$v.$ttt[2].$v.$ttt[3]."\n");
41 echo("\n");
42 }
43
44 //
45 function ttt_match($ttt, $place, $player) {
46 $x = $GLOBALS["cross"];
47 $o = $GLOBALS["circl"];
48
49 if( ($place>0 && $place<10) && ($ttt[$place] != $x && $ttt[$place] != $o) && is_numeric($place) ) {
50 $ttt[$place] = $player;
51 } else { $GLOBALS["err"] = true; }
52 return $ttt;
53 }
54
55 //
56 function ttt_check($ttt) {
57 $x = $GLOBALS["cross"];
58 $o = $GLOBALS["circl"];
59 //Horizontal
60 if( $ttt[7] == $ttt[8] && $ttt[8] == $ttt[9] ) { return 1; }
61 if( $ttt[4] == $ttt[5] && $ttt[5] == $ttt[6] ) { return 1; }
62 if( $ttt[1] == $ttt[2] && $ttt[2] == $ttt[3] ) { return 1; }
63 //Vertical
64 if( $ttt[7] == $ttt[4] && $ttt[4] == $ttt[1] ) { return 1; }
65 if( $ttt[8] == $ttt[5] && $ttt[5] == $ttt[2] ) { return 1; }
66 if( $ttt[9] == $ttt[6] && $ttt[6] == $ttt[3] ) { return 1; }
67 //Diagonal
68 if( $ttt[7] == $ttt[5] && $ttt[5] == $ttt[3] ) { return 1; }
69 if( $ttt[1] == $ttt[5] && $ttt[5] == $ttt[9] ) { return 1; }
70 //Nobody won
71 for($i=1;$i<strlen($ttt);$i++) {
72 if($ttt[$i] != $x && $ttt[$i] != $o) { return 0; }
73 }
74 return -1;
75 }
76
77 //Code///////////////////////////////////////////////////////////
78
79 //Init///////////////////////////////////////////////////////////
80 $__STDIN__ = fopen("php://stdin", "rb");
81 $err = false;
82 $time = @explode(",", trim(@shell_exec("echo %time%"))); @srand(time()*$time[1]); //print_r($time); //Debug
83
84 //Go/////////////////////////////////////////////////////////////
85 ttt_print($ttt);
86 while(1) {
87 if($p) { $player = $cross; } else { $player = $circl; }
88
89 echo("$player:");
90
91 if( ($p == $ai || $ai == 3) && $ai != 2 ) {
92 $c = rand(1,9);
93 echo($c);
94 } else {
95 $c = "nan";
96 while( !is_numeric($c) ) { $c = fgetc($__STDIN__); }
97 //echo($c); //Debug
98 }
99
100 $ttt = ttt_match($ttt, $c, $player); if($err) { $err = false; echo("-"); continue; }
101 elseif( ($p == $ai || $ai == 3) && $ai != 2 ) { echo("\n"); }
102
103 ttt_print($ttt);
104
105 if(ttt_check($ttt) == -1) { echo("!!! Nobody won !!!\n"); break; }
106 if(ttt_check($ttt)) { echo("!!! Player $player won !!!\n"); break; }
107
108 $p = !$p;
109 }
110
111 //system("pause");
This page took 0.391995 seconds and 4 git commands to generate.