2013-11-24 4 views
0

Я строю рубинную игру в блэкджек командной строки, используя методы. Я дошел до того, что игрок может ударить или придерживаться (после того, как раздадут 2 карты). Прямо сейчас я не могу заставить прыжок рассуждать о том, как ограничить игрока до четырех ударов. makeRuby Блэкджек и петли

Это говорит мне, что моя проблема связана с циклом - это то, что я приближаюсь к части цикла программы неправильно.

Вот мой код до сих пор:

def blackjack 
    promt 
end 

def promt 
    puts "Welcome! Would you like to play a game of blackjack? Enter Yes or No" 
    play = gets.chomp.downcase 
    if play == "yes" 
    game_plan 
    elsif play =="no" 
    puts "That's too bad. Come back when you feel like playing" 
    else 
    puts "Sorry but I don't understand your respones. Please type and enter yes to play Or no to to quit" 
    blackjack 
    end 
end 

def game_plan 
    wants_to_play = true 
    hand = [] 
    total = first_move(hand) 
    wants_to_play = hit_me(hand) 
    if wants_to_play == true 
    hit_me(hand) 
    end 
end 

def first_move(hand) 
    deal(hand) 
    deal(hand) 
    total(hand) 
end 

def deal(hand) 
    card = rand(12) 
    puts "You have been dealt a card with a value of #{card}" 
    hand << card 
end 

def total(hand) 
    total = 0 
    hand.each do |count| 
    total += count 
    end 
    puts "The sum of the cards you have been dealt is #{total}" 
    total 
end 

def hit_me(hand) 
    puts "Would you like to hit or stick?" 
    yay_or_nah = gets.chomp.downcase 
    if yay_or_nah == "stick" && total(hand) < 21 
    puts "Sorry! The sum of the cards you have been dealt is less than 21. You lost this round!" 
    else 
    deal(hand) 
    total(hand) 
    playing = true 
    end 
end 

blackjack 

То, что я хочу сделать, это предел моего игрока 2 удара (после первоначального первого удара, который наносит 2 карты). Я знаю, что это совершенно раздражающий вопрос новичков, но я действительно был бы признателен за любые отзывы, которые помогут мне правильно подобрать решение.

PS: пока я понимаю, как работают петли, я СТРУКТУЮ, зная, как и когда их реализовать ... так что любая обратная связь будет очень оценена. Спасибо!

ответ

2

Вы ищете что-то в этом роде?

MAX_HITS = 2 
hits = 0 
loop do 
    break if hits > MAX_HITS 
    puts "Would you like to hit or stick?" 
    … 
    else 
    hits += 1 
    … 
    end 
end 
Смежные вопросы