Click here to Skip to main content
6,630,586 members and growing! (18,610 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » Games     Intermediate

PocketDice

By Mike Ellison

Variation of the dice game Yacht for PocketPC
C#, Windows, .NET CF, .NET, MobileVS.NET2003, Dev
Posted:30 May 2004
Views:29,997
Bookmarked:15 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.14 Rating: 3.83 out of 5
1 vote, 8.3%
1
2 votes, 16.7%
2
3 votes, 25.0%
3
2 votes, 16.7%
4
4 votes, 33.3%
5

Introduction

I enjoy having some simple, small games on my PDA to offer a few minutes of distraction when needed. PocketDice, a variant of the dice game Yacht, is offered here as an entry for the Mobile Development Competition and to offer PocketPC owners a few free diversionary minutes. Code examples for persisting high scores and displaying online help are presented following a description of the game.

Playing the Game

PocketDice is a variant on the dice game Yacht with the twist that the dice values are suited like playing cards (clubs, hearts, diamonds, and spades). On each turn the player is allowed up to three rolls of five dice. On a second or third roll, the player may re-roll any or all of the dice. Clicking the Roll button rolls all "unlocked" dice. Tapping a die toggles whether the die is locked or unlocked.

Following any of the three rolls, the player may choose to score by tapping any available scoring box. A scoring box may be used only once in a game. If following a third roll, the player must choose an available scoring box. Points are awarded for a score according to the following table:

Scoring Option

Description

Aces

Score one point for each Ace

Twos

Score two points for each Two

Threes

Score three points for each Three

Fours

Score four points for each Four

Fives

Score five points for each Five

Sixes

Score six points for each Six

3 of a Kind

Score the sum of the five dice values if at least three dice share the same value; if not, score 0

4 of a Kind

Score the sum of the five dice values if at least four dice share the same value; if not, score 0

Full House

Score 25 if three of the dice share a value (i.e. 3 of a Kind) and the other two share a value (i.e. a pair)

Straight

Score 40 if the five dice represent a straight in poker; in this variant, an Ace may be treated as low or high, so possible straights include A-2-3-4-5, 2-3-4-5-6, and 3-4-5-6-A

Flush

Score 40 if the five dice share the same suit; otherwise score 0

Chance

Score the sum of the five dice values.

Reds

Score the sum of the five dice values provided they all share a red suit; otherwise score 0

Blacks

Score the sum of the five dice values provided they all share a black suit; otherwise score 0

5 of a Kind

Score 50 points if all five dice show the same value; if not, score 0

Straight Flush

Score 80 if the five dice share the same suit and form an A-2-3-4-5, 2-3-4-5-6, or 3-4-5-6-A straight; score 0 otherwise

In addition to the points above, a Bonus value of 35 points is added if the sum of the Aces through Sixes scores total 63 or more. When scoring, an Ace is considered to have a value of one.

The game is over when all scoring boxes have been used.

Code Structure

The project, targeting the .NET Compact Framework, includes four form classes and several other supporting classes.

Forms and Control Classes

The following forms comprise the user interface for the application:

frmMain

Contains the application entry point and functions as the game playing surface.

frmHighScores

Dialog for displaying recorded high scores.

frmAbout

Application about box; uses the reflection method Assembly.GetName() for displaying version information.

frmInputBox

Dialog for requesting text entry from the user.

Supporting the main form are two custom control classes: SingleDie and ScoringBox. Each overrides OnPaint() for its custom display.

Application Data

High Scores for the application are persisted in an XML file through functions in the Data class. The Data constructor attempts to load the scores from the persisted file with an XmlTextReader; if the file doesn't exist, a blank score array is created.

public class Data
{
    const int kMAX_HIGHSCORES = 10;
    const string kDATA_FILE = @"\Program Files\PocketDice\data.xml";

    private HighScoreRecord[] _list;
    
    . . .

    public Data()
    {

        // check if a high scores data file already exists

        // if not, use a blank score array and we'll

        // save it later

        _list = new HighScoreRecord[kMAX_HIGHSCORES];
        int count = -1;
        XmlTextReader xml = null;
        FileStream stream = null;

        try
        {
            stream = new FileStream(kDATA_FILE, FileMode.Open);
            xml = new XmlTextReader(stream);
            xml.MoveToContent();
            
            while (xml.Read())
            {
                switch (xml.Name)
                {
                    case "HighScore": 
                        count++;
                        if (count < kMAX_HIGHSCORES)
                        {
                            _list[count] = new HighScoreRecord(xml);
                        }
                        break;
                }
            }

            count++;
            if (count < kMAX_HIGHSCORES)
            {
                for (int i=count; i<kMAX_HIGHSCORES; i++)
                {
                    _list[i] = new HighScoreRecord();
                }
            }
            
        }
        catch (System.IO.FileNotFoundException)
        {
            // file isn't here; initialize the structure for later saving

            for (int i=0; i<kMAX_HIGHSCORES; i++)
            {
                _list[i] = new HighScoreRecord();
            }
        }

        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(
                ex.ToString(), "Error Loading High Scores");
            for (int i=0; i<kMAX_HIGHSCORES; i++)
            {
                _list[i] = new HighScoreRecord();
            }
        }

        finally
        {
            if (xml != null) xml.Close();
            if (stream != null) stream.Close();
        }

    }
    
    . . .
    
}

The HighScoreRecord class, also found in the source file Data.cs, contains a constructor which takes the XmlTextReader as a parameter.

  public HighScoreRecord(XmlTextReader xml)
  {
    // read attributes given this XmlTextReader

    _score = int.Parse(xml.GetAttribute("score"));
    _name = xml.GetAttribute("name");
    _date = XmlConvert.ToDateTime(xml.GetAttribute("date"), "yyyy-MM-dd");
  }

A Data object is created in the Load event handler for frmMain. Its Save() method is then called in frmMain's Closing event handler, persisting the scores with an XmlTextWriter:

public class Data
{
    . . .
    
    public void Save()
    {

        // before saving, sort the HighScores array

        SortHighScores();

        // now attempt to save;

        XmlTextWriter xml = null;

        try
        {
            xml = new XmlTextWriter(kDATA_FILE, System.Text.Encoding.ASCII);
            xml.WriteStartDocument();

            // write the HighScore list

            xml.WriteStartElement("HighScores");
            for (int i=0; i<_list.Length; i++)
            {
                _list[i].WriteXmlRecord(xml);
            }

            xml.WriteEndElement();  // </HighScores>


            xml.WriteEndDocument();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.ToString());
        }
        finally
        {
            if (xml != null) xml.Close();
        }

    }

}

Called in the Save() method, the HighScoreRecord object's WriteXmlRecord() method takes care of persisting its high score properties:

public class HighScoreRecord
{
    ...
    public void WriteXmlRecord(XmlTextWriter xml)
    {
        xml.WriteStartElement("HighScore");
        
        xml.WriteStartAttribute("score","");
            xml.WriteString(_score.ToString());
        xml.WriteEndAttribute();

        xml.WriteStartAttribute("name","");
            xml.WriteString(_name);
        xml.WriteEndAttribute();

        xml.WriteStartAttribute("date", "");
            xml.WriteString(_date.ToString("yyyy-MM-dd"));
        xml.WriteEndAttribute();

        xml.WriteEndElement();
    }
    
}

Points of Interest

When considering how to display online help, I decided that I wanted some formatting and elected to use an HTML file. The article Launching Applications and Resuming Processes [^], from the .NET Compact Framework QuickStart Tutorial [^], was very useful in offering a means for launching a process. I added a function OpenWebPage() and wrapped it in the Launcher class of the project. This is used to open the HTML file containing game instructions.

Summary

PocketDice, a variant of the dice game Yacht for PocketPC, provides a simple diversion, taking only a few minutes to play a full game. Code samples in the article demonstrate the use of XmlTextReader and XmlTextWriter objects for persisting game data in an XML file, and code borrowed from the .NET Compact Framework QuickStart Tutorial to launch processes offers a solution for displaying an HTML file for online help. Enjoy the game!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Mike Ellison


Member
I work for the University of Nevada, Las Vegas in the Office of Institutional Analysis and Planning. Among other things, our office is charged with the mission of deriving useful information in support of administrative decision-making from institutional data. Within the context of that mission, my office mates and I apply technology in the form of custom data processing applications, data extraction and analysis tools, reporting tools, relational databases, OLAP solutions, data warehousing, and data mining.

Visit my blog at MishaInTheCloud.com


Location: United States United States

Other popular Mobile Development articles:

  • Writing Your Own GPS Applications: Part 2
    In part two of the series, the author of "GPS.NET" teaches developers how to write GPS applications suitable for the real world by mastering GPS precision concepts. Source code includes a working NMEA interpreter and sample high-precision application in C# and VB.NET.
  • Writing Your Own GPS Applications: Part I
    What is it that GPS applications need to be good enough to use for in-car navigation? Also, how does the process of interpreting GPS data actually work? In this three-part series, I will cover both topics and give you the skills you need to write a commercial-grade GPS application.
  • Learn How to Find GPS Location on Any SmartPhone, and Then Make it Relevant
    A step by step tutorial for getting GPS from any SmartPhone, even without GPS built in, and then making location useful.
  • iPhone UI in Windows Mobile
    It's an interface that works with transparency effects. As a sample I used an interface just like the iPhone one. In this tutorial I am explaining how simple is working with transparency on Windows Mobile.
  • Pocket 1945 - A C# .NET CF Shooter
    An article on Pocket PC game development
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralHigh Scores form - OK button and Name field PinmemberGraham Downs0:29 28 Feb '08  
GeneralRe: High Scores form - OK button and Name field PinmemberMike Ellison6:19 28 Feb '08  
GeneralCool & well done ! PinmemberSDP13:02 6 Oct '05  
GeneralRe: Cool & well done ! PinmemberMike Ellison13:22 6 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 May 2004
Editor: Nishant Sivakumar
Copyright 2004 by Mike Ellison
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project