やりたい事
ジャンケンプログラムのデバックをしています。
1勝目の場合のみエラーが出てしまうので解消したいです。
2勝目、3勝目からは正常に蓄積されていきます。
ブラウザ
試した事
初回処理と2回目以降の処理の切り分けが出来ていないと思い
php
1if (! empty($_SESSION['result'])) { 2 $_SESSION['result'] = 0; 3}
! empty を記述して、要素が定義されているか確認しましたが変わらず。
初歩的な質問で大変申し訳ございません。
クラス・継承など勉強不足なので改めて勉強します。
分かる方居ましたら宜しくお願い致します。
コード
php
1<?php 2// デバック練習問題 3// あらかじめ壊れているプログラムを用意してあります。 4// コードを読みデバックしつつジャンケンゲームを完成させてください。 5// 判定が勝った時のみ勝利回数を表示させてください。 6// 例) 7// 山田太郎はグーを出しました。 8// 相手はチョキを出しました。 9// 勝敗は勝ちです。 10// 3回目の勝利です。 11 12 13if (! empty($_SESSION['result'])) { 14 $_SESSION['result'] = 0; 15} 16 17class Player 18{ 19 public function jankenConverter(int $choice): string 20 { 21 $janken = ''; 22 switch ($choice) { 23 case 1: 24 $janken = 'グー'; 25 break; 26 case 2: 27 $janken = 'チョキ'; 28 break; 29 case 3: 30 $janken = 'パー'; 31 break; 32 default: 33 } 34 return $janken; 35 } 36} 37 38class Me extends Player 39{ 40 private $name; 41 private $choice; 42 43 public function __construct(string $lastName, string $firstName, int $choice) 44 { 45 $this->name = $lastName.$firstName; 46 $this->choice = $choice; 47 } 48 49 public function getName(): string 50 { 51 return $this->name; 52 } 53 54 public function getChoice(): string 55 { 56 return $this->jankenConverter($this->choice); 57 } 58} 59 60class Enemy extends Player 61{ 62 private $choice; 63 public function __construct() 64 { 65 $this->choice = random_int(1, 3); 66 } 67 68 public function getChoice(): string 69 { 70 return $this->jankenConverter($this->choice); 71 } 72} 73 74class Battle 75{ 76 private $first; 77 private $second; 78 public function __construct(Me $me, Enemy $enemy) 79 { 80 $this->first = $me->getChoice(); 81 $this->second = $enemy->getChoice(); 82 } 83 84 public function judge(): string 85 { 86 if ($this->first === $this->second) { 87 return '引き分け'; 88 } 89 90 if ($this->first === 'グー' && $this->second === 'チョキ') { 91 return '勝ち'; 92 } 93 94 if ($this->first === 'グー' && $this->second === 'パー') { 95 return '負け'; 96 } 97 98 if ($this->first === 'チョキ' && $this->second === 'グー') { 99 return '負け'; 100 } 101 102 if ($this->first === 'チョキ' && $this->second === 'パー') { 103 return '勝ち'; 104 } 105 106 if ($this->first === 'パー' && $this->second === 'グー') { 107 return '勝ち'; 108 } 109 110 if ($this->first === 'パー' && $this->second === 'チョキ') { 111 return '負け'; 112 } 113 } 114 115 public function countVictories() 116 { 117 session_start(); 118 if (($this->judge() === '勝ち')) { 119 // $_SESSION = array(); 120 // session_destroy(); 121 $_SESSION['result'] += 1; 122 } 123 return $_SESSION['result']; 124 } 125 126 // public function getVictories() 127 // { 128 // return $_SESSION['result']; 129 // } 130 131 public function showResult() 132 { 133 return $this->judge(); 134 } 135} 136 137if (!empty($_POST)) { 138 $me = new Me($_POST['last_name'], $_POST['first_name'], $_POST['choice'], $_POST['choice']); 139 $enemy = new Enemy(); 140 echo $me->getName().'は'.$me->getChoice().'を出しました。'; 141 echo '<br>'; 142 echo '相手は'.$enemy->getChoice().'を出しました。'; 143 echo '<br>'; 144 $battle = new Battle($me, $enemy); 145 echo '勝敗は'.$battle->showResult().'です。'; 146 if ($battle->showResult() === '勝ち') { 147 echo '<br>'; 148 echo $battle->countVictories().'回目の勝利です。'; 149 } 150} 151 152?> 153<!DOCTYPE html> 154<html lang="ja"> 155<head> 156<meta charset="utf-8"> 157<title>デバック練習</title> 158</head> 159<body> 160 <section> 161 <form action='./debug03.php' method="post"> 162 <label>姓</label> 163 <input type="text" name="last_name" value="<?php echo '山田' ?>" /> 164 <label>名</label> 165 <input type="text" name="first_name" value="<?php echo '太郎' ?>" /> 166 <select name='choice'> 167 <option value=0 >--</option> 168 <option value=1 >グー</option> 169 <option value=2 >チョキ</option> 170 <option value=3 >パー</option> 171 </select> 172 <input type="submit" value="送信する"/> 173 </form> 174 </section> 175</body> 176</html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/02 02:10