やりたい事
ジャンケンプログラムのデバックをしています。
1勝目の場合のみエラーが出てしまうので解消したいです。
2勝目、3勝目からは正常に蓄積されていきます。
ブラウザ
試した事
初回処理と2回目以降の処理の切り分けが出来ていないと思い
php
if (! empty($_SESSION['result'])) { $_SESSION['result'] = 0; }
! empty を記述して、要素が定義されているか確認しましたが変わらず。
初歩的な質問で大変申し訳ございません。
クラス・継承など勉強不足なので改めて勉強します。
分かる方居ましたら宜しくお願い致します。
コード
php
<?php // デバック練習問題 // あらかじめ壊れているプログラムを用意してあります。 // コードを読みデバックしつつジャンケンゲームを完成させてください。 // 判定が勝った時のみ勝利回数を表示させてください。 // 例) // 山田太郎はグーを出しました。 // 相手はチョキを出しました。 // 勝敗は勝ちです。 // 3回目の勝利です。 if (! empty($_SESSION['result'])) { $_SESSION['result'] = 0; } class Player { public function jankenConverter(int $choice): string { $janken = ''; switch ($choice) { case 1: $janken = 'グー'; break; case 2: $janken = 'チョキ'; break; case 3: $janken = 'パー'; break; default: } return $janken; } } class Me extends Player { private $name; private $choice; public function __construct(string $lastName, string $firstName, int $choice) { $this->name = $lastName.$firstName; $this->choice = $choice; } public function getName(): string { return $this->name; } public function getChoice(): string { return $this->jankenConverter($this->choice); } } class Enemy extends Player { private $choice; public function __construct() { $this->choice = random_int(1, 3); } public function getChoice(): string { return $this->jankenConverter($this->choice); } } class Battle { private $first; private $second; public function __construct(Me $me, Enemy $enemy) { $this->first = $me->getChoice(); $this->second = $enemy->getChoice(); } public function judge(): string { if ($this->first === $this->second) { return '引き分け'; } if ($this->first === 'グー' && $this->second === 'チョキ') { return '勝ち'; } if ($this->first === 'グー' && $this->second === 'パー') { return '負け'; } if ($this->first === 'チョキ' && $this->second === 'グー') { return '負け'; } if ($this->first === 'チョキ' && $this->second === 'パー') { return '勝ち'; } if ($this->first === 'パー' && $this->second === 'グー') { return '勝ち'; } if ($this->first === 'パー' && $this->second === 'チョキ') { return '負け'; } } public function countVictories() { session_start(); if (($this->judge() === '勝ち')) { // $_SESSION = array(); // session_destroy(); $_SESSION['result'] += 1; } return $_SESSION['result']; } // public function getVictories() // { // return $_SESSION['result']; // } public function showResult() { return $this->judge(); } } if (!empty($_POST)) { $me = new Me($_POST['last_name'], $_POST['first_name'], $_POST['choice'], $_POST['choice']); $enemy = new Enemy(); echo $me->getName().'は'.$me->getChoice().'を出しました。'; echo '<br>'; echo '相手は'.$enemy->getChoice().'を出しました。'; echo '<br>'; $battle = new Battle($me, $enemy); echo '勝敗は'.$battle->showResult().'です。'; if ($battle->showResult() === '勝ち') { echo '<br>'; echo $battle->countVictories().'回目の勝利です。'; } } ?> <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>デバック練習</title> </head> <body> <section> <form action='./debug03.php' method="post"> <label>姓</label> <input type="text" name="last_name" value="<?php echo '山田' ?>" /> <label>名</label> <input type="text" name="first_name" value="<?php echo '太郎' ?>" /> <select name='choice'> <option value=0 >--</option> <option value=1 >グー</option> <option value=2 >チョキ</option> <option value=3 >パー</option> </select> <input type="submit" value="送信する"/> </form> </section> </body> </html>
まだ回答がついていません
会員登録して回答してみよう