CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2010
    Posts
    3

    Angry Need Help!!! Creating a Rummikub console app.

    I just started to write this program. Right now I have 2 main classes- class Rummikub which contains a pool of 106 tiles which I declared as a private data member- string tiles[106]. I also have another class, class Hand. Hand will need to maintain a dynamic "array" of some sort like a vector which will increase and/or decrease in size. The "hand" is the player's current hand of tiles that they have. This "Hand" class contains as private data members "vector<string> vecHand" and "vector<string>::iterator it" as the iterator.

    I'm needing this string vector to be able to access the private data member of class Rummikub, which is string tiles[106]. I know I can do this by declaring a friend of the class. I'm having a hard time with this and would appreciate any feedback on how to do this, not necessarily using the "friend" idea. I'm not that familiar with "friends" so I may have totally messed that up in the code below. Anything anyone has is appreciated. Any ideas on how to accomplish this would be terrific. Below are snippets of my code. Thanks a bunch for the help!!

    class Rummikub {
    friend class Hand; //class Rummikub is friends with class Hand
    public:
    ~Rummikub();
    Rummikub(); //constructor sets and shuffles the tiles
    string itos(int); //custom function
    private:
    string tiles[106];
    };

    class Hand {
    friend class Rummikub; //class Hand is friends with class Rummikub
    public:
    Hand();
    void checkForMoves();
    void dropTile();
    string accessTile();
    private:
    vector<string> vecHand;
    vector<string>::iterator it;
    };
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Need Help!!! Creating a Rummikub console app.

    1. If you are posting code, please always use code tags and ensure your code is properly indented. Check the FAQ for details.

    2. It would help if you'd outline the rules of the game you are trying to implement. I (for instance) am not familiar with it.

    3. You declared a class Rummikub. What for? (this is not a rhetorical question!)
    What does an object of the class represent in your game logic? Is it the deck containing all tiles? Is it all tiles that have not been dealt to the players yet? Is it the tiles lying on the table? Based on the answer, you have to think what operations should be allowed on the Rummikub object? Assuming that the object represents the stack of cards not yet dealt, you should define a function draw() which returns the next (random) tile (and internally marks it as dealt or deletes it).

    4. You use std::string to represent your tiles. This will make your code very difficult, e.g. assuming that tiles have some numeric value, how do you get this from the string? How do you determine if two tiles "match" or if one tile is "higher" than the other. Think of a class to represent your tile first.

    5. Your file naming is very weird. Normal file naming would be rummikub.h rummikub.cpp, hand.h, hand.cpp and main.cpp. I don't understand what you are trying to achieve with your naming.

    6. Good luck in continuing.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jan 2010
    Posts
    3

    Re: Need Help!!! Creating a Rummikub console app.

    Hi, and thanks for replying... I will be sure to indent my code. Sorry about that. Class Rummikub is the base class for the game. At the moment, all it does is create and shuffle the 106 tiles. I'm wanting it to do more, but at the moment that's not my priority. You had mentioned that I should have a draw() function in class Rummikub... my thought process was that when the code instantiates an object of class Hand, Hand will maintain the player's hand. When the object Hand is constructed, the initial "draw" would occur then- so I was thinking that this "draw()" function should be in class Hand, and it would "draw" from the tiles already part of the Rummikub object. I may be making this too complicated, sorry if I am.

    And to answer question number 4, I use std::string to represent the tiles for one reason. The tiles have a color associated with it. For example, tiles[0] = "13blue". tiles[1] = "4red". There may be a more efficient way to attack this, but that's what I thought of at the moment. I agree with you in that it would be harder to track and maintain- I would have to do some intense string manipulation to search for/do whatever on the string array of tiles. Maybe I could map those to an array of ints and use the ints as my operating array. Below are the rules of the game. The most difficult part of this project is developing an algorithm that will scan the pool of tiles and look at your hand and check for the best possible moves. I'm not at the part yet, which is why I didn't address that.

    And here are the rules of the game:

    Object of the game
    To be the first player to place all the tiles from your rack onto the table.

    Setup
    Select the room in which you wish to play and find an empty seat. You will notice a countdown at the bottom of the screen notifying you when the next game will begin.
    Once a game begins, the system will issue each player with a tile – the person with the highest numbered tile begins. The system will then issue each player with their 14 tiles; the remaining tiles become the 'pool'. The player can sort their tiles on their rack by colour or number– that is, into sets of "groups" (colour) or "runs" (numbers).

    Sets
    A "group" is a set of three or four tiles of the same number, but each in a different colour.

    A "run" is a set of three or more consecutive numbers which are of the same colour.

    Note: The number 1 can only be played as the lowest number and cannot follow number 13.

    Playing the game
    Players make sets and lay them on the table – this is called a 'meld'. The first set must add up to at least 30 points (Add up the tile values) – this is called the "initial meld". If unable to make an initial meld, or a player chooses to delay, they must click the 'get tile' button and pick up and additional tile from the pool. This concludes the players turn.
    Each player must lay down an initial meld, one cannot manipulate (add or take tiles away from an existing set) a set before laying down an initial meld.
    Players have a time limit of 60, 90, 120 seconds (as decided by room owner) per turn. If a player hasn’t completed their move before their time is up, they must replace all tiles to their previous position and pick up three tiles from the pool as a penalty.

    Manipulation
    Manipulating is the most exciting part of the Rummikub game as players try to lay down as many tiles as possible by rearranging or adding to existing sets.
    Below are examples of how sets can be manipulated.
    Note: After a round of manipulations – all sets must be 'legal' and there should be no loose tiles left on the table.

    Examples of manipulations.

    1. Adding tiles to an existing set:

    2. Removing a fourth tile from a set and using it in a new one:

    3. Adding a fourth tile to a set, removing another tile from that same set to create a new set.

    4. Splitting a run

    5. Combined split

    6. Multiple split

    The Joker
    A joker can be retrieved from a set on the table by any player who can replace it with another tile of the same numerical and colour value it represents. The tile used to replace the joker can come from the table or from the players rack. In the case of a group of three – the joker can represent either of the missing colours (there are four colours in the game). Once a player replaces a joker, they must use it in the same turn as part of a set. Sets containing jokers can be split and manipulated like regular sets.

    Notes:
    A joker cannot be retrieved before a player has laid down their initial meld.
    If a joker remains on a players rack at the end of the game, its penalty value is 30 points.

    The Winner
    The winner is the first person who empties their rack of tiles. The system will then calculate the score of each player.

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Need Help!!! Creating a Rummikub console app.

    Quote Originally Posted by mikemarks@live.com View Post
    Hi, and thanks for replying... I will be sure to indent my code. Sorry about that. Class Rummikub is the base class for the game. At the moment, all it does is create and shuffle the 106 tiles.
    So it maintains the 106 tiles. Another way to express it would be that the Rummiklub object owns the tiles.
    Quote Originally Posted by mikemarks@live.com View Post
    I'm wanting it to do more, but at the moment that's not my priority.
    The first priority is to choose a design that makes sense.
    Quote Originally Posted by mikemarks@live.com View Post
    You had mentioned that I should have a draw() function in class Rummikub... my thought process was that when the code instantiates an object of class Hand, Hand will maintain the player's hand. When the object Hand is constructed, the initial "draw" would occur then- so I was thinking that this "draw()" function should be in class Hand, and it would "draw" from the tiles already part of the Rummikub object.
    Does each hand know which tiles are still available to be drawn? It cannot, because it does not own all tiles. The process of drawing should be initiated by the hand object (you are correct there), but the decision which tile to give must be done by the tile owner.

    To give you an example in code:
    Code:
    class Tile { ... }
    
    class Deck {
    private:
      static const unsigned int tilecount = 106;
      Tile tiles[tilecount];
      unsigned int drawn;
    public:
      Deck() {
        // fill tiles array
        // shuffle array
        drawn = 0; // no tiles drawn yet
      }
      const Tile& draw() {
        if (drawn == tilecount) throw NoMoreTilesInDeckException;
        return tiles[drawn++];
      }
    }
    
    Deck deck; // Global deck object to keep example simple
    
    class Hand {
    private:
      vector<Tile> tiles;
    public:
      Hand() {
        //Fill Hand with initial tiles
        for (unsigned int i = 0; i < 14; ++i) {
          tiles.push_back(deck.draw());
        }    
    }
    
    int main() {
      Hand hand[4];
      // Each hand object will have 14 unique tiles. 
    }
    Quote Originally Posted by mikemarks@live.com View Post
    And to answer question number 4, I use std::string to represent the tiles for one reason. The tiles have a color associated with it. For example, tiles[0] = "13blue". tiles[1] = "4red". There may be a more efficient way to attack this, but that's what I thought of at the moment.
    So a tile has a color (an enum) and a number. This is quite easy to implement and if you overload operator<< you can have it output 13blue or 4red just if they were strings.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Need Help!!! Creating a Rummikub console app.

    Slightly OT.

    Sounds very much like a card game I used to play for money in east end snooker halls, kalookie.
    When you have the code for rummikub sorted out, it would take little expansion and you would also have kalookie too.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  6. #6
    Join Date
    Jan 2010
    Posts
    3

    Re: Need Help!!! Creating a Rummikub console app.

    Thank you very much treuss. Your help is greatly appreciated. I will heed your advice and see what I can come up with.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured