首页 > 其他分享 >农夫过河图形界面版

农夫过河图形界面版

时间:2022-09-27 15:00:33浏览次数:75  
标签:right 过河 图形界面 equals add place && new 农夫

游戏内容:设定兔子、羊、胡萝卜、卷心菜、狼、农夫等角色,设定一次只能带一个角色过河与一次可以带两个角色过河两种模式,玩家通过点击角色按钮来决定角色是否可以过河,在每一次过河后会对各个角色的状态进行一次判定,如果有角色处于死亡状态则会提示游戏失败,如果所有角色全部存活且过河,则游戏成功。

所用技术:JavaFX组件、Java的事件驱动机制、lambda表达式、shape形状类的使用。

代码如下:

package shiyan7;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.HashSet;
public class Main extends Application {
public static void main(String[] args)
{
launch(args);
}
int i=0,i1=0,k=0,k1=0,j=0,j1=0,l=0,l1=0,a=0,b=0,x=0;//,h=0,h1=0,h2=0,h3=0,h4=0,h5=0,h6=0;
Boolean h[]=new Boolean[6];
static ArrayList<MaterialObject> materialObjects = new ArrayList<>();
Animal wolf;
Animal sheep;
Animal rabbit;
Plant cabbage;
Plant carrot;
Person person;
@Override
public void start(Stage stage) {
for(x=0;x<h.length;x++)
{
h[x]=false;
}
person = new Person("person");
materialObjects.add(person);
wolf = new Animal("wolf");
materialObjects.add(wolf);
sheep = new Animal("sheep");
wolf.addedToRecipe(sheep);
materialObjects.add(sheep);
rabbit = new Animal("rabbit");
wolf.addedToRecipe(rabbit);
materialObjects.add(rabbit);
cabbage = new Plant("cabbage");
rabbit.addedToRecipe(cabbage);
sheep.addedToRecipe(cabbage);
materialObjects.add(cabbage);
carrot = new Plant("carrot");
sheep.addedToRecipe(carrot);
rabbit.addedToRecipe(carrot);
materialObjects.add(carrot);
Scene scene = new Scene(new Group());
Scene scene1 = new Scene(new Group());
Scene scene2 = new Scene(new Group());
Scene scene3 = new Scene(new Group());
stage.setWidth(600);
stage.setHeight(600);
stage.setTitle("农夫过河游戏");
VBox vbox = new VBox();
vbox.setLayoutX(0);
vbox.setLayoutY(0);
VBox w = new VBox();
w.setLayoutX(0);
w.setLayoutY(40);
VBox s = new VBox();
s.setLayoutX(0);
s.setLayoutY(80);
VBox c = new VBox();
c.setLayoutX(0);
c.setLayoutY(200);
VBox ca = new VBox();
ca.setLayoutX(0);
ca.setLayoutY(160);
VBox r = new VBox();
r.setLayoutX(0);
r.setLayoutY(120);
VBox sure = new VBox();
sure.setLayoutX(0);
sure.setLayoutY(300);
VBox vbox1 = new VBox();
vbox1.setLayoutX(0);
vbox1.setLayoutY(0);
VBox w1 = new VBox();
w1.setLayoutX(0);
w1.setLayoutY(40);
VBox s1 = new VBox();
s1.setLayoutX(0);
s1.setLayoutY(80);
VBox cc1 = new VBox();
cc1.setLayoutX(0);
cc1.setLayoutY(120);
VBox sure1 = new VBox();
sure1.setLayoutX(0);
sure1.setLayoutY(300);
VBox c1 = new VBox();
c1.setLayoutX(0);
c1.setLayoutY(150);
VBox c2 = new VBox();
c2.setLayoutX(200);
c2.setLayoutY(150);
Group m = new Group();
Text t = new Text();
t.setCache(true);
t.setX(0.0);
t.setY(100.0);
t.setFill(Color.BLACK);
t.setText("请选择模式:");
t.setFont(Font.font(null, FontWeight.BOLD, 30));
m.getChildren().add(t);
Group m1 = new Group();
Text t1 = new Text();
t1.setCache(true);
t1.setX(100.0);
t1.setY(250.0);
t1.setFill(Color.BLACK);
t1.setText("游戏结束,你赢了!");
t1.setFont(Font.font(null, FontWeight.BOLD, 40));
m1.getChildren().add(t1);
Group m2 = new Group();
Text t2 = new Text();
t2.setCache(true);
t2.setX(100.0);
t2.setY(250.0);
t2.setFill(Color.BLACK);
t2.setText("游戏结束,你输了!");
t2.setFont(Font.font(null, FontWeight.BOLD, 40));
m2.getChildren().add(t2);
final Button button1 = new Button("农夫");
final Button button2 = new Button("狼");
final Button button3 = new Button("羊");
final Button button4 = new Button("白菜");
final Button button5 = new Button("1.一次只能带一个过河.");
final Button button6 = new Button("2.一次可以带两个过河.");
final Button button7 = new Button("胡萝卜");
final Button button8 = new Button("兔子");
final Button button9 = new Button("农夫");
final Button button10 = new Button("狼");
final Button button11 = new Button("羊");
final Button button12 = new Button("白菜");
final Button button13 = new Button("渡船");
final Button button14 = new Button("渡船");
button1.setOnAction((ActionEvent e) -> {
h[0]=!h[0];
});
button2.setOnAction((ActionEvent e) -> {
h[1]=!h[1];
});
button3.setOnAction((ActionEvent e) -> {
h[2]=!h[2];
});
button4.setOnAction((ActionEvent e) -> {
h[3]=!h[3];
});
button5.setOnAction((ActionEvent e) -> {
stage.setScene(scene);
stage.show();
});
button6.setOnAction((ActionEvent e) -> {
stage.setScene(scene2);
stage.show();
});
button7.setOnAction((ActionEvent e) -> {
h[4]=!h[4];
});
button8.setOnAction((ActionEvent e) -> {
h[5]=!h[5];
});
button9.setOnAction((ActionEvent e) -> {
h[0]=!h[0];
});
button10.setOnAction((ActionEvent e) -> {
h[1]=!h[1];
});
button11.setOnAction((ActionEvent e) -> {
h[2]=!h[2];
});
button12.setOnAction((ActionEvent e) -> {
h[3]=!h[3];
});
button13.setOnAction((ActionEvent e) -> {
if(h[0]==false)
{
System.out.println("必须先让农夫上船");
}
else if(h[0]==true) {
if (person.place.equals("left") ) {
System.out.println("农夫到达对岸");
vbox1.setLayoutX(400);
vbox1.setLayoutY(0);
person.place = "right";
} else if (person.place.equals("right") ) {
System.out.println("农夫回到原地");
vbox1.setLayoutX(0);
vbox1.setLayoutY(0);
person.place = "left";
}
if(h[1]==true)
{
if(wolf.place.equals("left")&&!wolf.place.equals(person.place) )
{
System.out.println("狼到达对岸");
w1.setLayoutX(400);
w1.setLayoutY(40);
wolf.place="right";
}
else if(wolf.place.equals("right")&&!wolf.place.equals(person.place) )
{
System.out.println("狼回到原地");
w1.setLayoutX(0);
w1.setLayoutY(40);
wolf.place="left";
}
h[1]=false;
}
else if(h[2]==true)
{
if(sheep.place.equals("left")&&!sheep.place.equals( person.place))
{
System.out.println("羊到达对岸");
s1.setLayoutX(400);
s1.setLayoutY(80);
sheep.place="right";
}
else if(sheep.place.equals("right")&&!sheep.place.equals( person.place))
{
System.out.println("羊回到原地");
s1.setLayoutX(0);
s1.setLayoutY(80);
sheep.place="left";
}
h[2]=false;
}
else if(h[3]==true)
{
if(cabbage.place.equals("left")&&!cabbage.place.equals(person.place))
{
System.out.println("白菜到达对岸");
cc1.setLayoutX(400);
cc1.setLayoutY(120);
cabbage.place="right";
}
else if(cabbage.place.equals("right")&&!cabbage.place.equals(person.place) )
{
System.out.println("白菜回到原地");
cc1.setLayoutX(0);
cc1.setLayoutY(120);
cabbage.place="left";
}
h[3]=false;
}
h[0]=false;
}
if(person.place.equals("right")&&wolf.place.equals("left")&&sheep.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&wolf.place.equals("right")&&sheep.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("right")&&cabbage.place.equals("left")&&sheep.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&cabbage.place.equals("right")&&sheep.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("right")&&cabbage.place.equals("right")&&sheep.place.equals("right")&&cabbage.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m1);
stage.setScene(scene3);
stage.show();
}
});
button14.setOnAction((ActionEvent e) -> {
if(h[0]==false) {
System.out.println("必须先让农夫上船");
}
else if(h[0]==true) {
if (person.place .equals( "left")) {
System.out.println("农夫到达对岸");
vbox.setLayoutX(400);
vbox.setLayoutY(0);
person.place = "right";
} else if (person.place.equals("right")){
System.out.println("农夫回到原地");
vbox.setLayoutX(0);
vbox.setLayoutY(0);
person.place = "left";
}
for(x=0;x<2;x++)
{
if (h[1] == true) {
if (wolf.place.equals("left")&&!wolf.place.equals(person.place)) {
System.out.println("狼到达对岸");
w.setLayoutX(400);
w.setLayoutY(40);
wolf.place="right";
} else if (wolf.place.equals("right")&&!wolf.place.equals(person.place)) {
System.out.println("狼回到原地");
w.setLayoutX(0);
w.setLayoutY(40);
wolf.place="left";
}
h[1] = false;
} else if (h[2] == true) {
if (sheep.place.equals("left")&&!sheep.place.equals(person.place)) {
System.out.println("羊到达对岸");
s.setLayoutX(400);
s.setLayoutY(80);
sheep.place="right";
} else if (sheep.place.equals("right")&&!sheep.place.equals(person.place)) {
System.out.println("羊回到原地");
s.setLayoutX(0);
s.setLayoutY(80);
sheep.place="left";
}
h[2] = false;
} else if (h[3] == true) {
if (cabbage.place.equals("left")&&!cabbage.place.equals(person.place)) {
System.out.println("白菜到达对岸");
c.setLayoutX(400);
c.setLayoutY(200);
cabbage.place="right";
} else if (cabbage.place.equals("right")&&!cabbage.place.equals(person.place)) {
System.out.println("白菜回到原地");
c.setLayoutX(0);
c.setLayoutY(200);
cabbage.place="left";
}
h[3] = false;
} else if (h[4] == true) {
if (carrot.place.equals("left")&&!carrot.place.equals(person.place)) {
System.out.println("胡萝卜到达对岸");
ca.setLayoutX(400);
ca.setLayoutY(160);
carrot.place="right";
} else if (carrot.place.equals("right")&&!carrot.place.equals(person.place)) {
System.out.println("胡萝卜回到原地");
ca.setLayoutX(0);
ca.setLayoutY(160);
carrot.place="left";
}
h[4] = false;
} else if (h[5] == true) {
if (rabbit.place.equals("left")&&!rabbit.place.equals(person.place)) {
System.out.println("兔子到达对岸");
r.setLayoutX(400);
r.setLayoutY(120);
rabbit.place="right";
} else if (rabbit.place.equals("right")&&!rabbit.place.equals(person.place)) {
System.out.println("兔子回到原地");
r.setLayoutX(0);
r.setLayoutY(120);
rabbit.place="left";
}
h[5] = false;
}
}
h[0]=false;
}
if(person.place.equals("right")&&wolf.place.equals("left")&&sheep.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&wolf.place.equals("right")&&sheep.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("right")&&wolf.place.equals("left")&&rabbit.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&wolf.place.equals("right")&&rabbit.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("right")&&carrot.place.equals("left")&&sheep.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&carrot.place.equals("right")&&sheep.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("right")&&cabbage.place.equals("left")&&sheep.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&cabbage.place.equals("right")&&sheep.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("right")&&cabbage.place.equals("left")&&rabbit.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&cabbage.place.equals("right")&&rabbit.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("right")&&carrot.place.equals("left")&&rabbit.place.equals("left"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
else if(person.place.equals("left")&&carrot.place.equals("right")&&rabbit.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m2);
stage.setScene(scene3);
stage.show();
}
if(person.place.equals("right")&&carrot.place.equals("right")&&rabbit.place.equals("right")&&wolf.place.equals("right")&&sheep.place.equals("right")&&cabbage.place.equals("right"))
{
((Group) scene3.getRoot()).getChildren().add(m1);
stage.setScene(scene3);
stage.show();
}
});
vbox.getChildren().add(button1);
w.getChildren().add(button2);
s.getChildren().add(button3);
c.getChildren().add(button4);
vbox1.getChildren().add(button9);
w1.getChildren().add(button10);
s1.getChildren().add(button11);
cc1.getChildren().add(button12);
sure1.getChildren().add(button13);
c1.getChildren().add(button5);
c2.getChildren().add(button6);
ca.getChildren().add(button7);
r.getChildren().add(button8);
sure.getChildren().add(button14);
((Group) scene.getRoot()).getChildren().add(vbox1);
((Group) scene.getRoot()).getChildren().add(w1);
((Group) scene.getRoot()).getChildren().add(s1);
((Group) scene.getRoot()).getChildren().add(cc1);
((Group) scene.getRoot()).getChildren().add(sure1);
((Group) scene2.getRoot()).getChildren().add(vbox);
((Group) scene2.getRoot()).getChildren().add(w);
((Group) scene2.getRoot()).getChildren().add(s);
((Group) scene2.getRoot()).getChildren().add(c);
((Group) scene2.getRoot()).getChildren().add(ca);
((Group) scene2.getRoot()).getChildren().add(r);
((Group) scene2.getRoot()).getChildren().add(sure);
((Group) scene1.getRoot()).getChildren().add(m);
((Group) scene1.getRoot()).getChildren().add(c1);
((Group) scene1.getRoot()).getChildren().add(c2);
stage.setScene(scene1);
stage.show();
}
}
abstract class MaterialObject {// 抽象物品类
String type;
String place;
boolean isExist;
public void diedOut() {
isExist = false;
}
abstract boolean isExist();
}
class Person extends MaterialObject {
public Person() {
this.place = "left";
this.isExist = true;
}
public Person(String type) {
this.type = type;
this.place = "left";
this.isExist = true;
}
@Override
boolean isExist() {
// TODO 自动生成的方法存根
return isExist;
}
}
class Plant extends MaterialObject {
public Plant() {
this.place = "left";
this.isExist = true;
}
public Plant(String type) {
this.type = type;
this.place = "left";
this.isExist = true;
}
@Override
boolean isExist() {
// TODO 自动生成的方法存根
return isExist;
}
}
class Animal extends MaterialObject {// 动物类
HashSet<MaterialObject> recipe = new HashSet<>();

public Animal() {
this.place = "left";
this.isExist = true;
}
public Animal(String type) {
this.type = type;
this.place = "left";
this.isExist = true;
}
public void addedToRecipe(MaterialObject m)// 添加某物品进该动物的食谱
{
recipe.add(m);
}
public boolean isFood(MaterialObject m)// 判断某物品是否在它的食谱中
{
return recipe.contains(m);
}
public boolean canBeEat(MaterialObject m)// 判断某物品能不能被吃到
{
if (this.place.equals(m.place)) {
return true;
} else
return false;
}
public boolean eat(MaterialObject m)// 吃某样食物
{
if (canBeEat(m) && isFood(m)) {
m.diedOut();
return true;
}
else
return false;
}
@Override
boolean isExist() {
// TODO 自动生成的方法存根
return isExist;
}
}

 

标签:right,过河,图形界面,equals,add,place,&&,new,农夫
From: https://www.cnblogs.com/6t6yuu/p/16734579.html

相关文章

  • luogu P1052 [NOIP2005 提高组] 过河
    [NOIP2005提高组]过河题目描述在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧。在桥上有一些石子,青蛙很讨厌踩在这些石子上。由于桥的长度和青蛙一次跳......
  • MFC图形界面编辑工具
    MFC图形界面编辑工具选题之二:图形界面编辑工具功能要求基本功能能够在windows界面上画图(包括直线、空心矩形、填充矩形、空心圆、填充圆、填充圆角矩形、空心圆角矩......
  • 信息学奥赛一本通 1314:【例3.6】过河卒(Noip2002)
    时间限制:1000ms      内存限制:65536KB提交数:26367   通过数:11410【题目描述】棋盘上A点有一个过河卒,需要走到目标B点。卒行走的规则:可以向下......
  • CentOS设置默认启动命令行(不启动图形界面)
    CentOS设置默认启动命令行(不启动图形界面)-java_zhao-博客园 https://www.cnblogs.com/java-xz/p/7351867.htmlCentOS中如何进入图形界面和文字界面,Linux真正的服......
  • P1002 [NOIP2002 普及组] 过河卒 题解
    题目:[NOIP2002普及组]过河卒题目描述棋盘上\(A\)点有一个过河卒,需要走到目标\(B\)点。卒行走的规则:可以向下、或者向右。同时在棋盘上\(C\)点有一个对方的马,该......
  • linux安装图形界面
    目录linux安装图形界面1、查看图形界面的信息2、安装3、设置默认启动图形界面4、重启5、使用vncviewer操作图形界面linux安装图形界面1、查看图形界面的信息yumgroupli......
  • silk-GUI图形界面开发一个词典
    了解使用的库Silk内置了一些GUI类库供使用者开发MacOS上的图形界面程序,只需引用gui.si即可准备首先要知道app需要什么功能,这里我要的是查询单词,可以听语音,还可以存储生......
  • gui guider生成的代码无人工修改移植esp32 实现拖曳式傻瓜生成嵌入式图形界面 及p
    既然有了gui guider这么方便的东西,肯定想移植到实际的esp32单片机上就不用手敲代码去写widget了main.cpp改造lvgl自带的arduino例子写的比较随性 东一坨西一坨的 ......