Click here to Skip to main content
11,570,906 members (60,005 online)
Click here to Skip to main content
Add your own
alternative version

Checkers for Pocket PC using a Recursive Min-Max Tree

, 19 Jun 2008 CPOL 24.4K 1.6K 20
Checkers/Draughts game for Pocket PC, using the recursive Min-Max tree to calculate moves, coded with C# over the .NET Compact Framework
checkers20_cab.zip
GamesPackage
GamesPackage.csdproj
GamesPackage.csdproj.user
GamesPackage.csproj.user
GamesPackage.resharperoptions
GamesPackage.suo
SoundPPCLibrary
SoundPPCLibrary.csdproj
SoundPPCLibrary.csdproj.user
SoundPPCLibrary.csproj.user
SoundPPCLibrary.resharperoptions
SoundPPCLibrary.suo
BoardEngine
BoardEngine.csdproj
BoardEngine.csdproj.user
BoardEngine.csproj.user
Checkers
001_select_checker_0.wav
002_select_destination_0.wav
003_player_move_0.wav
004_eat_a_player_0.wav
005_end_of_the_board_0.wav
006_game_over_0.wav
007_win_game_0.wav
CHECKER.ico
Checkers.csdproj
Checkers.csdproj.user
Checkers.csproj.user
Checkers.resharper.user
Checkers.resharperoptions
Checkers.suo
checkersico.ico
Thumbs.db
checkers_source.zip
Checkers.csdproj
Checkers.csdproj.user
Checkers.csproj.user
checkersico.ico
001_select_checker_0.wav
002_select_destination_0.wav
003_player_move_0.wav
004_eat_a_player_0.wav
005_end_of_the_board_0.wav
006_game_over_0.wav
007_win_game_0.wav
CHECKER.ico
using System;
using System.IO;


namespace SoundPPCLibray
{
	public class MyWavStreamReader : MySoundReader
	{
		#region Members

		Stream myStream;
		int myDataLen;
		int mDataOffset;

		#endregion

		#region Constructor

		public MyWavStreamReader(Stream stream)
		{
			myStream = stream;
			myDataLen = 0;

			if (!CheckWavHeader())
			{
				Dispose();
				throw new ArgumentOutOfRangeException("Stream is not a WAV PCM mono 8 bit 11.025 kHz");
			}
		}

		#endregion

		#region public bool CheckWavHeader()

		public bool CheckWavHeader()
		{
			if (myStream == null)
				return false;

			if (myStream.Length < MyWaveOut.WF_OFFSET_DATA)
				return false;

			myStream.Seek(0, SeekOrigin.Begin);


			uint riff_typ = ReadUint();
			uint riff_len = ReadUint();

			if (riff_typ != 0x46464952) // "RIFF"
				return false;
			if (riff_len > myStream.Length - 8) // not possible...
				return false;


			uint wave_typ = ReadUint();
			uint fmt_typ = ReadUint();
			uint fmt_len = ReadUint();

			if (wave_typ != 0x45564157)
				return false;
			if (fmt_typ != 0x20746d66)
				return false;
			if (fmt_len != 0x10)
				return false;

			MyWaveOut.MyWAVEFORMATEX wfe = new MyWaveOut.MyWAVEFORMATEX();

			wfe.wFormatTag = ReadUshort();
			wfe.nChannels = ReadUshort();
			wfe.nSamplesPerSec = ReadUint();
			wfe.nAvgBytesPerSec = ReadUint();
			wfe.nBlockAlign = ReadUshort();
			wfe.wBitsPerSample = ReadUshort();


			if (wfe.wFormatTag != MyWaveOut.WAVE_FORMAT_PCM
				|| wfe.nChannels != 1
				|| wfe.nSamplesPerSec != 11025
				|| wfe.wBitsPerSample != 8
				|| wfe.nBlockAlign != (wfe.nChannels*wfe.wBitsPerSample/8)
				|| wfe.nAvgBytesPerSec != (wfe.nSamplesPerSec*wfe.nBlockAlign))
			{
				return false;
			}


			uint data_typ = ReadUint();

			if (data_typ != 0x61746164)
				return false;

			myDataLen = (int) ReadUint();
			mDataOffset = (int) myStream.Position;

			return myDataLen > 0;
		}

		#endregion

		#region public int Size()

		public int Size()
		{
			return myDataLen;
		}

		#endregion

		#region public bool Read(byte[] destination, int byte_offset, int byte_size)

		public bool Read(byte[] destination, int byte_offset, int byte_size)
		{
			if (myStream != null)
			{
				int nb_read = 0;
				try
				{
					myStream.Seek(byte_offset + mDataOffset, SeekOrigin.Begin);
					nb_read = myStream.Read(destination, 0, byte_size);
				}
				catch (Exception ex)
				{
					System.Diagnostics.Debug.Fail(ex.Message);
				}

				return nb_read == byte_size;
			}

			return false;
		}

		#endregion

		#region public void Dispose()

		public void Dispose()
		{
			myStream.Close();
			myStream = null;
		}

		#endregion

		#region private uint ReadUint()

		private uint ReadUint()
		{
			if (myStream == null)
				return 0;

			uint a = (uint) myStream.ReadByte();
			uint b = (uint) myStream.ReadByte();
			uint c = (uint) myStream.ReadByte();
			uint d = (uint) myStream.ReadByte();

			return a | (b << 8) | (c << 16) | (d << 24);
		}

		#endregion

		#region private ushort ReadUshort()

		private ushort ReadUshort()
		{
			if (myStream == null)
				return 0;

			ushort a = (ushort) myStream.ReadByte();
			ushort b = (ushort) myStream.ReadByte();

			return (ushort) (a | (b << 8));
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Leonardo Paneque
Architect
United States United States
Leonardo loves to code with C# on any platform and OS.
He has a Master degree in Computer Sciences and likes to share code and ideas.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 20 Jun 2008
Article Copyright 2008 by Leonardo Paneque
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid