 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 23rd, 2005, 09:38 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
poker sorting
Modify the following code, write a hand-sorting routine. In card game, most players keep their cards sorted by pip value. The routine places ace first, kings next, and to forth, down to tows. A hand is five cards.
public class Card
{
public enum Suit { clubs, diamonds, hearts, spades }
public class Pips
{ // inner class
public void SetPips(int n) { p = n % 13 + 1; }
public int GetPips() { return p; }
private int p; // values 1 to 13
}
public void SetCard(int n)
{ s = (Suit)(n/13); p = new Pips(); p.SetPips(n); }
public Suit GetSuit() { return s; }
public Pips GetPips() { return p; }
private Suit s;
private Pips p;
}// end of Class Card
public class Deck
{
public void SetDeck()
{
d = new Card[52];
for (int i = 0; i < 52; ++i)
{
d[i] = new Card();
d[i].SetCard(i);
}
}
public void Shuffle()
{
Random t = new Random();
for (int i = 0; i < 52; ++i)
{
int k = i + (t.Next() % (52 - i));
Card cTemp = d[i]; // swap cards
d[i] = d[k];
d[k] = cTemp;
}
}
public void Deal(int n, int pos, Card[] hand)
{
for (int i = pos; i < pos + n; ++i)
hand[i - pos] = d[i];
}
private Card[] d;
}
class Flush
{
public static void Main()
{
Card[] oneHand = new Card[7];
Deck dk = new Deck();
int i, j, k, flushCount = 0;
int [] sval= new int[4];
int ndeal= 1000, nc = 7, nhand = 52/nc;
dk.SetDeck();
for (k = 0; k < ndeal; k += nhand)
{
if ((nhand + k) > ndeal)
nhand = ndeal - k;
dk.Shuffle();
for (i = 0; i < nc * nhand; i += nc)
{
for (j = 0; j < 4; ++j)
sval[j] = 0;
dk.Deal(nc, i, oneHand); // deal next hand
for (j = 0; j < nc; ++j)
sval[(int)oneHand[j].GetSuit()]++;
for (j = 0; j < 4; ++j)
if (sval[j] >= 5) // 5 or more is flush
flushCount++;
}
}
Console.WriteLine("In " + ndeal + " " + nc + "-card hands there were " + flushCount + " flushes");
}
}
|
|

April 23rd, 2005, 04:19 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Ok... are you giving away your code or to you have a question about it?
|
|

April 23rd, 2005, 08:04 PM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I need somebody to help me to rewrite the code that I post
|
|

April 26th, 2005, 09:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
yui0329,
I don't know if your teacher let you use other interfaces,I didn't find the meaning of some words in your post in my english dictionary!
anyway when I want to do such things..I do like below,
Code:
public enum Sort { Name, Average, Age, ID }
public class Student:IComparable
{
string m_name=string.Empty;
int m_average=0;
int m_age=0;
int m_id=0;
public Student(string name,int age,int average,int ID)
{
m_name=name;
m_average=average;
m_age=age;
m_id=ID;
}
#region IComparable Members
public int CompareTo(object obj)
{
return m_id.CompareTo(((Student)obj).m_id);
}
#endregion
class comparer:IComparer
{
Sort m_sort;
public comparer(Sort sort)
{
m_sort=sort;
}
#region IComparer Members
public int Compare(object x, object y)
{
Student studentx=(Student)x;
Student studenty=(Student)y;
switch(m_sort)
{
case Sort.Age:
return studentx.m_age.CompareTo(studenty.m_age);
case Sort.Average:
return studentx.m_average.CompareTo(studenty.m_average);
case Sort.ID:
return studentx.m_id.CompareTo(studenty.m_id);
case Sort.Name:
return studentx.m_name.CompareTo(studenty.m_name);
}
return studentx.m_id.CompareTo(studenty.m_id);
}
#endregion
}
public static IComparer SortStudents(Sort sort)
{
return new Student.comparer(sort);
}
public override string ToString()
{
return string.Format("name is {0},age is {1},average is {2},ID is {3}",
m_name,m_age.ToString(),m_average.ToString(),m_id.ToString());
}
}
to use
Code:
private void ToUse()
Code:
{
Student student1=new Student("mehdi",21,0,1);
Student student2=new Student("reza",21,10,2);
Student student3=new Student("armin",17,12,3);
ArrayList students=new ArrayList();
students.Add(student1);
students.Add(student2);
students.Add(student3);
IComparer comparer=Student.SortStudents(Sort.Age);
students.Sort(comparer);
string result=string.Empty;
foreach(Student student in students)
result+=student.ToString()+"%%";
textBox1.Text=result;
}
you see I used some built-in interfaces.it doesn't solve your problem just shows you a way.I hope you get its scenario..
_____________
Mehdi.
software student.
|
|

April 26th, 2005, 09:37 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This question is difficult to understand what it really want, I will check it with my teacher later, anyway, thanks for the contribution, I will post the right answer if I finish it, thank you very much!!
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| sorting |
member |
XSLT |
25 |
July 13th, 2007 08:44 AM |
| Sorting |
sunny76 |
Excel VBA |
2 |
September 19th, 2005 09:31 PM |
| sorting |
kondapally |
Crystal Reports |
2 |
January 21st, 2005 10:51 AM |
| Datagrid sorting by non alphabetical sorting? |
LLAndy |
VS.NET 2002/2003 |
1 |
July 15th, 2004 01:20 AM |
| Sorting? |
pbernardo |
XSLT |
2 |
October 27th, 2003 11:34 AM |
|
 |