农夫过河两种玩法:
1.农夫带一种东西过河
(原始玩法)
2.农夫带两种东西过河
(新增两个角色,胡萝卜,兔子,农夫不在时兔子会吃胡萝卜,狼会吃兔子)
先选择玩法:


第一种玩法的正确输入是:3123413

第二种玩法的正确步骤留给你们,下面上代码:

package ShiYan;import java.util.Scanner;
public class Cross {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请输入你想进行的游戏模式(1为传统玩法)(2为带两种东西过河),请选择:");int Ze = input.nextInt();if (Ze == 1) {game11 game = new game11();game.play();} else if (Ze == 2) {game2 game2 = new game2();game2.play2();} else {System.out.println("输入错误!");System.exit(0);}}
}
class thing {private boolean hasCross = false;private boolean isAlive = true;public boolean isHasCross() {return hasCross;}public void setHasCross(boolean hasCross) {this.hasCross = hasCross;}public boolean isAlive() {return isAlive;}public void setAlive(boolean alive) {this.isAlive = alive;}public void showStatus() {}public void eatSheep(thing sheep, thing farmer) {}public void eatCabbage(thing cabbage, thing farmer) {}public void eatHuLuoBu(thing huLuoBu, thing farmer) {}
}class cabbage11 extends thing {public void showStatus() {System.out.println("Cabbage is alive:" + super.isAlive());System.out.println("Cabbage has Cross:" + super.isHasCross());}
}class wolf11 extends thing {String name;public wolf11(String name) {this.name = name;System.out.println("啊呜~~,我是可爱小狼仔 " + this.name + ",我又回来了!");}public void eatSheep(thing sheep, thing farmer) {if (!(sheep.isHasCross() == super.isHasCross() && farmer.isHasCross() == sheep.isHasCross())) {if ((sheep.isHasCross() == super.isHasCross()))sheep.setAlive(false);} elsesheep.setAlive(true);}public void showStatus() {System.out.println("Wolf " + this.name + " is alive  :" + super.isAlive());System.out.println("Wolf " + this.name + " has Cross  :" + super.isHasCross());}
}class farm11 extends thing {public void showStatus() {System.out.println("Farmer has Cross  :" + super.isHasCross());}
}class sheep11 extends thing {String name;public sheep11(String name) {this.name = name;System.out.println("咩咩,我是可爱的小羊 " + this.name);}public void eatCabbage(thing cabbage, thing farmer) {if (!(cabbage.isHasCross() == super.isHasCross() && farmer.isHasCross() == cabbage.isHasCross())) {if ((cabbage.isHasCross() == super.isHasCross()))cabbage.setAlive(false);} elsecabbage.setAlive(true);}public void showStatus() {System.out.println("Sheep " + this.name + " is alive :" + super.isAlive());System.out.println("Sheep " + this.name + " has Cross :" + super.isHasCross());}
}class tuZi extends thing {String name;public tuZi(String name) {this.name = name;System.out.println("我是可爱的小兔兔" + this.name);}public void eatHuLuoBu(thing huLuoBu, thing farmer) {if (!(huLuoBu.isHasCross() == super.isHasCross() && farmer.isHasCross() == huLuoBu.isHasCross())) {if ((huLuoBu.isHasCross() == super.isHasCross()))huLuoBu.setAlive(false);elsehuLuoBu.setAlive(true);}}public void showStatus() {System.out.println("tuZi " + this.name + " is alive :" + super.isAlive());System.out.println("tuZi " + this.name + " has Cross :" + super.isHasCross());}
}class huLuoBu extends thing {public void showStatus() {System.out.println("huLuoBu is alive:" + super.isAlive());System.out.println("huLuoBu has Cross:" + super.isHasCross());}
}class boat11 {void boat(thing s) {s.setHasCross(!s.isHasCross());}void boat1(thing s1, thing s2) {s1.setHasCross(!s1.isHasCross());s2.setHasCross(!s2.isHasCross());}void boat2(thing a1, thing a2, thing a3) {a1.setHasCross(!a1.isHasCross());a2.setHasCross(!a2.isHasCross());a3.setHasCross(!a3.isHasCross());}
}class gamegui11 {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 wolf=========");System.out.println("\t==========3:Cross the river with sheep============");System.out.println("\t==========4:Cross the river with cabbage==========");System.out.println("\t==========0:Quit===============");System.out.println("===================================================");System.out.println("Input the number(0~4):");}public static void showStatus(thing farmer, thing wolf, thing sheep, thing cabbage) {farmer.showStatus();wolf.showStatus();sheep.showStatus();cabbage.showStatus();}
}class game11 {thing wolf;thing sheep;thing cabbage;thing farmer;boat11 boat;game11() {wolf = new wolf11("jack");sheep = new sheep11("nancy");cabbage = new cabbage11();farmer = new farm11();boat = new boat11();}void play() {Scanner input = new Scanner(System.in);int choice = 0;            //用户输入选择boolean gameOver = false,//游戏结束标志,默认为false,代表游戏进行中,未结束win = false;     //游戏输赢标志,默认为false,代表未赢得游戏。while (!gameOver) {gamegui11.menu();               /错误原因:无法从 static 上下文引用非static方法menu()choice = input.nextInt();       ///所以将menu()改为static类型switch (choice) {case 0:System.out.println("Quit");System.exit(0);break;case 1:/* 农夫独自过河的处理 */boat.boat(farmer);break;case 2:/* 农夫带狼的处理 */boat.boat1(farmer, wolf);break;case 3:/* 农夫带羊的处理 */boat.boat1(farmer, sheep);break;case 4:/* 农夫带白菜的处理 */boat.boat1(farmer, cabbage);break;}wolf.eatSheep(sheep, farmer);//狼吃羊,如果羊不在同一边,则吃不到,如果在同一边,羊被吃sheep.eatCabbage(cabbage, farmer);//同上gamegui11.showStatus(farmer, wolf, sheep, cabbage);gameOver = isGameOver();}win = this.hasWin();if (win) {System.out.println("game over: you win !");} else {System.out.println("game over: you lose !");}input.close();}public boolean isGameOver() {if (sheep.isAlive() == false || cabbage.isAlive() == false) {return true;}if (wolf.isHasCross() && sheep.isHasCross() && cabbage.isHasCross()) {return true;}return false;}public boolean hasWin() {if (sheep.isAlive() == false || cabbage.isAlive() == false) {return false;}if (wolf.isHasCross() && sheep.isHasCross() && cabbage.isHasCross()) {return true;} else {return false;}}
}class game2 {thing wolf;thing sheep;thing cabbage;thing farmer;thing tuZi;thing huLuoBu;boat11 boat;game2() {wolf = new wolf11("yueHan");tuZi = new tuZi("KaKa");sheep = new sheep11("Cindy");huLuoBu = new huLuoBu();cabbage = new cabbage11();farmer = new farm11();boat = new boat11();}void play2() {Scanner input = new Scanner(System.in);int choice = 0;            //用户输入选择boolean gameOver2 = false,//游戏结束标志,默认为false,代表游戏进行中,未结束win2 = false;     //游戏输赢标志,默认为false,代表未赢得游戏。while (!gameOver2) {gamegui2.menu();               /错误原因:无法从 static 上下文引用非static方法menu()choice = input.nextInt();       ///所以将menu()改为static类型switch (choice) {case 0:System.out.println("Quit");System.exit(0);break;case 1:/* 农夫独自过河的处理 */boat.boat(farmer);break;case 2:/* 农夫带狼的处理 */boat.boat1(farmer, wolf);break;case 3:/* 农夫带羊的处理 */boat.boat1(farmer, sheep);break;case 4:/* 农夫带白菜的处理 */boat.boat1(farmer, cabbage);break;case 5://带兔子过河boat.boat1(tuZi, farmer);break;case 6://带胡萝卜过河boat.boat1(huLuoBu, farmer);break;case 7:boat.boat2(wolf, sheep, farmer);break;case 8:boat.boat2(wolf, cabbage, farmer);break;case 9:boat.boat2(wolf, tuZi, farmer);break;case 10:boat.boat2(wolf, huLuoBu, farmer);break;case 11:boat.boat2(cabbage, sheep, farmer);break;case 12:boat.boat2(tuZi, sheep, farmer);break;case 13:boat.boat2(huLuoBu, sheep, farmer);break;case 14:boat.boat2(cabbage, tuZi, farmer);break;case 15:boat.boat2(cabbage, huLuoBu, farmer);break;case 16:boat.boat2(tuZi, huLuoBu, farmer);break;}wolf.eatSheep(sheep, farmer);        //狼吃羊,如果羊不在同一边,则吃不到,如果在同一边,羊被吃sheep.eatCabbage(cabbage, farmer);sheep.eatCabbage(huLuoBu, farmer);//同上tuZi.eatHuLuoBu(huLuoBu, farmer);wolf.eatSheep(tuZi, farmer);         //狼吃兔子gamegui2.showStatus(farmer, wolf, sheep, cabbage, tuZi, huLuoBu);gameOver2 = isGameOver2();}win2 = this.hasWin2();if (win2) {System.out.println("game over: you win !");} else {System.out.println("game over: you lose !");}input.close();}public boolean isGameOver2() {if (sheep.isAlive() == false || cabbage.isAlive() == false || tuZi.isAlive() == false || huLuoBu.isAlive() == false) {return true;}if (wolf.isHasCross() && sheep.isHasCross() && cabbage.isHasCross() && tuZi.isHasCross() && huLuoBu.isHasCross()) {return true;}return false;}public boolean hasWin2() {if (sheep.isAlive() == false || cabbage.isAlive() == false || tuZi.isAlive() == false || huLuoBu.isAlive() == false) {return false;}if (wolf.isHasCross() && sheep.isHasCross() && cabbage.isHasCross() && tuZi.isHasCross() && huLuoBu.isHasCross()) {return true;} else {return false;}}
}class gamegui2 {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 wolf=========");System.out.println("\t==========3:Cross the river with sheep============");System.out.println("\t==========4:Cross the river with cabbage==========");System.out.println("\t==========5:Cross the river with tuZi==========");System.out.println("\t==========6:Cross the river with huLuoBu==========");System.out.println("\t==========7:Cross the river with wolf and sheep=========");System.out.println("\t==========8:Cross the river with wolf and cabbage=========");System.out.println("\t==========9:Cross the river with wolf and tuZi=========");System.out.println("\t==========10:Cross the river with wolf and huLuoBu=========");System.out.println("\t==========11:Cross the river with sheep and cabbage============");System.out.println("\t==========12:Cross the river with sheep and tuZi============");System.out.println("\t==========13:Cross the river with sheep and huLuoBu============");System.out.println("\t==========14:Cross the river with cabbage and tuZi==========");System.out.println("\t==========15:Cross the river with cabbage and huLuoBu==========");System.out.println("\t==========16:Cross the river with tuZi and huLuoBU==========");System.out.println("\t==========0:Quit===============");System.out.println("===================================================");System.out.println("Input the number(0~16):");}public static void showStatus(thing farmer, thing wolf, thing sheep, thing cabbage, thing tiZi, thing huLuoBu) {farmer.showStatus();wolf.showStatus();sheep.showStatus();cabbage.showStatus();tiZi.showStatus();huLuoBu.showStatus();}
}

面向对象程序设计:农夫过河问题相关推荐

  1. 面向对象程序设计 - 农夫过河 - Java

    农夫过河 - Java 概述 胜利条件: 所有单位安全渡河 游戏模式: 经典模式(农夫,狼,羊,菜),狼会吃羊.羊会吃菜,小船最大承载量为 2 全新模式(农夫,狼,羊,菜,兔子,胡萝卜),狼会吃羊.羊 ...

  2. 农夫过河问题——程序设计

    农夫过河问题--程序设计 一.问题需求分析 一个农夫带着一只狼.一只羊和一棵白菜,身处河的南岸.他要把这些东西全部运到北岸.问题是他面前只有一条小船,船小到只能容下他和一件物品,另外只有农夫能撑船.另 ...

  3. 人狼羊菜过河matlab,农夫过河(带羊,菜,狼,过河) C语言程序设计流程图

    农夫过河(带羊,菜,狼,过河) C语言程序设计流程图0 shadowylpw2013.05.15浏览830次分享举报 #include #include #include #define MAX_ST ...

  4. java农夫过河问题_农夫过河问题——C语言程序设计(转)

    一个农夫带着一只狼.一只羊和一棵白菜,身处河的南岸.他要把这些东西全部运到北岸.问题是他面前只有一条小船,船小到 只能容下他和一件物品,另外只有农夫能撑船.另外,因为狼能吃羊,而羊爱吃白菜,所以农夫不 ...

  5. 农夫过河算法最简便的c语言,C语言农夫过河

    问题描述 一个农夫在河边带了一只狼.一只羊和一颗白菜,他需要把这三样东西用船带到河的对岸.然而,这艘船只能容下农夫本人和另外一样东西.如果农夫不在场的话,狼会吃掉羊,羊也会吃掉白菜.请编程为农夫解决这 ...

  6. c语言课程农夫过河问题实验心得,农夫过河实验报告

    <农夫过河实验报告>由会员分享,可在线阅读,更多相关<农夫过河实验报告(22页珍藏版)>请在人人文库网上搜索. 1.数据结构与算法综合实验"课程设计报告题目:农夫过河 ...

  7. java农夫过河_C语言实现农夫过河代码及解析

    问题描述 一个农夫在河边带了一只狼.一只羊和一颗白菜,他需要把这三样东西用船带到河的对岸.然而,这艘船只能容下农夫本人和另外一样东西.如果农夫不在场的话,狼会吃掉羊,羊也会吃掉白菜.请编程为农夫解决这 ...

  8. 农夫过河实验报告c语言,农夫过河实验报告.doc

    . . "数据结构与算法综合实验"课程设计报告 题目: 农夫过河问题 学 院 计算机科学技术 年 级 2014级 专 业 计算机科学与技术 学 号姓 名 高晗 日 期 2016年3 ...

  9. c语言课程农夫过河问题实验心得,农夫过河实验报告.docx

    农夫过河实验报告 "数据结构与算法综合实验"课程设计报告题目: 农夫过河问题学 院计算机科学技术年 级2014级专 业计算机科学与技术学 名高晗日 期2016年3月30日星期三成 ...

最新文章

  1. 打开 XP Pro SP2 远程桌面的多用户支持
  2. C#将对象序列化成JSON字符串
  3. CMOS图像传感器——噪声模型
  4. 用AI写出的第一本书面世:先进算法能对机器生成的内容负责吗?
  5. 比特币程序_比特币如何运作? 我建立了一个应用程序向您展示。
  6. 【推荐实践】Hulu-视频理解在个性化推荐与内容发现中的应用.pdf(附下载链接)...
  7. php 可变变量 数组赋值,PHP可变变量学习小结
  8. @程序员,如何花式构建线程?
  9. oracle的nvl和nvl2是什么函数,两者区别
  10. 虚拟化云计算-centos7上使用virt-manager安装虚拟机
  11. java 数学公式解析_JAVA 文本表达式解析成数学公式,计算出结果
  12. Java静态语句块、语句块、构造方法执行顺序
  13. Haar特征提取算法的实现
  14. 介绍两种常见软件开发模式:“敏捷”和“瀑布”
  15. 免费的视频格式转换器哪个最好用呢?
  16. 研发人员欠缺的“不要脸”文化
  17. 实现 酷狗音乐 歌词播放效果
  18. 子矩阵的和(Python实现)
  19. 使用ubuntu20.04一个月后的感受
  20. 解决MySQL导入.CSV数据中文乱码

热门文章

  1. geomagic 色谱分析
  2. 基于二维数组的六边形地图数据结构的实现(蜂窝拓补)
  3. 《FreeSWITCH: VoIP实战》: 一个在FreeSWITCH中外呼的Lua脚本
  4. IOS RSA加密解密
  5. python dash库_15个好用到哭的python库,太牛了!
  6. 项目管理指导委员会是什么意思?
  7. 专业兴趣小组的建设实施方案(讨论稿)
  8. vue中的请求拦截器与响应拦截器的使用
  9. 2017TFC上道第15届厦门泛游戏企业对接会精彩盘点
  10. 跟着王进老师学开发Python篇第五季:面向对象篇-王进-专题视频课程