1715735914

Ruby BlackJack


Requirements: ruby, no other dependencies. ez This is single player BTW. Pretty easy game that uses alot of methods on the array of cards (deck) that will come in handy when making larger applications. Creating the Deck:<br> The first step is to create a method that generates a deck of cards. A standard deck has four suits, each with cards numbered from 1 to 13. ```ruby def generate_deck cards = (1..13).to_a suits = ["Diamonds", "Clubs", "Spades", "Hearts"] deck = [] suits.each do |suit| cards.each do |card| deck << [card, suit] end end deck end ``` cards = (1..13).to_a: Generates an array of numbers from 1 to 13, representing card ranks. suits: An array of string values representing the four suits. Nested Loops: The outer loop iterates through each suit, and the inner loop iterates through each card rank, creating a pair of card and suit which is appended to the deck array. Shuffling the Deck<br> To make the game fair and random, we need to shuffle the deck. ```ruby def shuffle_deck(deck) deck.shuffle end ``` deck.shuffle: Ruby's Array class provides a shuffle method that randomly rearranges the elements of the array. This is used to shuffle the deck. Displaying Cards<br> When playing, it's necessary to show cards in a human-readable format. We'll create a method to convert numerical card values to their names for display. ```ruby def card_label(card, format: :short) card_labels = {1 => "Ace", 11 => "Jack", 12 => "Queen", 13 => "King"} card_suit_labels = {"Diamonds" => "♦", "Clubs" => "♣", "Spades" => "♠", "Hearts" => "♥"} card_index, card_suit = card label = card_labels[card_index] || card_index icon = card_suit_labels[card_suit] if format == :short "#{label}#{icon}" else "#{label} of #{card_suit}" end end ``` Mappings: Maps integers and suit strings to their respective card names and suit symbols. Ternary Operator: Decides whether to return a short or long format of the card's description. Dealing Cards<br> Players need to be able to draw cards from the deck. ```ruby def deal_cards(deck, amount) deck.shift(amount) end ``` deck.shift(amount): Removes the first amount of cards from the deck and returns them. This simulates dealing cards from the top of the deck. Calculating the Total Value of Cards<br> Blackjack values need to sum the cards, with Aces being either 1 or 11. ```ruby def calculate_total(hand) hand.inject(0) { |sum, card| sum + card_value(card[0]) } end def card_value(card) case card when 11..13 then 10 when 1 then 11 else card end end ``` calculate_total: Uses Ruby’s inject method to iterate over the hand and sum the values of the cards. card_value: Determines the value of each card based on Blackjack rules. The Game Logic<br> The play method needs to orchestrate the game, handling turns, player decisions, and determining the winner. ```ruby def play(deck) player_hand = deal_cards(deck, 2) dealer_hand = deal_cards(deck, 2) # Add game logic for player decisions and outcomes end ``` Initial dealing: Both the player and the dealer start with two cards. Game Logic: This would involve loops and conditions handling hits, stands, busts, etc. This structured approach, breaking down the game into these essential functions and then integrating them in the play method, provides a clear development pathway. Each method performs a distinct role, making the code modular, easier to manage, and debug. Completed code: ``` def generate_deck cards = (1..13).to_a suits = ["Diamonds", "Clubs", "Spades", "Hearts"] deck = [] suits.each do |suit| cards.each do |card| deck << [card, suit] end end deck end def shuffle_deck(deck) deck.shuffle end def inspect_cards(cards, format: :short) card_labels = { 1 => "Ace", 11 => "Jack", 12 => "Queen", 13 => "King" } card_suit_labels = { "Diamonds" => "♦", "Clubs" => "♣", "Spades" => "♠", "Hearts" => "♥" } hand = cards.map { |c| card_label(c, format: format) }.join(",") total = calculate_total(cards) "#{hand} (#{total})" end def card_label(card, format: :short) card_labels = { 1 => "Ace", 11 => "Jack", 12 => "Queen", 13 => "King" } card_suit_labels = { "Diamonds" => "♦", "Clubs" => "♣", "Spades" => "♠", "Hearts" => "♥" } card_index, card_suit = card label = card_labels[card_index] || card_index icon = card_suit_labels[card_suit] format == :short ? "#{label}#{icon}" : "#{label} of #{card_suit}" end def calculate_total(hand) hand.inject(0) { |sum, card| sum + card_value(card[0]) } end def card_value(card) case card when 11..13 then 10 when 1 then 11 else card end end def deal_cards(deck, amount) deck.shift(amount) end def play(deck) player_hand = deal_cards(deck, 2) dealer_hand = deal_cards(deck, 2) puts "Player has: #{inspect_cards(player_hand)}" puts "Dealer has: #{inspect_cards([dealer_hand[0]])}, <other card hidden>" choice = nil while choice != 'stay' && calculate_total(player_hand) <= 21 do print "Do you want to hit or stay?" choice = gets.chomp if choice == 'hit' player_hand += deal_cards(deck, 1) end puts "Your cards are now: #{inspect_cards(player_hand)}" end while calculate_total(dealer_hand) <= 17 do dealer_hand += deal_cards(deck, 1) end player_total = calculate_total(player_hand) dealer_total = calculate_total(dealer_hand) if player_total > 21 puts "Player busts! #{player_total}" elsif player_total == 21 && dealer_total != 21 puts "21! A winner!" elsif player_total > dealer_total || dealer_total > 21 puts "Player wins!" elsif player_total == dealer_total puts "It's a tie!" else puts "Dealer wins!" end puts "Dealer hand: #{inspect_cards(dealer_hand)}" puts "Player hand: #{inspect_cards(player_hand)}" end shuffled_deck = shuffle_deck(generate_deck) choice = 'y' while shuffled_deck.length > 4 && choice.downcase != 'n' do puts "Deck has: #{shuffled_deck.length} cards left" print "Do you want to play a hand?[Yn]" choice = gets.chomp if choice.downcase == 'y' play(shuffled_deck) end end ``` If you have any questions ill be in the ruby chat.

(0) Comments