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