As promised, here's the source code for the design. It includes, as before, the Node class, the Stick class, and the Board class. The Stick and Board classes in next post.
The last time I posted code, the same file also included all the tests, but I've added a lot of tests since then. Let's just post the C# code.
The code for the fall() method needs work. I might break it down into a “fall from position 0” private method, “fall from position 1” private method and “fall from position 3” private method. The logic behind it would be clearer that way.
Add-Type @'
using System;
using System.Collections.Generic;
public class Node
{
// FIELDS
// Array of the four locations around this node.
private int[] position = new int[4];
// The board object associated with this node.
private Board board;
// CONSTRUCTORS
/*
Assigns position numbers to the specified node.
temp: The number associated with this node. Not use elsewhere, even by Baord.
currentBoard: The board object associated with this node.
*/
public Node(int temp, Board currentBoard)
{
board = currentBoard;
// GENERAL CASE
position[0] = temp + (((temp / 6) - 1) * 5);
position[1] = position[0] + 6;
position[2] = position[0] + 11;
position[3] = position[0] + 5;
// EDGE CASES: TOP EDGE
if (position[0] < 1) position[0] = -1;
// BOTTOM EDGE
if (position[2] > 60) position[2] = -1;
//LEFT EDGE
if (temp % 6 == 0) position[3] = -1;
//RIGHT EDGE
if ((temp + 1) % 6 == 0) position[1] = -1;
}
// PROPERTIES
// PUBLIC METHODS
/*
Implments the move. If move is valid, makes the
origin have a value of false and the destination have a value of true.
origin: Starting location for move.
destination: Ending location for move.
returns: True if move suceeds, false if it fails for any reason.
*/
public bool doMove(int origin, int destination)
{
// Do the origin and destination belong to the same node?
if (!currentNode(origin)) return false;
if (!currentNode(destination)) return false;
// Are they offset by 90 degrees?
if (!offset90(origin, destination)) return false;
// Is origin filled?
if (!isFilled(origin, board)) return false;
// Is the destination empty?
if (isFilled(destination, board)) return false;
// Do the move and return true.
board.getStick(origin).Location = destination;
return true;
}
// TODO: Review this method and try to clean up the code.
/*
Implments the fall. If the stick can fall, it falls as far as it can.
Returns a value of false if the stick cannot fall to position 2 for this
node.
This means that this method can return false even after a stick has been moved.
location: Location of stick to fall.
returns: True if stick can fall all the way to position 2, false if it cannot.
*/
public bool fall(int location)
{
// Does this location belong to this node?
if (!(currentNode(location))) return false;
// Does this location have a stick?
if (!isFilled(location, board)) return false;
// Is this at position 2
if (position[2] == location) return false; // when no move takes place, return false. True means more movement possible.
// Is this at position 0?
if (position[0] == location)
{
// Can it fall to position 1?
if ((!isFilled(position[1], board)) && (position[1] > -1))
{
doMove(position[0], position[1]);
return fall(position[1]);
}
// Can it fall to position 3?
else if ((!isFilled(position[3], board)) && (position[3] > -1))
{
doMove(position[0], position[3]);
return fall(position[3]);
}
}
// Is this at position 1?
else if (position[1] == location)
{
if ((!isFilled(position[2], board)) && (position[2] > -1))
{
return doMove(position[1], position[2]);
}
}
else if (position[3] == location)
{
if ((!isFilled(position[2], board)) && (position[2] > -1))
{
return doMove(position[3], position[2]);
}
// Is this at position 3?
}
return false;
}
/*
Whether a specified location is associated with the current node.
possible: location to be checked.
returns: true if the location is associated with this node, false otherwise.
*/
public bool currentNode(int location)
{
foreach (int x in position) {
if (x == location) return true;
}
return false;
}
// PRIVATE METHODS
/*
Whether a location has a stick with a value of true.
location: The location to be checked.
board: The board to check against.
returns: The value (true or false) of a stick at the location.
*/
private bool isFilled(int location, Board board)
{
return board.getPositionValue(location);
}
/*
Whether two locations are offset by 90 degrees around this node.
origin: One of the two locations.
destination: One of the two locations.
returns: true if and only if the two locations are 90 degrees offset.
*/
private bool offset90(int origin, int destination)
{
if ((nodePosition(origin) == 0) && (nodePosition(destination) == 2)) return false;
if ((nodePosition(origin) == 1) && (nodePosition(destination) == 3)) return false;
if ((nodePosition(origin) == 2) && (nodePosition(destination) == 0)) return false;
if ((nodePosition(origin) == 3) && (nodePosition(destination) == 1)) return false;
return true;
}
/*
Gets the node position of a location with respect to this node.
This only makes sense internal to this node.
returns: Node position (0 to 3) or 77 as an error code.
*/
private int nodePosition (int location)
{
for (int x = 0; x < 4; x++)
{
if (position[x] == location) return x;
}
return 77; // error code, somewhat hacky.
}
// TESTING CODE
/*
Gets the location associated with a specified element of a node.
This concept is meaningless within the program design.
DO NOT USE, EXCEPT IN TESTS
*/
public int getLocation (int temp)
{
if (temp < 0) return -1;
if (temp > 3) return -1;
return position[temp];
}
/*
Returns a null node for testing purposes.
DO NOT USE, EXCEPT IN TESTS
*/
public Node getNull() // I think this is a hack.
{
return null;
}
// OVERRIDES
public override int GetHashCode()
{
return (position[3] * 1000000) + (position[2] * 10000) + (position[1] * 100) + position[0];
}
public override bool Equals(System.Object obj)
{
if (obj != null) { // Needs to fail for null objects.
if (obj.GetType().Equals(this.GetType())) { // Check types. Might be redundant on next.
if (ReferenceEquals(this, obj)) { return true;} // Check reference equals.
}
}
return base.Equals(obj); // This is a Best Practice - if your equals fails, the base equals should be returned.
}
}
No comments:
Post a Comment