我正在尝试制作一个解决日本益智游戏“河流智商测试”的程序,我知道,我可以查找解决方案,但现在不会有趣和有教育意义吗?:)
这是游戏的目标:
使用木筏,用户可以运送人过河,用户必须将所有的人从左侧运送到右侧。每次使用木筏时,它都会停留在另一侧,直到另一侧的人再次将它引到另一侧以吸引更多人。
左边有以下人开始:
- 1 囚犯
- 1名警察
- 1 父亲
- 2 个儿子
- 1 母亲
- 2个女儿
下面是类的层次结构:
乘客
- 飞行员
- 家长
- 母亲
- 父亲
- 警察
- 家长
- 囚犯
- 孩子
- 女儿
- 儿子
必须遵守以下规则:
- 一次只有 2 个人在木筏上。
- 只有警察、父亲和母亲才能驾驶木筏
- 在没有警察在场的情况下,囚犯不能在其他人在场的情况下被安置在木筏上或河的任何一侧。
- 没有母亲在场,父亲不能和女儿一起在木筏上或河的两边。
- 没有父亲在场,母亲不能和儿子一起在木筏上或河的两边。
我还需要完成的是:
- 使用试错解决逻辑直到解决难题的逻辑
- 打印最终成功解决方案的逻辑
- 检查是否每个人都到达另一边的逻辑。
这是我当前的代码:
程序类(解决逻辑应该放在这里)
class Program
{
Side _leftSide = new Side(Side.RL_Side.RL_LeftSide);
Side _rightSide = new Side(Side.RL_Side.RL_RightSide);
Raft _myRaft = new Raft();
static void Main(string[] args)
{
// TODO: put systematic trial-and-error solving logic here
// TODO: make sure that successful solution is printed to console
Console.ReadLine();
}
}
乘客列表类
public class PassengerList : List<Passenger>
{
public bool CheckRulesObeyed()
{
bool isPoliceman = isPresent<Policeman>();
bool isPrisoner = isPresent<Prisoner>();
bool isFather = isPresent<Father>();
bool isSon = isPresent<Son>();
bool isMother = isPresent<Mother>();
bool isDaughter = isPresent<Daughter>();
// ----------------------------------------------
bool isPrisoner_NonPoliceman = (isPrisoner && (isFather || isMother || isDaughter || isSon));
bool isPrisonerRuleViolated = (!(isPoliceman && isPrisoner) && isPrisoner_NonPoliceman);
bool isDaughterRuleViolated = ((isFather && isDaughter) && !isMother);
bool isSonRuleViolated = ((isMother && isSon) && !isFather);
bool AreAllRulesObeyed = !(isPrisonerRuleViolated && isDaughterRuleViolated && isSonRuleViolated);
return AreAllRulesObeyed;
}
private bool isPresent<T>() where T: Passenger
{
foreach (Passenger p in this)
{
if (p is T)
return true;
}
return false;
}
}
Side Class(如在河边)
public class Side
{
public enum RL_Side
{
RL_RightSide,
RL_LeftSide
}
public RL_Side _whichSide;
public PassengerList _myPeople;
public Side(RL_Side side)
{
_whichSide = side;
_myPeople = new PassengerList();
// left side starts with all the people
if (_whichSide == RL_Side.RL_LeftSide)
{
_myPeople.Add(new Prisoner());
_myPeople.Add(new Policeman());
_myPeople.Add(new Father());
_myPeople.Add(new Son());
_myPeople.Add(new Son());
_myPeople.Add(new Mother());
_myPeople.Add(new Daughter());
_myPeople.Add(new Daughter());
}
}
public bool didEveryoneMakeItToRightSide()
{
if (_whichSide == RL_Side.RL_RightSide)
{
// TODO: implement logic that checks and returns if everyone is on the right side.
}
return false;
}
}
筏类
public class Raft
{
public Side _mySide;
public PassengerList _myPassengers;
public void ChangeSides(Side Destination)
{
_mySide = Destination;
}
public bool LoadRaft(Pilot myPilot, Passenger myPassenger)
{
bool areRulesObeyed = true;
_myPassengers.Add(myPilot);
_myPassengers.Add(myPassenger);
areRulesObeyed = _myPassengers.CheckRulesObeyed();
if (areRulesObeyed == false)
{
UnloadRaft();
}
return areRulesObeyed;
}
public void UnloadRaft()
{
foreach (Passenger p in _myPassengers)
{
_mySide._myPeople.Add(p);
_myPassengers.Remove(p);
}
}
}