其他分享
首页 > 其他分享> > 实现猜拳游戏

实现猜拳游戏

作者:互联网

通过控制台方式实现一个人机对战的猜拳游戏,用户通过输入(1.剪刀 2.石头 3.布),机器随机生成(1.剪刀 2.石头 3.布),胜者积分,n局以后通过积分的多少判定胜负。

package homework.chapter3.section5;

import java.util.Random;
import java.util.Scanner;

public class FingerGuessingGame {

    private String peoplePlayer;
    private String computerPlayer;
    /**
     * 用于计算人类玩家赢局数
     */
    private int countPeople;
    /**
     * 用于计算电脑玩家赢局数
     */
    private int countComputer;

    public void fingerGuessing() {
        // 解释比赛规则
        System.out.println("===猜拳游戏===\n比赛规则:用户通过输入数字(1-剪刀,2—石头,3-布)与电脑进行对战,当某一方的分数达到率先达到3分,该方胜利!");

        while(true) {
            System.out.println("请输入1~3之间的整数:");
            int a;

            // 判断人类玩家输入的内容
            while (true) {
                // 正确接收用户的输入
                Scanner input = new Scanner(System.in);

                if (input.hasNextInt()) {
                    int b = input.nextInt();
                    if (b > 0 && b < 4) {
                        a = b;
                        break;
                    } else {
                        System.out.println("输入的数字不在规定的范围,请重新输入!");
                    }
                } else {
                    System.out.println("输入的不是整数,请重新输入!");
                }
            }

            peoplePlayer = getResult(a);

            // 电脑玩家随机产生的内容
            Random r = new Random();
            int b = r.nextInt(3) + 1;

            computerPlayer = getResult(b);

            //比较两者胜负
            boolean peopleWin = ((a == 1 && b == 3) || (a == 2 && b == 1) || (a == 3 && b == 2));
            if(a == b) {
                System.out.println("本场比赛平局!因为你输入的是“" + peoplePlayer + "”,电脑输入的是“" +
                        computerPlayer + "”。\n用户积分为:" + countPeople + ",电脑积分为:" + countComputer);
            } else if (peopleWin) {
                countPeople++;
                System.out.println("恭喜你,本局你获胜!因为你输入的是“" + peoplePlayer + "”,电脑输入的是“" +
                        computerPlayer + "”。\n用户积分为:" + countPeople + ",电脑积分为:" + countComputer);
            } else {
                countComputer++;
                System.out.println("很遗憾,本局电脑获胜!因为你输入的是“" + peoplePlayer + "”,电脑输入的是“" +
                        computerPlayer + "”。\n用户积分为:" + countPeople + ",电脑积分为:" + countComputer);

            }

            //比较二者的积分,确定最终的胜利者
            if (countPeople == 3 || countComputer == 3) {
                System.out.println("比赛结束!");
                if (countPeople > countComputer) {
                    System.out.println("恭喜你,获得最终胜利!\n你获得" + countPeople + "分,电脑获得" + countComputer + "分。");
                }else {
                    System.out.println("很遗憾,电脑获得最终的胜利。\n你获得" + countPeople + "分,电脑获得" + countComputer + "分。");
                }
                break;
            }
        }
    }

    /**
     * 把通过输入数字产生对应结果的操作封装为一个私有方法
     * @param num
     * @return
     */
    private String getResult(int num) {
        String result = null;
        switch (num) {
            case 1:
                result = "剪刀";
                return result;
            case 2:
                result = "石头";
                return result;
            case 3:
                result = "布";
                return result;
            default:
        }
        return result;
    }
}

package homework.chapter3.section5;

public class FingerGuessingGameTest {

    public static void main(String[] args) {
        FingerGuessingGame fingerGuessingGame = new FingerGuessingGame();
        fingerGuessingGame.fingerGuessing();
    }
}

运行结果:
在这里插入图片描述

标签:猜拳,countPeople,实现,countComputer,System,游戏,println,输入,out
来源: https://blog.csdn.net/ahwhlcz/article/details/119855890