   c# noob shooting for card game
  hey there i'm trying to get a card game foundation & for now i have very little but i'd like to show it so that yall can tell me if doing it all wrong for now the deck class is functionless it's just a placeholder to be enumerable/enumerator of cards later on & i'm trying to make the whole logic non ui aware so i can 1st make a console app & then evolve it to a UI enables app.using System;using System.Collections;using System.Collections.Generic;using System.Text;using System.IO;using MyCardGame;namespace ConsoleApplication1{    class Program    {   static void Main(string[] args)   {  deck CurrentDeck = new deck();  HandOfCards P1Hand = new HandOfCards(CurrentDeck, 0 , 9 );  HandOfCards P2Hand = new HandOfCards(CurrentDeck, 10, 19);  IEnumerator P1HandEnumerator = P1Hand.m_Hand.GetEnumerator();  IEnumerator P2HandEnumerator = P2Hand.m_Hand.GetEnumerator();  for (int i = 0; i < 10; i++)  { P1HandEnumerator.MoveNext(); P2HandEnumerator.MoveNext(); Console.WriteLine(((card)P1HandEnumerator.Current).fight((card)P2HandEnumerator.Current));  }  Console.Read();   }    }}namespace MyCardGame{    enum CardColor{   heart   = 1,   spade   = 2,   diamond = 3,   club    = 4    }    class card    {   public static string[] ColorNames ={ "error", "heart", "spade", "diamond", "club" };   public static string[] StrenghtNames ={"error", "two" ,"three" ,"four" ,"five" ,"six" ,"seven" ,  "eight", "nine", "ten", "jack", "queen", "king", "ace"};   public card(CardColor color ,int strenght)   {  m_Color = color;  m_Strenght = strenght;  m_CardId = ((int)m_Color * m_Strenght - 1);  m_ColorString = ColorNames[((int)m_Color)];  m_StrenghtString = StrenghtNames[strenght];   }   public card(int CardId)   {  if (CardId < 13) m_Color = CardColor.spade;  else if (CardId < 26) m_Color = CardColor.heart;  else if (CardId < 39) m_Color = CardColor.club;  else if (CardId < 52) m_Color = CardColor.diamond;  m_ColorString = ColorNames[((int)m_Color)];  int m_CardId = (CardId+1) % 13;  int temp = (m_CardId % 13);  m_Strenght = temp;  if (m_Strenght == 0) m_Strenght = 13;  m_StrenghtString = StrenghtNames[m_Strenght];   }   CardColor m_Color;   string m_ColorString;   string m_StrenghtString;   int m_Strenght;   int m_CardId;   public void show()   {  Console.WriteLine(m_ColorString);  Console.WriteLine(m_StrenghtString);   }   public string fight(card EnnemyCard)   {  if (this.m_Strenght > EnnemyCard.m_Strenght) return "win";  else if (this.m_Strenght < EnnemyCard.m_Strenght) return "loose";  else if (this.m_Strenght == EnnemyCard.m_Strenght) return "even";  return "error";   }    }    class deck:IEnumerable    {   public deck()   {  CardList = new card[52];  for (int i = 0; i < 52; i++)  { CardList = new card(i);  } shuffle();   }   protected void shuffle()   {  card[] TempCardList = new card[52];  bool[] Used = new bool[52];  for (int i = 0; i < 52; i++)  { Used = false;  }    Random rnd = new Random((int)DateTime.Now.Ticks);  int Current = 0;  int RndNum = 0;  while (Current < 52)  { RndNum = rnd.Next(0,52); if (!Used[RndNum]) {TempCardList[Current] = CardList[RndNum];Used[RndNum] = true;Current++; }  }  for (int i = 0; i < 52; i++) CardList = TempCardList;   }   public IEnumerator GetEnumerator()   {  return new DeckEnumerator(this);   }   protected card[] CardList;   class DeckEnumerator : IEnumerator   {  public DeckEnumerator(deck TheDeck)  { m_EnumeratedDeck = TheDeck; index = -1;  }  deck m_EnumeratedDeck;  int index;  public object Current  { get {return m_EnumeratedDeck.CardList[index]; }  }  public bool MoveNext()  { index++; if (index >= 52) {return false; } return true;  }  public void Reset()  { index = -1;  }   }    }    class BatailleDeck : deck    {   public void fight()   {  int p1=0;  int p2=0;  for (int i = 0; i < 51; i++)  { Console.WriteLine("player 1"); CardList.show(); Console.WriteLine("player 2"); CardList.show(); <span class="cpp-keyword">if</span> (CardList<span style="font-weight:bold;">.fight(CardList) == <span class="cpp-literal">"win"</span>) {p1++; } <span class="cpp-keyword">else</span> <span class="cpp-keyword">if</span> (CardList<span style="font-weight:bold;">.fight(CardList) == <span class="cpp-literal">"loose"</span>) {p2++; } Console.WriteLine(<span class="cpp-literal">"end of round"</span>);  }  <span class="cpp-keyword">if</span>(p1&gt;p2)  { Console.WriteLine(<span class="cpp-literal">"player 1 wins"</span>);  }  <span class="cpp-keyword">if</span>(p1&lt;p2)  { Console.WriteLine(<span class="cpp-literal">"player 2 wins"</span>);  }  <span class="cpp-keyword">else</span>  { Console.WriteLine(<span class="cpp-literal">"even"</span>);  }   }    }    <span class="cpp-keyword">class</span> HandOfCards    {   <span class="cpp-keyword">public</span> HandOfCards(deck CurrentDeck, <span class="cpp-keyword">int</span> start, <span class="cpp-keyword">int</span> end)   {  m_Hand = <span class="cpp-keyword">new</span> LinkedList&lt;card&gt;();  IEnumerator CurrentDeckEnumerator=CurrentDeck.GetEnumerator();  <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i=<span class="cpp-number">0</span> ; i&lt;=start;i++)  { CurrentDeckEnumerator.MoveNext();  }  <span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> i = start; i &lt;= end; i++)  { m_Hand.AddLast((card)CurrentDeckEnumerator.Current); CurrentDeckEnumerator.MoveNext();  }  CurrentDeckEnumerator.Reset();   }   <span class="cpp-keyword">public</span> LinkedList&lt;card&gt; m_Hand;    }}</pre></div><!–ENDSCRIPT–>i kinda know where to go next & what needs done so really just wondering if there are things that are totally wrong yet that i should redo before i go any furtheredit: last addition the hand class did it with a linked list hope it was the right way (i still need to make it ienumerable/ienumerator too)<!–EDIT–><span class=editedby><!–/EDIT–>[Edited by - ranakor on August 4, 2006 7:30:02 PM]<!–EDIT–></span><!–/EDIT–>
  Look's fine I think you could probably change it around, perhaps methods like placard could be added which would return the card and delete it from the deck since its in play. I would also avoid using magic numbers and convert them to constants. For example 52 is the number of cards correct? Why not just name it CARDS_PER_DECK or something. Using magic numbers is usually bad practice, this is because it less readable. [smile]   ;  bump edited it with some new stuff will keep editing it as i progress   ;  ok am kinda stuck with what i'd like to do next (i would like to randomize the card order in the deck but it'd be pretty complicated to do with a simple randomizer as i'd have to do another card array & keep track of wich one has been assigned a place in the new array & wich hasn't )are there any functions that allow to have an array of say int of an arbitraty size & randomize the numbers in there so i could do something like this?card[] tempdeck=new card[52];randomizablearray myrand= new randomizeablearray(52);for (int i=0;i<52;i++)tempdeck=CardList[myrand];or is there maybe some interface i could add to my classes to make them useable with some shuffles function? i'm still a total noob to the whole .net framework;  It's easier to: *randomize two integers a and b, swap cards a and b repeat from * until you think it's enough.Cheers;Quote:Original post by ernowIt's easier to: *randomize two integers a and b, swap cards a and b repeat from * until you think it's enough.Cheersdidn't think of that but you're right that's quite a lot simpler than what i thought off & i'll prolly go for this for now (was kinda hoping there was something built in in .net to shuffle arrays so that i'd get some decent randomizing tho i assume it'll take a lot of shuffling for the deck to look random... on the other hand i assume it's damn fast so maybe i can do it a few million times & hope for it to take a few seconds?)   ;  bump added some 0player game lol (the computer plays alone & display the results the deck is shuffled then p1 gets a card & p2 the next till the deck is empty whoever won each round (strongest card) the most time wins the gamesame question as always is there anything wrong with how i did it?   ;  changed a few thing & added the hand class 1st time i use lists still looking for some feedback on the design before it actually gets big & i realise i have to redo it all p   ;  The shuffling part is perhaps a bit tricky. In the end you're randomizing until you hit the single card that hasn't been picked yet...Why not like this:(Pseudo code)while(elapsed_time < 5 seconds){  int c1 = Rnd.Next()  int c2 = Rnd.Next()  card1 = deck[c1]  deck[c1] = deck[c2]  deck[c2] = card1}Why is the Hand a linkedlist?What is your naming standard for public, private, members and fields? Perhaps you should consider to use the Microsoft standard... Methods, events and public properties use PascalCasingCheers   ;  #include <algorithm>...std::random_shuffle()Is better as it chooses randomly from all possible permutations.  Plus, it's done for you!EDIT: Funny I just assume you're using c++ when you explicitly say you aren't.  Still there has to be something similar.   