Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 729511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:48:19+00:00 2026-05-14T06:48:19+00:00

as I am learning the Ruby language, I am getting closer to actual programming.

  • 0

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 know want to learn how to solve this problem with a genuine OOP approach. In my card game, I want to have four players, using a standard deck with 52 cards, no jokers/wildcards. In the game, I won’t use the ace as a dual card, it is always the highest card.

So, the programming problems I wonder about are the following:

  1. How can I sort/randomize the deck of cards? There are four types, each having 13 values. Eventually there can be only unique values, so picking random values could generate duplicates.

  2. How can I implement a simple AI? As there are tons of card games, someone would have figured this part out already, so references would be great.

I am a true Ruby nuby, and my goal here is to learn to solve problems, so pseudo code would be great, just to understand how to solve the problem programmatically. I apologize for my grammar and writing style if it’s unclear, for it is not my native language.

Also, pointers to sites where such challenges are explained would be a great resource!

Thank you for your comments, answers and feedback!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:48:19+00:00Added an answer on May 14, 2026 at 6:48 am

    Something to get you started

    You can ensure unique cards very easily by using numbers from 0 to 51.

    The Array#shuffle method is based off the Knuth-Fisher-Yates shuffle algorithm. http://en.wikipedia.org/wiki/Fisher–Yates_shuffle

    class Card
      RANKS = %w(2 3 4 5 6 7 8 9 10 J Q K A)
      SUITS = %w(Spade Heart Club Diamond)
    
      attr_accessor :rank, :suit
    
      def initialize(id)
        self.rank = RANKS[id % 13]
        self.suit = SUITS[id % 4]
      end
    end
    
    class Deck
      attr_accessor :cards
      def initialize
        # shuffle array and init each Card
        self.cards = (0..51).to_a.shuffle.collect { |id| Card.new(id) }
      end
    end
    
    # people with Ruby 1.9 (or 1.8.7 with backports) can safely ignore this duck punch
    class Array
      # knuth-fisher-yates shuffle algorithm
      def shuffle!
        n = length
        for i in 0...n
          r = rand(n-i)+i
          self[r], self[i] = self[i], self[r]
        end
        self
      end
      def shuffle
        dup.shuffle!
      end
    end
    

    test

    d = Deck.new
    d.cards.each do |card|
      puts "#{card.rank} #{card.suit}"
    end
    

    output

    6 Spade
    5 Heart
    2 Heart
    8 Heart
    8 Diamond
    7 Club
    J Diamond
    4 Club
    K Spade
    5 Diamond
    J Heart
    8 Spade
    10 Club
    4 Diamond
    9 Heart
    7 Diamond
    3 Diamond
    K Diamond
    7 Spade
    Q Diamond
    9 Diamond
    6 Heart
    A Heart
    9 Club
    A Spade
    5 Club
    J Club
    Q Spade
    2 Club
    2 Spade
    Q Heart
    A Diamond
    10 Spade
    10 Diamond
    Q Club
    3 Club
    A Club
    K Club
    6 Club
    10 Heart
    2 Diamond
    3 Spade
    K Heart
    5 Spade
    9 Spade
    7 Heart
    4 Spade
    J Spade
    3 Heart
    4 Heart
    8 Club
    6 Diamond
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning ruby, so this question is simple. I created @subject in
I have recently started learning Ruby, as my first programming language. I feel comfortable
I am learning programming and I choose Ruby as the first language to learn.
I'm learning Ruby in my spare time, and I have a question about language
I am thinking about cross-platform with nice programming language bindings (Java, Ruby and Python).
I'm just learning Ruby and making a simple Hello World program, put for some
The break statement for blocks (as per The Ruby Programming Language ) is defined
Ruby looks a very cool language. I've started learning it for the past two
I've been slowly learning Ruby (at this point, maybe the first language I've invested
I am learning Ruby. My background is C++/Java/C#. Overall, I like the language, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.