// 验证状态是否合法 private bool isLegalState(stateNode state) { return((state.priest_num >= state.devil_num || state.priest_num == 0) && ((3 - state.priest_num) >= (3 - state.devil_num) || (3 - state.priest_num) == 0) && (state.priest_num <= 3 && state.devil_num <= 3) && ((3 - state.priest_num) <= 3 && (3 - state.devil_num) <= 3)); }
// 图生成函数 private void generateGraph() { // 首先加入初始状态节点 nodes.Add(startState); for (int i = 0; i < nodes.Count; i++) { stateNode currentState = nodes[i]; foreach (move mv in prosibleMove) { stateNode nextState; if (currentState.boat) { nextState = currentState - mv; } else { nextState = currentState + mv; } if (isLegalState(nextState)) { nextState = addNewStateToGraph(nextState); currentState.addNextState(nextState); nextState.addNextState(currentState); } } } }
public stateGraph() { nodes = new List <stateNode>(); startState = new stateNode(3, 3, true); endState = new stateNode(0, 0, false); generateGraph(); }
public move getNextMove(stateNode currentState) { // 找到图中真正的节点 currentState = getGraphNode(currentState); stateNode next = getNextState(currentState); return(new move(Mathf.Abs(currentState.priest_num - next.priest_num), Mathf.Abs(currentState.devil_num - next.devil_num))); }
private stateNode getGraphNode(stateNode newState) { foreach (stateNode state in nodes) { if (state == newState) { return(state); } } return(null); }
// 找到最短路径中当前状态的下一步 private stateNode getNextState(stateNode from) { List <stateNode> alreadSearchState = new List <stateNode>(); alreadSearchState.Add(from); foreach (stateNode state in from.nextStates) { // 判断是不是终点 if (state == endState) { return(state); } else { state.start = state; alreadSearchState.Add(state); } } for (int i = 1; i < alreadSearchState.Count; i++) { foreach (stateNode state in alreadSearchState[i].nextStates) { // 判断是不是终点 if (state == endState) { state.start = alreadSearchState[i].start; return(state.start); } else { bool flag = false; // 检查是否已经搜索过 foreach (stateNode searchedState in alreadSearchState) { if (state == searchedState) { flag = true; break; } } if (!flag) { state.start = alreadSearchState[i].start; alreadSearchState.Add(state); } } } } return(null); }
// 加入相邻节点 public bool addNextState(stateNode nextState) { foreach (stateNode node in nextStates) { if (nextState == node) { return(false); } } nextStates.Add(nextState); return(true); }
public void Restart() { for (int i = 0; i < 3; i++) { Priests[i].Reset(); Priests[i].SetPosition(PrisetsOriginPositions[i]); Devils[i].Reset(); Devils[i].SetPosition(DevilsOriginPositions[i]); Devils[i].Idle(); } Boat.Reset(); gaming = true; currState = new stateNode(3, 3, true); }
// 将节点加入图中 private stateNode addNewStateToGraph(stateNode newState) { // 先判断列表里当前有没有 foreach (stateNode state in nodes) { if (state == newState) { return(state); } } nodes.Add(newState); return(newState); }
public bool gaming = true; // 用于判断游戏是否正在进行,由于目前还不存在起始界面,所以一开始游戏就开始了 void Awake() { speed = 4; SSDirector director = SSDirector.GetInstance(); director.CurrentSceneController = this; userGui = gameObject.AddComponent <UserGUI>() as UserGUI; director.CurrentSceneController.LoadResource(); judger = gameObject.AddComponent <Judger>() as Judger; actionManager = gameObject.AddComponent <CCActionManager>() as CCActionManager; graph = new stateGraph(); currState = new stateNode(3, 3, true); }
// 船行驶回右岸,相当于右岸多人了 public static stateNode operator +(stateNode from, move mv) { stateNode to = new stateNode(from.priest_num + mv.priest_num, from.devil_num + mv.devil_num, true); return(to); }
// 船行驶向左岸,相当于右岸少人了 public static stateNode operator -(stateNode from, move mv) { stateNode to = new stateNode(from.priest_num - mv.priest_num, from.devil_num - mv.devil_num, false); return(to); }