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

02_人机猜拳

时间:2016-06-09 17:21:30      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:

package com.renjicaiquan.entity;

public class Computer {
    private String name;
    private int score;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public Computer(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }
    public Computer() {
        super();
    }
    @Override
    public String toString() {
        return "Computer [name=" + name + ", score=" + score + "]";
    }
    public int doGame(){
        int x=(int) (Math.random()*3+1);
        switch(x){
        case 1:
            System.out.println(this.getName()+"出拳:剪刀");
            break;
        case 2:
            System.out.println(this.getName()+"出拳:石头");
            break;
        case 3:
            System.out.println(this.getName()+"出拳:布");
            break;
        }
        return x;
    }

}

package com.renjicaiquan.entity;

import java.util.Scanner;

public class Game {
    private User user=new User();
    private Computer computer=new Computer();
    private int count;
    static Scanner input=new Scanner(System.in);
    public void welcome(){
        System.out.println("-----------------------欢迎进入游戏------------------------");
        System.out.println("\t\t***********************");
        System.out.println("\t\t**  猜拳,开始  **");
        System.out.println("\t\t***********************");
    }
    public void start(){
        welcome();
        System.out.println("\n出拳规则:1、剪刀 2、石头 3、布");
        System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");
        int check_1=getIntOneToThree();
        switch(check_1){
        case 1:
            computer.setName("刘备");
            break;
        case 2:
            computer.setName("孙权");
            break;
        case 3:
            computer.setName("曹操");
            break;
        }
        System.out.print("请输入你的名字:");
        user.setName(input.next());
        System.out.println(user.getName()+"\tVS\t"+computer.getName()+"\n");
        
        System.out.println("要开始吗(y/n)?");
        String answer=getYorN();    
        if("n".equals(answer)){
            System.out.println("谢谢使用!");
            System.exit(0);
        }
        String isAgain="";
        do{
            int a=user.doGame();
            int b=computer.doGame();
            compareUserAndComputer(a, b);
            System.out.print("\n是否继续开始下一轮(y/n):");
            isAgain=getYorN();
        }while(isAgain.equals("y"));
        showResult();
    }
    public static int getIntOneToThree(){
        while(!input.hasNext("[1,2,3]{1}")){
            input=new Scanner(System.in);
            System.out.println("输入有误,请重新输入!");
        }
        return input.nextInt();
    }
    public void setUser(User user) {
        this.user = user;
    }
    public User getUser() {
        return user;
    }
    public void setComputer(Computer computer) {
        this.computer = computer;
    }
    public Computer getComputer() {
        return computer;
    }
    public void compareUserAndComputer(int a,int b){
        //a表示人出的拳 b表示电脑出的拳
        int x=a-b;
        this.setCount(this.getCount()+1);
        switch(x){
        case 1:
        case -2:        
            user.setScore(user.getScore()+1);
            System.out.println("结果:恭喜,你赢了!");
            break;
        case -1:
        case 2:
            computer.setScore(computer.getScore()+1);
            System.out.println("结果:^_^,你输了,真笨!");
            break;
        default:
            System.out.println("结果:平局!");    
        }
    }
    public void setCount(int count) {
        this.count = count;
    }
    public int getCount() {
        return count;
    }
    public void showResult(){
        System.out.println("--------------------------------");
        System.out.println(user.getName()+"\tVS\t"+computer.getName());
        System.out.println("对战次数:"+this.getCount());
        System.out.println("\n姓名\t得分");
        System.out.println(user.getName()+"\t"+user.getScore());
        System.out.println(computer.getName()+"\t"+computer.getScore()+"\n");
        if(user.getScore()>computer.getScore()){
            System.out.println(computer.getName()+":这不可能!");
        }else if(user.getScore()==computer.getScore()){
            System.out.println(computer.getName()+":再战300回合!");
        }else{
            System.out.println(computer.getName()+":我"+computer.getName()+"是无敌的,哈哈哈...");
        }
        System.out.println("--------------------------------");
        
    }
    public static String getYorN(){
        while(!input.hasNext("[y,n]{1}")){
            input=new Scanner(System.in);
            System.out.println("输入有误,请重新输入!");
        }
        return input.next();
    }

}

package com.renjicaiquan.entity;

public class User {
    private String name;
    private int score;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }
    public User(String name, int score) {
        super();
        this.name = name;
        this.score = score;
    }
    public User() {
        super();
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", score=" + score + "]";
    }
    
    public int doGame(){
        System.out.println("你出拳:");
        int x=Game.getIntOneToThree();
        switch(x){
        case 1:
            System.out.println("你出拳:剪刀");
            break;
        case 2:
            System.out.println("你出拳:石头");
            break;
        case 3:
            System.out.println("你出拳:布");
            break;
        }
        return x;
    }
    
}

package com.renjicaiquan.test;

import com.renjicaiquan.entity.Game;

public class TestGame {
    public static void main(String[] args){
        Game g=new Game();
        g.start();
    }
}

 

02_人机猜拳

标签:

原文地址:http://www.cnblogs.com/aigeileshei/p/5572446.html

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