|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.