猜拳游戏代码(java编写)

猜拳游戏

实现功能:
1.定义电脑类,玩家类
2.可以添加玩家,选择玩家进行游戏
3.游戏分为人机游戏和双人游戏两种模式
4.玩家可以设定每轮游戏局数,每局游戏均显示猜拳结果,系统根据结果为玩家积分,胜出玩家获得一积分。
5.每轮游戏结束,显示玩家积分,系统根据玩家积分判断玩家输赢。
6.每轮游戏结束,玩家可以继续开始新一轮游戏,也可以退出游戏。


//电脑类
class ComputerPlayer {
    private String name="电脑";
    private int score=0;
    public int guessing (){
        return (int)(Math.random()*3)+1;
    }
    public String getName() {
        return name;
    }
    public int getScore() {
        return score;
    }
    public void setScore() {
        score=0;
    }
    public void addScore() {
        score++;
    }
}
//玩家类
class People {
    private String name="";
    private int score=0;
    private static int number=0;
    People(){
        number++;
        name="玩家"+number;
    }
    People(String name){
        number++;
        this.name=name;
    }
    public int guessing(){
        Scanner sc=new Scanner(System.in);
        int result;
        System.out.println("请输入您的猜拳结果:(1.石头 2.剪刀 3.布)");
        result=sc.nextInt();
        return result;
    }
    public String getName() {
        return name;
    }
    public int getScore() {
        return score;
    }
    public void setScore() {
        score=0;
    }
    public void addScore() {
        score++;
    }
    public static int getNumber() {
        return number;
    }
}
//游戏类
class Game{

    //人机游戏
    public void peopleAndComputerPK(People person,ComputerPlayer computer){
        Scanner sc=new Scanner(System.in);
        boolean start=true;
        int startCount=0,count=0,startJudge=1;
        while(start){
            startCount++;
            person.setScore();
            computer.setScore();;
            System.out.println("请输入第"+startCount+"轮游戏局数:");
            count=sc.nextInt();
            int n=1;
            while(count-->0){
                int pResult,cResult,win;
                System.out.println("第"+n+++"局:");
                System.out.println("玩家出拳中......");
                pResult=person.guessing();
                System.out.println("电脑出拳中......");
                cResult=computer.guessing();
                win=judge(pResult,cResult);
                if(win==1){
                    person.addScore();
                }
                if(win==2){
                    computer.addScore();
                }
                result(person.getName(), pResult, computer.getName(), cResult, win);
            }
            int winAll=judgeAll(person.getScore(),computer.getScore());
            reaultAll(n-1, person.getName(), person.getScore(), computer.getName(), computer.getScore(), winAll);
            System.out.println("本轮游戏结束,是否进入下一轮游戏?(1.是 2.否 )");
            startJudge=sc.nextInt();
            if(startJudge==2){
                start=false;
            }
        }
        System.out.println("人机游戏结束!");
    }

    //双人游戏
    public void peopleAndPeoplePK(People person1,People person2){
        Scanner sc=new Scanner(System.in);
        boolean start=true;
        int startCount=0,count=0,startJudge=1;
        while(start){
            startCount++;
            person1.setScore();
            person2.setScore();
            System.out.println("请输入第"+startCount+"轮游戏局数:");
            count=sc.nextInt();
            int n=1;
            while(count-->0){
                int p1Result,p2Result,win;
                System.out.println("第"+n+++"局:");
                System.out.println(person1.getName()+"出拳中......");
                p1Result=person1.guessing();
                System.out.println(person2.getName()+"出拳中......");
                p2Result=person2.guessing();
                win=judge(p1Result,p2Result);
                if(win==1){
                    person1.addScore();
                }
                if(win==2){
                    person2.addScore();
                }
                result(person1.getName(), p1Result, person2.getName(), p2Result, win);
            }
            int winAll=judgeAll(person1.getScore(),person2.getScore());
            reaultAll(n-1, person1.getName(), person1.getScore(), person2.getName(), person2.getScore(), winAll);
            System.out.println("本轮游戏结束,是否进入下一轮游戏?(1.是 2.否 )");
            startJudge=sc.nextInt();
            if(startJudge==2){
                start=false;
            }
        }
        System.out.println("双人游戏结束!");
    }

    //判断猜拳胜负
    private static int judge(int first,int second){
        if(first==second){
            return 0;
        }else if(first==1&&second==2||first==2&&second==3||first==3&&second==1){
            return 1;
        }else{
            return 2;
        }
    }

    //判断每轮游戏胜负
    private static int judgeAll(int first,int second){
        if(first==second){
            return 0;
        }else if(first>second){
            return 1;
        }else{
            return 2;
        }
    }

    //输出出拳结果
    private static String threeResults(int i){
        String result="";
        switch(i){
        case 1:
            result="石头";
            break;
        case 2:
            result="剪刀";
            break;
        case 3:
            result="布";
            break;
        }
        return result;
    }

    //输出每局结果
    private static void result(String person1,int result1,String person2,int result2,int win){
        System.out.println(person1+": "+threeResults(result1)+" PK "+person2+": "+threeResults(result2));
        if(win==0){
            System.out.println("平局!");
        }else if(win==1){
            System.out.println(person1+"胜,积分+1!");
        }else{
            System.out.println(person2+"胜,积分+1!");
        }
    }

    //输出每轮结果
    private static void reaultAll(int count,String person1,int score1,String person2,int score2,int winAll){
        System.out.print("总共"+count+"局:");
        System.out.println("其中"+person1+" 积分 "+score1+"分,"+person2+" 积分 "+score2+"分.");
        if(winAll==0){
            System.out.println("最终平局!");
        }else if(winAll==1){
            System.out.println("最终"+person1+"胜!");
        }else{
            System.out.println("最终"+person2+"胜!");
        }
    }

}
public class TestDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int meanSel,peopleNumber=0;
        boolean peopleJudge=false;
        People[] players=new People [10];
        Game game=new Game();
        System.out.println("欢迎进入猜拳游戏!");
        System.out.println("请输入您的选择:\n1.人机游戏\n2.双人游戏\n3.添加玩家\n0.退出游戏");
        meanSel=sc.nextInt();
        while(meanSel!=0){
            switch(meanSel){
            case 1://人机游戏
                ComputerPlayer computer=new ComputerPlayer();
                if(peopleJudge){
                    int num=players.length;
                    if(peopleNumber==1){
                        num=0;
                    }else{
                        String name="";
                        boolean judge=false;
                        while(!judge){
                            System.out.println("请输入玩家姓名:");
                            name=sc.next();
                            for(int i=0;iif(players[i].getName().equals(name)){
                                    num=i;
                                    judge=true;
                                    break;
                                }
                            }
                            if(!judge){
                                System.out.println("玩家信息错误,请重新输入玩家姓名:");
                            }
                        }
                    }
                    game.peopleAndComputerPK(players[num], computer);
                }else{
                    People person=new People();
                    game.peopleAndComputerPK(person, computer);
                }
                break;
            case 2://双人游戏
                if(peopleJudge){
                    String name1="";
                    String name2="";
                    boolean judge1=false;
                    boolean judge2=false;
                    int num1=0;
                    int num2=0;
                    while(!judge1){
                        System.out.println("请输入玩家a姓名:");
                        name1=sc.next();
                        for(int i=0;iif(players[i].getName().equals(name1)){
                                num1=i;
                                judge1=true;
                                break;
                            }
                        }
                        if(!judge1){
                            System.out.println("玩家a信息错误,请重新输入玩家a姓名:");
                        }
                    }
                    if(peopleNumber==1){
                        num2=peopleNumber;
                        players[num2]=new People();
                    }else{
                        while(!judge2){
                            System.out.println("请输入玩家b姓名:");
                            name2=sc.next();
                            for(int i=0;iif(players[i].getName().equals(name2)){
                                    num2=i;
                                    judge2=true;
                                    break;
                                }
                            }
                            if(!judge2){
                                System.out.println("玩家b信息错误,请重新输入玩家b姓名:");
                            }
                        }
                    }
                    game.peopleAndPeoplePK(players[num1], players[num2]);
                }else{
                    People person1=new People();
                    People person2=new People();
                    game.peopleAndPeoplePK(person1, person2);
                }
                break;
            case 3://添加玩家
                peopleJudge=true;
                int num;
                String name="";
                System.out.println("请输入添加玩家人数:");
                num=sc.nextInt();
                System.out.println("请输入"+num+"个玩家的姓名:");
                while(num-->0){
                    name=sc.next();
                    players[peopleNumber++]=new People(name);
                }
                System.out.println("添加完成!");
                break;
            default:
                System.out.println("您的选择输入错误!");
                break;
            }
            System.out.println("欢迎进入猜拳游戏!");
            System.out.println("请输入您的选择:\n1.人机游戏\n2.双人游戏\n3.添加玩家\n0.退出游戏");
            meanSel=sc.nextInt();
        }
        System.out.println("游戏结束,欢迎下次再玩!");
    }

}

你可能感兴趣的:(java学习)