Inheritance and polymorphic implementation of Java farmer crossing the river
Title Description:
A farmer takes a wolf, a sheep and a cabbage to cross the river. There is only one boat, and the farmer can only take one animal or object to cross the river at most at a time. When the farmer is away, the wolf will eat the sheep and the sheep will eat the cabbage., Please design a program. The user selects the character to cross the river, and the system automatically judges the outcome of the game: when a creature is eaten, the game fails, all the characters go to the other side of the river, and the game succeeds. Don't say much, just look at the code:
package Test1; import java.util.Scanner; public class Client { static Farmer farmer = new Farmer(); public static void menu() { System.out.println("==================Please choose operation============"); System.out.println("\t==========1:Cross the river alone==========="); System.out.println("\t==========2:Cross the river with ("+farmer.wolf.name+")"+"========="); System.out.println("\t==========3:Cross the river with ("+farmer.sheep.name+")"+"============"); System.out.println("\t==========4:Cross the river with ("+farmer.cabbage.name+")"+"=========="); System.out.println("\t==========0:Quit==============="); System.out.println("==================================================="); System.out.println("Input the number(0~4):"); } public static void show()/* 输出农夫、各种动物、物品的状态(生存、位置) */ { System.out.println("过河状态:"); System.out.println(farmer.sheep.name+": 是否在左边河:"+farmer.sheep.is_across_left+ " 是否在右边河"+farmer.sheep.is_across_right+" 是否存活:"+farmer.sheep.is_alive); //如果羊过河成功则河左边显示false河右边显示true System.out.println(farmer.cabbage.name+": 是否在左边河:"+farmer.cabbage.is_across_left+ " 是否在右边河"+farmer.cabbage.is_across_right+" 是否存活:"+farmer.cabbage.is_alive); //如果白菜过河成功则河左边显示false河右边显示true System.out.println(farmer.wolf.name+": 是否在左边河:"+farmer.wolf.is_across_left+ " 是否在右边河"+farmer.wolf.is_across_right+" 是否存活:"+farmer.wolf.is_alive); //如果狼过河成功则河左边显示false河右边显示true System.out.println("农夫: 是否在左边河:"+farmer.is_across_left+" 是否在右边河"+farmer.is_across_right); } public static void is_alive() { //判断羊和白菜是否被吃 if(farmer.sheep.is_across_left==farmer.wolf.is_across_left&&farmer.sheep.is_across_right==farmer.wolf.is_across_right &&farmer.is_across_left==farmer.sheep.is_across_right&&farmer.is_across_right==farmer.sheep.is_across_left &&farmer.is_across_left==farmer.wolf.is_across_right&&farmer.is_across_right==farmer.wolf.is_across_left) { //如果羊和狼在同一边且农夫在另外一边时则羊会被吃 farmer.sheep.is_alive=false; } if(farmer.sheep.is_across_left==farmer.cabbage.is_across_left&&farmer.sheep.is_across_right==farmer.cabbage.is_across_right &&farmer.is_across_left==farmer.cabbage.is_across_right&&farmer.is_across_right==farmer.cabbage.is_across_left &&farmer.is_across_left==farmer.sheep.is_across_right&&farmer.is_across_right==farmer.sheep.is_across_left) { //如果羊和白菜在同一边且农夫在另外一边时则白菜会被吃 farmer.cabbage.is_alive=false; } } public static int is_win(){ //判断是否成功过河 if(farmer.sheep.is_alive==false||farmer.cabbage.is_alive==false) { return 0; //如果羊或白菜被吃了则返回0直接退出游戏失败 } if(farmer.is_across_right==true&&farmer.sheep.is_across_right==true&&farmer.wolf.is_across_right&&farmer.cabbage.is_across_right==true) { //如果农夫羊狼白菜都到了河的右边则返回1游戏成功 return 1; } return 2; //其他情况则继续进行 } public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(system.in); int choice = 0; int m=2; //将m的初始值设置为2表示正在进行的情况 boolean gamevoer=false,win=false; while(!gamevoer) { if(m==1||m==0) { //如果m=0或1则直接退出显示游戏结果 break; } menu(); choice = input.nextInt(); System.out.println("\n"); switch(choice) { case 0: gamevoer=true; break; case 1:{ farmer.cross_alone(); /* 农夫独自过河的处理 */ //农夫位置的判断 is_alive(); show(); m=is_win(); //m用来记录方法的返回值0,1,2 if(m==1) //如果m=1,则表示过河成功 { win=true;//直接输出游戏成功 } break; } //以下情况类似 case 2:{ farmer.cross_wolf();/* 农夫带狼的处理 */ is_alive(); show(); m=is_win(); if(m==1) { win=true; } break; } case 3:{ farmer.cross_sheep();/* 农夫带羊的处理 */ is_alive(); show(); m=is_win(); if(m==1) { win=true; } break; } case 4:{ farmer.cross_cabbage(); /* 农夫带白菜的处理 */ is_alive(); show(); m=is_win(); if(m==1) { win=true; } break; } } } if(win) { System.out.println("game over: you win !"); }else { System.out.println("game over: you lose !"); } input.close(); } }
package Test1; public class Cabbage extends wuti { public Cabbage(){ super.name="白菜"; } }
package Test1; public class Farmer{ boolean is_across_left = true ; //默认河左边为开始的一边 boolean is_across_right = false; Sheep sheep = new Sheep(); Wolf wolf = new Wolf(); Cabbage cabbage = new Cabbage(); public void cross_cabbage () { if(cabbage.is_across_left==is_across_left||cabbage.is_across_right==is_across_right) { //如果白菜农夫在一边 if(cabbage.is_across_left==false) { //白菜右边到左边 cabbage.is_across_left=true; cabbage.is_across_right=false; } else { //白菜左边到右边 cabbage.is_across_left=false; cabbage.is_across_right=true; } if(is_across_left==false) { //农夫右边到左边 is_across_left=true; is_across_right=false; } else { //农夫左边到右边 is_across_left=false; is_across_right=true; } } else { //如果白菜农夫不在一边则白菜无法过河 System.out.println(cabbage.name+"不再农夫这边"); } } public void cross_sheep() { if(sheep.is_across_left==is_across_left||sheep.is_across_right==is_across_right) { //如果羊农夫在一边 if(sheep.is_across_left==false) { //羊右边到左边 sheep.is_across_left=true; sheep.is_across_right=false; } else{ //羊左边到右边 sheep.is_across_left=false; sheep.is_across_right=true; } if(is_across_left==false) { //农夫右边到左边 is_across_left=true; is_across_right=false; } else{ //农夫左边到右边 is_across_left=false; is_across_right=true; } } else { //如果羊农夫不在一边则羊无法过河 System.out.println(sheep.name+"不再农夫这边"); } } public void cross_wolf() { if(wolf.is_across_left==is_across_left||wolf.is_across_right==is_across_right) { //如果狼农夫在一边 if(wolf.is_across_left==false) { //狼右边到左边 wolf.is_across_left=true; wolf.is_across_right=false; } else { //狼左边到右边 wolf.is_across_left=false; wolf.is_across_right=true; } if(is_across_left==false) { //农夫右边到左边 is_across_left=true; is_across_right=false; } else { //农夫左边到右边 is_across_left=false; is_across_right=true; } } else { //如果狼农夫不在一边则狼无法过河 System.out.println(wolf.name+"不再农夫这边"); } } public void cross_alone() { if(is_across_left==false) { //农夫右边到左边 is_across_left=true; is_across_right=false; } else{ //农夫左边到右边 is_across_left=false; is_across_right=true; } } }
package Test1; public class Sheep extends wuti{ public Sheep(){ super.name="羊"; } }
package Test1; public class Wolf extends wuti{ public Wolf(){ super.name="狼"; } }
package Test1; public class wuti { String name; boolean is_across_left = true ; boolean is_across_right = false; boolean is_alive = true; }
First create an object class Wuti Java, including name, used to describe the object to cross the river, is_ across_ Left indicates the left side of the river. The default value is true and is_ across_ Right means on the right side of the river. The default value is false and is_ Alive indicates that the object has not been eaten. This class is the parent class. There will be three classes inherited from this class, namely, cage, sheet and wolf. However, in these three classes, there is only the construction method representing the name respectively. Although it is simple, it is conducive to the change of the game. For example, the experiment requires changing the wolf, sheep and cabbage into Fox, rabbit and carrot. In this case, only super Change the name to the desired object name. Then create a farmer class, including cross_ Cage() method, cross_ Sheet() method, cross_ Wolf () method. These three methods are used to represent the situation of the farmer crossing the river with cabbage, sheep and wolf respectively. Moreover, the farmer must be on the side of the object, otherwise a prompt will be output, indicating that the object is not in the same position as the farmer and cannot cross the river. Finally, create a client class, including menu () method, show () method and is_ Alive() method, is_ Win () method and menu () method display a menu like selection item, which can select the situation you want to cross the river. The show () method outputs the status (survival and location) of farmers, various animals and objects. If the object crosses the river successfully, false is_across_left and true is_across_right will be displayed. The is_alive () method is used to judge whether sheep and cabbage are eaten. If sheep and wolves are on the same side and farmers are on the other side, sheep will be eaten, If the sheep and cabbage are on the same side and the farmer is on the other side, the cabbage will be eaten. is_ The win () method judges whether the river is crossed successfully. If the sheep or cabbage are eaten, it returns 0 and directly exits the game. If the farmer, sheep, wolf and cabbage all reach the right of the river, it returns 1 and the game is successful. In the main method, set the initial value of m to 2 to indicate that it is in progress. In the while loop, if M = 0 or 1, the game results will be directly exited and displayed. In the switch statement, select the objects crossing the river in case0, 2, 3 and 4 respectively, and farmer in case cross_ Similar methods such as alone () indicate where the farmer and object cross the river and whether they successfully cross the river, is_ The live () method means to judge whether the object is eaten, the show () method outputs the status (survival and location) of farmers, various animals and objects, and the is_win () method determines whether the river is successfully crossed. Skillfully use the value of m to judge whether it is successful to cross the river.
The results show that:
You can try.
The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.