Click here to Skip to main content
15,896,456 members
Articles / Artificial Intelligence

Reversi

Rate me:
Please Sign up or sign in to vote.
4.79/5 (15 votes)
18 Mar 2008CPOL5 min read 85.5K   4.9K   46  
An implementation of the popular game Reversi, written as a project for an AI course.
using System;
using System.Collections.Generic;
using System.Text;

namespace Reversi.Classes
{
    public delegate void GameEventHandler();

    public class Game
    {
        #region Events

        public event GameEventHandler Started;
        public event FieldColorEventHandler MoveFinished;
        public event GameEventHandler MoveStarted;
        public event GameEventHandler Finished;

        #endregion

        #region Fields

        private Board mBoard;
        private Player mPlayer1;
        private Player mPlayer2;

        private Player mCurrentPlayer;
        private bool mIsFinished = false;
        private bool mIsStopped = false;
        private bool mIsPaused = false;

        #endregion

        #region Constructors

        public Game(Board board)
        {
            this.mBoard = board;
        }

        #endregion

        #region Properties

        public Board Board
        {
            get
            {
                return this.mBoard;
            }
        }

        public Player Player1
        {
            get
            {
                return this.mPlayer1;
            }
        }

        public Player Player2
        {
            get
            {
                return this.mPlayer2;
            }
        }

        public Player CurrentPlayer
        {
            get
            {
                return this.mCurrentPlayer;
            }
        }

        public bool IsFinished
        {
            get
            {
                return this.mIsFinished;
            }
        }

        public bool IsStarted
        {
            get
            {
                return (this.CurrentPlayer != null) || this.IsFinished;
            }
        }

        public bool IsStopped
        {
            get
            {
                return this.mIsStopped;
            }
        }

        public bool IsPaused
        {
            get
            {
                return this.mIsPaused;
            }
        }

        #endregion

        #region Methods

        public Player CreatePlayer1(PlayerProperties properties)
        {
            this.mPlayer1 = this.CreatePlayer(properties, DiscColor.Black);
            return this.mPlayer1;
        }

        public Player CreatePlayer2(PlayerProperties properties)
        {
            this.mPlayer2 = this.CreatePlayer(properties, DiscColor.White);
            return this.mPlayer2;
        }

        private Player CreatePlayer(PlayerProperties properties, DiscColor color)
        {
            switch (properties.Type)
            {
                case PlayerType.Human:
                    return new HumanPlayer(this, color, properties.Name);
                    
                case PlayerType.Computer:
                    return new ComputerPlayer(this, color, properties.Name, properties.MaxDepth);
            }

            return null;
        }


        public bool Start()
        {
            if ((this.Player1 != null) && (this.Player2 != null))
            {
                this.Board.MoveFinished += new FieldColorEventHandler(this.OnMoveFinished);

                if (this.Started != null)
                {
                    this.Started();
                }

                this.SetPlayerToMove(this.Player1);                               
                return true;
            }
            else
            {
                return false;
            }
        }

        private void SetPlayerToMove(Player player)
        {
            this.mCurrentPlayer = player;

            if (this.MoveStarted != null)
            {
                this.MoveStarted();
            }

            this.CurrentPlayer.StartMove();
        }

        public void Stop()
        {
            this.mIsStopped = true;
        }

        public void Pause()
        {
            if (!this.IsFinished && !this.IsStopped)
            {
                this.mIsPaused = true;
            }
        }

        public void Continue()
        {
            if (!this.IsFinished && !this.IsStopped)
            {
                this.mIsPaused = false;
                this.SetPlayerToMove(this.CurrentPlayer);
            }
        }

        #endregion

        #region Events Handling

        private void OnMoveFinished(int rowIndex, int columnIndex, DiscColor color)
        {
            if (!this.mIsStopped)
            {
                if (this.MoveFinished != null)
                {
                    this.MoveFinished(rowIndex, columnIndex, color);
                }

                Player opositePlayer = (this.CurrentPlayer == this.Player1) ? this.Player2 : this.Player1;
                if (opositePlayer.CanMove())
                {
                    this.SetPlayerToMove(opositePlayer);
                }
                else if (this.CurrentPlayer.CanMove())
                {
                    this.SetPlayerToMove(this.CurrentPlayer);
                }
                else
                {
                    this.mCurrentPlayer = null;
                    this.mIsFinished = true;

                    if (this.Finished != null)
                    {
                        this.Finished();
                    }
                }                
            }
        }

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


Written By
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions