質問編集履歴
1
前職の社外秘の内容が含まれていたため、可能であれば投稿を削除してほしいです。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,229 +1,4 @@
|
|
1
1
|
### 前提
|
2
2
|
|
3
3
|
PHPにてじゃんけんゲーム制作コードのデバッグ練習中です。
|
4
|
-
クラス内のメソッドにPOST(グー "1"、チョキ "2"、パー "3")の値を代入したいのですが、値がnullとなりエラーになってしまいます。
|
5
4
|
|
6
|
-
|
7
|
-
### 実現したいこと
|
8
|
-
|
9
|
-
エラーを取り除き、以下のように画面に表示させたいです。
|
10
|
-
|
11
|
-
山田太郎はグーを出しました。
|
12
|
-
相手はチョキを出しました。
|
13
|
-
勝敗は勝ちです。
|
14
|
-
3回目の勝利です
|
15
|
-
|
16
|
-
### 発生している問題・エラーメッセージ
|
17
|
-
|
18
|
-
```
|
19
|
-
[05-Jan-2023 00:03:50 Asia/Tokyo] PHP Notice: Undefined variable: choice in /Users//04_php_debug/debug03.php on line 60
|
20
|
-
[05-Jan-2023 00:03:50 Asia/Tokyo] PHP Notice: Undefined property: Me::$ in /Users//04_php_debug/debug03.php on line 60
|
21
|
-
[05-Jan-2023 00:03:50 Asia/Tokyo] PHP Fatal error: Uncaught TypeError: Argument 1 passed to Player::jankenConverter() must be of the type int, null given, called in /Users//04_php_debug/debug03.php on line 60 and defined in /Users//04_php_debug/debug03.php:22
|
22
|
-
Stack trace:
|
23
|
-
#0 /Users//04_php_debug/debug03.php(60): Player->jankenConverter(NULL)
|
24
|
-
#1 /Users//04_php_debug/debug03.php(141): Me->getChoice()
|
25
|
-
#2 {main}
|
26
|
-
thrown in /Users/04_php_debug/debug03.php on line 22
|
27
|
-
|
28
|
-
```
|
29
|
-
|
30
|
-
### 該当のソースコード
|
31
|
-
|
32
|
-
```PHP
|
33
|
-
<?php // phpcs:ignore
|
34
|
-
|
35
|
-
// デバック練習問題
|
36
|
-
// コードを読みデバックしつつジャンケンゲームを完成させてください。
|
37
|
-
// 判定が勝った時のみ勝利回数を表示させてください。
|
38
|
-
// 例)
|
39
|
-
// 山田太郎はグーを出しました。
|
40
|
-
// 相手はチョキを出しました。
|
41
|
-
// 勝敗は勝ちです。
|
42
|
-
// 3回目の勝利です。
|
43
|
-
// $_SESSIONの挙動やswitch文については調べてみてください。
|
44
|
-
|
45
|
-
session_start();
|
46
|
-
|
47
|
-
if (! isset($_SESSION['result'])) {
|
48
|
-
$_SESSION['result'] = 0;
|
49
|
-
}
|
50
|
-
|
51
|
-
|
52
|
-
class Player //phpcs:ignore
|
53
|
-
{
|
54
|
-
public function jankenConverter(int $choice): string
|
55
|
-
{
|
56
|
-
$janken = '';
|
57
|
-
switch ($choice) {
|
58
|
-
case 1:
|
59
|
-
$janken = 'グー';
|
60
|
-
break;
|
61
|
-
case 2:
|
62
|
-
$janken = 'チョキ';
|
63
|
-
break;
|
64
|
-
case 3:
|
65
|
-
$janken = 'パー';
|
66
|
-
break;
|
67
|
-
default:
|
68
|
-
}
|
69
|
-
return $janken;
|
70
|
-
}
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
class Me extends Player // phpcs:ignore
|
75
|
-
{
|
76
|
-
private $name;
|
77
|
-
private $choice;
|
78
|
-
|
79
|
-
public function __construct(string $lastName, string $firstName, int $choice)
|
80
|
-
{
|
81
|
-
$this->name = $lastName.$firstName;
|
82
|
-
$this->choice = $choice;
|
83
|
-
}
|
84
|
-
|
85
|
-
public function getName(): string
|
86
|
-
{
|
87
|
-
return $this->name;
|
88
|
-
}
|
89
|
-
|
90
|
-
public function getChoice(): string
|
91
|
-
{
|
92
|
-
return $this->jankenConverter($this->$choice);
|
93
|
-
}
|
94
|
-
}
|
95
|
-
|
96
|
-
class Enemy extends Player // phpcs:ignore
|
97
|
-
{
|
98
|
-
private $choice;
|
99
|
-
public function __construct()
|
100
|
-
{
|
101
|
-
$this->choice = random_int(1, 3);
|
102
|
-
}
|
103
|
-
|
104
|
-
public function getChoice(): string
|
105
|
-
{
|
106
|
-
return $this->jankenConverter($this->choice);
|
107
|
-
}
|
108
|
-
}
|
109
|
-
|
110
|
-
class Battle // phpcs:ignore
|
111
|
-
{
|
112
|
-
private $first;
|
113
|
-
private $second;
|
114
|
-
public function __construct(Me $me, Enemy $enemy)
|
115
|
-
{
|
116
|
-
$this->first = $me->getChoice();
|
117
|
-
$this->second = $enemy->getChoice();
|
118
|
-
}
|
119
|
-
|
120
|
-
private function judge(): int
|
121
|
-
{
|
122
|
-
if ($this->first === $this->second) {
|
123
|
-
return '引き分け';
|
124
|
-
}
|
125
|
-
|
126
|
-
if ($this->first === 'グー' && $this->second === 'チョキ') {
|
127
|
-
return '勝ち';
|
128
|
-
}
|
129
|
-
|
130
|
-
if ($this->first === 'グー' && $this->second === 'パー') {
|
131
|
-
return '負け';
|
132
|
-
}
|
133
|
-
|
134
|
-
if ($this->first === 'チョキ' && $this->second === 'グー') {
|
135
|
-
return '負け';
|
136
|
-
}
|
137
|
-
|
138
|
-
if ($this->first === 'チョキ' && $this->second === 'パー') {
|
139
|
-
return '勝ち';
|
140
|
-
}
|
141
|
-
|
142
|
-
if ($this->first === 'パー' && $this->second === 'グー') {
|
143
|
-
return '勝ち';
|
144
|
-
}
|
145
|
-
|
146
|
-
if ($this->first === 'パー' && $this->second === 'チョキ') {
|
147
|
-
return '負け';
|
148
|
-
}
|
149
|
-
}
|
150
|
-
|
151
|
-
private function countVictories()
|
152
|
-
{
|
153
|
-
if ($this->judge() === '勝ち') {
|
154
|
-
$_SESSION['result'] += 1;
|
155
|
-
}
|
156
|
-
return $_SESSION['result'];
|
157
|
-
}
|
158
|
-
|
159
|
-
// public function getVictories()
|
160
|
-
// {
|
161
|
-
// return $_SESSION['result'];
|
162
|
-
// }
|
163
|
-
|
164
|
-
public function showResult()
|
165
|
-
{
|
166
|
-
return $this->judge();
|
167
|
-
}
|
168
|
-
}
|
169
|
-
|
170
|
-
if (! empty($_POST)) {
|
171
|
-
$me = new Me($_POST['last_name'], $_POST['first_name'], $_POST['choice']);
|
172
|
-
$enemy = new Enemy();
|
173
|
-
echo $me->getName().'は'.$me->getChoice().'を出しました。';
|
174
|
-
echo '<br>';
|
175
|
-
echo '相手は'.$enemy->getChoice().'を出しました。';
|
176
|
-
echo '<br>';
|
177
|
-
$battle = new Battle($me, $enemy);
|
178
|
-
echo '勝敗は'.$battle->showResult().'です。';
|
179
|
-
if ($battle->showResult() === '勝ち') {
|
180
|
-
echo '<br>';
|
181
|
-
echo $battle->countVictories().'回目の勝利です。';
|
182
|
-
}
|
183
|
-
}
|
184
|
-
|
185
|
-
|
186
|
-
?>
|
187
|
-
<!DOCTYPE html>
|
188
|
-
<html lang="ja">
|
189
|
-
<head>
|
190
|
-
<meta charset="utf-8">
|
191
|
-
<title>デバック練習</title>
|
192
|
-
</head>
|
193
|
-
<body>
|
194
|
-
<section>
|
195
|
-
<form action='./debug03.php' method="POST">
|
196
|
-
<label>姓</label>
|
197
|
-
<input type="text" name="last_name" value="<?php echo '山田' ?>" />
|
198
|
-
<label>名</label>
|
199
|
-
<input type="text" name="first_name" value="<?php echo '太郎' ?>" />
|
200
|
-
<select name="choice">
|
201
|
-
<option value=0 >--</option>
|
202
|
-
<option value=1 >グー</option>
|
203
|
-
<option value=2 >チョキ</option>
|
204
|
-
<option value=3 >パー</option>
|
205
|
-
</select>
|
206
|
-
<input type="submit" value="送信する"/>
|
207
|
-
</form>
|
208
|
-
</section>
|
209
|
-
</body>
|
210
|
-
</html>
|
211
|
-
|
212
|
-
```
|
213
|
-
|
214
|
-
### 試したこと
|
215
|
-
|
216
|
-
$player = new Player($_POST['choice']);
|
217
|
-
$choice = $_POST['choice'];
|
218
|
-
|
219
|
-
上記の変数の定義。
|
220
|
-
|
221
|
-
HTML内のselectタグのname="choice"をselectタグ内のoptionタグにそれぞれ付けてみたりしました。
|
222
|
-
|
223
|
-
### 補足情報(FW/ツールのバージョンなど)
|
224
|
-
|
225
|
-
vscode
|
226
|
-
MAMPを使用しております。
|
227
|
-
|
228
|
-
どうかよろしくお願いいたします。
|
229
|
-
|