diff --git a/comments/card.rb b/comments/card.rb new file mode 100644 index 0000000..b283987 --- /dev/null +++ b/comments/card.rb @@ -0,0 +1,10 @@ +class Card + + attr_reader :name, :count + + def initialize(name, count) + @name = name + @count = count + end + +end diff --git a/comments/controller.rb b/comments/controller.rb new file mode 100644 index 0000000..43b671e --- /dev/null +++ b/comments/controller.rb @@ -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) +end + +def restart_game + @deck.create + @player.new_game + @dealer.new_game +rescue => e + puts e.message + puts "New game? + 1) - yes + 2) - no" + loop do + user_input = gets.chomp.to_i + case user_input + 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 + elsif @player.points <= 21 && @dealer.points > 21 || @player.points <= 21 && @dealer.points <= 21 && @player.points > @dealer.points + 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}" + 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 \ No newline at end of file diff --git a/comments/dealer.rb b/comments/dealer.rb new file mode 100644 index 0000000..ef479e6 --- /dev/null +++ b/comments/dealer.rb @@ -0,0 +1,11 @@ +require_relative 'gamer' + +class Dealer < Gamer + def play? + if self.points > 18 + false + else + true + end + end +end diff --git a/comments/deck.rb b/comments/deck.rb new file mode 100644 index 0000000..3454f5c --- /dev/null +++ b/comments/deck.rb @@ -0,0 +1,28 @@ +require_relative 'card' + +class Deck + + attr_reader :main_deck + + 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 + end + + def create + @main_deck = [] + CARDS.each {|title, count| @main_deck << Card.new(title, count)} + 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) + a + end +end diff --git a/comments/gamer.rb b/comments/gamer.rb new file mode 100644 index 0000000..9ef8200 --- /dev/null +++ b/comments/gamer.rb @@ -0,0 +1,60 @@ +class Gamer + attr_accessor :name, :points, :money, :deck, :check_available, :user_cards + + def initialize(name, deck) + @name = name + @money = 100 + @deck = deck + new_game + end + + def new_game + make_bet + @check_available = true + @user_cards = [] + get_card(2) + calculate_points + 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}"} + end + + def get_card(n=1) + if @user_cards.size < 3 + @user_cards.concat(deck.get_card(n)) + calculate_points + else + raise 'max cards count reached' + end + end + + def calculate_points + @points = @user_cards[0].count + #значение очков туза == 11 + @user_cards[1..@user_cards.size-1].each do |card| + if card.count == 11 && @points + card.count > 21 + @points +=1 + else + @points += card.count + end + end + end + + #пропустить + def check + @check_available = false + end +end \ No newline at end of file diff --git a/comments/user.rb b/comments/user.rb new file mode 100644 index 0000000..394a939 --- /dev/null +++ b/comments/user.rb @@ -0,0 +1,4 @@ +require_relative 'gamer' + +class User < Gamer +end \ No newline at end of file