From: Ben Giddings Date: 2009-07-14T21:27:30+09:00 Subject: Re: Array function? 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