我为摇滚纸剪刀游戏做了一个小代码,它对我来说非常好。但是我需要计算最后一页的胜利,损失和数字。
我是php的新手,我无法这样做。
的index.php
<html>
<head>
<title>Rock Paper Scissors Game</title>
</head>
<body>
<h1> Rock Paper Scissors Game</h1>
<form action="submit.php" method="POST">
<p>Please Choose Your Player</p>
<h2>Rock<input type="radio" value="rock" name="playerturn" /><br />
Paper<input type="radio" value="paper" name="playerturn" /><br />
Scissors<input type="radio" value="scissors" name="playerturn" /><br />
</h2>
<p><input type="submit" value="Play"></p>
</form>
</body>
</html>
Submit.php(最终页面)
<html>
<head>
</head>
<body>
<h1> Rock Paper Scissors Game</h1>
<?php
if($_POST['playerturn']){
}
else if(!$_POST['playerturn']){
}
$playerturn = $_POST['playerturn'];
$rpc= array('rock', 'paper', 'scissors');
$rndm= rand(0,2);
$Computer=$rpc[$rndm];
echo '<h2>You picked: '.$playerturn.'</h2>';
echo '<br /><br />';
echo '<h2>The computer picked: '. $Computer .'</h2>';
echo '<br /><br />';
if($playerturn == $Computer){
}
if ($playerturn == $Computer){
echo ' Draw ';
}
else if($playerturn == 'rock ' && $Computer == 'scissors'){
echo '<h1>You Win!</h1>';
}
else if($playerturn == 'rock' && $Computer == 'paper'){
echo '<h1>You Lose! </h1>';
}
else if($playerturn == 'scissors' && $Computer == 'rock'){
echo ' <h1>You Lose!</h1> ';
}
else if($playerturn == 'scissors' && $Computer == 'paper'){
echo '<h1> You Win! </h1>';
}
else if($playerturn == 'paper' && $Computer == 'rock'){
echo '<h1>You Win!</h1>';
}
else if($playerturn == 'paper' && $Computer == 'scissors'){
echo '<h1>You Lose!</h1>' ;
}
?>
<hr>
</br></br>
<button type="button" onclick="window.location.href='exc3.php'">Play Again</button>
</body>
</html>
我们是否需要创建一个数据库然后才能运行。或者我们可以使用session来做同样的事情。
答案 0 :(得分:0)
要在PHP中使用会话,您需要
session_start();
位于PHP文件的顶部。
然后,您可以使用$ _SESSION作为关联数组,例如
$_SESSION['my_wins'] = 99;
$_SESSION['cpu_wins'] = 8;
答案 1 :(得分:0)
我正在为您的游戏提供工作代码:
gameStart.php
<?php
session_start();
if(isset($_GET['exit']) == 1){
session_destroy();
header('Location: gameStart.php');
}
?>
<html>
<head>
<title>Rock Paper Scissors Game</title>
</head>
<body>
<h1> Rock Paper Scissors Game</h1>
<form action="gameResult.php" method="POST">
<p>Please Choose Your Player</p>
<h2>Rock<input type="radio" value="rock" name="playerturn" /><br />
Paper<input type="radio" value="paper" name="playerturn" /><br />
Scissors<input type="radio" value="scissors" name="playerturn" /><br />
</h2>
<p><input type="submit" value="Play"></p>
</form>
</body>
</html>
gameResult.php
<?php
// Start the session
session_start();
?>
<html>
<head>
</head>
<body>
<h1> Rock Paper Scissors Game</h1>
<?php
if(empty($_SESSION['your_win']) &&
empty($_SESSION['cpu_win']) &&
empty($_SESSION['draw']) ){
$_SESSION['your_win'] = 0;
$_SESSION['cpu_win'] = 0;
$_SESSION['draw'] = 0;
//Printing the initiation of session variables for your reference
echo $_SESSION['your_win'];
echo $_SESSION['cpu_win'];
echo $_SESSION['draw'] ;
}
$playerturn = $_POST['playerturn'];
$rpc= array('rock', 'paper', 'scissors');
$rndm= rand(0,2);
$Computer=$rpc[$rndm];
echo '<h2>You picked: '.$playerturn.'</h2>';
echo '<br /><br />';
echo '<h2>The computer picked: '. $Computer .'</h2>';
echo '<br /><br />';
if ($playerturn == $Computer){
$_SESSION['draw']+=1;
echo "YOUR SCORE:".$_SESSION['your_win']."<br>";
echo "CPU SCORE:".$_SESSION['cpu_win']."<br>" ;
echo "DRAW:".$_SESSION['draw']."<br>";
}
else if($playerturn == 'rock' && $Computer == 'scissors'){
$_SESSION['your_win']+=1;
echo "YOUR SCORE:".$_SESSION['your_win']."<br>";
echo "CPU SCORE:".$_SESSION['cpu_win']."<br>" ;
echo "DRAW:".$_SESSION['draw']."<br>";
echo '<h1>You Win!</h1>';
}
else if($playerturn == 'rock' && $Computer == 'paper'){
$_SESSION['cpu_win']+=1;
echo "YOUR SCORE:".$_SESSION['your_win']."<br>";
echo "CPU SCORE:".$_SESSION['cpu_win']."<br>" ;
echo "DRAW:".$_SESSION['draw']."<br>";
echo '<h1>You Lose! </h1>';
}
else if($playerturn == 'scissors' && $Computer == 'rock'){
$_SESSION['cpu_win']+=1;
echo "YOUR SCORE:".$_SESSION['your_win']."<br>";
echo "CPU SCORE:".$_SESSION['cpu_win']."<br>" ;
echo "DRAW:".$_SESSION['draw']."<br>";
echo ' <h1>You Lose!</h1> ';
}
else if($playerturn == 'scissors' && $Computer == 'paper'){
$_SESSION['your_win']+=1;
echo "YOUR SCORE:".$_SESSION['your_win']."<br>";
echo "CPU SCORE:".$_SESSION['cpu_win']."<br>" ;
echo "DRAW:".$_SESSION['draw']."<br>";
echo '<h1> You Win! </h1>';
}
else if($playerturn == 'paper' && $Computer == 'rock'){
$_SESSION['your_win']+=1;
echo "YOUR SCORE:".$_SESSION['your_win']."<br>";
echo "CPU SCORE:".$_SESSION['cpu_win']."<br>" ;
echo "DRAW:".$_SESSION['draw']."<br>";
echo '<h1>You Win!</h1>';
}
else if($playerturn == 'paper' && $Computer == 'scissors'){
$_SESSION['cpu_win']+=1;
echo "YOUR SCORE:".$_SESSION['your_win']."<br>";
echo "CPU SCORE:".$_SESSION['cpu_win']."<br>" ;
echo "DRAW:".$_SESSION['draw']."<br>";
echo '<h1>You Lose!</h1>' ;
}
?>
<hr>
</br></br>
<button type="submit" onclick="window.location.href='gameStart.php'">Play Again</button><br>
<button type = "submit"><a href="gameStart.php?exit=1">EXIT</a></button>
</body>
</html>
请单击“退出”选项以正确销毁会话。希望这会对你有所帮助!!!。