JAVA面向对象实现猜拳小游戏

该代码示例展示了一个简单的Java程序,实现了一个石头剪刀布游戏。GameMachine类处理游戏逻辑,包括玩家(Player)与电脑的对战和结果判定。Player类存储玩家的姓名及胜、负、平局计数。Main_类作为主入口,负责用户交互和游戏循环。用户可以输入选择(0-石头,1-剪刀,2-布)并决定是否继续游戏。
摘要由CSDN通过智能技术生成

在这个实现中,

GameMachine 类代表游戏机,

包含 play 方法来模拟游戏的进行。,

包含displayGameInfo方法来显示玩家游戏信息。

Player 类代表玩家,

包含 属性姓名name,胜利数winCount,失败数loseCount,平局数tieCount

包含属性相关的get和set方法

Main_类是游戏入口类,负责程序的执行和与用户的交互。

代码实现如下:

GameMachine

package GameMachine;


import java.util.Random;

/**
 * @author Magic
 * @version 1.0
 */
public class GameMachine {
    /**
     *使用游戏机类实现玩游戏
     * @param player 游戏玩家
     * @param playerChoice 玩家出拳选择
     */
    public static void play(Player player,int playerChoice) {
        String[] choice = {"石头","剪刀","布"};//索引对应"0石头","1剪刀","2布"
        //电脑出拳0~2
        Random random = new Random();
        int computerChoice = random.nextInt(3);

        //显示出拳信息
        System.out.println("电脑出拳" + choice[computerChoice]);
        System.out.println("玩家出拳" + choice[playerChoice]);

        //胜负判断
        if (playerChoice == 0 && computerChoice == 1 ||
            playerChoice == 1 && computerChoice == 2 ||
            playerChoice == 2 && computerChoice == 0) {
            player.setWinCount();
            System.out.println("玩家" + player.getName() + "胜利");
        } else if (playerChoice == computerChoice) {
            player.setTieCount();
            System.out.println("平局");
        } else {
            player.setLoseCount();
            System.out.println("玩家" + player.getName() + "失败");
        }

        //显示玩家游戏信息
        displayGameInfo(player);
    }

    /**
     * 打印玩家游戏信息
     * @param player 玩家对象  用于获取玩家信息
     */
    public static void displayGameInfo(Player player) {
        System.out.println("玩家" + player.getName() + "游戏信息如下");
        System.out.println("胜利数" + player.getWinCount());
        System.out.println("失败数" + player.getLoseCount());
        System.out.println("平局数" + player.getTieCount());
    }
}

Player 类

package GameMachine;

/**
 * @author Magic
 * @version 1.0
 */
public class Player {
    //玩家名字
    private String name;
    //胜利次数
    private int winCount;
    //失败次数
    private int loseCount;
    //平局次数
    private int tieCount;


    //初始化次数
    public Player(String name) {
        this.name = name;
        this.winCount = 0;
        this.loseCount = 0;
        this.tieCount = 0;
    }

    public String getName() {
        return name;
    }

    public int getWinCount() {
        return winCount;
    }

    public int getLoseCount() {
        return loseCount;
    }

    public int getTieCount() {
        return tieCount;
    }

    public void setWinCount() {
        this.winCount++;
    }

    public void setLoseCount() {
        this.loseCount++;
    }

    public void setTieCount() {
        this.tieCount++;
    }
}


Main_类

package GameMachine;

import java.util.Scanner;

/**
 * @author Magic
 * @version 1.0
 */
public class Main_ {
    public static void main(String[] args) {
        //new 一个玩家对象
        Player aiKun = new Player("爱坤");
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.print("请输入\"0石头\",\"1剪刀\",\"2布\"");
            //接收用户出拳
            int playerChoice = scanner.nextInt();
            //开始玩游戏
            GameMachine.play(aiKun,playerChoice);
            //循环判断是否继续游戏
            String exit = null;
            while (!("y".equalsIgnoreCase(exit) || "n".equalsIgnoreCase(exit))) {
                System.out.println("是否继续游戏?(y/n)");
                exit = scanner.next();
            }
            //判断用户输入内容是否相等不区分大小写
            if (exit.equalsIgnoreCase("n")) {
                break;
            }
        }
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值