更新 2009-08-06
2009-02-15
Symfony + Ajax を使ってじゃんけんをする
================================================================================
じゃんけんの手の文字のところにマウスを置くと、コンピューターと勝負するようにして
ください。なお、コンピューター側は、乱数を使います。
version 2.2 のコードに対して、Symfony を使います。
なお、Symfony の設定は以下のとおりです。
プロジェクト: symfony
アプリケーション: ajax
モジュール: janken
■prptotype.jsの設定
config/view.ymlに次を追加
---
all:
javascripts: [/js/prototype]
---
prototype.jsファイルを次のディレクトリーにコピーする。
---
/home/student/symfony/web/js
---
■アクション
modules/janken/actions/actions.class.php
---
forward('default', 'module');
}
/**
* ユーザーが指定した手とコンピューターによってじゃんけんを行う。
* @return JSON形式の結果オブジェクト
*/
public function executeJanken()
{
$user = new Te($_POST["te"]);
$computer = new Te(rand(0, 2));
$play = new JanKenPlay();
$play->user = $user->__toString();
$play->computer = $computer->__toString();
$play->result = $user->play($computer);
return $this->renderText(json_encode($play));
}
}
?>
---
■ビュー
modules/janken/templates/indexSuccess.php
---
"
+ "あなたは " + play.user
+ " 、コンピューターは " + play.computer + " でした。";
if (play.result == "勝ち") {
document.getElementById("result").style.backgroundColor = "lightblue";
} else {
document.getElementById("result").style.backgroundColor = "lightgreen";
}
}
') ?>
じゃんけんの手の文字のところにマウスを置いてください。
グー
チョキ
パー