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

Reversi for Windows Mobile

, 12 May 2009
The game Reversi for Windows Mobile using the Compact Framework.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace GameExample.Reversi
{
    /// <summary>
    /// The game board.
    /// </summary>
    public class Board
    {
        /// <summary>
        /// The width of the board.
        /// </summary>
        private readonly int _boardWidth;
        /// <summary>
        /// The height of the board.
        /// </summary>
        private readonly int _boardHeight;
        /// <summary>
        /// The board data.
        /// </summary>
        private readonly Position[] _data;


        /// <summary>
        /// Initializes a new instance of the <see cref="Board"/> class.
        /// </summary>
        /// <param name="size">The size.</param>
        public Board(int size)
            : this ( size,size)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Board"/> class.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        public Board(int width, int height)
        {
            if (width < 3 || (width % 2) != 0)
            {
                throw new ArgumentException();
            }
            if (height < 3 || (height % 2) != 0)
            {
                throw new ArgumentException();
            }
            _data = CreateBoard(width, height);
            _boardHeight = height;
            _boardWidth = width;
        }

        /// <summary>
        /// Creates the board.
        /// </summary>
        /// <param name="size">The size.</param>
        /// <returns></returns>
        private static Position[] CreateBoard(int width, int height)
        {
            int centerX = width / 2, centerY = height / 2;
            int cellCount = width * height;
            Position[] data = new Position[cellCount];

            // Set the four center points
            data[((centerY * width) + centerX)] = new Position(Player.PlayerTwo);
            data[(((centerY - 1) * width) + centerX)] = new Position(Player.PlayerOne);
            data[((centerY * width) + (centerX - 1))] = new Position(Player.PlayerOne);
            data[(((centerY - 1) * width) + (centerX - 1))] = new Position(Player.PlayerTwo);
            
            return data;
        }

        /// <summary>
        /// Gets the <see cref="GameExample.Reversi.Position"/> with the specified coordinates
        /// </summary>
        /// <value></value>
        public Position this[int x, int y]
        {
            get
            {
                if (x >= _boardWidth)
                {
                    throw new ArgumentOutOfRangeException("x", "X must be greater than or equal to zero and less than the board size !");
                }
                if (y >= _boardHeight)
                {
                    throw new ArgumentOutOfRangeException("y", "Y must be greater than or equal to zero and less than the board size !");
                }

                return _data[(y * _boardWidth) + x];
            }
            set {
                if (x >= _boardWidth)
                {
                    throw new ArgumentOutOfRangeException("x", "X must be greater than or equal to zero and less than the board size !");
                }
                if (y >= _boardHeight)
                {
                    throw new ArgumentOutOfRangeException("y", "Y must be greater than or equal to zero and less than the board size !");
                }

                _data[(y * _boardWidth) + x] = value;
            }
        }

        /// <summary>
        /// Gets the board data.
        /// </summary>
        /// <value>The data.</value>
        public Position[] Data
        {
            get
            {
                return _data;
            }
        }

        public int BoardWidth
        {
            get
            {
                return _boardWidth;
            }
        }

        public int BoardHeight
        {
            get
            {
                return _boardHeight;
            }
        }
        
    }


}

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

Ziad Elmalki

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 13 May 2009
Article Copyright 2009 by Ziad Elmalki
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid