【Java语言】项目实践:猜拳游戏(源码)(面向过程方法)

package com.njwbhz.path0214;

import java.util.Scanner;

public class Mora {
    public static void main(String[] args) {
        //敌方的游戏初始化参数
        Scanner enemyInput = new Scanner(System.in);
        int enemy;
        String enemyName = "";
        int enemyFlag = 0;

        //玩家自己的游戏参数
        Scanner playerInput = new Scanner(System.in);
        String playerName = "";

        //是否开始的参数设置
        Scanner isStartInput = new Scanner(System.in);
        String  isStart = "";

        //出拳参数
        Scanner playerPunchInput = new Scanner(System.in);
        int playerPunch = 0;
        int enemyPunch = 0;
        String playerPunchName = "";
        String enemyPunchName = "";
        int playerPunchFlag = 0;

        //统计
        //对站次数
        int matchNumber = 0;
        //敌方玩家得分
        float enemyScore = 0;
        //玩家得分
        float playerScore = 0;

        //欢迎部分
        System.out.println("——————————欢迎老板——————————");
        System.out.println("\n");
        System.out.println("\t话不多说,开始猜拳");
        System.out.println("————————————————————————");

        //初始化部分
        //介绍游戏操作
        System.out.println("赢一把加一分,其余情况均不加分");
        System.out.println("出拳规则:1. 剪刀 2. 石头 3. 布");
        //选择对方的游戏角色
        System.out.print("请输入对方的角色(1:刘备 2:孙权 3:曹操):");
        //保存敌方角色的初始化数据
        while(enemyFlag == 0){
            enemy = enemyInput.nextInt();
            switch (enemy){
                case 1:
                    enemyName = "刘备";
                    enemyFlag = 1;
                    break;
                case 2:
                    enemyName = "孙权";
                    enemyFlag = 1;
                    break;
                case 3:
                    enemyName = "曹操";
                    enemyFlag = 1;
                    break;
                default:
                    System.out.println("老板,你输入的有问题,请重新输入");
                    enemyFlag = 2;
                    break;
            }
            if (enemyFlag == 1){
                //如果输入正确,程序继续运行
                break;
            }else{
                enemyFlag = 0;
                //如果输入错误,重新进行游戏的初始化
                continue;
            }
        }
        //恢复初始化
        enemyFlag = 0;
        //代码测试
//        System.out.println("你选择的敌方游戏角色的名字为:" + enemyName );
        //玩家自己的游戏ID设置
        System.out.print("请输入你的姓名:");
        playerName = playerInput.next();
        //代码测试
//        System.out.print(playerName);
        System.out.println("【" + playerName + "】" + "对战" + "【" + enemyName + "】");
        System.out.println("\n");

        //游戏开始部分
        do {
            //提示玩家出拳
            System.out.print("请出拳:1. 剪刀 2. 石头 3. 布(输入相应的数字):");
            //保存玩家出拳数据
            while(playerPunchFlag == 0){
                playerPunch = playerPunchInput.nextInt();
                switch (playerPunch){
                    case 1:
                        playerPunchName = "剪刀";
                        playerPunchFlag = 1;
                        break;
                    case 2:
                        playerPunchName = "石头";
                        playerPunchFlag = 1;
                        break;
                    case 3:
                        playerPunchName = "布";
                        playerPunchFlag = 1;
                        break;
                    default:
                        System.out.println("老板,你输入的有问题,请重新输入");
                        playerPunchFlag = 2;
                        break;
                }
                if (playerPunchFlag == 1){
                    //如果输入正确,程序继续运行
                    break;
                }else{
                    playerPunchFlag = 0;
                    //如果输入错误,重新进行游戏的初始化
                    continue;
                }
            }
            //恢复初始化
            playerPunchFlag = 0;
            //输出玩家出的是什么拳
            System.out.println(playerName + "出拳:" + playerPunchName);
            //保存敌方出拳数据
            enemyPunch = (int)(Math.random() * 3 + 1);
            switch (enemyPunch){
                case 1:
                    enemyPunchName = "剪刀";
                    break;
                case 2:
                    enemyPunchName = "石头";
                    break;
                case 3:
                    enemyPunchName = "布";
                    break;
            }
            //输出敌方玩家出的是什么拳
            System.out.println(enemyName + "出拳:" + enemyPunchName);
            //游戏结果公布
            if (playerPunch == enemyPunch){
                System.out.println("平局");
            }else if(playerPunch == 1 && enemyPunch == 2){
                System.out.println("请" + playerName + "再接再厉");
                enemyScore++;
            }else if(playerPunch == 1 && enemyPunch == 3){
                System.out.println("恭喜" + playerName + "获得胜利");
                playerScore++;
            }else if(playerPunch == 2 && enemyPunch == 1){
                System.out.println("恭喜" + playerName + "获得胜利");
                playerScore++;
            }else if(playerPunch == 2 && enemyPunch == 3){
                System.out.println("请" + playerName + "再接再厉");
                enemyScore++;
            }else if(playerPunch == 3 && enemyPunch == 1){
                System.out.println("请" + playerName + "再接再厉");
                enemyScore++;
            }else if(playerPunch == 3 && enemyPunch == 2){
                System.out.println("恭喜" + playerName + "获得胜利");
                playerScore++;
            }
            matchNumber++;
            //询问玩家是否进入下一局游戏
            System.out.println("\n");
            System.out.println("是否开始下一轮(y/n)");
            isStart = isStartInput.next();
        }while(isStart.equalsIgnoreCase("y"));

        //游戏统计
        System.out.println("游戏统计");
        System.out.println("总共对战:" + matchNumber + "局");
        System.out.println("玩家\t\t得分");
        System.out.println(playerName + "\t\t" + playerScore);
        System.out.println(enemyName + "\t\t" + enemyScore);
        System.out.println(playerName + "的胜率为" + playerScore / enemyScore * 100 + "%");
    }
}

面向对象方法实现人机猜拳游戏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FairyKunKun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值