面向对象程序设计:农夫过河问题

面向对象程序设计:农夫过河问题,第1张

面向对象程序设计:农夫过河问题

农夫过河两种玩法
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);
        } else
            sheep.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);
        } else
            cabbage.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);
            else
                huLuoBu.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();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5718377.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存