Skip to content

Add comments#1

Open
psylone wants to merge 1 commit into
dronky:masterfrom
psylone:master
Open

Add comments#1
psylone wants to merge 1 commit into
dronky:masterfrom
psylone:master

Conversation

@psylone
Copy link
Copy Markdown

@psylone psylone commented Jul 2, 2017

No description provided.

Comment thread comments/deck.rb

class Deck

attr_reader :main_deck
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы назвал это свойство просто cards.

Comment thread comments/deck.rb
'A': 11}

def initialize
create
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В данном случае можно было не определять дополнительный метод для создания колоды.

Comment thread comments/deck.rb
def get_card(n = 1)
raise "You should create a main deck before adding card" if @main_deck.empty?
a = @main_deck.sample(n)
@main_deck.delete(a)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно использовать метод pop, который принимает в качестве аргумента количество объектов которые последовательно нужно извлечь с конца массива.

Comment thread comments/deck.rb

def create
@main_deck = []
CARDS.each {|title, count| @main_deck << Card.new(title, count)}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно было использовать массив с номиналом карт, вместо хэша. Вообще, у нас 3 ситуации:

  • Если карта от 2-х до 10-ти, сумма считается по её номиналу, то есть фактически это title.to_i
  • Если карта является картинкой - это 10 очков
  • Если туз - это случай, требующий отдельной обработки

Исходя из этого, достаточно определить метод в классе Card который будет возвращать правильное количество очков и случай с тузом потом обрабатывать отдельно. Обязанность знать сколько очков приносит отдельная карта вполне подходит для объектов класса Card.

Comment thread comments/user.rb
@@ -0,0 +1,4 @@
require_relative 'gamer'

class User < Gamer
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На мой взгляд, класс User является более общим поэтому я бы сделал его базовым и потом наследовал от него классами Gamer и Dealer.

Comment thread comments/controller.rb
2) - no"
loop do
user_input = gets.chomp.to_i
case user_input
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Либо case, либо весь обработчик исключения лучше вынести в отдельный метод.

Comment thread comments/controller.rb
───▐░▀▄▄█▄▄█▄▄█▄▄█▄▄█▄▄▀░▌───
───▐░░░░░░░░░░░░░░░░░░░░░▌───
───▐░░░░░░░░░░░░░░░░░░░░░▌───
────▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀────"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙂

Comment thread comments/controller.rb
user_get_card
when 3
user_open_cards
puts "your money: #{@player.money}"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Похожая логика была внутри метода restart_game, это ещё одно подтверждение что нужен отдельный метод.

Comment thread comments/controller.rb
@dealer.print_cards

if @dealer.points > 21 && @player.points > 21 || @dealer.points == @player.points
draw
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если у обоих очков больше 21-го, значит оба проиграли.

Comment thread comments/controller.rb

if @dealer.points > 21 && @player.points > 21 || @dealer.points == @player.points
draw
elsif @player.points <= 21 && @dealer.points > 21 || @player.points <= 21 && @dealer.points <= 21 && @player.points > @dealer.points
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Чтобы игрок выиграл, достаточно чтобы количество его очков было больше чем у дилера и меньше чем 21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant