CardsGame.cs 7.5 KB

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