java剪刀石头布游戏代码_Java游戏大全「建议收藏」

java剪刀石头布游戏代码_Java游戏大全「建议收藏」所以我们需要一个游戏玩家类(Player),电脑玩家类(Computer),玩家类(Joiner),裁判类(Judge),游戏模式选择类(GameMode)5.游戏模式类(GameMode):玩

java剪刀石头布游戏代码_Java游戏大全「建议收藏」"

一、功能分析

玩家和电脑进行对决,通过玩家输入的数字来决定出拳,比如石头为1,剪刀为2,布为3,电脑则随机在1-3之间生成整数,通过判断决定胜负。所以我们需要一个游戏玩家类(Player),电脑玩家类(Computer),玩家类(Joiner),裁判类(Judge),游戏模式选择类(GameMode)

游戏主界面类(GameMain)。

1.游戏玩家类(Player):玩家进入游戏后输入玩家名称,然后进行选择游戏模式。

import java.util.Scanner;

public class Player extends Joiner{

    public Player() {
    }

    //获取玩家名称
    public void play_name(){
        System.out.println("请输入玩家名称:");
        Scanner scanner = new Scanner(System.in);
        String player = scanner.nextLine();
        this.setName(player);
    }

    //游戏模式的选择
    public void game_mode(){
        System.out.println("------------------模式选择------------------");
        System.out.println("1.一局决胜负   2.三局两胜   3.五局三胜");
        Scanner scanner = new Scanner(System.in);
        int option = scanner.nextInt();

        //定义一个数组用来存放选项
        int[] mode = {1,3,5};
        //buer类型用来判断
        boolean flag = false;

        //for循环判断用户输入是否是1-3,否则则提示用户重新选择
        for (int i = 0;i < 3;i++){
            if((option-1) == i){
                flag = true;
                option = mode[i];
                break;
            }
        }
        if (flag){
            this.setOption(option);
        }else {
            System.out.println("请选择对战模式:");
            this.game_mode();
        }

    }


    //获取玩家出拳结果
    public void player_choice(){
        //玩家重下面选择想要出的拳
        System.out.println("1-----剪刀");
        System.out.println("2-----石头");
        System.out.println("3-----布");

        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();

        try{
            //玩家输入1-3,则将输入结果记录下来
            this.setChoice_name(choice);
        }catch (Exception e){
            //否则将随机生成1-3之间任意数字
            this.setChoice_name((int)(Math.random()*3+1));
        }

    }
}

2.电脑玩家类(Computer):玩家可以选择哪个电脑角色进行对战。

import java.util.Scanner;

public class Computer extends Joiner{
    public Computer() {
    }

    //电脑角色选择
    public void computer_role(){
        //创建一个数组,用来获取电脑角色名,结果用来输出电脑角色名
        String[] role = {"钢铁侠","蜘蛛侠","蝙蝠侠","猪猪侠"};
        System.out.println("------------------角色选择------------------");
        System.out.println("请选择电脑角色进行对战:");
        System.out.println("1----钢铁侠");
        System.out.println("2----蜘蛛侠");
        System.out.println("3----蝙蝠侠");
        System.out.println("4----猪猪侠");


        Scanner scanner = new Scanner(System.in);
        int com_role = scanner.nextInt();
        this.setName(role[com_role-1]);

    }

    //电脑生成随机数,在1-3之间
    public void com_choice(){
        this.setChoice_name((int)(Math.random()*3+1));
    }

}

3.玩家类(Joiner):接收玩家输入的数字以及电脑随机生成的数字,还有玩家输入的名称以及选择的电脑角色名称。

public class Joiner {
    private String name;
    private int choice_name;
    private int option;

    public Joiner(String name, int choice_name) {
        this.name = name;
        this.choice_name = choice_name;
    }

    public Joiner() {
    }


    //以下是构造函数
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChoice_name() {
        return choice_name;
    }

    public void setChoice_name(int choice_name) {
        this.choice_name = choice_name;
    }

    public int getOption() {
        return option;
    }

    public void setOption(int option) {
        this.option = option;
    }

    @Override
    public String toString() {
        return "Joiner{" +
                "name='" + name + '\'' +
                ", choice_name=" + choice_name +
                ", option=" + option +
                '}';
    }
}

4.裁判类(Judge):接收玩家输入的名称和电脑名称,以及玩家输入的数字和电脑随机生成的数字,然后通过判断来判胜负。

import java.util.Scanner;

public class Judge {
    private int result;
    private int player_score = 0;
    private int computer_score = 0;
    public int continue_option;

    public Judge(int result) {
        this.result = result;
    }

    public Judge() {
    }

    public void winner(Player player,Computer computer){
        String[] arr = {"剪刀","石头","布"};
        System.out.println("玩家\t" + player.getName() + "\t出:" + arr[player.getChoice_name()-1]);
        System.out.println("电脑\t" + computer.getName() + "\t出:" + arr[computer.getChoice_name()-1]);

        this.result = player.getChoice_name() - computer.getChoice_name();
//        String[] arr = {"平局","赢","输"};
        if (this.result == 1 || this.result == -2){
            this.player_score += 1;
            System.out.println("玩家获胜!目前得分为:" + this.player_score);

        }else if(this.result == -1 || this.result == 2){
            this.computer_score += 1;
            System.out.println("电脑获胜!目前得分为:" + this.computer_score);

        }else {
            System.out.println("平局!");
        }

    }

    //最后结果
    public void last_winner(){
        System.out.println("------------------游戏结果------------------");
        System.out.println("玩家最终得分为" + this.player_score + "分");
        System.out.println("电脑最终得分为" + this.computer_score + "分");

        if (this.player_score - this.computer_score > 0){
            System.out.println("玩家获得最终胜利!");
        }else if(this.player_score - this.computer_score < 0){
            System.out.println("电脑获得最终胜利!");
        }else {
            System.out.println("平局!");
        }

        System.out.println("------------------是否继续?------------------");
        System.out.println("1.继续,与同角色再来一轮!\n2.继续,重新选择角色对决!\n3.不玩了,退出游戏");
        Scanner scanner = new Scanner(System.in);
        continue_option = scanner.nextInt();
        this.player_score = 0;
        this.computer_score = 0;

    }


    //以下是get和set方法
    public int getResult() {
        return result;
    }

    public void setResult(int result) {
        this.result = result;
    }

}

5.游戏模式类(GameMode):玩家选择游戏模式后,根据玩家选择的模式,从而决定进行多少次的对决,最终对决完成后,玩家可以选择继续游戏或退出游戏。

public class GameMode extends Judge{
    public GameMode() {
    }

    public void play_game(Player player, Computer computer, Judge judge){
        int i = 1;
        System.out.println("------------------开始对决------------------");
        while (i <= player.getOption()){
            System.out.println("                   第" + i + "轮                   ");
            //玩家出拳选择
            player.player_choice();
            //电脑出拳,随机生成
            computer.com_choice();
            //判断结果
            System.out.println("                  第" + i + "轮结果                   ");
            judge.winner(player,computer);
            i += 1;
        }
    }

    //一轮结束后由玩家选择是否继续进行游戏
    public void continue_game(Player player, Computer computer, Judge judge){
        //输出最后结果
        judge.last_winner();

        while (judge.continue_option < 3){
            switch (judge.continue_option){
                case 1:
                    player.game_mode();
                    this.play_game(player,computer,judge);
                    judge.last_winner();
                    break;
                case 2:
                    computer.computer_role();
                    player.game_mode();
                    play_game(player,computer,judge);
                    judge.last_winner();
                default:
                    System.out.println("正在退出.....");
            }
    }

    }

}

6.游戏主界面类(GmaeMain):玩家在主界面开始游戏。

import java.util.Scanner;

public class GameMain {
    public static void main(String[] args) {
        //调用类
        Player player = new Player();
        Computer computer = new Computer();
        Judge judge = new Judge();
        GameMode gameMode = new GameMode();

        //游戏界面
        System.out.println("-----------------------------------");
        System.out.println("           剪刀石头布         ");
        System.out.println("                    作者:萧萧");
        System.out.println("                    版本:1.0");
        System.out.println("-----------------------------------");
        System.out.println("------------------游戏开始------------------");

        //玩家输入名称
        player.play_name();
        //玩家选择电脑角色
        computer.computer_role();
        //游戏模式选择
        player.game_mode();
        //根据玩家选择的游戏模式,来输出多少次对决,while控制
        gameMode.play_game(player, computer, judge);
        //玩家选择是否继续进行游戏
        gameMode.continue_game(player, computer, judge);
    }
}

今天的文章java剪刀石头布游戏代码_Java游戏大全「建议收藏」分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/87509.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注