public class Board
{
// FIELDS
// The sticks associated with a board, referred to as 1 to 60.
List
// The nodes of the board.
private Node[] nodes = new Node[36];
// Maximum number of sticks possible on the board.
private int max = 60;
// CONSTRUCTORS
/*
Sets the maximum value for the board object and initializes nodes array.
*/
public Board(int maximum)
{
max = maximum; // FLAG for future: perform validation on maximum.
for (int i = 0; i < 36; i++)
{
nodes[i] = new Node(i, this);
}
}
// PROPERTIES
/* Returns the number of sticks (value of true) on the board. */
public int Value
{
get
{
int temp = 0;
foreach (Stick i in stickList)
{
if (i.Value) temp++;
}
return temp;
}
}
// METHODS -- PUBLIC
/*
Moves a stick according the established rules: A stick must exist (value of true) and can
be moved into a valid open space (value of false) 90 degrees offset.
if the move is not valid for any reason, the method changes nothing.
origin: Starting location of stick.
destination: Final location for stick.
returns: True if the move is accomplished. False if the move is not accomplished for any reason.
*/
public bool moveStick(int origin, int destination)
{
if (!(isValid(origin))) return false;
if (!(isValid(destination))) return false;
// origin and destination must share a node
foreach (Node x in nodes) {
if (x.doMove(origin, destination)) return true;
}
return false;
}
/*
Causes a stick to fall from the specified location to the lowest it can fall to
within the same node.
location: Stick to fall.
Returns true if a stick can fall to the lowest location. False if the stick is stopped
for any reason.
*/
public bool dropStick(int location)
{
if (!(isValid(location))) return false;
foreach (Node x in nodes)
{
if (x.fall(location)) return true;
}
return false;
}
/*
Gets the value of a stick at the specified location.
location: The location to be queried.
returns: True if the location is valid and the stick has a value of true.
False otherwise, including if the location is invalid for this board.
*/
public bool getPositionValue(int location)
{
if (!(isValid(location))) return false;
return getStick(location).Value;
}
/*
Sets the value of a stick at the specified location to true.
location: The stick to be changed.
returns: True on sucess. False if the location is not valid or if the value
of the stick is already true.
*/
public bool addStick(int location)
{
if (!(isValid(location))) return false;
if (getStick(location).Value) return false;
if (this.Value >= max) return false;
stickList.Add(new Stick(location));
return true;
}
/*
Sets the value of a stick at the specified location to false.
location: The stick to be changed.
returns: True on sucess. False if the location is not valid or if the value
of the stick is already false.
*/
public bool removeStick(int location)
{
if (!(isValid(location))) return false;
if (!getStick(location).Value) return false;
stickList.Remove(getStick(location));
return true;
}
public override int GetHashCode()
{
int temp = 0;
foreach (Stick i in stickList)
{
if (i.Value)
{
temp = temp + 2^(i.Location);
}
}
return temp;
}
public override bool Equals(System.Object obj) // Not sure this is valid. FLAG
{
if (obj.GetType() != this.GetType()) {return false;}
Board temp = (Board)obj;
if (this.Value == temp.Value) {return true;}
return false;
}
/* Gets the stick object associated with a location.
Works by testing the location against all the locations in the board.
This approach does not depend on the number of positions available in the board.
location: Location of stick to get.
returns: Stick at specified location.
*/
public Stick getStick(int location)
{
if (isValid(location)) {
foreach (Stick x in stickList) {
if (x.Location == location) return x;
}
}
return new Stick(false);
}
// METHODS -- PRIVATE
/* Checks whether a position is a valid position in this board.
Works by testing the location against all the locations in the board.
This approach does not depend on the number of positions available in the board.
candidate: The integer being tested.
returns: True if the candidate value is a valid position on the board. False otherwise.
*/
private bool isValid(int candidate)
{
foreach (Node x in nodes)
{
if (x.currentNode(candidate)) return true;
}
return false;
}
// TESTING CODE
/*
Gets the stick object at a specified board location.
loc: Location.
returns: Stick at loc.
TESTING CODE: DO NOT CALL EXCEPT FOR TEST
This code refers directly to the grid array. As such, it is subject to out-of-bounds errors.
The private method getStick is not subject to such errors.
*/
public Stick test_getStick(int loc)
{
if (isValid(loc)) {
return getStick(loc);
}
return new Stick(false); // Return to this later
}
/*
Gets the node object specified by 0-based index.
loc: Node to be returned
returns: Node at specified index.
TESTING CODE: DO NOT CALL EXCEPT FOR TEST
*/
public Node test_getNode(int loc)
{
return nodes[loc];
}
}
'@
No comments:
Post a Comment