猜拳游戏(题目+源码)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猜拳游戏(题目+源码)相关的知识,希望对你有一定的参考价值。

题目:

随机数的应用-猜拳游戏:石头、剪刀、布
分析:1 两个对象(人person和机器compter),抽象出公共的部分(Player类,包含玩家姓名属性、积分属性、出拳的抽象方法),让人和机器实现它。
2 定义一个Game游戏类,包含PK的方法和显示最终结果的方法,
3 定义一个测试类,进行游戏,直到玩家选择退出游戏才结束对战

 例如;

第1局开始
请输入1石头 2剪刀 3布
-->用户选择
-->输出结果
小白出拳:石头
电脑出拳:布
结果:电脑赢了

提示是否继续,继续请输入Y?【若继续】:

第2局开始
请输入1石头 2剪刀 3布
-->用户选择
-->输出结果 小白出石头 电脑出布 电脑赢了


【若不继续】:输出积分:小白积分:1 ,电脑积分:0

 

import java.util.Scanner;

public class TestGame {

    public static void main(String[] args) {
        
//        玩家对象
        Person p = new Person("小白");
//        电脑对象
        Computer c = new Computer("电脑CC");
        
        Game game = new Game(p, c);
        
//        game.pk();
        Scanner input = new Scanner(System.in);
        while (true) {
            game.pk();
            
            System.out.println("是否继续,继续Y");
            String str = input.next();
            //忽略大小写
            if (!"Y".equalsIgnoreCase(str)) {
                break;
            }    
        }
        


 

public abstract class Player {                  //抽象父类

    private String name;//用户名
    private int    score;//积分
    
    public Player() {
        
    }

    public Player(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    /**
     * 出拳
     */
    public abstract int showFist();
}

 

 

import java.util.Scanner;

public class Person extends Player{

    public Person(String pname){
        super(pname);
    }
    
    @Override
    public int showFist() {
        System.out.println("请用户输入1石头 2剪刀 3布");
        Scanner input = new Scanner(System.in);
        int fist  = input.nextInt();
        return fist;
    }

}

 

 

import java.util.Random;

/**
 * 电脑出拳
 * @author Administrator
 *
 */
public class Computer extends Player{

    public Computer(String cname){
        super(cname);
    }
    
    @Override
    public int showFist() {
    
        //随机数 1-3
        Random random = new Random();
        int fist = random.nextInt(3) +1; 
        return fist;
    }

    
    
}

 

public class Game {
    
    private Person p;
    private Computer c;
//    private int i = 1;
    
    public Game(Person p,Computer c){
        
        this.p = p;
        this.c = c;
    }

    /**
     * pk
     * 判断当前局的输赢结果
     */
    public void pk(){
    
        //提升部分:五局
        for (int i = 1; i <= 5; i++) {
            
            System.out.println("第" + i +"局");
            //获取玩家出拳的数字
            int pfist = p.showFist();
            //获取电脑出拳的数字
            int cfist = c.showFist();            
            //获取玩家出拳的具体
            String pValue = getFistValue(pfist);                
            //获取电脑出拳的具体
            String cValue = getFistValue(cfist);    
                            
            //比较规则
            if ((pfist == 1 &&  cfist == 2) ||(pfist == 2 &&  cfist == 3) || (pfist == 3 &&  cfist == 1) ) {
                
                System.out.println(p.getName() + "玩家出拳:" + pValue);        
                System.out.println(c.getName() + "电脑出拳:" + cValue);
                System.out.println("恭喜你:" + p.getName() );
                
                //用户积分 +1
                p.setScore(  p.getScore() + 1  );
                        
            }else if (pfist == cfist) {
                System.out.println(p.getName() + "玩家出拳:" + pValue);        
                System.out.println(c.getName() + "电脑出拳:" + cValue);
                System.out.println("平局" );
                
            }else{
                System.out.println(p.getName() + "玩家出拳:" + pValue);        
                System.out.println(c.getName() + "电脑出拳:" + cValue);
                System.out.println("你输了" +  ",电脑:" + c.getName() + "赢了");
                
                c.setScore(  c.getScore() + 1  );                
            }
        }
        
//        i++;
        
        // 提升完之后:调用显示结果的方法
        showResult();
                
    }
    
    //获取猜拳对应的中文
    public String getFistValue(int fist){
        
        String fistValue = "";
        switch (fist) {
        case 1:
            fistValue = "石头";
            break;
        case 2:
            fistValue = "剪刀";
            break;
        case 3:
            fistValue = "布";
            break;
        default:
            break;
        }
        return fistValue;
        
    }
    
    //最终结果
    public void showResult() {
        System.out.println("最终结果:");
        System.out.println(p.getName()  +"最终积分: " + p.getScore());
        System.out.println(c.getName()  +"最终积分: " + c.getScore());
        
        
    }
}

 

以上是关于猜拳游戏(题目+源码)的主要内容,如果未能解决你的问题,请参考以下文章

第83篇 笔记-猜拳游戏智能合约

猜拳游戏

三Java面向对象之猜拳游戏

猜拳游戏全代码

猜拳游戏三局两胜------java实现代码

猜拳游戏