Java實現和電腦玩剪刀石頭佈遊戲

剪刀石頭佈遊戲大傢都玩過吧,今天分享Java實現和電腦玩剪刀石頭佈遊戲的具體代碼,供大傢參考,具體內容如下

遊戲總類

import java.util.Scanner;
public class Game {
    Scanner input = new _Scanner(System.in);
    String judge;
    int num1;//人返回的數字
    int num2;//機器返回的數字
    Computer computer = new Computer();
    Person person = new Person();
    boolean flage=false;
    int computerCount=0;
    int personCount=0;
    int pingCount=0;
    int count=0;
    public Game( ){
    }
    public boolean  start(String computerName,String inputName) {
        System.out.println("要開始瞭嗎?(y/n):");
        judge = input.next();
        if (judge.equals("y")) {
            do{
                count+=1;
                flage=false;
                num1 = person.hand();
                System.out.print(computerName+"出拳:");
                num2 = computer.hand();
                if((num1==1&&num2==1)||(num1==2&&num2==2)||(num1==3&&num2==3)){
                    System.out.println("平局");
                    System.out.println("運氣平分秋色!!!!");
                    System.out.println("出拳規則:1.剪刀2.石頭3.佈");
                    pingCount+=1;
                }else if((num1==1&&num2==3)||(num1==2&&num2==1)||(num1==3&&num2==2)){
                    System.out.println("恭喜你,你贏瞭!!!!!!");
                    personCount+=1;
                }else if((num2==1&&num1==3)||(num2==2&&num1==1)||(num2==3&&num1==2)){
                    System.out.println("運氣真不好,你輸瞭!!!!!!");
                    computerCount+=1;
                }
                System.out.println("\n是否進行下一輪?(y/n):");
                judge = input.next();
                switch (judge){
                    case "y":
                        flage=true;
                        break;
                    case "n":
                        System.out.println(computerName+"\t\t"+"vs\t\t"+inputName );
                        System.out.println("對戰次數:"+count);
                        System.out.println("姓名\t\t"+"得分");
                        System.out.println(inputName +"\t\t"+personCount);
                        System.out.println(computerName+"\t\t"+ computerCount);
                        System.out.println("平局\t\t"+ pingCount);
                        System.out.println("結果: \t\t呵呵 \t\t笨笨\t\t 下次加油吧");
                        break;
                }
            }while (flage);
        } else if (judge.equals("n")) {
        }
        return flage;
    }
}

人類

import java.util.Scanner;
public class Person {
    Scanner input=new Scanner(System.in);

    String personName;
    int personNum;
    int score;
    int num;
    //輸入名字
    public String personName(String iputName){
        this.personName=iputName;
        return this.personName;
    }
    //出拳
    public int hand(){
        System.out.print("請出拳:1.剪刀\t2.石頭\t3.佈(請輸入相應的數字):");
        num=input.nextInt();
        System.out.print("你出拳:");
        if(num==1){
            System.out.println("剪刀");
        }else if(num==2){
            System.out.println("石頭");
        }else if(num==3){
            System.out.println("佈");
        }
        return num;
    }
}

電腦類

import java.util.Random;
import java.util.Scanner;
public class Computer {
    Scanner input = new Scanner(System.in);
    String name;
    Random random = new Random();

    public String computerName( int num) {
        if (num == 1) {
            name = "劉備";
        } else if (num == 2) {
             name = "孫權";
        } else if (num == 3) {
            name = "曹操";
        }
        return name;
    }
    //出拳
    public int hand(){
        int randomNum = random.nextInt(3) % (3 - 1 + 1) + 1;
        if(randomNum==1){
            System.out.println("剪刀");
        }else if(randomNum==2){
            System.out.println("石頭");
        }else if(randomNum==3){
            System.out.println("佈");
        }
        return randomNum;
    }
}

main()函數

import java.util.Scanner;
public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        Computer computer=new Computer();
        Person person=new Person();
        Game game=new Game();
        System.out.println("************************************");
        System.out.println("\t\t\t猜拳\t開始");
        System.out.println("************************************");
        System.out.println("出拳規則:1.剪刀2.石頭3.佈");
        System.out.println("請選擇對方角色:1.劉備\t2.孫權\t3.曹操");
        System.out.print("請輸入對戰的角色:");
        int num = input.nextInt();
        String computerName= computer.computerName(num);
        System.out.print("請輸入你的名字:");
        String inputName=input.next();
        String personName= person.personName(inputName);
        System.out.println(computerName+"\t\tvs\t"+personName);
        game.start(computerName,inputName) ;
    }

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: