2012-05-02 6 views
4

私はルビーでスロットマシンゲームを作ろうとしています。これは私が得た限りです。それでも実行されず、最後の行にエラーがあると言われています。私はそれが何であるか、または修正する方法がわかりません。ルビースロットマシンゲームをプログラミングする

私はそれがこのような出力持っている必要があります:あなたは2を取得する場合、あなたは二回あなたの賭けに勝つようにし、次の3つを取得する場合、

How much total money would you like to play with today? 25
Total cash: $ 25
How much would you like to bet? 10
Cherry - Orange - Orange
You have won $ 20
Would you like to continue? (yes to continue) yes
Total cash: $ 35
How much would you like to bet? etc…

私はすでにそこに設定された賞金を持っているがあなたはあなたの賭け金の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 
+0

具体的にどのようなエラーメッセージが表示されますか? 33: – sarnold

+0

@sarnoldは、これは私が受け取ったエラーメッセージで構文エラー、予期しない$エンド、kEND 現金+ =現金+賞金 ^ – user1368970

+2

あなたが実行しているので、さて、あなたは、エラーメッセージを取得している何かを期待していますPythonのようにRubyインタプリタにフォーマットされています。 –

答えて

2

あなたのメッセージが表示された場合

unexpected $end, expecting kEND

をあなたはそれが、は「($エンド ")私は、ファイルの終わりに達した」に変換することができますが、私はそれを期待していませんでした、私はまだendのステートメントを見るのを待っていたからです。少なくとも1つの対を少なくとも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 
+0

私はこのスロットマシンを使用したいと思う;-) – Mischa

+0

Ooh。ありがとうございました。 kEND 現金+ =現金+賞金 ^ この理由はあるのを期待して、構文エラー、予期しない$エンド: 33:鉱山とあなたのコードの両方を持つ しかし、私がこのエラーを取得しますか?私はRubyの新機能です... ありがとうございました! 2行目の – user1368970

+3

には、余分なコロン( ':')があります。 'cash'変数は' bet = '変数と同じ方法で読み込まなければなりません:' cash = gets.chomp.to_i'。あなたは 'cash + = cash + winnings'の代わりに' cash + = winnings'を行うべきです、 'gets.chomp =〜/ [yY] [eE]?[sS]?/ '' gets.chomp == "yes" 'の代わりに' '。 'unless'と' end'の間のコードは1レベル下にインデントされなければなりません。文字列補間 '' foo#{bar} ''をより頻繁に使うと、'#{} 'の中の式を文字列(' to_s'を呼ぶ)に変換します。 –

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

関連する問題