Click here to Skip to main content
6,597,576 members and growing! (20,034 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Mobile Development » Games     Intermediate

Minesweeper and MinsweeperFlags (a multiplayer version) in one for PocketPC

By George Mamaladze

Pocket PC .NET Compact Framework version of Minesweeper and MinsweeperFlags (multiplayer) game in one
C#, Windows, .NET CF, .NET, MobileVS.NET2003, Dev
Posted:30 May 2004
Views:40,514
Bookmarked:12 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
14 votes for this article.
Popularity: 4.58 Rating: 4.00 out of 5

1
4 votes, 28.6%
2

3
2 votes, 14.3%
4
8 votes, 57.1%
5

Introduction

This is my version of an old, well known game and a contribution to the CodeProject Mobile Developer competition. This is my second .NET Compact Framework based application (The first was of course a HelloWorld.) and this is also my first CodeProject article.

One thing I always missed on my PocketPC was our old sweet Minesweeper. So I have decided to invest a couple evenings to play with Visual Studio .NET. After one week I had a normal single player version.

The second step was a multiplayer modification of this game (like MinsweeperFlags from MSN Messenger). A hot seat version (both players on the same Pocket PC) was ready very soon, but my goal was a game over a Network or an IrDA. I have written the network layer and ... that's where I am staying now.

Game rules

Single player

Everyone knows simple rules of Minesweeper. My single player mode works the same way.

Multiplayer

Because MinesweeperFlags is not so well known as the classical Minesweeper I'd like to say some words about its rules. There are two players Red and Blue one. Game objective is to find and flag mines that are concealed on the board. The first player to flag more than half of the mines wins. Click your left mouse button on a square to see if a mine is there. If a mine is there it will be revealed and your colored flag will be planted on it. You will then get another turn. If the square does not contain a mine, your turn will be over. Each player receives one point for each mine they flagged.

Using the code

In fact the whole Program can be divided into three layers.

  • The first layers is implemented in the Field class. It describes location of mines on the field and a current state of each cell. So this class is only design as data storage structure.

  • The game logic and rules are implemented in SinglePlayerGame and MultiplayerGame classes. These classes are derived from an abstract class GameBase, which implements common behaviour for both game versions. All these classes implement the IGame interface. It makes possible to operate with them from GUI without exactly knowing (in most cases) which version is currently running.

I use events to simplify the intaraction between layers. For example OnCellChanged event will be thrown to notify the presentation layer that content of a specific cell was changed and it must be redrawn.

  public class SinglePlayerGame: GameBase
  {

    public SinglePlayerGame(byte aWidth, byte aHeight, 
     byte aBombCount): base(aWidth, aHeight, aBombCount)
    {
    }

    public override void ClickCell(byte x, byte y)
    {
      // Open a cell and open all neigbors whenn needed. 

      // This all is implemented in our base class.

      base.ClickCell(x,y);

      // The condition to loose, because it is game version secific.

      if (_field.Bombs[x,y]==Field.ISBOMB) GameOver(Player.None);
      // The condition to win, because it is game version secific.

      if (ClosedCount==_field.BombCount ) GameOver(CurrentPlayer);
    }

    // The SetFlag behavior is different in both versions 

    // that is why it must be implemented here.

    public override void SetFlag(byte x, byte y)
    {
      // When cell is already opened do nothing

      if (_field.CellStates[x,y]==CellState.Open) return;
      // remember cell state before changes are made

      CellState BeforeState = _field.CellStates[x,y];
      // Unmark when already marked

      if (_field.CellStates[x,y]==GetFlagColorOfCurrentPlayer()) 
        _field.CellStates[x,y]=CellState.Closed; 
      // Mark when not marked

      else _field.CellStates[x,y]=GetFlagColorOfCurrentPlayer(); 
      // Inform others that this cell was modified.

      CellChanged(x,y, BeforeState, _field.CellStates[x,y]);
    }
  }
  
  • The GamePresentation class is responsible for drawing the game field and the interaction with the main form. The whole user interaction, sound effects and other stuff like that are concentrated in frmMain.
  • There is in fact one more layer (UdpComm class) to implement network communication. In the current version of this program it will be used only in frmStartMultiplayer form to detect existence of a network, and to perform a handshake.

Points of Interest

There are some interesting issues I'd like to share with you.

  • Application Icon on Pocket PC - When a windows application has only one large icon (32 � 32 pixels) and it must be shown small icon view it will be scaled automatically. To my surprise it was not a case with my Pocket PC 2002, it showed only left top 16 � 16 pixel rectangle. That is why I had to create both 32 � 32 and 16 � 16 icons.
  • ImageButton - As you probably know there is no ImageButton in Compact Framework. My initial idea was to present cells as ImageButtons and change image when necessary. I found a Microsoft article How to Create a Microsoft .NET Compact Framework-based Image Button and used their implementation of ImageButton. To increase the performance by drawing the field I switched to manual drawing using Graphics. This way it works considerably faster.
  • Threading in Compact Framework - To simplify the development I wrote and tested almost all modules (except GUI of course) in a Windows application. Then I have transferred this code into Mobile application. In most cases it worked without any changes, but for instance I have discovered that Thread in Compact Framework has for instance no IsAlive property and no Abort method. So when programming Compact Framework always think about it's limitations.
  • Moreover there are number of useful things you can find in the source code. For example working with embedded resources or playing a WAV file asynchronously. To my surprise the 2002 emulator plays normal system sounds but not my WAVs. On the other hand 2003 emulator does both.

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

George Mamaladze


Member

Occupation: Software Developer (Senior)
Location: Germany Germany

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 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralI hate to be such a nitpick PinmemberJörgen Sigvardsson10:35 31 May '04  
GeneralRe: I hate to be such a nitpick PinmemberGeorge Mamaladze12:37 31 May '04  
GeneralRe: I hate to be such a nitpick PinmemberVladimir Ralev20:02 31 May '04  

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 George Mamaladze
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project