CardsGame.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Cardsgame.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace CardsFramework
  17. {
  18. /// <summary>
  19. /// A cards-game handler
  20. /// </summary>
  21. /// <remarks>
  22. /// Use a singleton of a class that derives from class to empower a cards-game, while making sure
  23. /// to call the various methods in order to allow the implementing instance to run the game.
  24. /// </remarks>
  25. public abstract class CardsGame
  26. {
  27. #region Fields and Properties
  28. protected List<GameRule> rules;
  29. protected List<Player> players;
  30. protected CardPacket dealer;
  31. public int MinimumPlayers { get; protected set; }
  32. public int MaximumPlayers { get; protected set; }
  33. public string Theme { get; protected set; }
  34. protected internal Dictionary<string, Texture2D> cardsAssets;
  35. public GameTable GameTable { get; protected set; }
  36. public SpriteFont Font { get; set; }
  37. public SpriteBatch SpriteBatch { get; set; }
  38. public Game Game { get; set; }
  39. #endregion
  40. #region Initializations
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="CardsGame"/> class.
  43. /// </summary>
  44. /// <param name="decks">The amount of decks in the game.</param>
  45. /// <param name="jokersInDeck">The amount of jokers in each deck.</param>
  46. /// <param name="suits">The suits which will appear in each deck. Multiple
  47. /// values can be supplied using flags.</param>
  48. /// <param name="cardValues">The card values which will appear in each deck.
  49. /// Multiple values can be supplied using flags.</param>
  50. /// <param name="minimumPlayers">The minimal amount of players
  51. /// for the game.</param>
  52. /// <param name="maximumPlayers">The maximal amount of players
  53. /// for the game.</param>
  54. /// <param name="tableBounds">The table bounds.</param>
  55. /// <param name="dealerPosition">The dealer position.</param>
  56. /// <param name="placeOrder">A function which translates a player's index to
  57. /// his rendering location on the game table.</param>
  58. /// <param name="theme">The name of the theme to use for the
  59. /// game's assets.</param>
  60. /// <param name="game">The associated game object.</param>
  61. public CardsGame(int decks, int jokersInDeck, CardSuit suits, CardValue cardValues,
  62. int minimumPlayers, int maximumPlayers, GameTable gameTable, string theme, Game game)
  63. {
  64. rules = new List<GameRule>();
  65. players = new List<Player>();
  66. dealer = new CardPacket(decks, jokersInDeck, suits, cardValues);
  67. Game = game;
  68. MinimumPlayers = minimumPlayers;
  69. MaximumPlayers = maximumPlayers;
  70. this.Theme = theme;
  71. cardsAssets = new Dictionary<string, Texture2D>();
  72. GameTable = gameTable;
  73. GameTable.DrawOrder = -10000;
  74. game.Components.Add(GameTable);
  75. }
  76. #endregion
  77. #region Virtual Methods
  78. /// <summary>
  79. /// Checks which of the game's rules need to be fired.
  80. /// </summary>
  81. public virtual void CheckRules()
  82. {
  83. for (int ruleIndex = 0; ruleIndex < rules.Count; ruleIndex++)
  84. {
  85. rules[ruleIndex].Check();
  86. }
  87. }
  88. /// <summary>
  89. /// Returns a card's value in the scope of the game.
  90. /// </summary>
  91. /// <param name="card">The card for which to return the value.</param>
  92. /// <returns>The card's value.</returns>
  93. public virtual int CardValue(TraditionalCard card)
  94. {
  95. switch (card.Value)
  96. {
  97. case CardsFramework.CardValue.Ace:
  98. return 1;
  99. case CardsFramework.CardValue.Two:
  100. return 2;
  101. case CardsFramework.CardValue.Three:
  102. return 3;
  103. case CardsFramework.CardValue.Four:
  104. return 4;
  105. case CardsFramework.CardValue.Five:
  106. return 5;
  107. case CardsFramework.CardValue.Six:
  108. return 6;
  109. case CardsFramework.CardValue.Seven:
  110. return 7;
  111. case CardsFramework.CardValue.Eight:
  112. return 8;
  113. case CardsFramework.CardValue.Nine:
  114. return 9;
  115. case CardsFramework.CardValue.Ten:
  116. return 10;
  117. case CardsFramework.CardValue.Jack:
  118. return 11;
  119. case CardsFramework.CardValue.Queen:
  120. return 12;
  121. case CardsFramework.CardValue.King:
  122. return 13;
  123. default:
  124. throw new ArgumentException("Ambigous card value");
  125. }
  126. }
  127. #endregion
  128. #region Abstract Methods
  129. /// <summary>
  130. /// Adds a player to the game.
  131. /// </summary>
  132. /// <param name="player">The player to add to the game.</param>
  133. public abstract void AddPlayer(Player player);
  134. /// <summary>
  135. /// Gets the player who is currently taking his turn.
  136. /// </summary>
  137. /// <returns>The currently active player.</returns>
  138. public abstract Player GetCurrentPlayer();
  139. /// <summary>
  140. /// Deals cards to the participating players.
  141. /// </summary>
  142. public abstract void Deal();
  143. /// <summary>
  144. /// Initializes the game lets the players start playing.
  145. /// </summary>
  146. public abstract void StartPlaying();
  147. #endregion
  148. #region Loading
  149. /// <summary>
  150. /// Load the basic contents for a card game (the card assets)
  151. /// </summary>
  152. public void LoadContent()
  153. {
  154. SpriteBatch = new SpriteBatch(Game.GraphicsDevice);
  155. // Initialize a full deck
  156. CardPacket fullDeck = new CardPacket(1, 2, CardSuit.AllSuits,
  157. CardsFramework.CardValue.NonJokers | CardsFramework.CardValue.Jokers);
  158. string assetName;
  159. // Load all card assets
  160. for (int cardIndex = 0; cardIndex < 54; cardIndex++)
  161. {
  162. assetName = UIUtilty.GetCardAssetName(fullDeck[cardIndex]);
  163. LoadUITexture("Cards", assetName);
  164. }
  165. // Load card back picture
  166. LoadUITexture("Cards", "CardBack_" + Theme);
  167. // Load the game's font
  168. Font = Game.Content.Load<SpriteFont>(string.Format(@"Fonts\Regular"));
  169. GameTable.Initialize();
  170. }
  171. /// <summary>
  172. /// Loads the UI textures for the game, taking the theme into account.
  173. /// </summary>
  174. /// <param name="folder">The asset's folder under the theme folder. For example,
  175. /// for an asset belonging to the "Fish" theme and which sits in
  176. /// "Images\Fish\Beverages\Soda" folder under the content project, use
  177. /// "Beverages\Soda" as this argument's value.</param>
  178. /// <param name="assetName">The name of the asset.</param>
  179. public void LoadUITexture(string folder, string assetName)
  180. {
  181. cardsAssets.Add(assetName,
  182. Game.Content.Load<Texture2D>(string.Format(@"Images\{0}\{1}",
  183. folder, assetName)));
  184. }
  185. #endregion
  186. }
  187. }