Click here to Skip to main content
15,896,557 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.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Reversi.Classes;

namespace Reversi.Controls
{
    public partial class BoardFieldControl : UserControl
    {
        #region ReadOnly

        public const int SIZE = 48;

        #endregion

        #region Fields

        private DiscColor mCurrentColor;

        private BoardControl mBoardControl;
        private int mRowIndex;
        private int mColumnIndex;

        private bool mIsMouseOver = false;

        #endregion

        #region Constructors

        public BoardFieldControl(BoardControl boardControl, int rowIndex, int columnIndex, DiscColor currentColor)
        {
            this.mBoardControl = boardControl;
            this.mRowIndex = rowIndex;
            this.mColumnIndex = columnIndex;
            this.mCurrentColor = currentColor;

            this.InitializeComponent();

            this.Width = SIZE;
            this.Height = SIZE;
        }

        #endregion

        #region Properties

        public BoardControl BoardControl
        {
            get
            {
                return this.mBoardControl;
            }
        }

        public int RowIndex
        {
            get
            {
                return this.mRowIndex;
            }
        }

        public int ColumnIndex
        {
            get
            {
                return this.mColumnIndex;
            }
        }

        public DiscColor CurrentColor
        {
            get
            {
                return this.mCurrentColor;
            }
            set
            {
                this.mCurrentColor = value;
            }
        }


        private bool CanMove()
        {
            return this.BoardControl.Game.Board.CanSetFieldColor(
                this.RowIndex,
                this.ColumnIndex,
                this.BoardControl.Game.CurrentPlayer.Color);
        }

        #endregion

        #region Methods

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            this.DrawDisc(e);
            this.DrawBorder(e);           
        }

        private void DrawDisc(PaintEventArgs e)
        {
            if (this.CurrentColor != DiscColor.None)
            {
                Color discColor = this.CurrentColor.Color;
                int discStart = this.Width / 6;
                int discWidth = 2 * this.Width / 3;
                Rectangle rectangle = new Rectangle(discStart, discStart, discWidth, discWidth);

                e.Graphics.FillEllipse(new SolidBrush(discColor), rectangle);
                e.Graphics.DrawEllipse(Pens.Black, rectangle);
            }
        }

        private void DrawBorder(PaintEventArgs e)
        {
            if (this.RowIndex == 0)
            {
                e.Graphics.DrawLine(Pens.Black, 0, 0, this.Width - 1, 0);
            }
            if (this.ColumnIndex == 0)
            {
                e.Graphics.DrawLine(Pens.Black, 0, 0, 0, this.Height - 1);
            }
            e.Graphics.DrawLine(Pens.Black, 0, this.Height - 1, this.Width - 1, this.Height - 1);
            e.Graphics.DrawLine(Pens.Black, this.Width - 1, 0, this.Width - 1, this.Height - 1);
        }        


        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);

            if (this.BoardControl.IsHumanPlayerToMove && !this.BoardControl.Game.IsPaused && this.CanMove())
            {
                this.mIsMouseOver = true;                
                this.Cursor = Cursors.Hand;
                this.DrawPotentialMove();
            }
            else
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void DrawPotentialMove()
        {
            if (this.mIsMouseOver)
            {
                Graphics graphics = this.CreateGraphics();
                graphics.DrawRectangle(Pens.Black, 0, 0, this.Width - 2, this.Height - 2);
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (this.mIsMouseOver)
            {
                // Clears potential move
                this.Refresh();
                this.mIsMouseOver = false;
            }
        }        


        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            if (this.mIsMouseOver)
            {
                this.BoardControl.Game.Board.SetFieldColor(this.RowIndex, this.ColumnIndex, this.BoardControl.Game.CurrentPlayer.Color);
                this.mIsMouseOver = false;
            }
        }

        #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