Title: Overview and Discussion of Lab 1
1Overview and Discussion of Lab 1
- Replace blocking event handling with reactive
event handling architecture on the server and the
clients - Still single-threaded, but handles asynchrony
better - Acceptor and connector become event handlers
- Use that architecture STL to play 5 card stud
poker - 52 card standard deck, ordered by a supplied
functor - 5 to 10 players join asynchronously (assume dont
leave) - Design for testing and evolution of your solution
- Identify what may change for other later games
- E.g., number of players, number of cards per
hand, when cards are discarded or added, etc. - Identify what may change for other later
architectures - E.g., with multiple threads, where are the points
of contention?
2Reactive Event Handling
- Suggestion evolve your lab 0 solution to
reactive architecture - Acceptor and connector become event handlers
- Start with just them, and they add additional
handlers - Each connection is managed by a separate service
handler - Imposes a different style of programming (there
is no main) - Sequence of activities
- Dealer waits until at least 5 players have joined
(handshake like lab 0) - Deals cards to all players who are in the game
- Still accepts new player registrations and deals
to them also after starting - All players use (semantically) same functor to
score hands - Print out their hands, as ordered for the purpose
of scoring - Report their scores asynchronously to dealer
- Dealer waits for scores from all registered
players - A form of distributed barrier synchronization
- Once dealer decides all have sent scores, new
registrations are denied - Declares winner (or n-way tie unlikely with a
randomly shuffled deck)
3Card Values, Ranges, Iterators, Functors
- Values of cards 52 combinations of
- 4 Suits S, H, D, C (descending order)
- 13 Ranks A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2
- Different orders may be helpful for different
uses - E.g., ltrank, suitgt vs. ltsuit, rankgt vs. 5 card
hands score - Ranges, iterators, and functors
- Writing enumerations of suit and rank give
options for encoding different sequences within
iterators - E.g., iterating through all cards in the deck
- E.g., iterating in different collating orders
- Iterators may be interchanged with functors
- E.g., predicate for identifying all 4-of-a-kind
hands - E.g., generator for constructing the next
4-of-a-kind hand
4STL Containers for Holding Cards
- Keeping cards in a deck or a hand
- Low overhead of copying card objects
- Card objects are not polymorphic
- Different orders are enforced for different uses
- E.g., rank then suit vs. suit then rank
- Which container(s) should we use, and when?
Suggestion set (or multi-set)
Suggestion vector (or deque)
7S
AD
7D
AH
7S
AD
AC
7D
AH
AC
5STL Containers for Players in a Game
- Keeping players in a game
- Higher overhead than copying card objects
- Not too expensive, but maybe enough to matter
- I.e., name, socket handle, score, etc.
- Order depends on only one field
- players name
- Which container(s) should we use, and when?
Suggestion map or multimap
key players name
item player object
6STL Containers for Orders, other Abstractions
- E.g., persistent order in scoring players hands
- Can use to print hand as well as to score it
- May want to alias existing objects
- Rather than making a (more expensive) deep copy
- Order is (re-)established at run-time
- As cards are dealt and drawn
- Which container(s) should we use, and when?
Suggestion vector (or deque) of pointers to
players or cards
7Scoring 5 Card Stud Poker Game
- 5 cards dealt to each of 5-10 players
- Need to define a scoring functor
- Could use a polynomial function to construct a
unique integer for each hands score - 6 digit base 13 number is one approach
- Bounded by 136 4,826,809 (much less than 232)
- Could use a different abstraction instead
- E.g., a pair pairltHandRank, SortedHandgt
hand rank
card rank
card rank
card rank
card rank
card rank
a
b
c
d
e
f
score a135 b134 c133 d132 e13 f
85 Card Stud Poker, Continued
- Ranks of poker hands (for all poker games well
use) - straight flush gt four-of-a-kind gt full-house gt
flush gt - straight gt three-of-a-kind gt two-pair gt one-pair
gt nothing - Can simply number these 8 down to 0 for value of
a - Then, for each hand set some of b through f to
card ranks, and set the others to zero - For card rank can say ace is 12 down to 2 which
is 0 - E.g., for straight flush a 8, b rank of high
card - E.g., for full house a 6, b the rank of the
triple, and c the rank of the pair
score a135 b134 c133 d132
e13 f
Note you are welcome to choose a different
polynomial, or another ordering mechanism (e.g.,
pair) as long as it ranks hands correctly
hand rank
card rank
card rank
card rank
card rank
card rank
a
b
c
d
e
f
9A Few More Comments
- Design decisions are largely yours to make
- If the specification I gave differs from what you
think should be done, please ask / suggest - If its a reasonable alternative, Ill approve it
- Extra credit (up to 5 points) is available for
writing up your design decisions thoroughly - Please do this as you go so you can remember what
you did and why, and which alternatives you
discarded along the way (document all these) - Please start early so I can be of the most help
in answering questions, discussing ideas, etc.