- import java.util.ArrayList;
- public class Flash {
- static Body sheep;
- static Body dish;
- static Body wolf;
- static Body people;
- static Body tarBody; //当前运送的东西
- final static ArrayList<Body> tarList = new ArrayList<Body>();
- final static ArrayList<Body> curList = new ArrayList<Body>();
- /**
- * @param args
- */
- public static void main(String[] args) {
- //初始化羊
- sheep = new Body(Body.SHEEP);
- sheep.setnAfraidIndex(Body.WOLF);
- sheep.setnLikeIndex(Body.DISH);
- //初始化菜
- dish = new Body(Body.DISH);
- dish.setnAfraidIndex(Body.NONE);
- dish.setnLikeIndex(Body.NONE);
- //初始化狼
- wolf = new Body(Body.WOLF);
- wolf.setnAfraidIndex(Body.NONE);
- wolf.setnLikeIndex(Body.SHEEP);
- //初始化特殊的人
- 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){
- //随即取出一个备选的运送的Body
- boolean bFlag = false;
- if(!cur.equals(curList)){
- boolean bFlag2 = false;
- //比较剩下的Body,是否会出现弱者被吃掉的情况
- //首先取出一个Body,看下剩下的Body中有没有对自己不利的。。
- for(Body bb : cur){
- if(!bb.equals(people)){
- for(Body bbb : cur){
- if(!bbb.equals(people) && !bbb.equals(bb)){
- if(bbb.getnLikeIndex() == bb.getIndex() || bbb.getnAfraidIndex() == bb.getIndex()
- || bb.getnAfraidIndex() == 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("向目的地运送:空");
- }else{
- System.out.println("向目原来地方运送:空");
- }
- }else{
- for(Body b : cur){
- bFlag = false;
- if(!b.equals(people)){
- if(tarBody == null){
- //比较剩下的Body,是否会出现弱者被吃掉的情况
- //首先取出一个Body,看下剩下的Body中有没有对自己不利的。。
- 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;
- }
- }
- }
- }else if(!tarBody.equals(b)){
- //比较剩下的Body,是否会出现弱者被吃掉的情况
- //首先取出一个Body,看下剩下的Body中有没有对自己不利的。。
- 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 = "菜";
- break;
- case Body.SHEEP:
- str = "羊";
- break;
- case Body.WOLF:
- str = "狼";
- break;
- }
- if(flag == 1){
- System.out.println("向目的地运送:"+str);
- }else{
- System.out.println("向目原来地方运送:"+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){
- //比较剩下的Body,是否会出现弱者被吃掉的情况
- //首先取出一个Body,看下剩下的Body中有没有对自己不利的。。
- 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;
- }
- }
- }
- }else if(!tarBody.equals(b)){
- //比较剩下的Body,是否会出现弱者被吃掉的情况
- //首先取出一个Body,看下剩下的Body中有没有对自己不利的。。
- 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 = "菜";
- break;
- case Body.SHEEP:
- str = "羊";
- break;
- case Body.WOLF:
- str = "狼";
- break;
- }
- if(flag == 1){
- System.out.println("向目的地运送:"+str);
- }else{
- System.out.println("向目原来地方运送:"+str);
- }
- tarBody = b;
- tar.add(b);
- tar.add(people);
- cur.remove(b);
- cur.remove(people);
- break;
- }
- }
- }
- }
- }
- }
- 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;
- }
- }