文章目录

  • 题目
  • 第一种写法(面向对象)
  • 题目分析
  • 代码展示
  • Player类
  • Computer类
  • Judge类
  • Test类
  • 程序运行结果
  • 第二种写法(面向过程)
  • 题目分析
  • 代码展示
  • Player类
  • Computer类
  • Test类
  • 程序运行结果


题目

设计一个猜拳游戏,完成人机猜拳互动游戏的开发。
功能:

  1. 选取对战角色
  2. 猜拳
  3. 记录分数

第一种写法(面向对象)

题目分析

  • 共需要6个类
  • 自定义类:Player玩家类 Computer电脑类 Judge裁判类 Test测试类
  • 导入类:Scanner类 Random类

代码展示

Player类

public class Player {
    private  int score;
    private String name;
    private String[] quan = {" ","剪刀","石头","布"};
    //出拳的方法
    public String chuQuan(int index){
     return  quan[index];
    }
    //name和score的Getter and Setter
    public String getName() {
        return name;
    }

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

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String[] getQuan() {
        return quan;
    }

    public void setQuan(String[] quan) {
        this.quan = quan;
    }
}

Computer类

import java.util.Random;
public class Computer {
    //电脑名字
    private String name;
    //电脑分数
    private  int score;
    //角色数组
    private String[] roles = {" ","刘备","孙权","曹操"};
    //出拳数组
    private String[] quan = {" ","剪刀","石头","布"};
    Random r = new Random();
    //出拳的方法
    public String chuQuan(){
        return quan[r.nextInt(3)+1];
    }
    //获取角色的方法
    public String getRole(int index){
        return roles[index];
    }
    //name,score,roles的Getter and Setter
    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String[] getRoles() {
        return roles;
    }

    public void setRoles(String[] roles) {
        this.roles = roles;
    }

    public String[] getQuan() {
        return quan;
    }

    public void setQuan(String[] quan) {
        this.quan = quan;
    }

    public String getName() {
        return name;
    }

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

Judge类

public class Judge {
    //结果数组
    private String[] result = {"^_^平手,继续努力","^_^恭喜你,你赢了!","^_^你输了,真笨!"};
    //判断的方法
    public int judge(String player,String computer) {
        if (computer.equals("剪刀")) {
            switch (player) {
                case "剪刀":
                    return 0;
                case "石头":
                    return  1;
                case "布":
                    return  2;
                default:
                    System.out.println("输入错误");
            }
        } if(computer.equals("石头")){
            switch (player) {
                case "剪刀":
                    return 2;
                case "石头":
                    return  0;
                case "布":
                    return  1;
                default:
                    System.out.println("输入错误");
            }
        }if (computer.equals("布")){
            switch (player) {
                case "剪刀":
                    return 1;
                case "石头":
                    return 2;
                case "布":
                    return 0;
                default:
                    System.out.println("输入错误");
            }
        }
         return  0 ;
    }
    //判断得到结果的方法
    public String getResult(int index){
        return result[index];
    }
    //得分的方法
    public void getScore(Player player,Computer computer,int index){
        if(index == 1) {
            player.setScore(player.getScore()+1);
        }
        if(index == 2){
            computer.setScore(computer.getScore()+1);
        }
    }
  //谁赢的方法
    public void victory(Player player,Computer computer){
        if(player.getScore() == computer.getScore()){
            System.out.println(result[0]);
        }
        if(player.getScore() > computer.getScore()){
            System.out.println(result[1]);
        }
        if(player.getScore() < computer.getScore()){
            System.out.println(result[2]);
        }
    }
}

Test类

import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        //创建电脑对象
        Computer computer = new Computer();
        //创建玩家对象
        Player player = new Player();
        //创建裁判对象
        Judge judge = new Judge();
        //对战次数初始化
        int times = 0;
        //调用Scanner类
        Scanner sc = new Scanner(System.in);
        System.out.println("*******************");
        System.out.println("**猜拳游戏  马上开始**");
        System.out.println("*******************");
        System.out.println("出拳规则:1.剪刀 2.石头 3.布");
        System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操)");
        //选择电脑的角色,输入1 2 或3 如果输入错误 可以重新输入
        while (true) {
            int index1 = sc.nextInt();
            if (index1<4 && index1>0) {
                computer.setName(computer.getRole(index1));
                break;
            } else {
                System.out.println("输入的数字不正确,请重试");
            }
        }
        //输入自己的姓名
        System.out.print("请输入你的姓名");
        player.setName(sc.next());
        System.out.println(player.getName() + " vs " + computer.getName() + "对战开始");
        System.out.print("要开始吗?输入y/n");
        while (true) {
            String gameStart = sc.next();
            if (gameStart.equals("y") || gameStart.equals("Y")) {
                times ++;
                System.out.println("对战次数" + times);
                System.out.println("请出拳:1.剪刀 2.石头 3.布");
                int index2 = sc.nextInt();
                System.out.println("你出:"+player.chuQuan(index2));
                String computerQuan = computer.chuQuan();
                System.out.println(computer.getName()+"出"+computerQuan);
                //判断结果的方法
                int judge1 = judge.judge(player.chuQuan(index2), computerQuan);
                //得到结果的方法
                System.out.println(judge.getResult(judge1));
                //得分的方法
                judge.getScore(player,computer,judge1);
                //显示姓名和得分
                System.out.println("姓名 得分");
                System.out.println(player.getName()+" " +player.getScore());
                System.out.println(computer.getName()+" "+computer.getScore());
                System.out.println("是否开始下一轮(y/n)?");
            }else if(gameStart.equals("n")  || gameStart.equals("N")){
                break;
        }else {
                System.out.println("输入错误,请重试");
            }
        }
        if(times == 0){
            System.out.println("连挑战我的勇气都没有吗?");
        }else {
            //根据总分,判断谁胜利
        judge.victory(player,computer);}
        System.out.println("游戏结束");
    }
}

程序运行结果

java猜拳小游戏窗口可视化 eclipse猜拳游戏_java猜拳小游戏窗口可视化

第二种写法(面向过程)

题目分析

  • 共需要5个类
  • 自定义类:Player玩家类 Computer电脑类 Test测试类
  • 导入类:Scanner类 Random类

代码展示

Player类

public class Player {
    //玩家名字
    private  String name;
    //玩家得分
    private  int score;
    //无参构造方法
    public Player() {
    }
    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public Player(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

Computer类

public class Computer {
    //电脑名字
    private  String name;
    //电脑得分
    private int score;
    public Computer(String name) {
        this.name = name;
    }

    public Computer() {

    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getName() {
        return name;
    }

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

Test类

import java.util.Random;
import java.util.Scanner;
public class Test {
    //猜拳游戏
    public static void fingerGuessingGame(int select,int selectOfComputer,Player player,Computer computer){
        //包含了玩家和电脑都是1 玩家和电脑都是2 玩家和电脑都是3
       boolean b1 = (select == selectOfComputer);//平手
       boolean b2 = (select ==2 && selectOfComputer ==1);//玩家石头 电脑剪刀
        boolean b3 = (select ==3 && selectOfComputer ==2);//玩家布 电脑石头
        boolean b4 = (select ==1 && selectOfComputer ==3);//玩家剪刀 电脑布
        if(b1){
            System.out.println("双方平手");
        }else if(b2 || b3 || b4){
            System.out.println("没想到你还挺厉害的");
            player.setScore(player.getScore()+1);
        }else{
            System.out.println("你输了,你真笨");
            computer.setScore(computer.getScore()+1);
        }
    }
    public static void main(String[] args) {
        //对战次数
        int times = 0;
        //创建玩家对象
        Player player = new Player();
        //创建电脑对象
        Computer computer = new Computer();
        //Scanner类
        Scanner sc = new Scanner(System.in);
        //Random类
        Random random = new Random();
        System.out.println("*******************");
        System.out.println("**猜拳游戏  马上开始**");
        System.out.println("*******************");
        System.out.println("出拳规则:1.剪刀 2.石头 3.布");
        System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操)");
        loop1:while (true) {
            //获取选择的对方角色数字(1,2或3)
            int computerName = sc.nextInt();
            switch (computerName) {
                case 1:
                    computer.setName("刘备");
                    break loop1;
                case 2:
                    computer.setName("孙权");
                    break loop1;
                case 3:
                    computer.setName("曹操");
                    break loop1;
                default:
                    System.out.println("你输入的数字不正确,请重试");
                    break;
            }
        }
        System.out.print("请输入你的姓名");
        player.setName(sc.next());
        System.out.println(player.getName() + " vs "+computer.getName() +"对战开始");
        System.out.print("要开始吗?输入y/n");
        loop2:while(true) {
            String gameStart = sc.next();
            if(gameStart.equals("y")  || gameStart.equals("Y") ){
                System.out.println("对战次数"+times);
                System.out.println("请出拳:1.剪刀 2.石头 3.布");
                int select = sc.nextInt();
                switch (select){
                    case 1:
                        System.out.println("你出剪刀");break;
                    case 2:
                        System.out.println("你出石头");;break;
                    case 3:
                        System.out.println("你出布");break;
                    default:
                        System.out.println("你输入的数字错误,请重试");
                }
                //random类的nextInt方法是左开右闭区间
                int selectOfComputer = random.nextInt(3)+1;
                switch (selectOfComputer){
                    case 1:
                        System.out.println(computer.getName()+"出剪刀");break;
                    case 2:
                        System.out.println(computer.getName()+"出石头");;break;
                    default:
                        System.out.println(computer.getName()+"出布");break;
                }
                fingerGuessingGame(select,selectOfComputer,player,computer);
                   times++;
                System.out.println("姓名 得分");
                System.out.println(player.getName()+" " +player.getScore());
                System.out.println(computer.getName()+" "+computer.getScore());
                System.out.println("是否开始下一轮(y/n)?");
            }else if(gameStart.equals("n")  || gameStart.equals("N")){
                break loop2;
            }
        }
        if(times == 0){
            System.out.println("连挑战我的勇气都没有吗?");
        }
        else if(player.getScore() == computer.getScore()) {
            System.out.println("竟然是平手,看来小瞧你了");
        }else if(player.getScore() < computer.getScore()){
            System.out.println("呵呵,下次加油啊");
        }else{
            System.out.println("败给你了,我以后不会再放水了");
        }
    }
}

程序运行结果

java猜拳小游戏窗口可视化 eclipse猜拳游戏_游戏_02