Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Script question - arrays/hashes

So, I thought a nice place to start learning about arrays would be a poker system for 2-4 players. So, I started thinking along these lines:

Code:
  def game_start(players)
    hands[]
    i = 0
    j = 0
    while i < players
      while j < 4
        hands[i] = 
        j++
      end
    i++
    end
  end

But now I'm stuck. Don't think I can really go by this particular method. I'd like to have it so that hands_0 would be the array of [2A, 10C, 5B, 6B, 11D], or I might need a hash instead of {2 => A} or something like that. But I'm not sure how to get only an amount of hands as players. I'm sure the process is similar to the character stats, but I just can't seem to figure out the connections there.
 
Sorry, I just used the ++ thing as a symbolizing thing. I'm really trying to figure out how to put this correctly. I see it all in my head, but it's difficult for me to find a way to say it. But I need cards to be held in an array per person, but I'm not sure how to do that with a variable amount of players (the other players would just be AI). I think about how they have actor.hp and stuff. So that for the actor id, it looks up the hp. I guess what I'm trying to say is that for the player's hand id I'd want to have an array for the cards in that hand, and be able to deal cards to only the amount of hands as there are players.

Does that make any more sense than my first post?
 
not so sure what you need but here goes:

some_hash = { 1 => { 1 => [5,1], 2 => [11,3] },...}

in this format: { PlayerID => {1 => [CardNum,Suit], 2 =>[CardNum,Suit]},....}

then you do something like:
Code:
@player = some_hash[1]
@card1 = @player[1]
@card2 = @player[2]
@card1_num = @card1[0]
@card1_suit = @card1[1]
@card2_num = @card2[0]
@card2_suit = @card2[1]

then you do stuff from there? >__<
 

OS

Sponsor

Yes, nesting is allowed, but don't make hands that way! You should create a Hash that holds arrays like this:

Code:
class Game_Hand
  def initialize
    #Hash of Card Suites and each set of the suites values:
    CARDS = { #suite => [values]
    #Hearts
    'H' => [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
    #Diamonds
    'D' => [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
    #Clubs
    'C' => [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
    #Spades
    'S' => [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
    }
    @hand = []
    @max = 5 #MAximum amount of Cards in hand at start
    create_hand(@hand)
  end
  
  def create_hand(hand)
    for i in 0..@max - 1
      if @hand[i] == nil
        random_suite = rand(3)
        random_val = (rand(CARDS[random_suite].size))
        @hand[i] = [random_suite, random_val]
      end
    end
  end
end

This creates the Card Data (which can also be made inside a Game_Deck script instead) and randomly chooses a suite and a value in that suite. Now you just need to add methods for returning the hand and manipulating the hand (i.e. lose/gain cards, etc). Hope this is helpfull!

Peace Out!

~Broken

EDIT: Forgot to mention, in case you didn't know, you can create new hands by doing this:
Code:
player1_hand = Game_Hand.new

Sorry if you already knew this! Peace!
 
Thanks! I'm just trying to work a particular type of system, so it's been heck trying to make sure everything works together (and a large part has to deal with trying to name my cards). But that does help out a lot!

Trickster;140783 said:
http://www.rmxp.org/forums/showthread.php?t=11707

You know It does make me a bit sad to not see anyone not taking advantage of this.

I know. And no offense, I just wanted to figure out how to script the entire thing myself. Partially pride, and partially because it's the best way for me to learn. Just following texts don't do as well as prodding everything and trying to figure out what everything is.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top