じゃんけんゲームコード(java作成)

24102 ワード

じゃんけんゲーム
実現機能:1.コンピューター類、プレイヤー類を定義する2.プレイヤーを追加し、プレイヤーを選択してゲームを行う3.ゲームはヒューマンゲームとダブルゲームの2種類のモードに分けられる4.プレイヤーは1ラウンドあたりのゲーム局数を設定することができ、各局のゲームはすべてじゃんけんの結果を表示し、システムは結果によってプレイヤーのポイントとなり、プレイヤーに勝ってポイントを獲得する.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("       :
1.
2.
3.
0. "
); 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(" :
1.
2.
3.
0. "
); meanSel=sc.nextInt(); } System.out.println(" , !"); } }