|
/*************************************************************
This class was originally created in C++ by:
Copyright Daniel M. O'Brien (c) 2002
Copyright Daniel M. O'Brien (c) 2004
This class was made for C# by:
Copyright Jason Barry (c) 2008
This code may be used for any legal purpose as long as the
original copyright above is shown.
*************************************************************/
using System;
namespace PegThat
{
/// <summary>
/// Stores the position of a peg.
/// </summary>
public class Position
{
#region Variables
private int row;
private int column;
#endregion
#region Properties
/// <summary>
/// Gets or sets the row index number for the position.
/// </summary>
public int Row
{
get { return row; }
set { row = value; }
}
/// <summary>
/// Gets or sets the column index number for the position.
/// </summary>
public int Column
{
get { return column; }
set { column = value; }
}
#endregion
#region Constructors
/// <summary>
/// A new position object using a specific row and column.
/// </summary>
/// <param name="row">
/// The row index number for the position.
/// </param>
/// <param name="column">
/// The column index number for the position.
/// </param>
public Position(int row, int column)
{
this.row = row;
this.column = column;
}
/// <summary>
/// A new position object using another position object's row and column.
/// </summary>
/// <param name="position">
/// An position object.
/// </param>
public Position(Position position)
: this(position.row, position.column)
{
}
#endregion
#region Overloaded Operators
/// <summary>
/// This overloaded operator will add two position objects together.
/// </summary>
/// <param name="position1">
/// The first position object.
/// </param>
/// <param name="position2">
/// The second position object.
/// </param>
/// <returns>
/// The new position object.
/// </returns>
public static Position operator +(Position position1, Position position2)
{
// Add both of the rows and both of the columns and return
// it as a new position object.
return new Position(position1.row + position2.row, position1.column + position2.column);
}
/// <summary>
/// This overloaded operator will multiply a position object and any number.
/// </summary>
/// <param name="position1">
/// The first position object.
/// </param>
/// <param name="number">
/// The number to multiply by.
/// </param>
/// <returns>
/// The new position object.
/// </returns>
public static Position operator *(Position position1, int number)
{
// Multiply both of the rows and both of the columns and return
// it as a new position object.
return new Position(position1.row * number, position1.column * number);
}
#endregion
#region Functions
/// <summary>
/// Checks to see if the position is valid.
/// </summary>
/// <param name="position">
/// The position object to validate.
/// </param>
/// <returns>
/// Returns true if it is valid and false if it's not.
/// </returns>
private bool Valid(Position position)
{
// The row can't be greater or equal
// to the size of the board.
if (position.row >= Settings.BoardSize)
return false;
// The row can't be less than zero.
if (position.row < 0)
return false;
// The column can't be greater or equal
// to the size of the board.
if (position.column >= Settings.BoardSize)
return false;
// The column can't be less than zero.
if (position.column < 0)
return false;
return true;
}
/// <summary>
/// Moves the peg from the current position to a new position.
/// </summary>
/// <param name="position">
/// The position to move to.
/// </param>
/// <returns>
/// Returns true if the peg was moved and false if it wasn't.
/// </returns>
public bool Move(Position position)
{
// Copy the current position object.
Position current = new Position(this);
// Add the positions together.
current += position;
// Make sure that the position is valid.
if (Valid(current) == false)
return false;
// Assign the new row and column to
// the existing position.
row = current.row;
column = current.column;
return true;
}
#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.
I received an associate degree in computer programming in 2005. At work, I create and maintain software that is used in the hospital industry all across the world. I write the software using C# and ASP.NET MVC.