java 人机猜拳 游戏

人机猜拳-游戏

掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用

  1. 定义一个电脑类:Computer.java
点击查看【Computer.java】代码
/**
 * @Title: 电脑类
 * @Description: 定义电脑类创建时将拥有的属性和方法
 * @author: TabKey9
 * @CreateDate: 2022.2.26
 */
public class Computer {
    static String name = "关羽";//名字
    static double integral;//总积分
    static String chu;//本次出拳状态

    /**
     * @description: 出拳动作
     * @param: null
     * @return: null
     * @author: TabKey9
     * @update: 2022/2/26  17:35
     */
    public static void chu(){
        int val = (int)(Math.random()*3) + 1;//1个随机数:取值范围0~2
        switch(val){
            case 1:
                chu = "石头";
                System.out.println( name +" 电脑出【石头】");
                break;
            case 2:
                chu = "剪刀";
                System.out.println( name +" 电脑出【剪刀】");
                break;
            case 3:
                chu = "布";
                System.out.println(name +" 电脑出【布】");
                break;
        }
    }

}


2.定义一个玩家类:Mankind.java

点击查看【Mankind.java】代码
/**
 * @Title: 人类
 * @Description: 定义人类创建时将拥有的属性和方法
 * @author: TabKey9
 * @CreateDate: 2022.2.26
 */
public class Mankind {
    static String name = "王富贵";//名字
    static double integral;//总积分
    static String chu;//本次出拳状态

    /**
     * @description: 出拳动作
     * @param: int i
     * @return: null
     * @author: TabKey9
     * @update: 2022/2/26  17:35
     */
    public static void chu(int i){
        switch(i){
            case 1:
                chu = "石头";
                System.out.println( name +" 出【石头】");
                break;
            case 2:
                chu = "剪刀";
                System.out.println( name +" 出【剪刀】");
                break;
            case 3:
                chu = "布";
                System.out.println(name +" 出【布】");
                break;
        }
    }

}


  1. 最后再写一个游戏类:Game.java
点击查看【Game.java】代码
import java.util.Scanner;

/**
 * @Title: 人机猜拳-游戏
 * @Description: 掌握类和对象的使用,掌握方法的定义和返回值,掌握封装的运用
 * @author: TabKey9
 * @CreateDate: 2022.2.26
 */
public class Game {
    public static void main(String[] args) {
        System.out.println("【========== 人机猜拳游戏 ==========】");
        Scanner input = new Scanner(System.in);// 创建扫描仪,用于获取用户输入值

        Computer dn = new Computer();//创建一个电脑玩家
        System.out.println("请选择电脑玩家:1、小伙子;2、小姑凉;2、关羽;4、唐僧");
        switch(input.nextInt()){
            case 1:
                dn.name ="小伙子";
                break;
            case 2:
                dn.name ="小姑凉";
                break;
            case 3:
                dn.name ="关羽";
                break;
            case 4:
                dn.name ="唐僧";
                break;
        }

        Mankind ren = new Mankind();//创建一个玩家
        System.out.print("请输入玩家昵称:");
        ren.name = input.next();

        while(true){
            System.out.print("【三局两胜】开始游戏?(y/n):");// 开始游戏?
            String start = input.next();
            if (start.equals("n")){ // 判断字符串需要用字符串的函数方法,而不是用运算符比较字符串
                System.out.println("游戏结束,下次再玩吧!");
                System.exit(0);// 终止游戏
            }else if (!start.equals("y")){
                System.out.println("请按要求重新输入!!!");
                continue;// 跳过本次循环
            }
            System.out.println("游戏规则:1、石头;2、剪刀;3、布");// 声明游戏规则

            for (int i=1,d=0,r=0;i<3 || d==r;i++){
                System.out.println();
                System.out.print("请出拳:");
                int val = input.nextInt();
                ren.chu(val);//玩家出拳一次
                dn.chu();//电脑出拳一次

                //        判断胜负结果
                if ( (dn.chu=="剪刀" && ren.chu=="石头") || (dn.chu=="石头" && ren.chu=="布") || (dn.chu=="布" && ren.chu=="剪刀") ){
                    r++;
                    System.out.println("人/机 比分:"+ d +":"+ r);
                    System.out.println( "你 得分!");
                }else if ( dn.chu == ren.chu ){
                    i--;// 平局还得i--,不然1:0也会提前结束游戏
                    System.out.println("人/机 比分:"+ d +":"+ r);
                    System.out.println("平局!再来?");
                }else{
                    d++;
                    System.out.println("人/机 比分:"+ d +":"+ r);
                    System.out.println( "电脑 得分");
                }

//                累计总积分:输了--,赢了++
                if ( r==2 && d<r ){
                    ren.integral++;
                    dn.integral--;
                }else if ( d==2 && d>r ){
                    ren.integral--;
                    dn.integral++;
                }
            }
//            展示总积分
            System.out.println();
            System.out.println("你 的总积分:"+ ren.integral +";\t\t"+ dn.name +" 的总积分:"+ dn.integral);
            System.out.println();
        }
    }

}


游戏体验:

posted @ 2022-02-28 13:58  TabKey9  阅读(306)  评论(0编辑  收藏  举报