Programming to solve a variety of problems (i)----------------Flash Classic puzzle Game (a) human lamb dishes and the story of the Wolf __ programming

Source: Internet
Author: User

First question: To help men ship wolves, sheep, and a box of vegetables across the river, boats can only be transported at once, and there are harsh conditions attached: when a man is absent, wolves eat sheep and sheep eat cabbage. Move your mouse and start solving the problem.

This has a game. Address: http://www.chinaren.com/20050330/n224934721.shtml


Have not played the best play before you see my code, do not understand can ask me, write more chaotic ...

One has two classes, this is the main processing class


Import java.util.ArrayList;
public class Flash {static Body sheep;
	
	static Body dish;
	
	Static Body Wolf;
	
	static Body people;		Static Body Tarbody;
	What is currently being shipped final static arraylist<body> tarlist = new arraylist<body> ();
	
	Final static arraylist<body> curlist = new arraylist<body> ();
		
		/** * @param args */public static void main (string[] args) {//Initialize sheep sheep = new Body (body.sheep);
		Sheep.setnafraidindex (Body.wolf);
		
		Sheep.setnlikeindex (Body.dish);
		
		Initial dish dish = new Body (body.dish);
		Dish.setnafraidindex (Body.none);
		
		Dish.setnlikeindex (Body.none);
		
		init wolf = new Body (Body.wolf);
		Wolf.setnafraidindex (Body.none);
		
		Wolf.setnlikeindex (Body.sheep);
		
		Initializes a special person people = new Body (body.none);
		People.setnafraidindex (Body.none);
		
		People.setnlikeindex (Body.none);
		Curlist.add (people);
		Curlist.add (Wolf);
		Curlist.add (sheep);
		
		Curlist.add (dish); New Thread () {public void run () {while (Tarlist.toarray (). Length < 4) {if (Curlist.indexof (people)! =-1) {Transportation (tarlist,curlist,1);
					}else{Transportation (curlist,tarlist,0);
	}}}}.start (); } public static void transportation (arraylist<body> tar,arraylist<body> cur,int flag) {//immediately takes out an alternative shipping b
		Ody Boolean bflag = false;
			if (!cur.equals (Curlist)) {Boolean bFlag2 = false;
			Compare the remaining body, whether there will be the situation of the weak is eaten//first Take out a body, look at the rest of the body there is no harm to themselves. for (body bb:cur) {if (!bb.equals (people)) {if (body bbb:cur) {if (!bbb.equals (people) &&!bbb.equ ALS (BB)) {if (bbb.getnlikeindex () = = Bb.getindex () | | Bbb.getnafraidindex () = = Bb.getindex () | | bb.getnafra Idindex () = = Bbb.getindex () | |
								Bb.getnlikeindex () = = Bbb.getindex ()) {BFlag2 = true;
							Break
					}}} if (BFlag2) {break;
				}}} if (Cur.toarray (). length = = 2 | | bFlag2 = = false) {Tar.add (people);
				Cur.remove (people); if (flag = = 1) {SysTem.out.println ("Ship to Destination: empty");
				}else{System.out.println ("Delivery to original place: empty");
					}}else{for (Body b:cur) {bflag = false; if (!b.equals (people)) {if (tarbody = = null) {//Compare the remainder of the body, whether there will be a situation where the weak is eaten//first Take out a body, look at the rest of the body there is no harm to themselves
							。。 for (body bb:cur) {if (!bb.equals (b) &&!bb.equals (People)) {for (body bbb:cur) {if (!b Bb.equals (b) &&!bbb.equals (People) &&!bbb.equals (BB)) {if (bbb.getnlikeindex () = = Bb.getindex ( ) || Bbb.getnafraidindex () = = Bb.getindex () | | Bb.getnafraidindex () = = Bbb.getindex () | |
												Bb.getnlikeindex () = = Bbb.getindex ()) {bflag = true;
											Break
									}}} if (Bflag) {break; }}}}else if (!tarbody.equals (b)) {//Compare the remaining body, whether there will be a case of the weak being eaten///First Take out a body and see if the remaining body has
							Have the disadvantage to oneself. for (Body bb:cur) {if (!bb.equals (b) &&!bb.equals (people)) {for (Body bbb:cur) {if (!bbb.equals (b) &&!bbb.equals (People) &&!bbb.equals (BB)) { if (bbb.getnlikeindex () = = Bb.getindex () | | Bbb.getnafraidindex () = = Bb.getindex () | | bb.getnafraidi Ndex () = = Bbb.getindex () | |
												Bb.getnlikeindex () = = Bbb.getindex ()) {bflag = true;
											Break
									}}} if (Bflag) {break;
							}}}} if (!bflag) {String str = "";
								Switch (B.getindex ()) {Case Body.DISH:str = "dish";
							Break
								Case Body.SHEEP:str = "sheep";
							Break
								Case Body.WOLF:str = "Wolf";
							Break
							} if (flag = = 1) {System.out.println ("Ship To Destination:" +STR);
							}else{System.out.println ("Delivery to the original place:" +STR);
							} tarbody = b;
							Tar.add (b);
							Tar.add (people);
							Cur.remove (b);
							Cur.remove (people);
						Break
}
					}				}}}else{for (Body b:cur) {bflag = false; 
						if (!b.equals (people)) {if (tarbody = = null) {//Compare the remainder of the body, whether there will be a situation where the weak is eaten//first Take out a body, look at the rest of the body there is no harm to themselves. for (body bb:cur) {if (!bb.equals (b) &&!bb.equals (People)) {for (body bbb:cur) {i F (!bbb.equals (b) &&!bbb.equals (People) &&!bbb.equals (BB)) {if (bbb.getnlikeindex () = = Bb.getind EX () | | Bbb.getnafraidindex () = = Bb.getindex () | | Bb.getnafraidindex () = = Bbb.getindex () | |
											Bb.getnlikeindex () = = Bbb.getindex ()) {bflag = true;
										Break
								}}} if (Bflag) {break; }}}}else if (!tarbody.equals (b)) {//Compare the remaining body, whether there will be a case of the weak being eaten///First Take out a body, look at the rest of the body there is no
						Lee's.. for (body bb:cur) {if (!bb.equals (b) &&!bb.equals (People)) {for (body bbb:cur) {if (!bbb. Equals (b) &&!bbb.equals (people) &&!Bbb.equals (BB)) {if (bbb.getnlikeindex () = = Bb.getindex () | | Bbb.getnafraidindex () = = Bb.getindex () | | Bb.getnafraidindex () = = Bbb.getindex () | |
											Bb.getnlikeindex () = = Bbb.getindex ()) {bflag = true;
										Break
								}}} if (Bflag) {break;
						}}}} if (!bflag) {String str = "";
							Switch (B.getindex ()) {Case Body.DISH:str = "dish";
						Break
							Case Body.SHEEP:str = "sheep";
						Break
							Case Body.WOLF:str = "Wolf";
						Break
						} if (flag = = 1) {System.out.println ("Ship To Destination:" +STR);
						}else{System.out.println ("Delivery to the original place:" +STR);
						} tarbody = b;
						Tar.add (b);
						Tar.add (people);
						Cur.remove (b);
						Cur.remove (people);
					Break }
				}
			}
		}
		
	}
}
This is the object class, I put the human food wolf and sheep to the object
public class Body {public
		
	static final int NONE = 0;
	public static final int SHEEP = 1;
	public static final int DISH = 2;
	public static final int WOLF = 3;
	
	private int index;
	
	private int nlikeindex;
	
	private int nafraidindex;
	
	public Body (int index) {
		this.index = index;
	}


	public int GetIndex () {
		return index;
	}


	public void Setindex (int index) {
		this.index = index;
	}


	public int Getnlikeindex () {
		return nlikeindex;
	}


	public void Setnlikeindex (int nlikeindex) {
		this.nlikeindex = Nlikeindex;
	}


	public int Getnafraidindex () {
		return nafraidindex;
	}


	public void Setnafraidindex (int nafraidindex) {
		this.nafraidindex = Nafraidindex;
	}	
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.