Voglio espandere il mio portafoglio di progetti personali, quindi ho deciso di creare un gioco di carte. Per essere più precisi, si chiama Macau. Ho letto questo answer su StackOverflow e ho provato a seguire i passaggi dalla prima risposta. Ho realizzato una narrativa in cui descrivo le regole generali del gioco e le sue particolarità.
Macao game. The minimum number of players is 2. The deck of cards initially contains 52 cards. Each card has the following attributes:
- Suit (diamonds, clubs, hearts and spades)
- Rank (2, 3, 4, 5, 6, 7, 8, 9, A, J, Q, K, Joker)
Starting the game, the deck is shuffled and every player receives 5 cards from it. The first player that remains out of cards is the winner. Afterwards, a card is taken from the deck and put on the table with the face up.
The current player looks at the card that's been put on the table and decides whether he can put a card from his packet on the table or not. A player can put a card on the table if their card is compatible with the other one.
Two cards are compatible if they accomplish one of the following conditions: they have the same rank or the same suit. Expanding upon the cards domain, there exist several special cards. These have to obey the specified rules too, but they have an extra attribute: the special ability. They may have any suit, but these ranks qualify a card to be special: 2, 3, 4, 7, A, and Joker. The Joker is a wildcard as it doesn’t need to obey to the earlier specified rule. That is, the player can put it on the table at any time.
The next section gives a thorough description of the mentioned cards’ special abilities:
Rank: 7. When put on the table, this card requires the current player to specify a suit. The next player is obliged to put any card that has the earlier specified suit, otherwise he will receive a new card from the deck.
Rank: 2 and 3. This card obliges the next player to receive 2/3 (2 cards if the rank is 2, same rule for 3) cards from the deck. Now, if the next player also has a 2 or a 3 with the same suit, or a Joker, then he can put it on the table and the next player has to receive the total amount of cards. The same rules apply in a circular manner. If the next player has a 4 with the same suit, then he can stop the obligation injected by the previous player.
Rank: 4. Special ability: it can stop an obligation injected by the previous player. If it is put over a regular card (i.e. a card that has no special ability), then it acts like one.
- Rank: A (Ace). Special ability: it skips the next player's turn.
- Rank: Joker. It is a wildcard so it can be put on the table at almost all instances. The exception arises when the card that is on the top of the table is an A (Ace), then the current player's turn is skipped. Special ability: the same as the 2/3 card, except that the next player will have to receive 5 cards (for the Black Joker) or 10 (in case of the Red one).
Having the rules clarified, the game continues. If the current player owns such a compatible card (or more), then it's his choice whether to put one on the table or receive a new card from the deck. Also, while in game, there can appear the situation when the deck would be emptied. In that case, all the cards from the table that are located under the top one are taken, shuffled, and used to refill the deck. The card on the top is untouched.
Now it's the next player's turn, and the earlier described steps repeat until one of the players remains out of cards. That player is the winner.
Da questa narrazione ho estratto le seguenti classi:
- Scheda
- Deck
- Player (forse Hand?)
- Tabella (che contiene il mazzo e la pila di carte già giocate)
- gioco
Ecco i miei problemi.
- Non sono sicuro di aver fatto le giuste astrazioni.
- Non saprei come progettare le carte speciali. Dovrebbe esistere una classe separata (ereditata dalla carta) per ogni carta speciale? O dovrei usare la composizione? Inoltre, come devo implementare le carte Joker? Non hanno un completo, hanno solo il grado.
- La classe di gioco dovrebbe gestire i turni dei giocatori e i casi eccezionali (quei casi in cui la prima carta sarebbe stata speciale)?
- Dovrebbe Player essere solo un'interfaccia? Ciò lascerebbe spazio per funzionalità aggiuntive come un giocatore di IA (che implementa il Player).
Questo è il mio primo progetto orientato agli oggetti di medie dimensioni e voglio essere sicuro di essere sulla strada giusta.