java第一阶段(day06)人机猜拳

人机猜拳

(1) 用户类

@Setter
@Getter
public class UserInfo {
    private int userScore;
    private String username;

    public int showFist(Scanner input) {
        System.out.println("开始出拳,请选择:1.剪刀 2.石头 3.布(填写对应的数字,进行出拳)");
        int choice = input.nextInt();
        String result = FistUtil.fist(choice);
        System.out.println(username+"出拳:" + result);
        return choice;
    }
}

(2)出拳规则

public class FistUtil {
    private FistUtil() {
    }

    public static String fist(int num) {
        String result = "";
        switch (num) {
            case 1:
                result = "剪刀";
                break;
            case 2:
                result = "石头";
                break;
            case 3:
                result = "布";
                break;
        }
        return result;
    }
}

(3)计算机类

@Setter
@Getter
public class Computer {
    private String computerName;
    private int computerScore;

    public int showFist() {
        //随机的
        int num = (int) (Math.random() * 3 + 1);
        String result = FistUtil.fist(num);
        System.out.println(computerName + "出拳:" + result);
        return num;
    }
}

(4)游戏类

public class StartGame {
    private static Scanner input;
    private static UserInfo userInfo;
    private static Computer computer;

    static {
        input = new Scanner(System.in);
        userInfo = new UserInfo();
        computer = new Computer();
}
    public void start() {
        String nextJu;
        do {
            System.out.println("-----------欢迎进入游戏世界-----------");
            System.out.println("**********************");
            System.out.println("**    猜拳游戏开始    **");
            System.out.println("**********************");
            System.out.println("出拳规则:1.剪刀 2.石头 3.布");
            //给计算机+用户给定名称
            choseRole();

            System.out.println("要开始吗?y/n");
            String answer = input.next();
            if ("n".equals(answer)) {
                System.out.println("程序结束");
                input.close();
                return;
            }
            String nextCircle;
            int count = 0;
            do {
                count++;
                //开始出拳
                int userFist = userInfo.showFist(input);
                int computerFist = computer.showFist();
                // 1  2  3
                // 3  1  2
                battle(userFist, computerFist);
                System.out.println("是否继续下一轮?y/n");
                nextCircle = input.next();
            } while ("y".equals(nextCircle));

            //不继续下一轮的操作
            calFinalResult(count);

            System.out.println("是否开始下一局?y/n");
            nextJu = input.next();
            userInfo.setUserScore(0);
            computer.setComputerScore(0);
        } while ("y".equals(nextJu));

        System.out.println("程序结束");
        input.close();

    }

    private void calFinalResult(int count) {
        String username = userInfo.getUsername();
        String computerName = computer.getComputerName();
        int userScore = userInfo.getUserScore();
        int computerScore = computer.getComputerScore();

        System.out.println(username + " VS " + computerName);

        System.out.println("对战次数:" + count);
        System.out.println("姓名\t得分");
        System.out.println(username + "\t" + userScore);
        System.out.println(computerName + "\t" + computerScore);

        if (userScore == computerScore) {
            System.out.println(username + "与" + computerName + "最终是平手");
        } else if (userScore > computerScore) {
            System.out.println(username + "赢了");
        } else {
            System.out.println(computerName + "赢了");
        }
    }

    private void battle(int userFist, int computerFist) {
        int num = userFist - computerFist;
        if (userFist == computerFist) {
            System.out.println("平局");
        } else if (num == 1 || num == -2) {
            userInfo.setUserScore(userInfo.getUserScore() + 1);
            System.out.println(userInfo.getUsername() + "赢了");
        } else {
            computer.setComputerScore(computer.getComputerScore() + 1);
            System.out.println(computer.getComputerName() + "赢了");
        }
    }

    //给计算机选择角色
    private void choseRole() {
        System.out.println("请选择对方角色:(1.刘备 2.孙权 3.曹操):");
        int choice = input.nextInt();
        String roleName = "";
        switch (choice) {
            case 1:
                roleName = "刘备";
                break;
            case 2:
                roleName = "孙权";
                break;
            case 3:
                roleName = "曹操";
                break;
        }
        System.out.println("请录入你的名称:");
        String username = input.next();

        computer.setComputerName(roleName);
        userInfo.setUsername(username);
        System.out.println(username + " VS " + roleName);
    }
}

(5)程序入口

public class APP {
    public static void main(String[] args) {
        new StartGame().start();
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值