Madcap (zimdanen) wrote,
Madcap
zimdanen

Can anyone tell me why this isn't working?

queue.Add(states[missionaries,cannibals,boat] =
CheckIfSafe(0, null, missionaries, cannibals, boat));


It tells me that the array index was out of bounds. I don't see how that's possible.

relevant code:

int missionaries = NUMBER_OF_MISSIONARIES;
int cannibals = NUMBER_OF_CANNIBALS;
int boat = ON_THIS_SIDE;

///
/// Node array to keep track of different states, and
/// the Queue that helps solve the problem.
///

private static Node[, ,] states = new Node
[NUMBER_OF_MISSIONARIES + 1,
NUMBER_OF_CANNIBALS + 1,
NUMBER_OF_SHORES];
private static Queue queue = new Queue();

///
/// NOT MAGIC NUMBERS!!
///

private static readonly bool SAFE = true;
private static readonly bool NOT_SAFE = false;
private static readonly int ON_THIS_SIDE = 0;
private static readonly int ON_OTHER_SIDE = 1;
private static readonly int NUMBER_OF_MISSIONARIES = 3;
private static readonly int NUMBER_OF_CANNIBALS = 3;
private static readonly int NUMBER_OF_SHORES = 2;

private static Node CheckIfSafe(int l, Node b, int m, int c, int boat)
{
bool isSafe = SAFE;
if ((c > m && m != 0) || ((NUMBER_OF_CANNIBALS - c)
> (NUMBER_OF_MISSIONARIES - m)
&& m!=NUMBER_OF_MISSIONARIES)) isSafe = NOT_SAFE;
string toString = "" + m;
toString += c;
if (boat == ON_THIS_SIDE) toString += "b";
toString += "|" + (NUMBER_OF_MISSIONARIES - m);
toString += (NUMBER_OF_CANNIBALS - c);
if (boat == ON_OTHER_SIDE) toString += "b";
return new Node(l, b, isSafe, toString);
}


Nevermind, I figured it out while walking to class. I'm accessing [3,3,1] instead of [2,2,1]. I think I'm just going to make the array bigger and pretend I'm coding in Basic.

I made the array bigger but I'm still getting the exception. WTF?! HELP!


I don't think anyone quite understands how much I appreciate my brother. Thanks, Alex!
Subscribe
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 2 comments