Click here to Skip to main content
15,879,474 members
Articles / Mobile Apps

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

Rate me:
Please Sign up or sign in to vote.
4.75/5 (13 votes)
4 Aug 20043 min read 91.4K   4.4K   36  
A C# implementation of Reversi (Othello) Game for PocketPC and Windows.
using System;
using System.Collections;
	
namespace gma.Reversi 
{

	public class BoardComplex : Board 
	{


		public override event OnNeedRedrawEvent OnNeedRedrawAll;


		public BoardComplex():base()
		{
		}

		public BoardComplex(BoardComplex aParent)
		{
			_square = (int[,])aParent.Square.Clone();

			_listOfRed=(ArrayList)aParent.ListOfRed.Clone();
			_listOfWhite=(ArrayList)aParent.ListOfWhite.Clone(); 

			_weightRed = aParent.WeightRed;
			_weightWhite = aParent.WeightWhite;
			_currentPlayer = aParent.CurrentPlayer;
		}


		public override void StartGame()
		{
			_square= (int[,])InitBoard.Clone();
			_listOfWhite.Add(new Point(3,3));
			_listOfWhite.Add(new Point(4,4));
			_listOfRed.Add(new Point(3,4));
			_listOfRed.Add(new Point(4,3));
			_weightRed = 0;
			_weightWhite = 0;
			AffectedSquares.Clear();
			_availableMoves=null;

			if (OnNeedRedrawAll != null) OnNeedRedrawAll();
			// black starts
			_currentPlayer = PLAYER_WHITE;
			// the move will be transfered to black
			TransferMove(0);
		}

		private ArrayList _listOfRed= new ArrayList();
		public ArrayList ListOfRed
		{
			get { return _listOfRed;}
		}

		private ArrayList _listOfWhite= new ArrayList();
		public ArrayList ListOfWhite
		{
			get { return _listOfWhite;}
		}


		protected override void SetStateAt(Point aPoint, int state)
		{
			//----- Statisticts
			// Decrease
			switch (_square[aPoint.X, aPoint.Y])
			{
				case PLAYER_WHITE:
					_listOfWhite.Remove(aPoint);
					_weightWhite-=LocationWeight[aPoint.X, aPoint.Y];
					break;

				case PLAYER_RED:
					_listOfRed.Remove(aPoint);
					_weightRed-=LocationWeight[aPoint.X, aPoint.Y];
					break;
			}

		
			// Increase
			switch (state)
			{
				case PLAYER_WHITE:
					_listOfWhite.Add(aPoint);
					_weightWhite+=LocationWeight[aPoint.X, aPoint.Y];
					break;
				case PLAYER_RED:
					_listOfRed.Add(aPoint);
					_weightRed+=LocationWeight[aPoint.X, aPoint.Y];
					break;
			}
			//-----END Statisticts
			
			_affectedSquares.Add(aPoint);
			_square[aPoint.X, aPoint.Y]=state;
		}

		public override int CounterRed
		{
			get	{return _listOfRed.Count;}
		}

		public override int CounterWhite
		{
			get{return _listOfWhite.Count;}
		}

		// Reduces search to entries in _listOfRed or in _listOfWhite
		public override int GetIrreversibleCount(int aPlayer)
		{
			ArrayList listOfPointsToCheck;
			if (aPlayer==PLAYER_RED)
				listOfPointsToCheck=_listOfRed;
			else
				listOfPointsToCheck=_listOfWhite;

			int counter=0;
			int aPlayerArrayIndex=aPlayer;
			if (aPlayer<0) aPlayerArrayIndex=0;
			foreach (Point aPointToCheck in listOfPointsToCheck)
					if (IsIrreversible(aPointToCheck, aPlayer, 0)) counter++;
			return counter;
		}

		// reduces search to neighbours of an opposite color
		public override IList GetAvailableMoves(int aPlayer)
		{
			if (_availableMoves==null)
			{
				// generate list of potential available moves
				ArrayList listOfPotentialAvailableMoves= new ArrayList();

				// list of opposite squares
				ArrayList listOfPointsToCheck;
				if (aPlayer==PLAYER_RED)
					listOfPointsToCheck=_listOfWhite;
				else
					listOfPointsToCheck=_listOfRed;

				// for all oponent squares
				foreach (Point aPointToCheck in listOfPointsToCheck)
					// for each direction
					for (int i=0; i<AllDirs.Length ; i++)
					{
						Point aNeighbourPoint = aPointToCheck + AllDirs[i];
						// if it is a legal square
						if (aNeighbourPoint.IsInRange())
						{
							int aNeigbourSquareState = GetStateAt(aNeighbourPoint);
							// and its state is empty
							if (aNeigbourSquareState == SQUARE_EMPTY ) 
							{
								//and it was not already considered
								if (!listOfPotentialAvailableMoves.Contains(aNeighbourPoint))
									listOfPotentialAvailableMoves.Add(aNeighbourPoint);
							}
						}
					}
				
				_availableMoves = new ArrayList();
				// now check all potential available moves
				foreach(Point aPotentialAvailableMove in listOfPotentialAvailableMoves)
				{
					if (Play(aPotentialAvailableMove,aPlayer, true)) 
						_availableMoves.Add(aPotentialAvailableMove);
				}
			}
			return _availableMoves;
		}


	}
}

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


Written By
Software Developer
Germany Germany
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com

Comments and Discussions