Programming Technique: How to create a simple card game

前端 未结 6 1167
自闭症患者
自闭症患者 2020-12-24 00:10

as I am learning the Ruby language, I am getting closer to actual programming. I was thinking of creating a simple card game. My question isn\'t Ruby oriented, but I do kno

6条回答
  •  庸人自扰
    2020-12-24 00:19

    Something very simple to get you started:

    class CardGame
      DECK = %w[A 2 3 4 5 6 7 8 9 T J Q K].product(%w[c d h s]).map(&:join)
    
      def initialize(decks=1)
        @decks = decks
      end
    
      def shuffle
        @playing_deck = (DECK*@decks).shuffle
      end
    
      def deal(players=1, cards=5)
        shuffle
        @dealt = Array.new(players) { Array.new }
    
        @dealt.map { |hand| cards.times { hand << @playing_deck.pop } }
      end
    
      def display
        @dealt.each_with_index { |cards, i| puts "Player #{i+1}: #{cards.join(' | ')}" }
        puts "Cards used: #{@dealt.flatten.size}"
        puts "Cards remaining: #{@playing_deck.size}"
      end
    
      private :shuffle   
    end
    
    game1 = CardGame.new   
    game1.deal   
    game1.display    
    puts 
    game1.deal(4)  
    game1.display   
    puts   
    game2 = CardGame.new(2)   
    game2.deal(6,10)   
    game2.display
    

提交回复
热议问题