<?php
header("Content-Type: text/html; charset=Shift_JIS");
class jyanken_class
{
var $state; //Game State
var $id_a; //ID A
var $id_b; //ID B
var $play_a; //手 A
var $play_b; //手 B
var $judge; //勝敗
// Constructor
function jyanken_class()
{ $this->state= 0;
$this->id_a = '-';
$this->id_b = '-';
$this->play_a = 3;
$this->play_b = 3;
}
// $file に現在のパラメータを書き込みます
function put_file()
{ if (!($fp = fopen("jyanken.txt","w")))
{ die("jyanken.txt write open error"); }
flock($fp,LOCK_EX);
fputs($fp,"$this->state\r\n");
fputs($fp,"$this->id_a\r\n");
fputs($fp,"$this->id_b\r\n");
fputs($fp,"$this->play_a\r\n");
fputs($fp,"$this->play_b\r\n");
fputs($fp,"$this->judge\r\n");
flock($fp,LOCK_UN);
fclose($fp);
}
// $file からパラメータを読み込みます
function get_file()
{ if (is_file("jyanken.txt")==FALSE)
{ die("jyanken.txt not found"); }
if ($fp = fopen("jyanken.txt","r"))
{ $this->state = intval(fgets($fp));
$this->id_a = fgets($fp);
$this->id_b = fgets($fp);
$this->id_a = str_replace(array("\r", "\n"), '', $this->id_a);
$this->id_b = str_replace(array("\r", "\n"), '', $this->id_b);
$this->play_a = intval(fgets($fp));
$this->play_b = intval(fgets($fp));
$this->judge = intval(fgets($fp));
fclose($fp);
}
}
// 現在の設定値を表示
function check($id, $play)
{ print "<table border='1'>";
print "<tr><th>ID</th><th>PLAY</th><th>ST</th><th>ID_A</th><th>ID_B</th><th>A</th><th>B</th><th>JUDGE</th></tr>";
print "\n<tr>";
print "<td>$id</td>";
print "<td>$play</td>";
print "<td>$this->state</td>";
print "<td>$this->id_a</td>";
print "<td>$this->id_b</td>";
print "<td>$this->play_a</td>";
print "<td>$this->play_b</td>";
print "<td>$this->judge</td>";
print "</tr></table><br>\n";
}
// 進行状態を調べてゲームを実行
function action($id, $play)
{ switch($this->state)
{ case 0: // プレイヤーのログイン
if ($this->id_a=='-') $this->id_a= $id;
if ($this->id_a!=$id && $this->id_b=='-') $this->id_b= $id;
if ($this->id_a=='-' || $this->id_b=='-') break;
$this->state = 1;
break;
case 1: // じゃんけんをする
if ($this->id_a==$id && $this->play_a==3) $this->play_a= $play;
if ($this->id_b==$id && $this->play_b==3) $this->play_b= $play;
if ($this->play_a==3 || $this->play_b==3) break;
// 勝敗の判定
$this->judge = ($this->play_a-$this->play_b+3)%3;
$this->state = 2;
break;
case 2: // 勝敗の確認
if ($this->id_a==$id) $this->play_a= 3;
if ($this->id_b==$id) $this->play_b= 3;
if ($this->play_a!=3 || $this->play_b!=3) break;
$this->state = 1;
break;
default:
print "** GAME OVER **<br>\n";
exit();
}
$this->put_file();
}
// form の送信ボタンで PHP をコール
function form_call($id, $msg)
{ print "$id:$msg<br>\n";
print "<form action='jyanken.php' method='get'>";
print "<input type=text name='id' value=\"$id\"><br>";
print "<input type=radio name='play' value=0>グー<br>\n";
print "<input type=radio name='play' value=1>チョキ<br>\n";
print "<input type=radio name='play' value=2>パー<br><br>\n";
print "<input type=radio name='play' value=3 CHECKED>ログイン(確認)<br>\n";
print "<input type=radio name='play' value=4>リセット<br>\n";
print "<input type=radio name='play' value=5>ゲーム終了<br>\n";
print "<input type=submit value='送信'>";
print "</form>";
}
// $state に従ったメッセージを設定する
function message()
{ switch($this->state)
{ case 0: return "二人のプレイヤーがログインして下さい";
case 1: return "じゃんけんを待っています";
case 2:
switch($this->judge)
{ case 0: return "あいこです(確認して下さい)";
case 1: return "Bの勝ち(確認して下さい)";
case 2: return "Aの勝ち(確認して下さい)";
}
break;
case 9: return "** GAME OVER **";
default: return "* state error *: $game->state";
}
}
}
?>
|