我在php中制作游戏。我的问题是如何将其循环到10轮,以及如何使这个不区分大小写,并接受前2个字母单词,即使它被拼错了。需要将用户输入到字符串。我设法编码,不知道接下来该做什么。
<html>
<body>
<h1>ROCK PAPER SCISSORS</h1>
<?php
print ('<form action="" method="post">');
print ('<p>Player 1: <input type="text" name="p1" /></p>');
print ('<p>Player 2: <input type="text" name="p2" /></p>');
print ('<input type="submit" name="submit" value="PLAY" />');
print ('</form>');
if (isset($_POST['submit'])) {
$player1score = 0;
$player2score = 0;
$draw = 0;
$player1 = $_POST['p1'];
$player2 = $_POST['p2'];
if ($player1 == 'Scissors' && $player2 == 'Scissors') {
$draw++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif ($player1 == 'Rock' && $player2 == 'Rock') {
$draw++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif ($player1 == 'Paper' && $player2 == 'Paper') {
$draw++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif ($player1 == strcasecmp('Rock','ROCK') && $player2 == strcasecmp('Scissors','SCISSORS')){
$player1score++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif($player1 == 'Rock' && $player2 =='Paper'){
$player2score++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif ($player1 == 'Scissors' && $player2 == 'Rock') {
$player2score++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif ($player1 =='Scissors' && $player2 =='Paper') {
$player1score++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif ($player1 =='Paper' && $player2 =='Rock') {
$player1score++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
} elseif ($player1 =='Paper' && $player2 == 'Scissors') {
$player2score++;
print "Player 1: $player1score<br>";
print "Player 2: $player2score<br>";
print "DRAW: $draw";
}
}
?>
</body>
</html>
答案 0 :(得分:1)
如果对用户智商有疑问,请尽可能使用下拉框,收音机等限制选择预先定义的答案。
快速写了这个。如果有任何错误,我认为你将能够调试,但我认为你会得到这个... ...
<?php
session_start();
if(isset($_POST['play']))
{
$p1 = $_POST['p1']; //don't need to check if post empty, 'cause used has a default value
$p2 = $_POST['p2']; //don't need to check if post empty, 'cause used has a default value
$loop = intval($_POST['play']);
echo $p2;
switch($p1)
{
case 'ROCK':
if($p2=='PAPER')$_SESSION['game'][$loop]='Round '. $loop . ' - Player 2 WINS!';
else if($p2=='SCISSORS')$_SESSION['game'][$loop]='Round '. $loop . ' - Player 1 WINS!';
else if($p2=='ROCK')$_SESSION['game'][$loop]='Round '. $loop . ' - It\'s a DRAW!';
break;
case 'PAPER':
if($p2=='ROCK')$_SESSION['game'][$loop]='Round '. $loop . ' - Player 2 WINS!';
else if($p2=='SCISSORS')$_SESSION['game'][$loop]='Round '. $loop . ' - Player 1 WINS!';
else if($p2=='PAPER')$_SESSION['game'][$loop]='Round '. $loop . ' - It\'s a DRAW!';
break;
case 'SCISSORS':
if($p2=='PAPER')$_SESSION['game'][$loop]='Round '. $loop . ' - Player 1 WINS!';
else if($p2=='ROCK')$_SESSION['game'][$loop]='Round '. $loop . ' - Player 2 WINS!';
else if($p2=='SCISSORS')$_SESSION['game'][$loop]='Round '. $loop . ' - It\'s a DRAW!';
break;
}
$loop++;
}
?>
<html>
<body>
<h1>ROCK PAPER SCISSORS</h1>
<form method="post">
<input type="hidden" name="play" value='<?php if(isset($loop))echo $loop; else echo 1; ?>'>
<p>Player 1:
Rock<input type="radio" name="p1" value='ROCK' checked>
PAPER<input type="radio" name="p1" value='PAPER'>
SCISSORS<input type="radio" name="p1" value='SCISSORS'>
</p>
<p>Player 2:
Rock <input type="radio" name="p2" value='ROCK' checked>
PAPER <input type="radio" name="p2" value='PAPER'>
SCISSORS<input type="radio" name="p2" value='SCISSORS'>
</p>
<button>Submit</button>
</form>
<?php
if(!empty($_SESSION['game']))
{
$game=$_SESSION['game'];
foreach($game as $count=>$comment)echo $comment.'<br>';
if($count==10)echo '<h1>END OF GAME</h1>';
}
?>
</body>
</html>
答案 1 :(得分:0)
为了使其不区分大小写,请在输入上使用strtolower使其为小写,并使用小写字符串进行比较。 在代码中替换它:
$player1 = $_POST['p1'];
$player2 = $_POST['p2'];
有了这个:
$player1 = strtolower($_POST['p1']);
$player2 = strtolower($_POST['p2']);
您还可以通过使用多个表达式来减少elseif块的数量 之一。
两个
elseif($player1 == 'Rock' && $player2 =='Paper')
和
elseif ($player1 == 'Scissors' && $player2 == 'Rock')
导致相同的操作。您可以将两者组合成一个具有OR表达式的块,如下所示:
if(($player1 == 'Rock' && $player2 =='Paper') || ($player1 == 'Scissors' && $player2 == 'Rock'))
要循环10次,你可以像这样使用for循环:
for($round=1; $round<=10; $round++)
我建议你看看http://www.w3schools.com/php/default.asp,他们有很好的PHP指南和示例。
编辑:bluepinto使用switch获得了更好的结构解决方案。