简单的java小游戏-石头剪刀布(课程设计/课后习题)

石头剪刀布-简单的java小游戏

一、案例分析

隔壁秃头今天发来一个题目,叫我们用Java给他我们做一个小游戏,具体内容有:

  1. 实现传统益智游戏:石头剪刀布
  2. 用户进入OS,首先输入自己的游戏名称
  3. 选择一位对手进行比拼
  4. 每进行一轮比拼就记录结果,方便最后计分
  5. 最后根据胜场数评出最终获胜方

二、结构设计

对规则做了如下规定:

  • 数字1:剪刀、数字2:石头、数字3:布
    构建:

  • Dos:主类

  • User:用户类
    1)用户名
    2)得分

  • Opponent:对手类
    1)对手名
    2)得分
    3)挑战难度(0~1)

三、运行结果

简单的java小游戏-石头剪刀布(课程设计/课后习题)_第1张图片

四、具体代码

import java.util.*;

public class Dos {
     
    public static void main(String[] args) {
     
        Opponent[] ops = new Opponent[5];
        ops[0]=new Opponent("诸葛亮", 0.8);
        ops[1]=new Opponent("司马懿", 0.5);
        ops[2]=new Opponent("曹操", 0.4);
        ops[3]=new Opponent("刘备", 0.3);
        ops[4]=new Opponent("孙权", 0.1);
        ArrayList<Opponent> opList = new ArrayList<>();
        show();
        System.out.print("#请输入您的名称:");
        String Name = (new Scanner(System.in).nextLine());
        User mySelf = new User(Name, new Opponent("张飞", 0.1));
        String key = "y";
        while(key.equals("v")||key.equals("y")) {
     
            opList.clear();
            System.out.println("\n请选择您的对手:");
            for (int i=0;i<ops.length;i++){
     
                ops[i].Score=0;
                System.out.print("["+(i+1)+"]:"+ops[i].Name+"   ");
            }
            System.out.println();
            String opName = "";
            int num=new Scanner(System.in).nextInt();
            if(!(""+num).matches("[1-"+ops.length+"]{1}")){
     
                System.out.print((""+num).equals("0")?"":"对手不存在!");
                continue;
            }
            mySelf = new User(Name, ops[num-1]);
            key="y";
            while (key.equals("y")) {
     
                int result = mySelf.punch();
                if (result == 1)
                    mySelf.Score += 1;
                else if (result == 2)
                    ops[num-1].Score += 1;
                System.out.print("是否开始下一轮(y/n),重新选择对手(v):");
                key = (new Scanner(System.in).next()).toLowerCase();
                if (key.equals("v"))
                    break;
            }
            System.out.println("===》共进行比赛"+mySelf.times+"次,胜负结果如下:");

            opList.add(ops[num-1]);
            System.out.println("姓名      " + "得分");
            Opponent newop=new Opponent(mySelf.Name,0);
            newop.Score=mySelf.Score;
            opList.add(newop);
            opList.sort(new Comparator<Opponent>() {
     
                @Override
                public int compare(Opponent o1, Opponent o2){
     
                    return (""+o2.Score).compareTo(""+o1.Score);
                }
            });

            for (Opponent item : opList) {
     
                System.out.println(item.Name + "        " + item.Score);
            }
            System.out.println( (mySelf.Score>ops[num-1].Score)?"===>恭喜恭喜,你赢了!":(mySelf.Score<ops[num-1].Score)?"====>很遗憾,你输了!":"====>难分胜负平局,请再战三百回合!");
            System.out.println("#按任意键继续:");
        }
    }
    public static void show(){
     
        System.out.println("================欢迎来到人机对战==================");
        System.out.println("*********************************");
        System.out.println("**           猜拳,开始         **");
        System.out.println("*********************************\n\n");
    }
}

import java.util.Scanner;

public class User{
     
    String Name;
    Opponent opponent;
    int Score=0;

    User (String name,Opponent opponent){
     
        this.Name=name;
        this.opponent=opponent;
    }

    public void setOpponent(Opponent opponent) {
     
        this.opponent = opponent;
    }

    int times=1;
    public int punch(){
     
        int myPunch=0;
        int opponentPunch=0;

        System.out.print("\n*****************第"+(times++)+"轮大比拼***************\n" +"["+opponent.Name+" V S "+this.Name+"]\n" +"====>轮到您出拳了(1:剪刀  2:石头  3:布):");

        System.out.println("==>你出拳:"+ putName (myPunch =(new Scanner(System.in)).nextInt()) );
        if(!(myPunch>0&&myPunch<=3)){
     
            System.out.println("发现第"+(times--)+"次输入有输入错误!重新比拼!");
            return 0;
        }
        System.out.println("==>" + opponent.Name + "出拳:"+putName( opponentPunch = opponent.Punch(myPunch) ));

        return result(myPunch,opponentPunch);
    }

    int result(int myPunch,int opponentPunch){
     
        //1:win   2:over   3:平局   0:not star
        boolean bool= ((myPunch==1&&opponentPunch==3)||(myPunch==2&&opponentPunch==1)||(myPunch==3&&opponentPunch==2));

        System.out.println((myPunch!=0&&opponentPunch!=0) ?
                ( bool? "您赢了!"+opponent.Name+"输了!"
                        :(myPunch==opponentPunch) ? "平局!"
                                                    :"很遗憾,你输了!"+opponent.Name+"赢了!")
                :"");

        return (myPunch != 0&&opponentPunch != 0) ?(  bool? 1:(myPunch==opponentPunch) ? 3: 2):0;
    }

    public String putName(int style){
     
        return style==1?"剪刀":(style==2?"石头":"布");
    }
}

public class Opponent {
     
    String Name="";
    double Difficulty=0.0;
    int Score=0;

    Opponent(String name,double difficulty) 
        this.Difficulty=difficulty;
        this.Name=name;
    }

    public int Punch(int thisOpponent) {
     
        int times[]=new int[100],num=1;
        int opNum=(thisOpponent == 1) ? 2 : (thisOpponent==2 ? 3 : 1);

        for (int i=0;i<times.length;i++){
     
            while((num=(int)(Math.random()*3+1)) == opNum){
     }
            times[i]=(i<(int)(100*this.Difficulty)) ? opNum : num;
        }
        return times[(int)(Math.random()*100+0)];
    }
}

你可能感兴趣的:(学习,笔记,游戏,java,经验分享,面试,算法)