//复制构造函数 public PristsAndDevilsState(PristsAndDevilsState temp) { this.leftPriests = temp.leftPriests; this.rightPriests = temp.rightPriests; this.leftDevils = temp.leftDevils; this.rightDevils = temp.rightDevils; this.boat_pos = temp.boat_pos; this.parent_state = temp.parent_state; }
//带参构造函数 public PristsAndDevilsState(int leftPriests, int leftDevils, int rightPriests, int rightDevils, bool boat_pos, PristsAndDevilsState parent_state) { this.leftPriests = leftPriests; this.rightPriests = rightPriests; this.leftDevils = leftDevils; this.rightDevils = rightDevils; this.boat_pos = boat_pos; this.parent_state = parent_state; }
void Start() { action = Director.getInstance().sceneController as UserAction; style = new GUIStyle(); style.fontSize = 15; style.alignment = TextAnchor.MiddleLeft; style2 = new GUIStyle(); style2.fontSize = 30; style2.alignment = TextAnchor.MiddleCenter; end = new PristsAndDevilsState(0, 0, 3, 3, false, null); }
public override bool Equals(object obj) { if (obj == null) { return(false); } if (obj.GetType().Equals(this.GetType()) == false) { return(false); } PristsAndDevilsState temp = (PristsAndDevilsState)obj; return(this.leftPriests.Equals(temp.leftPriests) && this.rightPriests.Equals(temp.rightPriests) && this.rightDevils.Equals(temp.rightDevils) && this.leftDevils.Equals(temp.leftDevils) && this.boat_pos.Equals(temp.boat_pos)); }
public static PristsAndDevilsState BFS(PristsAndDevilsState start, PristsAndDevilsState end) { //存放到达目的的路径状态 Queue <PristsAndDevilsState> found = new Queue <PristsAndDevilsState>(); PristsAndDevilsState temp = new PristsAndDevilsState(start.leftPriests, start.leftDevils, start.rightPriests, start.rightDevils, start.boat_pos, null); //当前状态入队 found.Enqueue(temp); //队列元素数目大于0 while (found.Count > 0) { temp = found.Peek(); //当状态等于末状态时,可以终止继续寻找,只需通过parent_state来找出开始状态的下一个状态即可 //当然也可以把整个路径显示出来 if (temp == end) { while (temp.parent_state != start) { temp = temp.parent_state; } return(temp); } found.Dequeue(); // 判断该状态的船的位置,船若在左边 if (temp.boat_pos) { // 将一个牧师移动到右边 if (temp.leftPriests > 0) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = false; next.leftPriests--; next.rightPriests++; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将一个恶魔移动到右边 if (temp.leftDevils > 0) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = false; next.leftDevils--; next.rightDevils++; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将一个牧师以及一个恶魔移动到右边 if (temp.leftDevils > 0 && temp.leftPriests > 0) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = false; next.leftDevils--; next.rightDevils++; next.leftPriests--; next.rightPriests++; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将两个牧师移动到右边 if (temp.leftPriests > 1) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = false; next.leftPriests -= 2; next.rightPriests += 2; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将两个恶魔移动到右边 if (temp.leftDevils > 1) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = false; next.leftDevils -= 2; next.rightDevils += 2; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } } // 判断该状态的船的位置,船若在右边 else { // 将一个牧师移动到左边 if (temp.rightPriests > 0) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = true; next.rightPriests--; next.leftPriests++; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将一个恶魔移动到右边 if (temp.rightDevils > 0) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = true; next.rightDevils--; next.leftDevils++; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将一个牧师一个恶魔移动到右边 if (temp.rightDevils > 0 && temp.rightPriests > 0) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = true; next.rightDevils--; next.leftDevils++; next.rightPriests--; next.leftPriests++; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将两个恶魔移动到右边 if (temp.rightDevils > 1) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = true; next.rightDevils -= 2; next.leftDevils += 2; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } // 将两个牧师移动到右边 if (temp.rightPriests > 1) { PristsAndDevilsState next = new PristsAndDevilsState(temp); next.parent_state = new PristsAndDevilsState(temp); next.boat_pos = true; next.rightPriests -= 2; next.leftPriests += 2; if (next.isValid() && !found.Contains(next)) { found.Enqueue(next); } } } } return(null); }
void OnGUI() { if (status == -1) { GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 65, 100, 50), "Gameover!", style2); } else if (status == 1) { GUI.Label(new Rect(Screen.width / 2 - 50, Screen.height / 2 - 65, 100, 50), "You win!", style2); } buttonStyle = new GUIStyle("button"); buttonStyle.fontSize = 15; if (GUI.Button(new Rect(Screen.width / 2 - 50, 20, 100, 50), "Restart", buttonStyle)) { status = 0; action.restart(); } if (GUI.Button(new Rect(Screen.width / 2 - 50, 80, 100, 50), "Rule", buttonStyle)) { show = true; } GUI.Label(new Rect(Screen.width / 2 - 270, Screen.height / 2 - 165, 100, 50), tips, style); if (GUI.Button(new Rect(Screen.width / 2 - 170, 20, 100, 50), "Tips", buttonStyle)) { int[] arr = action.getNum(); leftPriests = arr[0]; leftDevils = arr[1]; rightPriests = arr[2]; rightDevils = arr[3]; Debug.Log(arr[4]); if (arr[4] == 0) { boat_pos = true; } else { boat_pos = false; } start = new PristsAndDevilsState(leftPriests, leftDevils, rightPriests, rightDevils, boat_pos, null); Debug.Log(start.leftPriests + " " + start.leftDevils + " " + start.rightPriests + " " + start.rightDevils + " " + start.boat_pos); Debug.Log(end.leftPriests + " " + end.leftDevils + " " + end.rightPriests + " " + end.rightDevils + " " + end.boat_pos); //bug PristsAndDevilsState temp = PristsAndDevilsState.BFS(start, end); leftPriests = temp.leftPriests; leftDevils = temp.leftDevils; rightPriests = temp.rightPriests; rightDevils = temp.rightDevils; tips = "try to make\nleftPriests : " + leftPriests + "\nleftDevils : " + leftDevils + "\nrightPriests : " + rightPriests + "\nrightDevils : " + rightDevils; } if (GUI.Button(new Rect(Screen.width / 2 - 170, 80, 100, 50), "Hide Tips", buttonStyle)) { tips = ""; } if (show) { GUI.Label(new Rect(Screen.width / 2 + 70, 20, 100, 100), "游戏规则:\n白色正方体为牧师,黑色球体为魔鬼。\n" + "船只最多能容纳两人,有人在船上才可开船\n" + "当某一岸恶魔数量大于牧师数量,游戏失败!\n" + "牧师与恶魔全部渡过河流,游戏胜利!\n", style); } }