人狼羊菜2018年对象版本

新建Transport类,因方法和属性都有对应的详细说明,故不再赘述。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// <summary>
/// 运输类
/// </summary>
public class Transport
{
/// <summary>
/// 运输对象名称
/// </summary>
public string ObjectName { get; set; }

/// <summary>
/// 运输方向描述
/// </summary>
public string DirectionDescription { get; set; }

/// <summary>
/// 校验初始状态的函数
/// </summary>
Func<int, bool> ValidInitStateFuc { get; set; }

/// <summary>
/// 校验结束状态的函数
/// </summary>
Func<int, bool> ValidEndStateFuc { get; set; }

/// <summary>
/// 单次运输函数
/// </summary>
Func<int, int> MoveFunc { get; set; }

public Transport(string objectName, Func<int, bool> validInitState, Func<int, bool> validEndState,
Func<int, int> moveFunc)
{
this.ObjectName = objectName;
this.ValidInitStateFuc = validInitState;
this.ValidEndStateFuc = validEndState;
this.MoveFunc = moveFunc;
}

/// <summary>
/// 运输方法
/// </summary>
/// <param name="state">初始状态</param>
/// <returns>运输之后的状态</returns>
public int Transfer(int state)
{
if (ValidInitStateFuc(state))
{
int result = MoveFunc(state);
if (ValidEndStateFuc(result))
{
DirectionDescription = result > 8 ? "从A岸到了B岸。" : "从B岸到了A岸。";
Console.WriteLine(ObjectName + DirectionDescription);
return result;
}
}

return state;
}
}

调用方法如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
static void Main(string[] args)
{
Console.WriteLine("transports 开始");

//判断是否合法状态
bool ValidEndState(int state) => state != 3 && !(state >= 6 && state <= 9) && state != 12;

//判断人是否和羊在一起
bool IsPeopleWithSheep(int state) => state / 2 % 2 == 0 && state < 8 || state / 2 % 2 == 1 && state >= 8;

//判断人是否和狼在一起
bool IsPeopleWithWolf(int state) => !(state >= 4 && state <= 11);

//判断人是否和菜在一起
bool IsPeopleWithGreens(int state) => state % 2 == 0 && state < 8 || state % 2 == 1 && state >= 8;

//运输人和青菜
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);
}
}

Console.WriteLine("transports 结束");
Console.ReadKey();
}

运行结果如下

1
2
3
4
5
6
7
8
9
transports 开始
人和羊从A岸到了B岸。
人从B岸到了A岸。
人和菜从A岸到了B岸。
人和羊从B岸到了A岸。
人和狼从A岸到了B岸。
人从B岸到了A岸。
人和羊从A岸到了B岸。
transports 结束

参考资料

作者

stone liu

发布于

2018-10-31

更新于

2024-09-06

许可协议

评论