Click here to Skip to main content
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
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.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using BoardEngine;
using GamesPackage;
using SoundPPCLibray;

namespace Checkers
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ImageList BoardCells;
		private System.Windows.Forms.ImageList WhiteImages;
		private System.Windows.Forms.ImageList BlackImages;
		private System.Windows.Forms.MainMenu mainMenu1;

		CheckersBoard MyBoard = new CheckersBoard();
		private System.Windows.Forms.Button MoveButton;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.PictureBox BoardPicturebox;

	    BoardDrawer bdrawer ;
		int CellHeight = 0;
		int CellWidth = 0;
		int BoardWidth  = 240;
		int BoardHeight = 240;
		int leftMargin = 0;
		int topMargin = 0;
		private System.Windows.Forms.MainMenu mainMenu2;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem RestartMenuItem;
		private System.Windows.Forms.MenuItem CloseMenuItem;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.MenuItem EasyMenuItem;
		private System.Windows.Forms.MenuItem ModerateMenuItem;
		private System.Windows.Forms.MenuItem HardMenuItem;
		private System.Windows.Forms.MenuItem HumanVSHumanMenuItem;
		private System.Windows.Forms.MenuItem HumanVSComputerMenuItem;
		private System.Windows.Forms.MenuItem ComputerVSHumanMenuItem;
		private System.Windows.Forms.MenuItem HowToplayMenuItem;
		Rectangle rect ;

		//*Rayhand
		public static SoundStorage SoundS;
		private System.Windows.Forms.ImageList redImgList;
		private System.Windows.Forms.ImageList yellowImgList;
		private System.Windows.Forms.ImageList blueImgList;
		private System.Windows.Forms.ImageList greenImgList;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.MenuItem menuItem11;
		private System.Windows.Forms.MenuItem menuItem12;
		private System.Windows.Forms.MenuItem menuItem13;
		private System.Windows.Forms.MenuItem menuItem14;
		private System.Windows.Forms.MenuItem menuItem15;
		private System.Windows.Forms.MenuItem menuItem16;
		private System.Windows.Forms.MenuItem menuItem17;
        private MenuItem menuItem19;
        private MenuItem RulesMenuItem;
        private MenuItem GeniusMenuItem;
		public MySound currentSound;
		//*End

		ImageList ResaveImageList (ImageList list)
		{
			Bitmap b=null;
			Graphics gr;
			Rectangle r = new Rectangle (0,0,30,30);
			System.Drawing.Imaging.ImageAttributes at = new System.Drawing.Imaging.ImageAttributes();
			
			ImageList temp = new ImageList ();
			for (int i=0; i<list.Images.Count; i++)
			{
				b = new Bitmap (list.Images[i].Width, list.Images[i].Height);
				gr = Graphics.FromImage (b);
				gr.DrawImage (list.Images[i], 0,0);
				gr.Dispose ();
				temp.Images.Add (b);
			}
			temp.ImageSize = list.ImageSize;
			b.Dispose(); 
			return temp;
		}

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			rect = new Rectangle(leftMargin,topMargin,BoardWidth,BoardHeight);
			
			CellWidth  =  30;//((BoardWidth)/8);
			CellHeight =  30;//((BoardHeight)/8);

			redImgList = ResaveImageList (redImgList);
			yellowImgList = ResaveImageList (yellowImgList);
			greenImgList = ResaveImageList (greenImgList);
			blueImgList = ResaveImageList (blueImgList);

			WhiteImages = yellowImgList;
			BlackImages = blueImgList;


			//*Rayhand
			//this.Text = GC.GetTotalMemory(false).ToString(); 
			GC.Collect();
			//*End
		
			//*Rayhand
			InitSounds();
			//*End

			//*Rayhand
			this.MinimizeBox = false;
			//*Ends
		}

		//*Rayhand
		#region private void InitSounds()
		private void InitSounds()
		{
			SoundS = new SoundStorage();
			
			SoundS.AddSound("selectChecker","001_select_checker_0.wav",false);
			SoundS.AddSound("selectDestination","002_select_destination_0.wav",false);
			SoundS.AddSound("move","003_player_move_0.wav",false);
			SoundS.AddSound("eat","004_eat_a_player_0.wav",false);
			SoundS.AddSound("queen","005_end_of_the_board_0.wav",false);
			SoundS.AddSound("lost","006_game_over_0.wav",false);
			SoundS.AddSound("win","007_win_game_0.wav",false);
			
			SoundStorage.SoundPlayer = new SoundPlayer();
		}
		#endregion
		//*Ends
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
			//*Rayhand
			SoundS.Dispose();
			SoundStorage.SoundPlayer.Dispose();
			//*End
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.BoardCells = new System.Windows.Forms.ImageList();
            this.WhiteImages = new System.Windows.Forms.ImageList();
            this.BlackImages = new System.Windows.Forms.ImageList();
            this.MoveButton = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.BoardPicturebox = new System.Windows.Forms.PictureBox();
            this.mainMenu2 = new System.Windows.Forms.MainMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.RestartMenuItem = new System.Windows.Forms.MenuItem();
            this.HumanVSHumanMenuItem = new System.Windows.Forms.MenuItem();
            this.HumanVSComputerMenuItem = new System.Windows.Forms.MenuItem();
            this.ComputerVSHumanMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItem17 = new System.Windows.Forms.MenuItem();
            this.menuItem16 = new System.Windows.Forms.MenuItem();
            this.CloseMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItem8 = new System.Windows.Forms.MenuItem();
            this.EasyMenuItem = new System.Windows.Forms.MenuItem();
            this.ModerateMenuItem = new System.Windows.Forms.MenuItem();
            this.HardMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItem7 = new System.Windows.Forms.MenuItem();
            this.menuItem9 = new System.Windows.Forms.MenuItem();
            this.menuItem10 = new System.Windows.Forms.MenuItem();
            this.menuItem11 = new System.Windows.Forms.MenuItem();
            this.menuItem6 = new System.Windows.Forms.MenuItem();
            this.menuItem12 = new System.Windows.Forms.MenuItem();
            this.menuItem13 = new System.Windows.Forms.MenuItem();
            this.menuItem14 = new System.Windows.Forms.MenuItem();
            this.menuItem15 = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.HowToplayMenuItem = new System.Windows.Forms.MenuItem();
            this.redImgList = new System.Windows.Forms.ImageList();
            this.yellowImgList = new System.Windows.Forms.ImageList();
            this.blueImgList = new System.Windows.Forms.ImageList();
            this.greenImgList = new System.Windows.Forms.ImageList();
            this.RulesMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItem19 = new System.Windows.Forms.MenuItem();
            this.GeniusMenuItem = new System.Windows.Forms.MenuItem();
            this.SuspendLayout();
            // 
            // BoardCells
            // 
            this.BoardCells.ImageSize = new System.Drawing.Size(32, 32);
            this.BoardCells.Images.Clear();
            this.BoardCells.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
            this.BoardCells.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
            // 
            // WhiteImages
            // 
            this.WhiteImages.ImageSize = new System.Drawing.Size(30, 30);
            // 
            // BlackImages
            // 
            this.BlackImages.ImageSize = new System.Drawing.Size(30, 30);
            // 
            // MoveButton
            // 
            this.MoveButton.Location = new System.Drawing.Point(16, 245);
            this.MoveButton.Name = "MoveButton";
            this.MoveButton.Size = new System.Drawing.Size(72, 20);
            this.MoveButton.TabIndex = 2;
            this.MoveButton.Text = "Move!";
            this.MoveButton.Click += new System.EventHandler(this.MoveButton_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(144, 245);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(72, 20);
            this.button2.TabIndex = 1;
            this.button2.Text = "Clear";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // BoardPicturebox
            // 
            this.BoardPicturebox.Location = new System.Drawing.Point(0, 0);
            this.BoardPicturebox.Name = "BoardPicturebox";
            this.BoardPicturebox.Size = new System.Drawing.Size(240, 240);
            // 
            // mainMenu2
            // 
            this.mainMenu2.MenuItems.Add(this.menuItem1);
            this.mainMenu2.MenuItems.Add(this.menuItem8);
            this.mainMenu2.MenuItems.Add(this.menuItem2);
            this.mainMenu2.MenuItems.Add(this.menuItem4);
            // 
            // menuItem1
            // 
            this.menuItem1.MenuItems.Add(this.RestartMenuItem);
            this.menuItem1.MenuItems.Add(this.menuItem17);
            this.menuItem1.MenuItems.Add(this.menuItem19);
            this.menuItem1.MenuItems.Add(this.RulesMenuItem);
            this.menuItem1.MenuItems.Add(this.menuItem16);
            this.menuItem1.MenuItems.Add(this.CloseMenuItem);
            this.menuItem1.Text = "Game";
            // 
            // RestartMenuItem
            // 
            this.RestartMenuItem.MenuItems.Add(this.HumanVSHumanMenuItem);
            this.RestartMenuItem.MenuItems.Add(this.HumanVSComputerMenuItem);
            this.RestartMenuItem.MenuItems.Add(this.ComputerVSHumanMenuItem);
            this.RestartMenuItem.Text = "New Game";
            // 
            // HumanVSHumanMenuItem
            // 
            this.HumanVSHumanMenuItem.Text = "Human vs Human";
            this.HumanVSHumanMenuItem.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // HumanVSComputerMenuItem
            // 
            this.HumanVSComputerMenuItem.Text = "Human vs Computer";
            this.HumanVSComputerMenuItem.Click += new System.EventHandler(this.menuItem3_Click);
            // 
            // ComputerVSHumanMenuItem
            // 
            this.ComputerVSHumanMenuItem.Text = "Computer vs Human";
            this.ComputerVSHumanMenuItem.Click += new System.EventHandler(this.menuItem6_Click);
            // 
            // menuItem17
            // 
            this.menuItem17.Text = "Mute";
            this.menuItem17.Click += new System.EventHandler(this.menuItem17_Click);
            // 
            // menuItem16
            // 
            this.menuItem16.Text = "-";
            // 
            // CloseMenuItem
            // 
            this.CloseMenuItem.Text = "Exit";
            this.CloseMenuItem.Click += new System.EventHandler(this.CloseMenuItem_Click);
            // 
            // menuItem8
            // 
            this.menuItem8.MenuItems.Add(this.EasyMenuItem);
            this.menuItem8.MenuItems.Add(this.ModerateMenuItem);
            this.menuItem8.MenuItems.Add(this.HardMenuItem);
            this.menuItem8.MenuItems.Add(this.GeniusMenuItem);
            this.menuItem8.Text = "Level";
            // 
            // EasyMenuItem
            // 
            this.EasyMenuItem.Checked = true;
            this.EasyMenuItem.Text = "Easy";
            this.EasyMenuItem.Click += new System.EventHandler(this.menuItem9_Click);
            // 
            // ModerateMenuItem
            // 
            this.ModerateMenuItem.Text = "Moderate";
            this.ModerateMenuItem.Click += new System.EventHandler(this.menuItem10_Click);
            // 
            // HardMenuItem
            // 
            this.HardMenuItem.Text = "Hard";
            this.HardMenuItem.Click += new System.EventHandler(this.menuItem11_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.MenuItems.Add(this.menuItem3);
            this.menuItem2.MenuItems.Add(this.menuItem6);
            this.menuItem2.Text = "Colors";
            // 
            // menuItem3
            // 
            this.menuItem3.MenuItems.Add(this.menuItem7);
            this.menuItem3.MenuItems.Add(this.menuItem9);
            this.menuItem3.MenuItems.Add(this.menuItem10);
            this.menuItem3.MenuItems.Add(this.menuItem11);
            this.menuItem3.Text = "Player 1";
            // 
            // menuItem7
            // 
            this.menuItem7.Text = "Red";
            this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click_1);
            // 
            // menuItem9
            // 
            this.menuItem9.Text = "Green";
            this.menuItem9.Click += new System.EventHandler(this.menuItem7_Click_1);
            // 
            // menuItem10
            // 
            this.menuItem10.Text = "Blue";
            this.menuItem10.Click += new System.EventHandler(this.menuItem7_Click_1);
            // 
            // menuItem11
            // 
            this.menuItem11.Checked = true;
            this.menuItem11.Text = "Yellow";
            this.menuItem11.Click += new System.EventHandler(this.menuItem7_Click_1);
            // 
            // menuItem6
            // 
            this.menuItem6.MenuItems.Add(this.menuItem12);
            this.menuItem6.MenuItems.Add(this.menuItem13);
            this.menuItem6.MenuItems.Add(this.menuItem14);
            this.menuItem6.MenuItems.Add(this.menuItem15);
            this.menuItem6.Text = "Player 2";
            // 
            // menuItem12
            // 
            this.menuItem12.Text = "Red";
            this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
            // 
            // menuItem13
            // 
            this.menuItem13.Text = "Green";
            this.menuItem13.Click += new System.EventHandler(this.menuItem12_Click);
            // 
            // menuItem14
            // 
            this.menuItem14.Checked = true;
            this.menuItem14.Text = "Blue";
            this.menuItem14.Click += new System.EventHandler(this.menuItem12_Click);
            // 
            // menuItem15
            // 
            this.menuItem15.Text = "Yellow";
            this.menuItem15.Click += new System.EventHandler(this.menuItem12_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.MenuItems.Add(this.menuItem5);
            this.menuItem4.MenuItems.Add(this.HowToplayMenuItem);
            this.menuItem4.Text = "Help";
            // 
            // menuItem5
            // 
            this.menuItem5.Text = "About";
            this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
            // 
            // HowToplayMenuItem
            // 
            this.HowToplayMenuItem.Text = "How to play";
            this.HowToplayMenuItem.Click += new System.EventHandler(this.HowToplay_Click);
            // 
            // redImgList
            // 
            this.redImgList.ImageSize = new System.Drawing.Size(30, 30);
            this.redImgList.Images.Clear();
            this.redImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
            this.redImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource3"))));
            // 
            // yellowImgList
            // 
            this.yellowImgList.ImageSize = new System.Drawing.Size(30, 30);
            this.yellowImgList.Images.Clear();
            this.yellowImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource4"))));
            this.yellowImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource5"))));
            // 
            // blueImgList
            // 
            this.blueImgList.ImageSize = new System.Drawing.Size(30, 30);
            this.blueImgList.Images.Clear();
            this.blueImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource6"))));
            this.blueImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource7"))));
            // 
            // greenImgList
            // 
            this.greenImgList.ImageSize = new System.Drawing.Size(30, 30);
            this.greenImgList.Images.Clear();
            this.greenImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource8"))));
            this.greenImgList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource9"))));
            // 
            // RulesMenuItem
            // 
            this.RulesMenuItem.Checked = true;
            this.RulesMenuItem.Text = "American Rules";
            this.RulesMenuItem.Click += new System.EventHandler(this.RulesMenuItem_Click);
            // 
            // menuItem19
            // 
            this.menuItem19.Text = "-";
            // 
            // GeniusMenuItem
            // 
            this.GeniusMenuItem.Text = "Genius (Slow)";
            this.GeniusMenuItem.Click += new System.EventHandler(this.menuItem18_Click);
            // 
            // Form1
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(240, 268);
            this.Controls.Add(this.BoardPicturebox);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.MoveButton);
            this.Menu = this.mainMenu2;
            this.Name = "Form1";
            this.Text = "Checkers";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
		//	bdrawer.PaintBoard(e.Graphics,0,0,this.Width,this.Height-50);		
		}

		public void RepaintBoard() 
		{
			Graphics g = Graphics.FromImage(bitmap);
			
			bdrawer.PaintBoard(g,leftMargin,topMargin,BoardWidth,BoardHeight);
			this.BoardPicturebox.Image = bitmap;
			g.Dispose();
		}


		CheckersPlayer CurrentPlayer ;
        CheckersPlayer WhitePlayer;
		CheckersPlayer BlackPlayer;

		private void Form1_Load(object sender, System.EventArgs e)
		{
            Queen.AmericanRules = RulesMenuItem.Checked;
			// Splahsing
            //About s = new About();
            //s.ShowDialog ();
			// end splashing

			//create the drawer
			MyBoard.InitializeBoard();
			bdrawer = new BoardDrawer(this.MyBoard);
			bdrawer.SelectionColor = Color.Yellow;
			bdrawer.WhiteCellImage = BoardCells.Images[0];
			bdrawer.BlackCellImage = BoardCells.Images[1];
			bdrawer.BlackImages = BlackImages;
			bdrawer.WhiteImages = WhiteImages;

			//starts the game with 2 human players
			HumanCheckersPlayer white = new HumanCheckersPlayer(PieceColor.White);
			white.OnPlay+=new PlayDelegate(white_OnPlay);
			white.OnOtherPlayerMovePerformed+=new PerformMove(white_OnOtherPlayerMovePerformed);
			white.OnInvalidMove+=new InvalidMoveDelegate(white_OnInvalidMove);
			white.OnGameOver+=new GameOverDelegate(white_OnGameOver);

			HumanCheckersPlayer black = new HumanCheckersPlayer(PieceColor.Black);
			black.OnPlay+=new PlayDelegate(black_OnPlay);
			black.OnOtherPlayerMovePerformed+=new PerformMove(black_OnOtherPlayerMovePerformed);
			black.OnInvalidMove+=new InvalidMoveDelegate(black_OnInvalidMove);
			black.OnGameOver+=new GameOverDelegate(black_OnGameOver);
           			
			bitmap = new Bitmap(this.Width,this.Height);
			Graphics.FromImage(bitmap).FillRectangle(new SolidBrush(Color.White),new Rectangle(0,0,this.Width,this.Height));
			BoardPicturebox.MouseUp+=new MouseEventHandler(BoardPicturebox_MouseUp);
			BoardPicturebox.MouseDown+=new MouseEventHandler(BoardPicturebox_MouseDown);
			BoardPicturebox.MouseMove+=new MouseEventHandler(BoardPicturebox_MouseMove);
			RepaintBoard();
			StartGame(white,black);

		}
        Bitmap bitmap;

		CheckersModerator MyModerator ;
		/// <summary>
		/// Starts the player given the two players
		/// </summary>
		/// <param name="White"></param>
		/// <param name="Black"></param>
		private void StartGame(CheckersPlayer White, CheckersPlayer Black) 
		{
            this.WhitePlayer = White;
            this.BlackPlayer = Black;  
			this.MyBoard.InitializeBoard();
			CurrentPlayer = WhitePlayer;
			MyModerator = new CheckersModerator(this.MyBoard,White,Black);
			RepaintBoard();
			MyModerator.StartGame();			
		}


		BoardEngine.BoardPosition LastSelectedPosition = new BoardEngine.BoardPosition(0,4);

		bool lastMoveOk = false;
		ArrayList moves = new ArrayList();
		
		private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			startdrag = false;
			if (dragging)
			{
				dragging = false;
				EndDrag ();
				return;
			} 

			//*Rayhand Added just to check memory performances
			//this.Text = GC.GetTotalMemory(false).ToString(); 
			//*Ends

			//if is not a human player the return
			if (!(CurrentPlayer is HumanCheckersPlayer)) return;

			//else ...
						
			int xCell = (e.X - leftMargin)/ CellWidth;
			int yCell = (e.Y - topMargin )/ CellHeight;
			
			if (xCell > 7 || yCell >7) return;

			bool xeven = (xCell % 2 == 0);
			bool yeven = (yCell % 2 == 0);

			if (!((xeven && yeven) || (!xeven && !yeven))) return;
			BoardPosition newpos = new BoardPosition(xCell,yCell);
			CheckersPiece piece = (CheckersPiece)MyBoard.GetPieceAt(newpos);

			//if the selected cell contains a piece of diferent color then return
			if ((piece!=null) && piece.Color != CurrentPlayer.Color) return;

			//*Rayhand
			if (piece!=null) currentSound = SoundS["selectChecker"];
			else
				currentSound = SoundS["selectDestination"];
				//*Ends;
			//*Ends


			//if not piece already selected and the user clicked and empty cell then return
			if (moves.Count == 0 && (piece == null)) return;

			//if the user is selected another piece of its color then clear the selection and select this piece
			if (piece!=null && piece.Color == CurrentPlayer.Color && moves.Count>0 && !moves[0].Equals(newpos))
			{
				moves.Clear();
				moves.Add(newpos);
				bdrawer.ClearSelection();
				bdrawer.Selected[xCell,yCell] = true;
			}
			else 
			{
				//not double cell consecuttively
				if ((moves.Count>=1) && moves[moves.Count-1].Equals(newpos)) return;
				moves.Add(newpos); 
				bdrawer.Selected[xCell,yCell] = true;
				
			}
		
			RepaintBoard();
			
			
			//*Rayhand
			SoundStorage.SoundPlayer.Play(currentSound);
			currentSound = null;
			//*End

			//*Rayhand
			GC.Collect();
			//*End
		}

		// My new instances
		ArrayList posiblemoves;
		ArrayList currentPath;
		bool dragging = false;
		bool startdrag = false;

		void StartDrag (int x, int y)
		{
			this.bdrawer.Selected [x,y] = true;
			RepaintBoard ();

			CheckersPiece piece = MyBoard.GetPieceAt (x,y) as CheckersPiece;
			posiblemoves = piece.PossibleMoves;
			currentPath = new ArrayList ();
			currentPath.Add (new BoardPosition (x,y));
			startdrag = true;
		}

		bool IsPrefix (ArrayList a, CheckersMove move)
		{
			if (a.Count > move.MovePath.Length)
				return false;
			for (int i=0; i<a.Count; i++)
				if (((BoardPosition)a[i]).X != move.MovePath[i].X || ((BoardPosition)a[i]).Y != move.MovePath[i].Y)
					return false;
			return true;
		}

		bool Check (ArrayList test, ref ArrayList currentPath)
		{
			foreach (CheckersMove move in posiblemoves)
			if (IsPrefix (test, move))
				{
					currentPath = test;
					return true;
				}
			return false;
		}

		void Dragging (int x, int y)
		{
			GC.Collect ();

			ArrayList test = currentPath.Clone () as ArrayList;
			BoardPosition p;
			foreach (BoardPosition pos in currentPath)
				bdrawer.Selected[pos.X, pos.Y] = false;
			
			p = new BoardPosition(x,y);
			test.Add (p);
			do
			{
				test.RemoveAt (test.Count-1);
				test.Add (p);
				if (Check (test, ref currentPath))
				{
					foreach (BoardPosition pos in currentPath)
						bdrawer.Selected[pos.X, pos.Y] = true;
					RepaintBoard ();
					break;
				}
				test.RemoveAt (test.Count-1);
			} while (test.Count > 0);
			foreach (BoardPosition pos in currentPath)
				bdrawer.Selected[pos.X, pos.Y] = true;
		}

		void EndDrag ()
		{
			moves = currentPath;
			if (moves.Count <= 1) this.button2_Click (null, null);
			MoveButton_Click (null, null);
		}

		#region White implemenation
		private void white_OnPlay(IGameStateForPlayer gamestate)
		{
			this.CurrentPlayer = WhitePlayer;
		}

		private void white_OnOtherPlayerMovePerformed(IPlayer player, IMove move)
		{
            //this.Invalidate();
			//MessageBox.Show("negras jugaron");
			RepaintBoard();
			System.Threading.Thread.Sleep(100);
			this.Refresh();
		}

		private void white_OnInvalidMove(IMove move)
		{
			lastMoveOk = false;
			this.CurrentPlayer = WhitePlayer;
            //this.Invalidate();
			RepaintBoard();
		}

		#endregion

		#region black implementation
		private void black_OnPlay(IGameStateForPlayer gamestate)
		{
            this.CurrentPlayer = BlackPlayer;
		}

		private void black_OnOtherPlayerMovePerformed(IPlayer player, IMove move)
		{
			RepaintBoard();
			System.Threading.Thread.Sleep(100);
			this.Refresh();
		}

		private void black_OnInvalidMove(IMove move)
		{
			this.CurrentPlayer = BlackPlayer;
			lastMoveOk = false;
			RepaintBoard();
			//this.Invalidate();
		}

		#endregion
		private void button2_Click(object sender, System.EventArgs e)
		{
			this.moves.Clear();
			this.bdrawer.ClearSelection();
			RepaintBoard();
			//this.Invalidate();
		}

		CheckersMove LastMove = null;
		
		private void MoveButton_Click(object sender, System.EventArgs e)
		{
			if (moves.Count<=1) return;
			
			//*Rayhand
			currentSound = SoundS["move"];
			//*End

			BoardPosition[] pos = new BoardPosition[moves.Count];
			moves.CopyTo(0,pos,0,moves.Count);
			bool eatMove=CheckersMove.IsEatMove(this.MyBoard, pos);
			
			//*Rayhand
			if (eatMove) currentSound = SoundS["eat"];
			//*End

            			
			CheckersMove move = new CheckersMove(pos, eatMove);
			lastMoveOk = true;
			((HumanCheckersPlayer)CurrentPlayer).MakeAMove(move);

			if (lastMoveOk) LastMove = move;
			else LastMove = null;

			CheckStatus();
			this.moves.Clear();
			this.bdrawer.ClearSelection();
			
			//*Rayhand
			SoundStorage.SoundPlayer.Play(currentSound);
			currentSound = null;
			//*End
			
			RepaintBoard() ;
			//this.Invalidate();
		}

		private void CheckStatus() 
		{
			//if the lat move was valid
			if (lastMoveOk) 
			{
				//update to queen if can be
				BoardPosition lastposition = (BoardPosition)LastMove.MovePath[LastMove.MovePath.Length-1];
				CheckersPiece piece = (CheckersPiece)MyBoard.GetPieceAt(lastposition);
				if (lastposition.Y == 0 &&  piece.Color == PieceColor.White && piece is Pawn)
				{
					MyBoard.RemovePiece(piece);
					MyBoard.PutPiece(new Queen(MyBoard,lastposition.X,lastposition.Y,PieceColor.White));
					currentSound = SoundS["queen"];
				}
				else if (lastposition.Y == 7 && piece.Color == PieceColor.Black && piece is Pawn) 
				{
					MyBoard.RemovePiece(piece);
					MyBoard.PutPiece(new Queen(MyBoard,lastposition.X,lastposition.Y,PieceColor.Black));
					currentSound = SoundS["queen"];
				}
			}
		}

		private void white_OnGameOver(bool winner)
		{
			RepaintBoard();
			System.Threading.Thread.Sleep(100);
			this.Refresh();
		}

		private void black_OnGameOver(bool winner)
		{
			RepaintBoard();
			System.Threading.Thread.Sleep(100);
			this.Refresh();
			
			//*Rayhand
			SoundStorage.SoundPlayer.Play(SoundS["win"]);
			//*End
			if (winner) MessageBox.Show("Player 2 Win!");
			else MessageBox.Show("Player 1 Win!");
		}

		private void BoardPicturebox_MouseUp(object sender, MouseEventArgs e)
		{
			Form1_MouseUp(sender,e);
		}

		private void CloseMenuItem_Click(object sender, System.EventArgs e)
		{
			DialogResult result = MessageBox.Show("Are you sure?","Exit",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
			if (result == DialogResult.Yes) this.Close();
		}

		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			DialogResult result = MessageBox.Show("The current game will be lost.\nAre you sure?","Restart the game",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
			if (result == DialogResult.Yes)	 
			{
				HumanCheckersPlayer white = new HumanCheckersPlayer(PieceColor.White);
				white.OnPlay+=new PlayDelegate(white_OnPlay);
				white.OnOtherPlayerMovePerformed+=new PerformMove(white_OnOtherPlayerMovePerformed);
				white.OnInvalidMove+=new InvalidMoveDelegate(white_OnInvalidMove);
				white.OnGameOver+=new GameOverDelegate(white_OnGameOver);

				HumanCheckersPlayer black = new HumanCheckersPlayer(PieceColor.Black);
				black.OnPlay+=new PlayDelegate(black_OnPlay);
				black.OnOtherPlayerMovePerformed+=new PerformMove(black_OnOtherPlayerMovePerformed);
				black.OnInvalidMove+=new InvalidMoveDelegate(black_OnInvalidMove);
				black.OnGameOver+=new GameOverDelegate(black_OnGameOver);
          
				this.StartGame(white,black);
			}
		}

		private void menuItem3_Click(object sender, System.EventArgs e)
		{
			DialogResult result = MessageBox.Show("The current game will be lost.\nAre you sure?","Restart the game",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
			if (result == DialogResult.Yes)	 
			{
				HumanCheckersPlayer white = new HumanCheckersPlayer(PieceColor.White);
				white.OnPlay+=new PlayDelegate(white_OnPlay);
				white.OnOtherPlayerMovePerformed+=new PerformMove(white_OnOtherPlayerMovePerformed);
				white.OnInvalidMove+=new InvalidMoveDelegate(white_OnInvalidMove);
				white.OnGameOver+=new GameOverDelegate(white_OnGameOver);

				SimpleCheckersPlayer black = new SimpleCheckersPlayer(PieceColor.Black);
				//black.OnPlay+=new PlayDelegate(black_OnPlay);
				black.OnOtherPlayerMovePerformed+=new PerformMove(black_OnOtherPlayerMovePerformed);
				black.OnInvalidMove+=new InvalidMoveDelegate(black_OnInvalidMove);
				black.OnGameOver+=new GameOverDelegate(black_OnGameOver);
				black.LEVELS=level;
          
				this.StartGame(white,black);
			}
		}

		private void menuItem6_Click(object sender, System.EventArgs e)
		{
			DialogResult result = MessageBox.Show("The current game will be lost.\nAre you sure?","Restart the game",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
			if (result == DialogResult.Yes)	 
			{
				SimpleCheckersPlayer white = new SimpleCheckersPlayer(PieceColor.White);
				//white.OnPlay+=new PlayDelegate(white_OnPlay);
				white.OnOtherPlayerMovePerformed+=new PerformMove(white_OnOtherPlayerMovePerformed);
				white.OnInvalidMove+=new InvalidMoveDelegate(white_OnInvalidMove);
				white.OnGameOver+=new GameOverDelegate(white_OnGameOver);
				white.LEVELS=level;

				HumanCheckersPlayer black = new HumanCheckersPlayer(PieceColor.Black);
				black.OnPlay+=new PlayDelegate(black_OnPlay);
				black.OnOtherPlayerMovePerformed+=new PerformMove(black_OnOtherPlayerMovePerformed);
				black.OnInvalidMove+=new InvalidMoveDelegate(black_OnInvalidMove);
				black.OnGameOver+=new GameOverDelegate(black_OnGameOver);
          
				this.StartGame(white,black);
			}
		}

		private void menuItem7_Click(object sender, System.EventArgs e)
		{
			DialogResult result = MessageBox.Show("The current game will be lost.\nAre you sure?","Restart the game",MessageBoxButtons.YesNo,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
			if (result == DialogResult.Yes)	 
			{
				SimpleCheckersPlayer white = new SimpleCheckersPlayer(PieceColor.White);
				//white.OnPlay+=new PlayDelegate(white_OnPlay);
				white.OnOtherPlayerMovePerformed+=new PerformMove(white_OnOtherPlayerMovePerformed);
				white.OnInvalidMove+=new InvalidMoveDelegate(white_OnInvalidMove);
				white.OnGameOver+=new GameOverDelegate(white_OnGameOver);
				white.LEVELS=level;

				SimpleCheckersPlayer black = new SimpleCheckersPlayer(PieceColor.Black);
				//black.OnPlay+=new PlayDelegate(black_OnPlay);
				black.OnOtherPlayerMovePerformed+=new PerformMove(black_OnOtherPlayerMovePerformed);
				black.OnInvalidMove+=new InvalidMoveDelegate(black_OnInvalidMove);
				black.OnGameOver+=new GameOverDelegate(black_OnGameOver);
				black.LEVELS=level;

				this.StartGame(white,black);
			}
		}

		int level=2;

		private void menuItem9_Click(object sender, System.EventArgs e)
		{
			if (!EasyMenuItem.Checked) {
				EasyMenuItem.Checked = true;
				ModerateMenuItem.Checked = false;
				HardMenuItem.Checked = false;
                GeniusMenuItem.Checked = false;
				level=2;
			}
		}

		private void menuItem10_Click(object sender, System.EventArgs e)
		{
			if (!ModerateMenuItem.Checked) {
				EasyMenuItem.Checked = false;
				ModerateMenuItem.Checked = true;
				HardMenuItem.Checked = false;
                GeniusMenuItem.Checked = false;
				level=4;
			}
		}

		private void menuItem11_Click(object sender, System.EventArgs e)
		{
			if (!HardMenuItem.Checked) {
				EasyMenuItem.Checked = false;
				ModerateMenuItem.Checked = false;
				HardMenuItem.Checked = true;
                GeniusMenuItem.Checked = false;
				level=6;
			}
		}

		private void menuItem5_Click(object sender, System.EventArgs e)
		{
			About ab = new About();
			ab.ShowDialog();
		}

		private void HowToplay_Click(object sender, System.EventArgs e)
		{
			HowToplay htp = new HowToplay();
			htp.ShowDialog();
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			//*Rayhand
			SoundS.Dispose();
			SoundStorage.SoundPlayer.Dispose();
			//*End
		}

		private void menuItem7_Click_1(object sender, System.EventArgs e)
		{
			menuItem7.Checked = menuItem9.Checked = menuItem10.Checked = menuItem11.Checked = false;
			if (sender == menuItem7) bdrawer.WhiteImages = redImgList;
			if (sender == menuItem9) bdrawer.WhiteImages = greenImgList;
			if (sender == menuItem10) bdrawer.WhiteImages = blueImgList;
			if (sender == menuItem11) bdrawer.WhiteImages = yellowImgList;
			((MenuItem)sender).Checked = true;
			RepaintBoard ();
		}

		private void menuItem12_Click(object sender, System.EventArgs e)
		{
			menuItem12.Checked = menuItem13.Checked = menuItem14.Checked = menuItem15.Checked = false;
			if (sender == menuItem12) bdrawer.BlackImages = redImgList;
			if (sender == menuItem13) bdrawer.BlackImages = greenImgList;
			if (sender == menuItem14) bdrawer.BlackImages = blueImgList;
			if (sender == menuItem15) bdrawer.BlackImages = yellowImgList;
			((MenuItem)sender).Checked = true;
			RepaintBoard ();
		}

		private void BoardPicturebox_MouseDown(object sender, MouseEventArgs e)
		{
			//*Rayhand Added just to check memory performances
			//this.Text = GC.GetTotalMemory(false).ToString(); 
			//*Ends

			//if is not a human player the return
			if (!(CurrentPlayer is HumanCheckersPlayer)) return;

			//else ...
						
			int xCell = (e.X - leftMargin)/ CellWidth;
			int yCell = (e.Y - topMargin )/ CellHeight;
			
			if (xCell > 7 || yCell >7) return;

			bool xeven = (xCell % 2 == 0);
			bool yeven = (yCell % 2 == 0);

			if (!((xeven && yeven) || (!xeven && !yeven))) return;

			BoardPosition newpos = new BoardPosition(xCell,yCell);
			CheckersPiece piece = (CheckersPiece)MyBoard.GetPieceAt(newpos);

			//if the selected cell contains a piece of diferent color then return
			if (piece==null || piece.Color != CurrentPlayer.Color) return;

			StartDrag (xCell, yCell);
		}

		private void BoardPicturebox_MouseMove(object sender, MouseEventArgs e)
		{
			if (!startdrag) return;
			int xCell = (e.X - leftMargin)/ CellWidth;
			int yCell = (e.Y - topMargin )/ CellHeight;
			if (xCell > 7 || yCell >7) return;
			bool xeven = (xCell % 2 == 0);
			bool yeven = (yCell % 2 == 0);
			if (!((xeven && yeven) || (!xeven && !yeven))) return;
			
			if (xCell != ((BoardPosition)currentPath[0]).X || yCell != ((BoardPosition)currentPath[0]).Y)
			{
				dragging = true;
				Dragging (xCell, yCell);
			}
		}

		private void menuItem17_Click(object sender, System.EventArgs e)
		{
			SoundStorage.SoundPlayer.Mute = (menuItem17.Checked = !menuItem17.Checked);
		}

        private void RulesMenuItem_Click(object sender, EventArgs e)
        {
            RulesMenuItem.Checked = !RulesMenuItem.Checked;
            Queen.AmericanRules = RulesMenuItem.Checked;
        }

        private void menuItem18_Click(object sender, EventArgs e)
        {
            if (!GeniusMenuItem.Checked)
            {
                EasyMenuItem.Checked = false;
                ModerateMenuItem.Checked = false;
                HardMenuItem.Checked = false;
                GeniusMenuItem.Checked = true;
                level = 8;
            }
        }
	}
}

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
Team Leader
United States United States
Leonardo loves to code with C# and thinks .NET platforms rocks.
He has a Master degree in Computer Sciences and likes to share code and ideas.
Follow on   Twitter

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