//运输人和青菜 int TransferPeopleWithGreens(int i) => i ^ 9;
//运输人和羊 int TransferPeopleWithSheep(int i) => i ^ 10;
//运输人和狼 int TransPeopleWithWolf(int i) => i ^ 12;
//运输人和狼 int TransPeople(int i) => i ^ 8; Transport transport1 = new Transport("人和菜", IsPeopleWithGreens, ValidEndState, TransferPeopleWithGreens); Transport transport2 = new Transport("人和羊", IsPeopleWithSheep, ValidEndState, TransferPeopleWithSheep); Transport transport3 = new Transport("人和狼", IsPeopleWithWolf, ValidEndState, TransPeopleWithWolf); Transport transport4 = new Transport("人", state => true, ValidEndState, TransPeople); List<Transport> transports = new List<Transport> {transport1, transport2, transport3, transport4}; int tempState = 0; int EndState = 15; while (tempState != EndState) { foreach (var actionItem in transports) { tempState = actionItem.Transfer(tempState); } }
public void Move() { int state = 0, tempState; int EndState = 15; while (state != EndState) { tempState = MovePerson(state); if (IsVaild(tempState)) { state = tempState; Console.WriteLine(state > 8 ? "人独自一人从A岸到达B岸" : "人独自一人从B岸到达A岸"); }
if (IsPeopleWithSheep(state)) { tempState = MovePersonWithSheep(state); if (IsVaild(tempState)) { state = tempState; Console.WriteLine(state > 8 ? "人带羊从A岸到达B岸" : "人带羊从B岸到达A岸"); } }
if (IsPeopleWithWolf(state)) { tempState = MovePersonWithWolf(state); if (IsVaild(tempState)) { state = tempState; Console.WriteLine(state > 8 ? "人带狼从A岸到达B岸" : "人带狼从B岸到达A岸"); } }
if (IsPeopleWithGreens(state)) { tempState = MovePersonWithGreens(state); if (IsVaild(tempState)) { state = tempState; Console.WriteLine(state > 8 ? "人带菜从A岸到达B岸" : "人带菜从B岸到达A岸"); } } } }
执行以上逻辑函数
1 2 3 4
Console.WriteLine("MoveClass 开始"); new MoveClass().Move(); Console.WriteLine("MoveClass 结束"); Console.ReadKey();