2012-05-02 4 views
4

루비에서 슬롯 머신 게임을 만들고 싶습니다. 그리고 지금까지 제가 얻은 것입니다. 여전히 실행되지 않으며 마지막 줄에 오류가 있으며 어떤 점이 무엇인지 또는 어떻게 수정해야하는지 모릅니다. ,루비 슬롯 머신 게임 프로그래밍하기

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

실행하고 있기 때문에 글쎄, 당신은 오류 메시지를 받고있어 뭔가를 기대하는 파이썬처럼 루비 인터프리터로 포맷됩니다. –

답변

2

라는 메시지가 표시되면 :

unexpected $end, expecting kEND

을 당신이, 은 "($ 끝") 나는 파일의 마지막에 도달 "로 번역 할 수 있습니다,하지만 난 그것을 기대하지 않았다, 나는 아직도 end 진술을보기를 기다리고 있었기 때문에. " 적어도 하나의 쌍을 입력하는 것을 잊어 버린 것을 의미합니다. end, 그리고 코드를 살펴보고 구문을 시각적으로 일치시킬 수 있도록 제대로 들여 쓰기해야합니다.

아래 코드는 올바른 코드를 수정 한 결과입니다.; 어떤 곳에서는 올바른 구문 대신에 파이썬처럼 블럭을 닫는 데 들여 쓰기를 사용하는 것처럼 보였습니다. 내가 있다면

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

우. 고맙습니다. kEND 현금 + = 현금 + 위닝 ^ 이것에 대한 이유가 기대, 구문 오류, 예기치 못한 $ 끝 : 33 : 광산과 코드를 모두 하지만, 나는이 오류가? Ruby를 처음 접했습니다 ... 정말 고마워요! – user1368970

+3

두 번째 줄에는 외부 콜론 (':')이 있습니다. 'cash' 변수는'bet ='변수와 같은 방법으로 읽혀 져야합니다 :'cash = gets.chomp.to_i'.당신은'cash + = cash + winnings' 대신'cash + = winnings'을해야합니다.'gets.chomp = ~/[yY] [eS]? [sS]?/''gets.chomp == "yes"'대신에. 'unless'와'end' 사이의 코드는 한 단계 더 들여 쓰기되어야하고 그 후에 또 다른'end'가 있어야합니다. 문자열 보간''foo # {bar} ''를 더 자주 사용하면'# {}'내부의 표현식을 문자열 ('to_s'를 호출)로 변환합니다. –

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 

은 soooo를 많이 모두 감사합니다! : D

관련 문제