4

我正在尝试用 Ruby 制作一个老虎机游戏,这就是我所得到的。它仍然无法运行,并说最后一行有错误,我不知道是什么或如何修复它。

我需要它有类似这样的输出:

你今天想玩多少钱?25
总现金:$ 25
您想下注多少?10
樱桃 - 橙子 - 橙子
你赢了 20 美元
你想继续吗?(是继续) 是
总现金:$ 35
您想下注多少?等等……</p>

我已经在那里设置了奖金,比如如果你得到两个,你赢了两倍的赌注,如果你得到三个,你赢了三倍的赌注。

但我得到了错误:33: syntax error, unexpected $end, expecting kEND cash += cash + winnings

我的代码有什么问题,我该如何解决?

    def multiplier(s1, s2, s3)

          if s1 == s2 and s2 == s3:
            multiplier = 3
          elsif s1 == s2 or s2 == s3 or s1 == s3:
            multiplier = 2
          else
            multiplier = 0;

          return multiplier


    def main()

    slotImageList = ['Cherry', 'Orange', 'Plum', 'Bell', 'Melon', 'Bar']

    cash = gets
    puts "How much total money would you like to play with today? " +cash
    while True:
        puts("Total cash:  $", cash)
        bet = gets
        puts "How much would you like to bet? " +bet

    cash = (cash - bet)

    slotImage1 = slotImageList.sample
    slotImage2 = slotImageList.sample
    slotImage3 = slotImageList.sample

    puts "slotImage1", " - ", "slotImage2", " - ", "slotImage3"

    winnings = bet * multiplier(slotImage1, slotImage2, slotImage3)
    puts "You have won $" +winnings

    cash = cash + winnings

    cont = gets
    puts "Would you like to continue? (yes to continue) " +cont
    if cont != "yes":
        puts "You have ended with $" +cash
    else
        puts " "
    end
4

2 回答 2

2

当您看到消息时:

意外的 $end,期待 kEND

你可以把它翻译成“我到达了文件结尾(“$end”),但我没想到,因为我还在等着看一个end声明。” 这意味着您忘记输入至少一个 pair end,并且您需要检查您的代码并确保它正确缩进,以便您可以直观地匹配语句。

以下是修复代码正确的结果。在某些地方,您似乎使用缩进来关闭一个块(如 Python)而不是正确的语法。

def multiplier(s1, s2, s3)
  if s1==s2 && s2==s3
    3
  elsif s1==s2 || s2==s3 || s1==s3
    2
  else
    0
  end
end

def run_slots!
  slotImageList = %w[Cherry Orange Plum Bell Melon Bar]

  print "How much total money would you like to play with today? "
  cash = gets.chomp.to_i
  loop do
    puts "Total cash:  $#{cash}"
    print "How much would you like to bet? "
    bet = gets.chomp.to_i

    cash -= bet

    slotImage1 = slotImageList.sample
    slotImage2 = slotImageList.sample
    slotImage3 = slotImageList.sample

    puts "#{slotImage1} - #{slotImage2} - #{slotImage3}"

    winnings = bet * multiplier(slotImage1, slotImage2, slotImage3)
    puts "You have won $#{winnings}"

    cash += winnings

    print "Would you like to continue? (yes to continue) "
    unless gets.chomp=="yes"
      puts "You have ended with $#{cash}"
      break
    end
  end
end

run_slots! if __FILE__==$0

如果我对它有更多的自由,我可能会这样写:

class SlotGame
  SLOT_COUNT = 3
  TOKENS     = %w[Cherry Orange Plum Bell Melon Bar]
  KEEP_PLAYING_RESPONSES = %w[y yes sure ok go]

  def initialize(cash=nil)
    unless cash
      begin
        print "How much total money would you like to play with today? "
        cash = gets.to_i
        puts "You must have a positive bank account to play!" if cash<=0
      end until cash > 0
    end
    @cash = cash
  end

  def play_forever
    begin
      # Using begin/end ensures one turn will be played
      # before asking the player if they want to go on
      play_one_turn
    end while @cash>0 && keep_playing?
    puts "You have ended with $#{@cash}; goodbye!"
  end

  def play_one_turn
    puts "Total cash: $#{@cash}"

    begin
      print "How much would you like to bet? "
      bet = gets.to_i
      puts "You only have $#{@cash}!" if bet > @cash
    end until bet <= @cash
    @cash -= bet

    results = SLOT_COUNT.times.map{ TOKENS.sample }
    puts results.join(' - ')
    winnings = bet * multiplier(results)

    if winnings>0
      @cash += winnings
      puts "You just won $#{winnings}!"
    else
      puts "Sorry, you're not a winner."
    end
  end

  def keep_playing?
    print "Would you like to continue? "
    KEEP_PLAYING_RESPONSES.include?(gets.chomp.downcase)
  end

  private # Don't let anyone outside run our magic formula!
    def multiplier(*tokens)
      case tokens.flatten.uniq.length
        when 1 then 3
        when 2 then 2
        else 0
      end
    end
end

SlotGame.new.play_forever if __FILE__==$0
于 2012-05-02T01:47:57.407 回答
1

行!!!我想我已经从你的代码中弄清楚了,@Phrogz !!!

为了从数组中随机选择,就像老虎机一样,我使用slotImageList.shuffle.first了 ,它对数组进行打乱,并获取打乱数组的第一个元素。

def multiplier(s1, s2, s3)
  if s1==s2 && s2==s3
    3
  elsif s1==s2 || s2==s3 || s1==s3
    2
  else
    0
  end
end

def run_slots!
  slotImageList = %w["Cherry", "Orange", "Plum", "Bell", "Melon", "Bar"]
  print "How much total money would you like to play with today? "
  cash = gets.chomp.to_i
  loop do
    puts "Total cash:  $#{cash}"
    print "How much would you like to bet? "
    bet = gets.chomp.to_i

    cash -= bet

    slotImage1 = slotImageList.shuffle.first
    slotImage2 = slotImageList.shuffle.first
    slotImage3 = slotImageList.shuffle.first

    puts "#{slotImage1} - #{slotImage2} - #{slotImage3}"

    winnings = bet * multiplier(slotImage1, slotImage2, slotImage3)
    puts "You have won $#{winnings}"

    cash += winnings

    print "Would you like to continue? (yes to continue) "
    unless gets.chomp=="yes"
      puts "You have ended with $#{cash}"
      break
    end
  end
end

run_slots! if __FILE__==$0

非常感谢大家!!!:D

于 2012-05-03T02:15:52.113 回答