ARTICLE

Yahtzee Program Using C#: Part II

Posted by Mike Gold Articles | Windows Forms C# January 31, 2002
This is an update of the Yahtzee program for VS 2005. Included in this version is a Game Reset and a High Score Tracker. The Top Ten High Scores are tracked using an Array with sortable components. In this article we will talk about the IComparable interface used to make an object stored in an array sortable.
Reader Level:

yahtzee1.jpg

 

Introduction


This is an update of the Yahtzee program. Included in this version is a Game Reset and a High Score Tracker. The Top Ten High Scores are tracked using an Array with sortable components. In this article we will talk about the IComparable interface used to make an object stored in an array sortable.

The ScorePair class is a class containing two members Player and Score. In order to make this class comparable, we simply add the interface IComparable and add a CompareTo Method as shown below:

public class ScorePair : IComparable
{
public int CompareTo(object a)
{
if (Score.ToInt32() > ((ScorePair)a).Score.ToInt32())
{
return -1;
}
if (Score.ToInt32() < ((ScorePair)a).Score.ToInt32())
{
return 1;
}
return 0;
}
public ScorePair()
{
Name = "";
Score = "0";
}
public ScorePair(string aName, string aScore)
{
Name = aName;
Score = aScore;
}
public string Name;
public string Score;
}

The CompareTo Function returns 1, 0, or -1 based on the comparison of the ScorePair objects whether the object compared is less than, equal to, or greater than the instance. In this case we return -1 if the Score is greater than the object to be compared to, because we want to sort in descending order when we call the sort function, the highest score being first. Below is the initial array of ScorePairs. 

public ScorePair[] ScoreArray = new ScorePair[11]
{
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair(),
new ScorePair()
};

The call to sort is made below using the static Array function Sort:

public void AddHighScore(string name, int score)
{
// put it as the last one, then sort it
ScoreArray[10] = new ScorePair(ThePlayerName, score.ToString());
Array.Sort(ScoreArray);
// sort the array
WriteScoreArray(); // make the scores persist in a text file
}

Elements of an array using the IComparable interface can also use the ArrayList.BinarySearch routine, and other algorithms requiring comparisons within the array.

Login to add your contents and source code to this article
post comment
     

Hi John,

check out the silverlight version at:

http://www.silverlightgamespace.com/Games/SilverDice/SilverlightYahtzeeTestPage.html

It has some animation in the game.  I'm hoping to write an article on this one soon.

Posted by Mike Gold Nov 17, 2010

Mike, any plans for rolling dice graphics? User interactivity with the pointer? Catagory recommendations?

Posted by John Luquer Nov 17, 2010

helllllllllllllllllllllllllllllllllllllllll it is good

Posted by Muhammad Nov 05, 2008

Where is the source code for this article?

Posted by TBermudez Apr 29, 2007
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter