项目案例:人机猜拳 1.面向对象设计的思想 2.使用类图理解类的关系 3 . 类的封装4. 构造方式的使用 5. this、static关键字的使用完成人机猜拳互动游戏的开发 选取对战角色 猜拳

用户类

分析业务

输入数值,转换为石头剪刀布

抽象出类、类的特征和行为

代码如下:

package day6.boxing;

import java.util.Scanner;

public class Administrator {

    String shape;//剪刀石头布
    int number;//输入数
    private int win;//得分     注意:得分不能随便被复制,用private进行保护。

    public int getWin() {
        return win;
    }

    public void setWin(int win) {
        this.win = win;
    }

    public void show(){
        Scanner input = new Scanner(System.in);
        System.out.print("请出拳:1.剪刀 2.石头 3.布 (请输入相应数字):");
        this.number =input.nextInt();
        switch (this.number){
            case 1:
                this.shape="剪刀";
                break;
            case 2:
                this.shape="石头";
                break;
            case 3:
                this.shape="布";
                break;
        }
    }
    public void print() {
        System.out.println("你出拳:" + this.shape);
    }
}

计算机类

分析业务

计算机随机给个数值,转换为石头剪刀布。
##创建计算机类Computer。实现计算机出拳,用随机数实现。

代码如下:

package day6.boxing;

import java.util.Random;

public class Computer {

    String shape;//剪刀石头布
    int number;//电脑随机数字
    private int win;//电脑赢得次数

    public int getWin() {
        return win;
    }

    public void setWin(int win) {
        this.win = win;
    }

    public void show(){
        Random random = new Random();
        int num = random.nextInt(2);
        this.number=num+1;
        switch (this.number){
            case 1:
                this.shape="剪刀";
                break;
            case 2:
                this.shape="石头";
                break;
            case 3:
                this.shape="布";
                break;
        }
    }
    public void print() {
        System.out.println("电脑出拳:" + this.shape);
    }
}

游戏类

创建游戏类Game

编写游戏类的初始方法initial()

在initial方法里,定义规则和循环使用;
##编写游戏类的开始游戏方法startGame()
在startGame方法里,只需要提示用户开始游戏

代码如下:

package day6.boxing;

import java.util.Scanner;

public class Game{

    String role;//电脑角色
    String name;//自己名字
    public void startGame(){
        System.out.println("----------------欢 迎 进 入 游 戏 世 界----------------\n\n");
        System.out.println("\t\t\t\t******************");
        System.out.println("\t\t\t\t**  猜拳,开始   **");
        System.out.println("\t\t\t\t******************\n\n");
    }
    public void initial(){
        Administrator administrator = new Administrator();
        Computer computer = new Computer();
        Scanner input = new Scanner(System.in);
        System.out.println("出拳规则:1.剪刀 2.石头 3.布");
        System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
        int num=input.nextInt();
        switch (num){
            case 1:
                this.role="刘备";
                break;
            case 2:
                this.role="孙权";
                break;
            case 3:
                this.role="曹操";
                break;
        }
        System.out.print("请输入你的姓名:");
       this.name=input.next();
        System.out.println(this.name+"VS"+this.role+"对战");
        System.out.print("要开始吗?(y/n)");
        String start = input.next();
        int i=0;
        while (start.equals("y")) {
            i++;
            administrator.show();
            computer.show();
            administrator.print();
            computer.print();
            {
                if (administrator.number == 1) {
                    switch (computer.number) {
                        case 1:
                            System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
                            break;
                        case 2:
                            System.out.println("结果:^_^,你输了,真笨!");
                            computer.setWin(computer.getWin()+1);
                            break;
                        case 3:
                            System.out.println("结果:恭喜,你赢了!");
                            administrator.setWin(administrator.getWin()+1);
                            break;
                    }
                }
                if (administrator.number == 2) {
                    switch (computer.number) {
                        case 1:
                            System.out.println("结果:恭喜,你赢了!");
                            administrator.setWin(administrator.getWin()+1);
                            break;
                        case 2:
                            System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
                            break;
                        case 3:
                            System.out.println("结果:^_^,你输了,真笨!");
                            computer.setWin(computer.getWin()+1);
                            break;
                    }
                }
                if (administrator.number == 3) {
                    switch (computer.number) {
                        case 1:
                            System.out.println("结果:^_^,你输了,真笨!");
                            computer.setWin(computer.getWin()+1);
                            break;
                        case 2:
                            System.out.println("结果:恭喜,你赢了!");
                            administrator.setWin(administrator.getWin()+1);
                            break;
                        case 3:
                            System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
                            break;
                    }
                }
            }
            System.out.println();
            System.out.print("要开始吗?(y/n)");
            start = input.next();
        }
        System.out.println("-----------------------------------------------------");
        System.out.println(this.role +"VS"+this.name);
        System.out.println("对战次数:"+i+"\n");
        System.out.println("姓名\t\t得分");
        System.out.println(this.name+"\t\t"+administrator.getWin());
        System.out.println(this.role+"\t\t"+computer.getWin()+"\n");

        if (administrator.getWin()==computer.getWin()){
            System.out.println("结果:打成平手,下次再和你一分高下!");
        }
        if (administrator.getWin()>computer.getWin()){
            System.out.println("结果:恭喜,你赢了!");
        }
        if (administrator.getWin()<computer.getWin()){
            System.out.println("结果:呵呵,笨笨,下次加油啊!");
        }
        System.out.println("-----------------------------------------------------");
    }
}

测试类

调用Game类的方法

代码如下:

package day6.boxing;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Game game = new Game();
        game.startGame();
        game.initial();
    }
}

#总结
1、比较输赢使用数字判断输赢;
2、Administrator类和Computer类都在Game类的初始化方法initial中调用,测试类只需要开始游戏即可;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以帮你实现一个简单的人机猜拳游戏。首先,我们需要定义一个对象来表示人和机器的玩家。下面是一个示例代码: ``` public class Player { private String name; private int score; private int choice; // 构造函数 public Player(String name) { this.name = name; this.score = 0; } // 获取玩家姓名 public String getName() { return this.name; } // 获取玩家分数 public int getScore() { return this.score; } // 玩家出拳 public void makeChoice(int choice) { this.choice = choice; } // 获取玩家出拳 public int getChoice() { return this.choice; } // 玩家得分 public void win() { this.score++; } } ``` 接下来,我们需要编写一个游戏来控制游戏流程。下面是一个示例代码: ``` import java.util.Random; import java.util.Scanner; public class Game { private Player human; private Player computer; private Scanner scanner; private Random random; // 构造函数 public Game() { this.human = new Player("Human"); this.computer = new Player("Computer"); this.scanner = new Scanner(System.in); this.random = new Random(); } // 游戏开始 public void start() { System.out.println("Welcome to the Rock-Paper-Scissors game!"); while (true) { // 玩家出拳 System.out.print("Please choose (1-Rock, 2-Paper, 3-Scissors): "); int choice = scanner.nextInt(); human.makeChoice(choice); // 机器出拳 int computerChoice = random.nextInt(3) + 1; computer.makeChoice(computerChoice); System.out.println("Computer chooses " + computerChoice); // 判断胜负 int result = calculateResult(human.getChoice(), computer.getChoice()); if (result == 1) { System.out.println("You win!"); human.win(); } else if (result == -1) { System.out.println("You lose!"); computer.win(); } else { System.out.println("It's a tie!"); } // 显示分数 System.out.println("Score: " + human.getName() + " " + human.getScore() + " - " + computer.getName() + " " + computer.getScore()); // 询问是否继续 System.out.print("Do you want to continue? (Y/N) "); String answer = scanner.next(); if (answer.equalsIgnoreCase("N")) { break; } } System.out.println("Thanks for playing!"); } // 计算胜负 private int calculateResult(int humanChoice, int computerChoice) { if (humanChoice == computerChoice) { return 0; } else if (humanChoice == 1 && computerChoice == 3 || humanChoice == 2 && computerChoice == 1 || humanChoice == 3 && computerChoice == 2) { return 1; } else { return -1; } } } ``` 最后,我们可以在主函数中创建一个游戏对象并启动游戏: ``` public static void main(String[] args) { Game game = new Game(); game.start(); } ``` 这样就完成了一个简单的人机猜拳游戏的实现。 ### 回答2: 使用Java编程语言实现人机猜拳可以通过对象完成。首先,我们可以创建一个Player来表示玩家对象,其中包括玩家的姓名和选择的手势。Player可以具有以下属性和方法: 属性: - 姓名:表示玩家的姓名。 - 手势:表示玩家选择的手势(剪刀、石头、布)。 方法: - 获取姓名:用于获取玩家的姓名。 - 设置手势:用于设置玩家选择的手势。 - 获取手势:用于获取玩家选择的手势。 然后,我们可以创建一个Computer来表示计算机对象,其中包括计算机的选择的手势。Computer可以具有以下属性和方法: 属性: - 手势:表示计算机选择的手势(剪刀、石头、布)。 方法: - 设置手势:用于设置计算机选择的手势。 - 获取手势:用于获取计算机选择的手势。 接下来,我们可以创建一个Game来处理玩家和计算机的猜拳游戏逻辑。Game可以具有以下方法: 方法: - 开始游戏:用于开始猜拳游戏。 - 玩家出拳:用于让玩家输入手势。 - 计算机出拳:用于让计算机随机选择手势。 - 判断结果:用于判断玩家和计算机的手势,显示获胜者。 在Game的开始游戏方法中,可以通过调用玩家出拳和计算机出拳的方法来让玩家和计算机进行选择手势,并通过判断结果的方法来判断获胜者。 以上就是使用Java对象完成人机猜拳的简要说明。具体实现的代码可以根据需要进行进一步编写和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值