示例#1
0
	private bool visit(Coordination cord, ArrayList list){
		for (int i = 0; i < list.Count; i++) {
			Coordination temp = list[i] as Coordination;
			if(cord.Equals(temp))
				return true;
		}
		
		return false;
	}
示例#2
0
	//find path almost connected
	//c -> starting coordinate | path -> arraylist to record checked path | length -> board size | skipped -> check if skipped once already
	private bool findCheckmatePath(Coordination c, ArrayList path, int length, int end, bool skipped, Coordination targetCoor){
		//is the path found
		bool pathFound = false;
		bool preSkipped = skipped;

		//if path finding finished, return blank targetCoor
		if (c.getX () == end) {
			Debug.Log ("<color=red>Found end</color>");
			path.Add (targetCoor);
			return true;
		}

		//find path upward
		if (!pathFound && c.getX () - 1 >= 0) {
			Coordination coor = new Coordination (c.getX () - 1, c.getY ());
			if (visitCol [c.getX () - 1, c.getY ()] == true) { 
				if (!checkCoorEqual(path, coor.getX(), coor.getY()) && targetCoor.Equals(coor) == false) {
					Debug.Log ("<color=orange>Check Coor: x," + coor.getX() + ", " + coor.getY() + "</color>");
					path.Add (coor);
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
				}
			}
		}

		//find path downward
		if (!pathFound && c.getX () + 1 <= (length)) {
			Coordination coor = new Coordination (c.getX () + 1, c.getY ());
			if (visitCol [c.getX (), c.getY ()] == true) {
				if (!checkCoorEqual(path, coor.getX(), coor.getY()) && targetCoor.Equals(coor) == false) {
					Debug.Log ("<color=orange>Check Coor: x," + coor.getX() + ", " + coor.getY() + "</color>");
					path.Add (coor);
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
				}
			}
		}

		Debug.Log (pathFound);
		//find path at left
		if(pathFound == false && c.getY() - 1 >= 0){
			Coordination coor = new Coordination (c.getX (), c.getY ()-1);
			if(visitRow[c.getX(), c.getY()-1] == true){
				if (!checkCoorEqual(path, coor.getX(), coor.getY()) && targetCoor.Equals(coor) == false) {
					Debug.Log ("<color=orange>Check Coor: x," + coor.getX() + ", " + coor.getY() + "</color>");
					path.Add (coor);
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
				}
			}
		}

		//find path at right
		if(!pathFound && c.getY() + 1 <= (length-1)){
			Coordination coor = new Coordination (c.getX (), c.getY ()+1);
			if(visitRow[c.getX(), c.getY()] == true){
				if (!checkCoorEqual(path, coor.getX(), coor.getY()) && targetCoor.Equals(coor) == false) {
					Debug.Log ("<color=orange>Check Coor: x," + coor.getX() + ", " + coor.getY() + "</color>");
					path.Add (coor);
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
				}
			}
		}

		//Skip upward
		if (!pathFound && c.getX () - 1 >= 0) {
			Coordination coor = new Coordination (c.getX () - 1, c.getY ());
			if (visitCol [c.getX () - 1, c.getY ()] != true) { 
				if (!skipped && !checkCoorEqual (path, coor.getX (), coor.getY ()) && !blockedCol [c.getX () - 1, c.getY ()] && targetCoor.Equals(coor) == false) {
					Debug.Log ("<color=orange>Skip to Coor: x," + coor.getX () + ", " + coor.getY () + "</color>");
					skipped = true;
					checkmateCol = false;
					skipLeft = false;
					skipUp = true;
					targetCoor = coor;
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
					if (!pathFound)
						skipped = false;
				}
			}
		}

		//Skip downward
		if (!pathFound && c.getX () + 1 <= (length)) {
			Coordination coor = new Coordination (c.getX () + 1, c.getY ());
			if (visitCol [c.getX (), c.getY ()] != true) {
				if (!skipped && !checkCoorEqual(path, coor.getX(), coor.getY()) && !blockedCol[c.getX(), c.getY()]  && targetCoor.Equals(coor) == false) {
					Debug.Log ("<color=orange>Skip to Coor: x," + coor.getX() + ", " + coor.getY() + "</color>");
					skipped = true;
					checkmateCol = false;
					skipLeft = false;
					skipUp = false;
					targetCoor = coor;
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
					if (!pathFound)
						skipped = false;
				}
			}
		}

		//Skip left
		if(pathFound == false && c.getY() - 1 >= 0){
			Coordination coor = new Coordination (c.getX (), c.getY ()-1);
			if(visitRow[c.getX(), c.getY()-1] != true){
				if (!skipped && !checkCoorEqual(path, coor.getX(), coor.getY()) && !blockedRow[c.getX(), c.getY()-1] && targetCoor.Equals(coor) == false) {
					Debug.Log (pathFound);
					Debug.Log ("<color=orange>Skip to Coor: x," + coor.getX() + ", " + coor.getY() + "</color>");
					skipped = true;
					checkmateCol = true;
					skipLeft = true;
					skipUp = false;
					targetCoor = coor;
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
					if (!pathFound)
						skipped = false;
				}
			}
		}

		//Skip right
		if(!pathFound && c.getY() + 1 <= (length-1)){
			Coordination coor = new Coordination (c.getX (), c.getY ()+1);
			if(visitRow[c.getX(), c.getY()] != true){
				if (!skipped && !checkCoorEqual(path, coor.getX(), coor.getY()) && !blockedRow[c.getX(), c.getY()]  && targetCoor.Equals(coor) == false) {
					Debug.Log ("<color=orange>Skip to Coor: x," + coor.getX() + ", " + coor.getY() + "</color>");
					skipped = true;
					checkmateCol = true;
					skipLeft = false;
					skipUp = false;
					targetCoor = coor;
					pathFound = findCheckmatePath (coor, path, length, end, skipped, targetCoor);
					if (!pathFound)
						skipped = false;
				}
			}
		}

		//if path not connected yet, return a null coordination
		if (!pathFound) {
			Debug.Log ("<color=red>Remove Coor: x," + c.getX () + ", " + c.getY () + "</color>");
			//path.Remove (c);
			//skipped = preSkipped;
			skipped = false;
			return false;
		} else
			return true;

		skipped = false;
		return false;
	}