开发者

Data Structure -- representation of cards

开发者 https://www.devze.com 2023-04-05 03:42 出处:网络
This is a data structure/mapping question. I\'m using MSSQL, with .NET and EF (and MVC if that\'s important). I have a table that represents a deck of cards. The states of a card are:

This is a data structure/mapping question. I'm using MSSQL, with .NET and EF (and MVC if that's important). I have a table that represents a deck of cards. The states of a card are:

  1. Face down in the deck
  2. Face up in the deck (discarded)
  3. In front of a player
  4. In a player's hand

...and there can be X players. I have a table that represents players as well, with their own unique key. Assume that each player is in a single game, and one deck of cards is in one game.

At first, I thought there would be a one-to-many relationship between players and cards, enforced by a foreign key in the database. Player 1P has card 1C, 2C, and 4C, so cards 1C, 2C, 4C and "1P" under the PlayerID. Then there is a bit field to represent if the card was face up or face down. That works for state 3 and state 4.

How should I handle state 1 and 2? Some options:

  1. Make the PlayerID on the Card table nullable. When I was using EF, I was running into foreign key constraints. Edit: I was running into foreign key constraints, but when I tried it now, it looks like it's working as one would expect.
  2. Creating a dummy player called "Deck", and assign all cards in state 1 and 2 to this player. But, this didn't seem elegant; the Deck player had a lot of other baggage that I didn't want to deal with, and if I started doing multiple games, I'd need multiple Deck players.
  3. Scrap the foreign key in the database, and make the PlayerID nullable. Enforce the constraint in the code. But then I can't do things like Player.Cards() without some extra extension code.
  4. Have two more bit fields: "IsInDeck" and "IsDiscarded" (or some field that represents multiple states, like an int that is 0: in deck; 1: in hand; 2: in front of player; 3: discarded). That way, we don't really care what the PlayerID is if the card is in the "Discarded" state.
  5. Some other option I haven't thought 开发者_如何学编程of.

Ideas? Thanks in advance.


You could try a schema like this:

Data Structure -- representation of cards

The tables PLAYER, CARD, and DECK are hopefully pretty clear.

LOCATION_TYPE is a list of the kinds of locations that might apply. This would include things like "in a player's hand", "in front of a player", "face down in the deck" and "discard pile". You could use a physical table for LOCATION_TYPE or you could use an enum. The advantage of a table is that you could include some business rules like whether the location type requires a PLAYER FK in CARD_LOCATION and whether the card is visible or invisible (face up/down).

CARD_LOCATION is therefore the intersection that tells you where each card is at any given time. Your Player.Cards navigation property will work well as will your Card.Location navigation property. It is important to note that the FK from CARD_LOCATION to PLAYER is optional.

0

精彩评论

暂无评论...
验证码 换一张
取 消