猜拳游戏

类图

image.png

源代码

User类

import java.util.Scanner;
public class User extends Player{
    public String sayName(){
        System.out.println("请输入您的名字:");
        Scanner input = new Scanner(System.in);
        return input.next();
    }
    public int play(){//出拳
        System.out.println("请"+super.getName()+"出拳");
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        while(true){
            if(x!=1&&x!=2&&x!=3) {
                System.out.println("输入有误!请输入1-3的数字");
                 x = input.nextInt();
            }
            else break;
        }
        return x;
    }
}

Computer类

import java.util.Random;
public class Computer extends Player{
    public int play(){//出拳
        Random r = new Random();
        return r.nextInt(3)+1;
    }
}

Player类

mport java.util.Scanner;
public class Player {
    private String name;
    private int count;//标记赢的次数
    {
        count = 0;
        name = "张三";
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setCount(int count) {
        this.count = count;
    }

    public int getCount() {
        return count;
    }

}

Judge

import java.util.Scanner;
public class Judge {
    public static String[] arr={"剪刀","石头","布"};
    public void print(){
        System.out.println("--------开始游戏--------");
        System.out.println("规则:1、石头 2、剪刀 3、布");
    }
    public String askName(Player player){
        return ((User)player).sayName();
    }
    public void playGame(Player player1,Player player2){
        for(int i=0;i<5;i++){
            System.out.println("--第"+(i+1)+"局游戏开始--");
            int r1 = ((User)player1).play();
            int r2 = ((Computer)player2).play();
            System.out.println(player1.getName()+"出"+arr[r1-1]);
            System.out.println(player2.getName()+"出"+arr[r2-1]);
            if((r1==1&&r2==2)||(r1==2&&r2==3)||(r1==3&&r2==1)) {
                player2.setCount(player2.getCount()+1);
                System.out.println(player2.getName()+"胜");
            }
            else if((r1==1&&r2==3)||(r1==2&&r2==1)||(r1==3&&r2==2)) {
                player1.setCount(player1.getCount() + 1);
                System.out.println(player1.getName()+"胜");
            }
             else System.out.println("平局");
        }

    }
    public String showResult(Player player1,Player player2){
        System.out.println("--------最终结果--------");
        if(player1.getCount()>player2.getCount()) return player1.getName()+"胜利";
        else if(player1.getCount()<player2.getCount()) return player2.getName()+"胜利";
        else return "平局";
    }
    public static void main(String[] args) {
        Player player1 = new User();
        Player player2 = new Computer();
        Judge judge = new Judge();
        judge.print();
        player1.setName(judge.askName(player1));
        judge.playGame(player1,player2);
        System.out.println(judge.showResult(player1,player2));

    }
}

效果图

image.png

image.png
image.png
image.png
image.png

posted @ 2021-01-11 19:45  0x3fffffff  阅读(65)  评论(0编辑  收藏  举报