Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

enter image description here

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.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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:

getPlayers()        // Get all player info
getCurrentPlayer()  // Get information about the active player in his turn (hand, money to bet with, etc.)
fold()              // Current player folds and current player changes
bet(float amount)   // Current player bets and current player changes

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 convert Game output to JSON, you do it outside of Game since that is not the purpose of Game!

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 would Game 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."

share|improve this answer
    
Aside, that number is anywhere from 3 to 9 depending on the person - I can do 4 or 5 depending on how alert I am. The usual test is to look at a bunch of dots and figure out how many you can perceive individually without grouping them mentally - at 6, I start to automatically group them into 3 + 3, for example, and can't easily separate them again. –  Izkata Jul 31 '13 at 17:04
    
@Izkata That's interesting. I wasn't aware that there was a test for this. Do you have a link? –  Neil Aug 1 '13 at 7:49
1  
en.wikipedia.org/wiki/… is the source, I believe. –  AakashM Aug 1 '13 at 8:33

Where should all that logic go?

Start with putting it in your Game class.

This is a rule based game with a limited number of states. I would model it as a state machine. The state pattern will help you immensely.

Eventually you will want to extract that FSM into separate class(es), but don't worry about that for now.

How should I keep state in a server/client scenario?

This is impossible to answer without knowing more about your setup. Serializing your Game object and states to the database every turn might do the trick.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.