我正在用perl编写一个掷骰子游戏(规则通过源代码注释提供)。用perl编写的此掷骰游戏的功能之一是能够询问用户是否要继续下注或是否要退出游戏。我遇到的问题是,每当用户在“ bet”中键入任何内容时,似乎都不会调用任何其他函数,并且该程序只是以与用户在“ leave”中键入时应终止的方式相同的方式终止。
我尝试将程序拆分成更多的函数,并手动跟踪函数调用的方向。虽然我认为自己有其他语言的编程经验,但是我对perl还是比较陌生,语法与其他语言有很大不同。 预期结果:
键入赌注会调用其他功能,并且取决于骰子掷骰,这些功能会调用其他功能,并且当用户输入离开程序时,程序便会终止。
实际结果:
只要用户在键盘上输入任何内容,程序就会终止
use strict;
use warnings;
my $user_money= 500;#AMOUNT OF MONEY THE USER STARTS OUT WITH
my $die=0;#WHAT NUMBER THE DIE ROLLS, IS BETWEEN 1 AND 6
my $total_dicenum=0; #TOTAL VALUE OF THE ROLL
my $betorleave="";#TAKES IN USER INPUT REGARDING WHETHER THEY WANT TO CONTINUE BETTING OR LEAVE THE GAME
my $wager=0;#HOW MUCH MONEY THE USER BETS
my $numrolls=0;#KEEPS TRACK OF THE NUMBER OF TIMES THE USER HAS ROLLED THE DICE
my $player_point=0;#THE PLAYER'S 'POINT' THAT GET'S DETERMINED AFTER THE FIRST ROLL OF DICE
#DETERMINES WHETHER THE PLAYER HAS WON OR LOST A GAME
sub result(){
if($numrolls==1){
if($total_dicenum==7 or $total_dicenum==11){
print"you won!\n";
$player_point=$total_dicenum;
$user_money = $user_money +($wager*2);
$total_dicenum==0;
}
elsif($total_dicenum==2 or $total_dicenum==3 or $total_dicenum==12){
print"you lost\n";
$player_point=$total_dicenum;
$user_money = $user_money-$wager;
$total_dicenum==0;
}
else{
bet();
}
}
else{#ROLLS FOLLWING THE INITAL ROLL
if($total_dicenum==$player_point){
print"you won!\n";
$user_money = $user_money+($wager*2);
$total_dicenum=0;
main();
}
elsif($total_dicenum==7){
print"you lost\n";
$user_money = $user_money-$wager;
$total_dicenum=0;
main();
}
else{
bet();
}
}
}
#DICE ROLLER FUNCTION
sub rollDice(){
print"rolling dice...\n";
$die = 1 + int rand(6);
print"you rolled $die\n";
}
#BETTING FUNCTION
sub bet(){
print"how much money do you want to wager?\n";
print"you currently have $user_money dollars\n";
$wager=<STDIN>;
rollDice();
$total_dicenum+=$die;
print"your total score is $total_dicenum\n";
$numrolls++;
result();
}
#BELOW IS MAIN SUBROUTINE WHERE ALL THE ABOVE SUBROUTINES ARE CALLED
sub main(){
print"Welcome to craps! Here's $user_money dollars!\n";
print"would you like to place a bet or would you like to leave?(bet/leave)\n";
$betorleave=<STDIN>;
if($betorleave eq 'bet'){
bet();
}
if($betorleave eq 'leave'){
print"FINAL BALANCE:\n";
print"$user_money\n";
}
}
#CALLS THE MAIN FUNCTION
main();
#WHAT THIS PROGRAM IS SUPPOSED TO DO:
#Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program.
#Implement this any way you wish, but make it clear to the user what their options are.
#If the user chooses to place a bet, ask for the amount that will be wagered and start that “turn,” or bet.
#Each turn will consist of one or more rolls of the dice.
#For each roll, the program should display the result of both die and the total value of the roll.
#Then indicate the outcome from that roll (win/lose/continue rolling).
#Once a turn, or bet is finished, indicate the outcome of the bet and the updated balance.
#When the user chooses to exit the program display their final balance.
#Total value of dice after first roll: 7 or 11 – player wins 2, 3, or 12 – player loses
#Any other value and the player rolls again – the total of the dice is now their “point”
#Total value of dice following the initial roll: The players “point” – player wins 7 – player loses
#Any other value and the player rolls again until rolling a 7 or their point
答案 0 :(得分:2)
每当您使用<STDIN>
从STDIN读取数据时,都会在字符串末尾附加换行符。您通常不希望使用它,因此可以使用chomp()
删除它。
因此,您在哪里:
$betorleave=<STDIN>;
您需要具备:
chomp($betorleave=<STDIN>);
更多(免费!)提示:
$betorleave
变量仅在main()
子例程中使用,因此应在该子例程中声明它。