# Array function?

**URL:** <https://www.ruby-forum.com/t/array-function/172085>\
**Category:** Ruby\
**Created:** [July 14, 2009, 11:08am UTC](https://www.ruby-forum.com/t/array-function/172085 "2009-07-14T11:08:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![justme1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@justme1](https://www.ruby-forum.com/u/justme1)\
**Post date:** [July 14, 2009, 11:08am UTC](https://www.ruby-forum.com/t/array-function/172085/1 "2009-07-14T11:08:09Z")

</div>

PLOT: I want to build a simple card counter function. When i make a  
object which is the player shall the player got one card (number between  
1-13 in type of hearts for example). When more players connect the  
function has to check that the number not has been given to an player  
already. So no player can have the same card number.

here is the code for the object persons:

```auto
#create class
class Player

#set instance variables
 attr_accessor :name,
    :card
end

#create object
player = Array.new

#loop data into objects
for i in (0..12)
#create object array
player[i] = Player.new

  player[i].name = "PlayerName#{i}"
  player[i].card = rand(12) + 1 #Here i don't know the function for
this

puts "#{player[i].name}
  #{player[i].card}
-----------------"
  end
```

---

<div class="post-metadata">

**Author:** ![justme1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@justme1](https://www.ruby-forum.com/u/justme1)\
**Post date:** [July 14, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/array-function/172085/2 "2009-07-14T14:28:19Z")

</div>

On Tuesday 14 July 2009 05:08:12 Justice Me wrote:

> #loop data into objects  
> for i in (0…12)  
> #create object array  
> player[i] = Player.new
> 
> player[i].name = “PlayerName#{i}”  
> player[i].card = rand(12) + 1 #Here i don’t know the function for  
> this

You could do something convoluted like this, where you generate a random  
card  
number, then check to see if anybody else has that card, but that isn’t  
really  
the way that card games work.

If you wanted to do it this way, you could do something along the lines  
of:

nobody\_has\_this\_card = true  
begin

new\_card = rand(52)

player.each do |pl|  
if pl.card == new\_card  
nobody\_has\_this\_card = false  
end  
end  
end until nobody\_has\_this\_card

But you shouldn’t do this. This really isn’t how card games work. This  
is  
the equivalent of dealing cards to people by saying:

“ok, choose a random card”  
“3 of diamonds”  
“let me check… no, sorry, one of the players already has that one, try  
again”  
“ok, 7 of spades”  
“let me check… ok, yeah, nobody has that one yet”

What you really want to do is create a deck of cards, shuffle it, then  
start  
handing out cards, removing them from the deck as you go.

The easiest way to do that is to create a list containing all the card  
values  
(there are various ways to do this, but I’ll do a simplified idea):

deck = [“2h”, “3h”, “4h”, “5h”, “2s”, “3s”, “4s”, “5s”, “2d”, “3d”,  
“4d”,  
“5d”, “2c”, “3c”, “4c”, “5c”]

Once you have the deck, you can shuffle it:

deck = deck.sort\_by { rand }

Then hand out cards by popping elements off the list (equivalent to  
taking the  
top card off of a deck of cards)

card = deck.pop

If you use this route, you can ensure that each new player gets a unique  
card,  
and that the deck gets smaller each time.

Then, just like in a real card game, you would need to notice when  
you’re  
running low on cards in the deck, shuffle them, etc.

Ben
