4

Rubyでスロットマシンゲームを作ろうとしていますが、これは私が得た限りです. それでも実行されず、最後の行にエラーがあると表示されますが、エラーの内容や修正方法がわかりません。

次のような出力が必要です。

今日の合計金額はいくらですか? 25
合計現金: $ 25
いくら賭けますか? 10
チェリー - オレンジ - オレンジ
$20 を獲得し
ました 続行しますか? (続行する場合ははい) はい
合計現金: $ 35
いくら賭けますか? など…</p>

私はすでにそこに賞金を設定しています.2つを獲得すると、賭け金の2倍を獲得し、3つを獲得すると、賭け金の3倍を獲得します.

しかし、私はエラーが発生します: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ステートメントが表示されるのをまだ待っていたため、予期していませんでした。 」と翻訳できます。これは、少なくとも1つのペアを入力するのを忘れたことを意味します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

Ok!!!私はあなたのコード、@ 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 に答える