我在为赌场经销商创建培训工具的早期阶段。我的代码工作率约为75%。
该工具非常简单:代码生成5到100之间的随机下注量,生成2到12之间的随机骰子数量,根据下注和$winning_bet
值计算$factor
数量,用户在文本字段中输入答案,代码会通过if-else
语句让您知道它是对还是错。
以下是我遇到的问题:
$dealer_answer
modulo
运算符有时会生成" 0"打赌。显然,你不能赌赌场游戏,仍然会赢。下注金额需要为5到100,增量为5.(5,10,55,80等)这是我的代码。我仍处于PHP的学习阶段,因此感谢任何和所有信息。我甚至不能完全确定我做对了。 (具有讽刺意味的是,如果这是C#,我就不会在这里......)
<?php
// Defaults
$dealer_answer = $_POST['dealer_answer'];
// Generate random bet
$random_bet = rand(5,100);
$random_bet = $random_bet % 5;
$random_bet = $random_bet *5;
// Generate roll
$roll = 10;
$factor = 1.8;
// Place bet
$winning_bet = $factor*$random_bet;
$winning_bet = floor($winning_bet);
echo $winning_bet; // Display current value for testing purposes
echo '<p>' . $dealer_answer . '</p>'; // Display current value for testing purposes
?>
<p>Player has a bet of <?php echo $random_bet; ?> on a Place Bet 10.</p>
<p>Player rolls a <?php echo $roll; ?>. How much does the player win?
<form method="POST" action="">
<input type="text" name="dealer_answer" value="<?php $dealer_answer; ?>">
<input type="hidden" name="winning_bet" value="<?php $winning_bet; ?>">
<input type="submit" value="Submit" name="submit">
</form>
<?php
session_start();
// Check if answer is right
if (isset($_POST['submit']))
if ($dealer_answer == $winning_bet) {
echo "That's right!";
} else {
echo "That is incorrect.";
}
答案 0 :(得分:1)
尝试将代码更改为:
<?php
// Defaults
//Generate random bet
$random_bet = rand(5,100);
$random_bet = $random_bet % 5;
$random_bet = $random_bet *5;
// Generate roll
$roll = 10;
$factor = 1.8;
// Place bet
$winning_bet = $factor*$random_bet;
$winning_bet = floor($winning_bet);
echo "Winning bet is = ".$winning_bet; // Display current value for testing purposes
if ( isset( $_POST ) && isset( $_POST['dealer_answer'] ) ) {
var_dump($_POST);
$dealer_answer = $_POST['dealer_answer'];
$winning_bet = $_POST['winning_bet'];
var_dump("Dealer answer = ".$dealer_answer);
var_dump("Winning bet = ".$winning_bet);
if ($dealer_answer == $winning_bet) {
echo "That's right!";
} else {
echo "That is incorrect.";
}
echo '<p>' . $dealer_answer . '</p>'; // Display current value for testing purposes
}
?>
<p>Player has a bet of <?php echo $random_bet; ?> on a Place Bet 10.</p>
<p>Player rolls a <?php echo $roll; ?>. How much does the player win?
<form method="POST" action="">
<input type="text" name="dealer_answer" value="<?php if (isset($_POST['dealer_answer'])) { echo $dealer_answer; } ?>">
<input type="hidden" name="winning_bet" value="<?php echo $winning_bet; ?>">
<input type="submit" value="Submit" name="submit">
</form>
我已将var_dump
留在那里,以便您可以看到变量。