-
Notifications
You must be signed in to change notification settings - Fork 0
Add comments #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add comments #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class Card | ||
|
|
||
| attr_reader :name, :count | ||
|
|
||
| def initialize(name, count) | ||
| @name = name | ||
| @count = count | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| require_relative 'deck' | ||
| require_relative 'dealer' | ||
| require_relative 'user' | ||
|
|
||
| # game_deck = Deck.new | ||
| # user = User.new('vasya', game_deck) | ||
| # p user.user_cards | ||
| # p user.money | ||
| # p user.points | ||
|
|
||
| puts " | ||
| ───────────────────────────── | ||
| ─────────────▐█▌───────────── | ||
| ─────────────▐░▌───────────── | ||
| ─────────────▐░▌───────────── | ||
| ─────────────▐░▌───────────── | ||
| ──────────▄▄▀░░░▀▄▄────────── | ||
| ────────▄▀░░░░░░░░░▀▄──────── | ||
| ──────▄▀░░░░░░░░░░░░░▀▄────── | ||
| ─────▐░░░░░░░░░░░░░░░░░▌───── | ||
| ────▐░░░░░░░░░░░░░░░░░░░▌──── | ||
| ───▐░░░░░░░░░░░░░░░░░░░░░▌─── | ||
| ───▐░░░░░░░░░░░░░░░░░░░░░▌─── | ||
| ───▐░░░░░░░░░░░░░░░░░░░░░▌─── | ||
| ───▐▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▌─── | ||
| ──▄███████████████████████▄── | ||
| ─████▀────▀███████▀────▀███▄─ | ||
| ─███▀───────█████────────███─ | ||
| ─███───███───███───███───███─ | ||
| ─███───▀▀▀───███───▀▀▀───███─ | ||
| ─▀███▄─────▄█████▄─────▄███▀─ | ||
| ──▀███████████████████████▀── | ||
| ───▐░░░░░░░░░░░░░░░░░░░░░▌─── | ||
| ───▐░░░░░░░░░░░░░░░░░░░░░▌─── | ||
| ───▐░▄▀▀█▀▀█▀▀█▀▀█▀▀█▀▀▄░▌─── | ||
| ───▐░█▄▄█▄▄█▄▄█▄▄█▄▄█▄▄█░▌─── | ||
| ───▐░█──█──█──█──█──█──█░▌─── | ||
| ───▐░█▀▀█▀▀█▀▀█▀▀█▀▀█▀▀█░▌─── | ||
| ───▐░▀▄▄█▄▄█▄▄█▄▄█▄▄█▄▄▀░▌─── | ||
| ───▐░░░░░░░░░░░░░░░░░░░░░▌─── | ||
| ───▐░░░░░░░░░░░░░░░░░░░░░▌─── | ||
| ────▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀────" | ||
|
|
||
| def init_game(username) | ||
| @deck = Deck.new | ||
| @player = User.new(username, @deck) | ||
| @dealer = Dealer.new('Bender', @deck) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не стоит хранить колоду внутри объекта игрока. Колода - это внешняя по отношению к игроку сущность. |
||
| end | ||
|
|
||
| def restart_game | ||
| @deck.create | ||
| @player.new_game | ||
| @dealer.new_game | ||
| rescue => e | ||
| puts e.message | ||
| puts "New game? | ||
| 1) - yes | ||
| 2) - no" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. puts <<~TEXT
New game?
1) - yes
2) - no
TEXT |
||
| loop do | ||
| user_input = gets.chomp.to_i | ||
| case user_input | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Либо |
||
| when 1 | ||
| @player.money = 100 | ||
| restart_game | ||
| break | ||
| when 2 | ||
| abort("Go home, baby") | ||
| else | ||
| puts "enter right num (1 or 2)" | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def print_status | ||
| puts "Check your cards:" | ||
| @player.print_cards | ||
| puts "Check your points:" | ||
| puts @player.points | ||
| puts "Dealer cards: #{"* " * @dealer.user_cards.size}" | ||
| end | ||
|
|
||
| def user_check | ||
| if @player.check_available | ||
| @player.check | ||
| puts "Player checks..." | ||
| if @dealer.play? | ||
| @dealer.get_card | ||
| puts "Dealer got one card..." | ||
| else | ||
| puts "Dealer checks..." | ||
| end | ||
| else | ||
| puts "You lost your check" | ||
| end | ||
| end | ||
|
|
||
| def user_get_card | ||
| @player.get_card | ||
| print_status | ||
| rescue => e | ||
| puts e.message | ||
| end | ||
|
|
||
| def draw | ||
| puts "DRAW" | ||
| @player.add_money(10) | ||
| @dealer.add_money(10) | ||
| end | ||
|
|
||
| def user_wins | ||
| puts "#{@player.name} wins with #{@player.points} points against #{@dealer.name}'s #{@dealer.points} points" | ||
| @player.add_money | ||
| end | ||
|
|
||
| def dealer_wins | ||
| puts "#{@dealer.name} wins with #{@dealer.points} points against #{@player.name}'s #{@player.points} points" | ||
| @dealer.add_money | ||
| end | ||
|
|
||
| def user_open_cards | ||
| # if @dealer.points > 21 && @player.points > 21 | ||
| # draw | ||
| # elsif @player.points > 21 || @dealer.points > 21 | ||
| # if @player.points > 21 | ||
| # dealer_wins | ||
| # else | ||
| # user_wins | ||
| # end | ||
| # elsif @player.points > @dealer.points | ||
| # user_wins | ||
| # elsif @player.points == @dealer.points | ||
| # draw | ||
| # elsif @dealer.points > @player.points | ||
| # dealer_wins | ||
| # end | ||
| puts "Dealer cards: " | ||
| @dealer.print_cards | ||
|
|
||
| if @dealer.points > 21 && @player.points > 21 || @dealer.points == @player.points | ||
| draw | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если у обоих очков больше |
||
| elsif @player.points <= 21 && @dealer.points > 21 || @player.points <= 21 && @dealer.points <= 21 && @player.points > @dealer.points | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Чтобы игрок выиграл, достаточно чтобы количество его очков было больше чем у дилера и меньше чем |
||
| user_wins | ||
| elsif @player.points > 21 && @dealer.points <= 21 || @player.points <= 21 && @dealer.points <= 21 && @dealer.points > @player.points | ||
| dealer_wins | ||
| end | ||
| end | ||
|
|
||
| puts "Welcome to Bender's Black Jack room. | ||
| Rules: | ||
| 1) At first you need to put your name | ||
| 2) Then game will be started. Enjoy! | ||
| 3) If you want go to your mom, put 'exit' | ||
|
|
||
| Put your name:" | ||
| user_input = gets.chomp | ||
| abort("Go home, baby") if user_input == 'exit' | ||
| puts "You have 100$" | ||
| init_game(user_input) | ||
|
|
||
| loop do | ||
| print_status | ||
| puts "\nChoose an action: | ||
| 1) Check | ||
| 2) Take card | ||
| 3) Open cards" | ||
|
|
||
| user_input = gets.chomp.to_i | ||
| case user_input | ||
| when 1 | ||
| user_check | ||
| when 2 | ||
| user_get_card | ||
| when 3 | ||
| user_open_cards | ||
| puts "your money: #{@player.money}" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Похожая логика была внутри метода |
||
| puts "\nOne more time? | ||
| 1) - yes | ||
| 2) - no" | ||
| loop do | ||
| user_input = gets.chomp.to_i | ||
| case user_input | ||
| when 1 | ||
| restart_game | ||
| break | ||
| when 2 | ||
| abort("Go home, baby") | ||
| else | ||
| puts "enter right num (1 or 2)" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| require_relative 'gamer' | ||
|
|
||
| class Dealer < Gamer | ||
| def play? | ||
| if self.points > 18 | ||
| false | ||
| else | ||
| true | ||
| end | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нет необходимости явно возвращать def play?
self.points > 18
end |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| require_relative 'card' | ||
|
|
||
| class Deck | ||
|
|
||
| attr_reader :main_deck | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я бы назвал это свойство просто |
||
|
|
||
| CARDS = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '9': 9, '10': 10, | ||
| 'K+': 10, 'K<3': 10, 'K^': 10, 'K<>': 10, | ||
| 'Q+': 10, 'Q<3': 10, 'Q^': 10, 'Q<>': 10, | ||
| 'V+': 10, 'V<3': 10, 'V^': 10, 'V<>': 10, | ||
| 'A': 11} | ||
|
|
||
| def initialize | ||
| create | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В данном случае можно было не определять дополнительный метод для создания колоды. |
||
| end | ||
|
|
||
| def create | ||
| @main_deck = [] | ||
| CARDS.each {|title, count| @main_deck << Card.new(title, count)} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно было использовать массив с номиналом карт, вместо хэша. Вообще, у нас 3 ситуации:
Исходя из этого, достаточно определить метод в классе |
||
| end | ||
|
|
||
| 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) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно использовать метод |
||
| a | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| class Gamer | ||
| attr_accessor :name, :points, :money, :deck, :check_available, :user_cards | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поскольку мы уже в контексте класса игрока, я бы назвал переменную с картами просто |
||
|
|
||
| def initialize(name, deck) | ||
| @name = name | ||
| @money = 100 | ||
| @deck = deck | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Колода - это нечто внешнее. Можно сделать отдельный класс |
||
| new_game | ||
| end | ||
|
|
||
| def new_game | ||
| make_bet | ||
| @check_available = true | ||
| @user_cards = [] | ||
| get_card(2) | ||
| calculate_points | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Очки считаются в методе |
||
| end | ||
|
|
||
| def make_bet | ||
| if @money - 10 >= 0 | ||
| @money -= 10 | ||
| else | ||
| raise 'no more money' | ||
| end | ||
| end | ||
|
|
||
| def add_money(cash = 20) | ||
| @money += cash | ||
| end | ||
|
|
||
| def print_cards | ||
| @user_cards.each {|card| puts "Name: #{card.name} Points: #{card.count}"} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если переопределить метод @user_cards.each { |card| puts card } |
||
| end | ||
|
|
||
| def get_card(n=1) | ||
| if @user_cards.size < 3 | ||
| @user_cards.concat(deck.get_card(n)) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| calculate_points | ||
| else | ||
| raise 'max cards count reached' | ||
| end | ||
| end | ||
|
|
||
| def calculate_points | ||
| @points = @user_cards[0].count | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поскольку мы храним количество очков в |
||
| #значение очков туза == 11 | ||
| @user_cards[1..@user_cards.size-1].each do |card| | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if card.count == 11 && @points + card.count > 21 | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше ориентироваться на |
||
| @points +=1 | ||
| else | ||
| @points += card.count | ||
| end | ||
| end | ||
| end | ||
|
|
||
| #пропустить | ||
| def check | ||
| @check_available = false | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| require_relative 'gamer' | ||
|
|
||
| class User < Gamer | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. На мой взгляд, класс |
||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙂