Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

A C# implementation of Reversi (Othello) Game for PocketPC and Windows

, 4 Aug 2004
A C# implementation of Reversi (Othello) Game for PocketPC and Windows.
pocketreversi_demo.zip
gma.Reversi.dll
gma.Windows.Reversi.exe
pocketreversi_setup.zip
install.ini
setup.exe
gma.ARMV4.CAB
pocketreversi_src.zip
gma.Windows.Reversi
gma.Windows.Reversi.csproj.user
Images
0.gif
1.gif
2.gif
3.gif
4.gif
5.gif
Reversi.ico
gma.Mobile.Reversi
gma.Mobile.Reversi.csdproj
gma.Mobile.Reversi.csdproj.user
Images
0.gif
1.gif
2.gif
3.gif
4.gif
5.gif
Reversi.ico
gma.Reversi
gma.Reversi.csproj.user
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using gma.Reversi;
using gma.Mobile.Controls;

namespace gma.Mobile.Reversi
{
	/// <summary>
	/// Zusammenfassung f�r Form1.
	/// </summary>
	public class frmMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel;
		private System.Windows.Forms.ProgressBar progressBar;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItemNewGame;
		private System.Windows.Forms.MenuItem menuItemQuit;
		private System.Windows.Forms.MenuItem menuItemUndo;
		private System.Windows.Forms.ImageList imageList;

		public frmMain()
		{
			//
			// Erforderlich f�r die Windows Form-Designerunterst�tzung
			//
			InitializeComponent();

			//
			// TODO: Konstruktorcode hinter dem InitializeComponent-Aufruf hinzuf�gen
			//
		}
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Vom Windows Form-Designer generierter Code
		/// <summary>
		/// Erforderliche Methode f�r die Designerunterst�tzung. 
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor ge�ndert werden.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItemNewGame = new System.Windows.Forms.MenuItem();
			this.menuItemQuit = new System.Windows.Forms.MenuItem();
			this.panel = new System.Windows.Forms.Panel();
			this.progressBar = new System.Windows.Forms.ProgressBar();
			this.imageList = new System.Windows.Forms.ImageList();
			this.menuItemUndo = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.Add(this.menuItem1);
			// 
			// menuItem1
			// 
			this.menuItem1.MenuItems.Add(this.menuItemNewGame);
			this.menuItem1.MenuItems.Add(this.menuItemUndo);
			this.menuItem1.MenuItems.Add(this.menuItemQuit);
			this.menuItem1.Text = "Game";
			// 
			// menuItemNewGame
			// 
			this.menuItemNewGame.Text = "New Game";
			this.menuItemNewGame.Click += new System.EventHandler(this.menuItemNewGame_Click);
			// 
			// menuItemQuit
			// 
			this.menuItemQuit.Text = "Quit";
			this.menuItemQuit.Click += new System.EventHandler(this.menuItemQuit_Click);
			// 
			// panel
			// 
			this.panel.Location = new System.Drawing.Point(2, 2);
			this.panel.Size = new System.Drawing.Size(232, 232);
			// 
			// progressBar
			// 
			this.progressBar.Location = new System.Drawing.Point(8, 240);
			this.progressBar.Size = new System.Drawing.Size(224, 24);
			// 
			// imageList
			// 
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource2"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource3"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource4"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource5"))));
			this.imageList.ImageSize = new System.Drawing.Size(28, 28);
			// 
			// menuItemUndo
			// 
			this.menuItemUndo.Text = "Undo";
			this.menuItemUndo.Click += new System.EventHandler(this.menuItemUndo_Click);
			// 
			// frmMain
			// 
			this.Controls.Add(this.progressBar);
			this.Controls.Add(this.panel);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu;
			this.Text = "Reversi B {0}: W{1}";
			this.Load += new System.EventHandler(this.frmMain_Load);

		}
		#endregion

		/// <summary>
		/// Der Haupteinstiegspunkt f�r die Anwendung.
		/// </summary>

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

		BoardComplex brd;
		GUIPlayer human;
		VirtualPlayer computer;

		private void frmMain_Load(object sender, System.EventArgs e)
		{
			panel.Location=new System.Drawing.Point((this.Width-panel.Width)/2, 10);

			progressBar.Location= new System.Drawing.Point(panel.Location.X , panel.Location.Y+panel.Height +3);
			progressBar.Size = new Size(panel.Width , 20 );

			brd = new BoardComplex();

			human= new GUIPlayer(brd);
			human.PlayerColor =  Board.PLAYER_RED;

			computer = new VirtualPlayer(brd);
			computer.PlayerColor = Board.PLAYER_WHITE;
			//computer.OnLog += new OnLogEvent(LogWrite);
			computer.OnProgress +=new OnProgressEvent(computer_OnProgress);

			progressBar.Minimum=0;
			progressBar.Maximum=100;
			
			brd.OnGameOver+=new OnGameOverEvent(brd_OnGameOver);
			brd.OnNeedRedraw += new OnNeedRedrawEvent(Redraw);
			brd.OnNeedRedrawAll += new OnNeedRedrawEvent(RedrawAll);
			menuItemNewGame_Click(null, null);
		}

		private void brd_OnGameOver()
		{
			int diference=brd.CounterRed-brd.CounterWhite;
			if (human.PlayerColor==Board.PLAYER_WHITE) diference=-diference;

			string txt=String.Empty;
			if (diference==0)  
				txt="The Game is a draw.\n\rB {0}: W {1}";
			else
				if (diference>0)  
					txt="Congratulations !\n\rYou are a Winner.\n\rB {0}: W {1}";
				else
				    txt="Sorry, you lose.\n\rB {0}: W {1}";
			MessageBox.Show(String.Format(txt,brd.CounterRed ,brd.CounterWhite ),"Game over");
		}


		private void computer_OnProgress(int aProgress)
		{
			progressBar.Value = aProgress;
		}


		private void LogWrite(string msg)
		{
			/*
			textBoxLog.AppendText(msg);
			textBoxLog.SelectionStart = textBoxLog.Text.Length;
			textBoxLog.ScrollToCaret();
			*/
		}

		public void RedrawCell(gma.Reversi.Point aPoint, int phase)
		{
			int i = aPoint.X;
			int j = aPoint.Y;

			ImageButton btn = allButtons[i, j];
			if (btn == null)
			{
				btn = new ImageButton();
				btn.Size = new System.Drawing.Size(28, 28);
				btn.Location = new System.Drawing.Point(i * 29, j * 29);
				btn.Tag = new gma.Reversi.Point(i, j);
				//btn.BorderStyle=BorderStyle.FixedSingle;
				btn.Click += new System.EventHandler(this.ButtonClick);
				panel.Controls.Add(btn);
				allButtons[i, j] = btn;
			}

			//btn.BorderStyle = BorderStyle.None;

			switch (brd.GetStateAt(i, j))
			{
				case Board.PLAYER_RED:
					btn.Image =  imageList.Images[5-phase];
					break;
				case Board.PLAYER_WHITE:
					btn.Image =  imageList.Images[1+phase];
					break;
				case Board.SQUARE_EMPTY:
					btn.Image =  imageList.Images[0];
					break;
			}
			btn.BackColor = Color.White;
			btn.Refresh();
		}


		private int animationSpeed = 4;
		IList OldAvailableMoves=new ArrayList();
		bool IsAnimating=false;
		public void Redraw()
		{
			IsAnimating=true;
			this.Text=String.Format("Reversi B {0}:{1} W"  ,brd.CounterRed ,brd.CounterWhite);
			for (int phase=3; phase>=0; phase--)
			{
				foreach (gma.Reversi.Point anAffectedPoint in brd.AffectedSquares)
					RedrawCell(anAffectedPoint, phase );
				Application.DoEvents();
				Thread.Sleep(animationSpeed*25);
			}

			foreach(gma.Reversi.Point aPoint in OldAvailableMoves)
				if ( !brd.AffectedSquares.Contains(aPoint)) RedrawCell(aPoint,0);
			OldAvailableMoves.Clear();

			IList availableMoves = brd.GetAvailableMoves(brd.CurrentPlayer);
			foreach(gma.Reversi.Point aPoint in availableMoves)
			{
				if (brd.CurrentPlayer!=computer.PlayerColor)
				{
					allButtons[aPoint.X,aPoint.Y].Image = null;
					allButtons[aPoint.X,aPoint.Y].Refresh();
					OldAvailableMoves.Add(aPoint);
				}
			}
			IsAnimating=false;
		}

		private void RedrawAll()
		{
			Application.DoEvents();
			for (int i = 0; i < Board.SIZEX; i++)
				for (int j = 0; j < Board.SIZEY; j++)
					RedrawCell(new gma.Reversi.Point(i,j),0);
		}


        
		ImageButton[,] allButtons = new ImageButton[8, 8];

		void ButtonClick(object sender, System.EventArgs e)
		{
			if (brd.CurrentPlayer!=human.PlayerColor || IsAnimating) return;
			gma.Reversi.Point aPoint = (gma.Reversi.Point)(((ImageButton)sender).Tag);
			human.MakeMove(aPoint);
		}

		private void menuItemQuit_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}

		frmStartGame frm=null;
		private void menuItemNewGame_Click(object sender, System.EventArgs e)
		{
			frm= new frmStartGame();
			frm.MyColor=human.PlayerColor;
			frm.SearchDepth=computer.MAX_DEPTH;
			frm.AnimationSpeed = animationSpeed;
			frm.OnOkClicked+=new EventHandler(drmStartGameOk_Click);
			frm.Show();
		}

		private void drmStartGameOk_Click(object sender, System.EventArgs e)
		{
			//if (frm.DialogResult!=DialogResult.OK) return;
			human.PlayerColor =  frm.MyColor;
			human.EnabelUndo=frm.EnableUndo; 
			menuItemUndo.Enabled=human.EnabelUndo;
			computer.PlayerColor = -human.PlayerColor;
			computer.MAX_DEPTH=frm.SearchDepth;
			animationSpeed = frm.AnimationSpeed;
			RedrawAll();
			frm.Close();
			Application.DoEvents();
			brd.StartGame();
		}

		private void menuItemUndo_Click(object sender, System.EventArgs e)
		{
			try 
			{
				brd=human.UndoLastMove();
				human.Board=brd;
				computer.Board=brd;
				brd.OnGameOver+=new OnGameOverEvent(brd_OnGameOver);
				brd.OnNeedRedraw += new OnNeedRedrawEvent(Redraw);
				brd.OnNeedRedrawAll += new OnNeedRedrawEvent(RedrawAll);
				RedrawAll();
				Redraw();
			}
			catch (Exception)
			{
				MessageBox.Show("Unable to undo last Move!");
			}
		}

	}
}

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 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

Share

About the Author

George Mamaladze
Software Developer
Germany Germany
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 5 Aug 2004
Article Copyright 2004 by George Mamaladze
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid