using System; //using System.Collections.Generic; //using System.Text; namespace MissinaryandCannibal { class State { // Instance fields public int MCount, CCount; public bool Side; private int firstN = 3; private String Name; private State PrevState; private int stateTL = 0; ///////////////////////////////////////////////////////////////////////////////////////////////// public State(String Name, int MCount, int CCount, bool Side, State PrevState, int stateTL) { //Assign parameters to local instance fields this.Name = Name; this.MCount = MCount; this.CCount = CCount; this.Side = Side; this.PrevState = PrevState; this.stateTL = stateTL; } //State Constructer (1), Use this for the root state //Simply creates a new State with the name, number of Missionaries, //number of Cannibals and side to match the values supplied by //the formal parameters. In this case there will be no PrevState as this //is the 1st state // //param : Name is the name for this State //param : MCount the number on Missionaries for this state //param : CCount the number on Cannibals for this state //param : Side the side of the river that the boat is now on //param : stateTL the level this state is on, 0=root / 1st layer, 1 = 2nd layer, 2 = 3rd layer public State(String Name, int MCount, int CCount, bool Side, int stateTL) : this(Name, MCount, CCount, Side, null, stateTL) { //Call the overloaded constructor with the formal parameters //provided, but make PrevState=null, as the 1st State does not //have a PrevState to point to //this(Name, MCount, CCount, Side, null, stateTL); } //State Constructer (2), Use this to create States based upon other States //Simply creates a new State with the name, number of Missionaries, //number of Cannibals,side and PrevState to match the values supplied by //the formal parameters. In this case PrevState will be a pointer to this //nodes parent node // // Name is the name for this State // // MCount the number on Missionaries for this state // / CCount the number on Cannibals for this state // //Side the side of the river that the boat is now on // //PrevState a pointer to this State's PrevState (parent) // // stateTL the level this state is on, 0=root / 1st layer, 1 = 2nd layer, 2 = 3rd layer public int getstateTL() { return this.stateTL; } public String getName() { return this.Name; } //Prints a full search path of how this state came to be at the //goal state. public void Print() { //Check that there is a PrevState, Root node will not have one, so //that is when all states from Goal - to start have been printed if (PrevState != null) { //Use recursion to allow Previous state to print its own data paths PrevState.Print(); } String whichSide = Side ? "~~~~~|_Boat_|~~~~~>" : "<~~~~|_Boat_| ~~~~~"; //Print the current state. Console.WriteLine(MCount + "Missionary & " + CCount + " Cannibal " + whichSide + " " + (firstN - MCount) + " Missionary &" + (firstN - CCount) + " Cannibal"); } public bool okChecked(State StateToCheck) { return (MCount == StateToCheck.MCount && CCount == StateToCheck.CCount && Side == StateToCheck.Side); } public bool wrongState() { int projType1 = 0; int projType2 = 0; //Check to see if the user requested that there be more Cannibals than //Missionaries. If this is the case set projType variables for this //situation if (mainclass.projType) { projType1 = CCount; projType2 = MCount; } //Otherwise set the siutation to be that there be more Missionaries than //Cannibals else { projType1 = MCount; projType2 = CCount; } // Check for < 0, which could actually happen unless it is checked for here if (MCount < 0 || CCount < 0 || MCount > firstN || CCount > firstN) return true; //Do projType2 outnumbers projType1(only worry when there is at least //one projType1) one Side1 if (projType1 < projType2 && projType1 > 0) return true; //Do projType2 outnumbers projType1(only worry when there is at least //one projType1) one Side2 if ( (firstN - projType1 < firstN - projType2) && (firstN - projType1 > 0)) return true; //At this point the State must be valid return false; } } //End of State class //#endregion }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)