码迷,mamicode.com
首页 > 其他好文 > 详细

猜拳游戏

时间:2021-01-13 11:06:51      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:system   extends   amp   ==   input   lazy   规则   sim   rgs   

类图

技术图片

源代码

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));

    }
}

效果图

技术图片

技术图片
技术图片
技术图片
技术图片

猜拳游戏

标签:system   extends   amp   ==   input   lazy   规则   sim   rgs   

原文地址:https://www.cnblogs.com/ljmmm1/p/14263883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!