実現したいこと
↑の「~回目の勝利です。」の数字が勝利するごとに増やしていくようにしたいです。countVictories()の記述をfor文やインクリメント等をしましたが、うまくいかず。その他のコードは予測通りの処理がされているので、countVictories()の部分をどのように記述すれば増えるのかご教授いただければ幸いです。
該当ソースコード
public function countVictories() { if ($this->judge() === '勝ち') { $_SESSION['result'] += + 1 ; } return $_SESSION['result']; }
ソースコード
php
1<?php 2// 判定が勝った時のみ勝利回数を表示させてください。 3// 例) 4// 山田太郎はグーを出しました。 5// 相手はチョキを出しました。 6// 勝敗は勝ちです。 7// 3回目の勝利です。 8 9 10if (! isset($_SESSION['result'])) { 11 $_SESSION['result'] = 0 ; 12} 13 14class Player 15{ 16 public function jankenConverter(int $choice): string 17 { 18 $janken = ''; 19 switch ($choice) { 20 case 1: 21 $janken = 'グー'; 22 break; 23 case 2: 24 $janken = 'チョキ'; 25 break; 26 case 3: 27 $janken = 'パー'; 28 break; 29 default: 30 } 31 return $janken; 32 } 33} 34 35 36class Me extends Player 37{ 38 private $name; 39 private $choice; 40 41 public function __construct(string $lastName, string $firstName, int $choice) 42 { 43 $this->name = $lastName.$firstName; 44 $this->choice = $choice; 45 } 46 47 public function getName(): string 48 { 49 return $this->name; 50 } 51 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 private 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 if ($this->judge() === '勝ち') { 118 $_SESSION['result'] += + 1 ; 119 } 120 return $_SESSION['result']; 121 } 122 123 // public function getVitories() 124 // { 125 // return $_SESSION['result']; 126 // } 127 128 public function showResult() 129 { 130 return $this->judge(); 131 } 132} 133 134if (! empty($_POST)) { 135 $me = new Me($_POST['last_name'], $_POST['first_name'], $_POST['choice'], $_POST['choice']); 136 $enemy = new Enemy(); 137 echo $me->getName().'は'.$me->getChoice().'を出しました。'; 138 echo '<br>'; 139 echo '相手は'.$enemy->getChoice().'を出しました。'; 140 echo '<br>'; 141 $battle = new Battle($me, $enemy); 142 echo '勝敗は'.$battle->showResult().'です。'; 143 if ($battle->showResult() === '勝ち') { 144 echo '<br>'; 145 echo $battle->countVictories().'回目の勝利です。'; 146 } 147} 148 149?> 150<!DOCTYPE html> 151<html lang="ja"> 152<head> 153<meta charset="utf-8"> 154<title>デバック練習</title> 155</head> 156<body> 157 <section> 158 <form action='./debug03.php' method="POST"> 159 <label>姓</label> 160 <input type="text" name="last_name" value="<?php echo '山田' ?>" /> 161 <label>名</label> 162 <input type="text" name="first_name" value="<?php echo '太郎' ?>" /> 163 <select name='choice'> 164 <option value=0 >--</option> 165 <option value=1 >グー</option> 166 <option value=2 >チョキ</option> 167 <option value=3 >パー</option> 168 </select> 169 <input type="submit" value="送信する"/> 170 </form> 171 </section> 172</body> 173</html> 174
回答2件
あなたの回答
tips
プレビュー