[C#] Project War(The Card Game)

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 33
Reputation: Poab9200 is an unknown quantity at this point 
Solved Threads: 1
Poab9200 Poab9200 is offline Offline
Light Poster

[C#] Project War(The Card Game)

 
0
  #1
Jul 4th, 2009
Hello all, and happy fourth.

First off, I've been trying to create this little app for quite some time now and I've gotten pretty far as the code just "came" to me the other day. So I've gotten... I'd say about 80% done with this project but I am having most likely a huge problem with my WarBattle() Method(which is located in the War class).

Problems:
1. It seems that every time I run the game and the same face value card is drawn for both players they enter the WarBattle stage... Now this part isn't the problem(Enter WarBattle) its that every time I try to burn 3 cards of each players hand/deck it doesn't seem to return those cards of which were burned...

2. Now with the above problem stated I run the game in a while() loop so that I can test if the game will end... Eventually. But thus far I have yet to succede. I tried to run the test but I removed the WarBattle Function. So If the cards drawn were the same the cards were returned to the users deck. I ran this test for about 2 hours and still no end result. So I stopped it and I ran a benchmark test to see how many battles occured in certain time span(3 minutes was the testing length) I ran the test using the System.Diagnostics.StopWatch Namespace. The results came back to show that Approximatly(Averaged) 107.68 Million battles were conducted in that time span. The Diagnostics part of the test along with the removed WarBattle Functions has been opted out of my code so it won't be too confusing.

What I'm asking. Can you(anyone) help me solve the WarBattle() Method or the checks that go into the while() loop in the War Class.

Greatly appreciated!
--- Poab9200, Happy 4th!

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace new_theroy
  6. {
  7. public enum Suit
  8. {
  9. Diamonds, Spades, Clubs, Hearts
  10. }
  11.  
  12. public enum FaceValue
  13. {
  14. Two = 2,
  15. Three = 3,
  16. Four = 4,
  17. Five = 5,
  18. Six = 6,
  19. Seven = 7,
  20. Eight = 8,
  21. Nine = 9,
  22. Ten = 10,
  23. Jack = 11,
  24. Queen = 12,
  25. King = 13,
  26. Ace = 14
  27. }
  28.  
  29. public class Card
  30. {
  31. // Members/Fields
  32. private readonly Suit suit;
  33. private readonly FaceValue faceVal;
  34.  
  35. // Properties
  36. public Suit Suit { get { return suit; } }
  37. public FaceValue FaceVal { get { return faceVal; } }
  38.  
  39. // Constructor
  40. public Card(Suit suit, FaceValue faceVal)
  41. {
  42. this.suit = suit;
  43. this.faceVal = faceVal;
  44. }
  45.  
  46. // Override
  47. public override string ToString()
  48. {
  49. return "The " + faceVal.ToString() + " of " + suit.ToString();
  50. }
  51.  
  52. }
  53.  
  54. public class Deck
  55. {
  56. // Members/Fields
  57. private string sDeck;
  58. protected List<Card> cards = new List<Card>();
  59.  
  60. // Properties
  61. public Card this[int position] { get { return (Card)cards[position]; } }
  62. public int Cards { get { return cards.Count; } }
  63.  
  64. // Constructor
  65. public Deck()
  66. {
  67. foreach (Suit s in Enum.GetValues(typeof(Suit)))
  68. {
  69. foreach (FaceValue f in Enum.GetValues(typeof(FaceValue)))
  70. {
  71. cards.Add(new Card(s, f));
  72. }
  73. }
  74. }
  75.  
  76. // Public Methods
  77. public Card Draw()
  78. {
  79. Card card = cards[0];
  80. cards.RemoveAt(0);
  81.  
  82. return card;
  83. }
  84. public void Shuffle()
  85. {
  86. Random random = new Random();
  87.  
  88. for (int i = 0; i < cards.Count; i++)
  89. {
  90. int index1 = i;
  91. int index2 = random.Next(cards.Count);
  92.  
  93. SwapCard(index1, index2);
  94. }
  95. }
  96. public void Shuffle(int SuffleAmount)
  97. {
  98. if (SuffleAmount == 0)
  99. {
  100. throw new ArgumentException("You must shuffle at least once");
  101. }
  102. else
  103. {
  104. Random random = new Random();
  105.  
  106. for (int n = 0; n != SuffleAmount; n++)
  107. {
  108.  
  109. for (int i = 0; i < cards.Count; i++)
  110. {
  111. int index1 = i;
  112. int index2 = random.Next(cards.Count);
  113.  
  114. SwapCard(index1, index2);
  115. }
  116.  
  117. }
  118. }
  119. }
  120.  
  121. // Private Method
  122. private void SwapCard(int index1, int index2)
  123. {
  124. Card card = cards[index1];
  125. cards[index1] = cards[index2];
  126. cards[index2] = card;
  127. }
  128.  
  129. // Override
  130. public override string ToString()
  131. {
  132. foreach (Card c in cards)
  133. {
  134. sDeck += string.Format("{0}\n", c);
  135. }
  136.  
  137. return sDeck;
  138. }
  139. }
  140.  
  141. public class Hand
  142. {
  143. // Members/Fields
  144. protected List<Card> cards = new List<Card>();
  145. // Properties
  146. public int NumCards { get { return cards.Count; } }
  147. public List<Card> Cards { get { return cards; } }
  148.  
  149. // Public Method
  150. public bool ContainsCard(FaceValue item)
  151. {
  152. foreach (Card c in cards)
  153. {
  154. if (c.FaceVal == item)
  155. {
  156. return true;
  157. }
  158. }
  159. return false;
  160. }
  161. }
  162.  
  163. public class Player
  164. {
  165. // Members/Fields
  166. private string sDeck;
  167. private Deck curDeck;
  168. private List<Card> cards = new List<Card>();
  169.  
  170. // Properties
  171. public Deck CurrentDeck { get { return curDeck; } set { curDeck = value; } }
  172. public int CardCount { get { return cards.Count; } }
  173.  
  174. // Public Methods
  175. public void AddCard(Card c)
  176. {
  177. cards.Add(c);
  178. }
  179. public Card RemCard()
  180. {
  181. Card card = cards[0];
  182. cards.RemoveAt(0);
  183. return card;
  184. }
  185.  
  186. // Override
  187. public override string ToString()
  188. {
  189. foreach (Card c in cards)
  190. {
  191. sDeck += string.Format("{0}\n", c);
  192. }
  193. return sDeck;
  194. }
  195. }
  196.  
  197. public class Deal
  198. {
  199. // Members/Fields
  200. private Deck deck = new Deck();
  201.  
  202. // Properties
  203. public Deck GetDeck { get { return deck; } }
  204.  
  205. // Public Methods
  206. public void DealCards(Player p1, Player p2)
  207. {
  208. deck.Shuffle(10);
  209. for (int i = 0; i != 26; i++)
  210. {
  211. p1.AddCard(deck.Draw());
  212. p2.AddCard(deck.Draw());
  213. }
  214. }
  215. }
  216.  
  217. public class War
  218. {
  219. // Members/Fields
  220. private Player player1 = new Player();
  221. private Player player2 = new Player();
  222. private Deal dealCards = new Deal();
  223. private int BattleCount = 0;
  224. private string sStatus = "";
  225.  
  226. // Constructors
  227. public War()
  228. {
  229. dealCards.DealCards(player1, player2); // Start The Game
  230. }
  231.  
  232. // Public Methods
  233. public void PlayGame()
  234. {
  235. while (player1.CardCount != 0 | player1.CardCount != 52)
  236. {
  237. Battle(player1, player2); // Battle
  238.  
  239. BattleCount++; // Keep count of the number of battles
  240.  
  241. if (sStatus == "!!! War !!!")
  242. {
  243. sStatus = WarBattle(player1, player2); //
  244. }
  245.  
  246. if (player1.CardCount == 0)
  247. Console.WriteLine("Player 2 Wins");
  248. if (player1.CardCount == 52)
  249. Console.WriteLine("Player 1 Wins");
  250. }
  251. }
  252.  
  253. // Private Methods
  254. private string Battle(Player p1, Player p2)
  255. {
  256. Card p1Card = p1.RemCard();
  257. Card p2Card = p2.RemCard();
  258.  
  259. //Console.WriteLine(string.Format("\nPlayer 1 Draws : {0}", p1Card));
  260. //Console.WriteLine(string.Format("Player 2 Draws : {0}", p2Card));
  261.  
  262. if (p1Card.FaceVal > p2Card.FaceVal)
  263. {
  264. p1.AddCard(p1Card);
  265. p1.AddCard(p2Card);
  266. return "Player 1 has won!";
  267. }
  268. else if (p2Card.FaceVal > p1Card.FaceVal)
  269. {
  270. p2.AddCard(p2Card);
  271. p2.AddCard(p1Card);
  272. return "Player 2 has won!";
  273. }
  274. else if (p1Card.FaceVal == p2Card.FaceVal)
  275. {
  276. // Were doing this until I can figure out a way to fix the War Method
  277. p1.AddCard(p1Card);
  278. p2.AddCard(p2Card);
  279. return "!!! War !!!";
  280. }
  281. else
  282. {
  283. return "eRR";
  284. }
  285. }
  286. private string WarBattle(Player p1, Player p2)
  287. {
  288. // Burn both player's cards
  289. Card p1Burn1 = p1.RemCard();
  290. Card p1Burn2 = p1.RemCard();
  291. Card p1Burn3 = p1.RemCard();
  292.  
  293. Card p2Burn1 = p2.RemCard();
  294. Card p2Burn2 = p2.RemCard();
  295. Card p2Burn3 = p2.RemCard();
  296.  
  297. // Draw the 'battle card'
  298. Card p1Card = p1.RemCard();
  299. Card p2Card = p2.RemCard();
  300.  
  301. // Display whats going on
  302. Console.WriteLine(string.Format("\nPlayer 1 Burns 3 Cards and Draws : {0}", p1Card));
  303. Console.WriteLine(string.Format("Player 2 Burns 3 Cards and Draws : {0}", p2Card));
  304.  
  305. if (p1Card.FaceVal > p2Card.FaceVal)
  306. {
  307. // First add the battle cards
  308. p1.AddCard(p1Card);
  309. p1.AddCard(p2Card);
  310. // Then add the burn cards
  311. p1.AddCard(p1Burn1);
  312. p1.AddCard(p1Burn2);
  313. p1.AddCard(p1Burn3);
  314. p1.AddCard(p2Burn1);
  315. p1.AddCard(p2Burn2);
  316. p1.AddCard(p2Burn3);
  317. return "Player 1 has won!";
  318. }
  319. else if (p2Card.FaceVal > p1Card.FaceVal)
  320. {
  321. // First add the battle cards
  322. p2.AddCard(p1Card);
  323. p2.AddCard(p2Card);
  324. // Then add the burn cards
  325. p2.AddCard(p2Burn1);
  326. p2.AddCard(p2Burn2);
  327. p2.AddCard(p2Burn3);
  328. p2.AddCard(p1Burn1);
  329. p2.AddCard(p1Burn2);
  330. p2.AddCard(p1Burn3);
  331. return "Player 2 has won!";
  332. }
  333. else if (p1Card.FaceVal == p2Card.FaceVal)
  334. {
  335. return "!!! War !!!";
  336. }
  337. else
  338. {
  339. return "eRR";
  340. }
  341. }
  342. }
  343.  
  344. class Program
  345. {
  346. static void Main(string[] args)
  347. {
  348. War w = new War();
  349. w.PlayGame();
  350. }
  351. }
  352. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC