1. 程式人生 > >Java-石頭剪刀布

Java-石頭剪刀布

/**
 * @ClassName TestDemo1
 * @Description 石頭剪刀布
 * @Author LiMingXu
 * @Date 2018/11/5 0:44
 * @Version 1.0
 **/
public class TestDemo1 {
    public static class People{
        private String name;
        private int score;
        public People(){

        }
        public People(String name){
            score = 0;
            this.name = name;
        }
        public String fist(){
            Scanner scanner = new Scanner(System.in);
            System.out.println("請輸入:石頭 剪刀 布");
            String str = scanner.next();
            return str;
        }
        public void addScore(){
            score += 1;
        }
        public int getScore(){
            return score;
        }
        public String getName(){
            return name;
        }
    }
    ///
    static class Computer {
        private String name;
        private int score;

        public Computer(String name) {
            score = 0;
            this.name = name;
        }

        public String fist() {
            Random random = new Random(3);
            int n = random.nextInt() + 1;//[1,3]
            String str = null;
            switch (n) {
                case 1:
                    System.out.print(name + "出石頭");str = "石頭";break;
                case 2:
                    System.out.print(name + "對方出剪刀");str = "剪刀";
                    break;
                case 3:
                    System.out.print(name + "對方出布");str = "布";
                    break;
            }
            return str;
        }

        public void addScore() {
            score += 1;
        }

        public int getScore() {
            return score;
        }

        public String getName(){
            return name;
        }
    }
    public static class Game{
        private People people;
        private Computer computer;
        private Game(){
            People  people= new People("Tyone");
            Computer  computer= new Computer("Alpha");
        }
        public Game(People people,Computer computer){
            this.computer=computer;
            this.people=people;
        }
        private void playThreeTime(){
            int count = 0;
            while(count < 3){
                String pFist = people.fist();
                String cFist = computer.fist();
                if(pFist.equals("石頭") && cFist.equals("剪刀") || pFist.equals("剪刀") && cFist.equals("布")||pFist.equals("布") && cFist.equals("石頭")){
                    System.out.println(people.getName() + "本局獲勝");
                    people.addScore();
                }else if(pFist.equals(cFist)){
                    System.out.println("平局!");
                }else{
                    System.out.println(computer.getName() + "本局獲勝");
                    computer.addScore();
                }
                count ++;
            }
        }
        private void getResult(){
            int pScore = people.getScore();
            int cScore = computer.getScore();
            if(pScore > cScore){
                System.out.println(people.getName()+ "最終結果贏了");
            }else if(pScore == cScore) {
                System.out.println("平局");
            }else{
                System.out.println(computer.getName() + "最終結果贏了");
            }
            System.out.println(people.getName()+":"+computer.getName()+"="+people.getScore()+":"+computer.getScore);
        }
        public void start(){
            while(true){
                playThreeTime();
                getResult();
                System.out.println("是否繼續?");
                Scanner scanner = new Scanner(System.in);
                String str = scanner.next();
                if(str.equals("是")){
                    continue;
                }else{
                    break;
                }
            }
        }
    }
    public static void main(String[] args) {
        People people= new People("Tyone");
        Computer computer = new Computer("Alpha");
        Game game = new Game(people,computer);
        game.start();
    }
}