I can not come up with a good architecture for my card game. I need help to understand how games usually are designed.
First, I will describe game rules.
Game Rules
Setup
- There are four players, each two player form a team.
- Each player gets 12 shuffled cards
- There are 4 blinded cards on table(river)
- Players order are like this
Betting
- Each player can pass or select a number greater than current bet between 100 to 160
- Betting starts from first player and circles until a team pass
- Once a player pass they can't bet anymore
- The team that wins the betting round should at least collect points equal to their bet in order to win the game
-
The team that lost the betting round should not allow the their team achieve their goal
-
If the team that won betting round gets all the points the other team will get negative points equal to their bet
- if the team that lost the betting round collects all the points the other team will get double negative points
Game flow and collecting points
- Player that won betting round (the king) gets four remaining cards on the table.
- Then she/he can save a set of four cards in their team cards bank without even playing them.
- The king will pick a suit as the ruler suit and let others know that
- King starts the game by putting a card from his/her hand on table. Each other player should play in this order
- if they have the same suit of that card in their hand, they have to play one of those cards
- if they don't have it, they can play any other suit
- After all other players played their hands, winner of the round will be:
- The one who has highest card if all cards are the same
- The one who has highest "ruler" card if there is any
- Winner of the round will collect cards and put it in their bank
- Player who won previous round will start the next round
- This will continue until everybody's hand is empty
Counting points
- Winning each round has 5 points. This means every 4 cards has at least 5 points.
- Having Ace, 10 or 5 in bank each adds 5 points
My Design
Classes
class Card {
string suit;
string rank
}
class Deck {
List cards = [];
List suits = ['S', 'H', 'D', 'C'];
List ranks = ['1', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
private void init(){..}
void shuffle(){...}
}
class Game{
Deck deck = new Deck();
Player player1;
Player player2;
Player player3;
Player player4;
int _isOn = 0;
Game(this.player1, this.player2, this.player3, this.player4){
deck.makeCards();
deck.shuffle();
start();
}
void start(){
player1.cards.addAll( deck.cards.getRange(0, 12) );
player2.cards.addAll( deck.cards.getRange(12, 24) );
player3.cards.addAll( deck.cards.getRange(24, 36) );
player4.cards.addAll( deck.cards.getRange(36, 48) );
deck.cards.removeRange(0, 48);
}
String toJson(){
}
String toString(){
}
}
class Player{
String name;
int points;
List cards = [];
Player(this.name, {this.points});
String toJson(){}
String toString(){}
}
My Problem
Now that I've defined all these classes I don't know how to bind this definitions to a database (like Mongo) and control the game flow.
- Where should all that logic go?
- How should I keep state in a server/client scenario?
Note:
I'm using Dart to program this but I don't necessary want answers be written in Dart.
Best Answer
You're making a classic mistake here. Someone told you to make a web application for a card game, and you're trying to determine how to make all of it at once. This approach confuses even the best programmers, since studies have shown that you can only retain 7 pieces of information readily available in your mind at one time. Trying to juggle with more requires intense concentration and you cannot expect to keep that up.
Rather, try to focus more on writing a card game library like someone were going to call your library in order to play your card game. You seem to have an excellent start on your model. That is good, but you need to make
Game
the interface by which callers can use make moves and so forth.So I would expect Game would have several new methods like:
In short, everything you need to know in order to run the game, you can do so using your object
Game
. Only once you have this do you proceed to step 2.Write a separate class whose sole purpose is to receive incoming requests, apply them to
Game
and send the output back to the user. If you want to convertGame
output to JSON, you do it outside ofGame
since that is not the purpose ofGame
!In order to persist this information, you could create another class which provides an interface to MongoDB. This class would not know of the existence of
Game
and neither wouldGame
know of the existence of this class.In short, focus on the individual components, and you'll be fine. You start to go wrong when you're trying to take too many aspects into consideration when writing your program. As a wise professor once told me, "Know and embrace the fact that the first program you write for a project will be rewritten in its entirety by the end."