Java 石头剪刀布(基础版)

package com.neusoft.data20180717;

public class Player {
    String playName ="";//玩家姓名
    String playType ="";//玩家出拳类型
    int playScore=0;//玩家积分
    /**
     * 无参构造
     */
    public Player() {
        super();
    }
    /**
     * 有参构造
     * @param playName
     * @param playType
     * @param playScore
     */
    public Player(String playName, String playType, int playScore) {
        super();
        this.playName = playName;
        this.playType = playType;
        this.playScore = playScore;
    }
    
    
    /**
     * setter getter方法
     * @return
     */
    public String getPlayName() {
        return playName;
    }
    public void setPlayName(String playName) {
        this.playName = playName;
    }
    public String getPlayType() {
        return playType;
    }
    public void setPlayType(String playType) {
        this.playType = playType;
    }
    public int getPlayScore() {
        return playScore;
    }
    public void setPlayScore(int playScore) {
        this.playScore = playScore;
    }
    
    /**
     * 玩家选择出拳方式
     * @param playT
     * @return
     */
    public String playTypeChooose(int playChoose){
        switch (playChoose) {
        case 1:
            playType="石头";
            break;
        case 2:
            playType="剪刀";
            break;
        case 3:
            playType="布";
            break;

        }
        return playType;
        
    }  

}

 

******************************************************************************************************************************

package com.neusoft.data20180717;

import java.util.Scanner;

public class Computer {
    String comName = "";//电脑名字
    String comType="";//电脑出拳类型
    int comScore = 0;//电脑积分
    /**
     * 无参构造
     */
    public Computer() {
        super();
    }
    /**
     * 有参构造
     * @param comName
     * @param comType
     * @param comScore
     */
    public Computer(String comName, String comType, int comScore) {
        super();
        this.comName = comName;
        this.comType = comType;
        this.comScore = comScore;
    }
    
    /**
     * setter getter 方法
     * @return
     */
    public String getComName() {
        
        return comName;
    }
    public void setComName(String comName) {
    
        this.comName = comName;
    }
    public String getComType() {
        return comType;
    }
    public void setComType(String comType) {
        this.comType = comType;
    }
    public int getComScore() {
        return comScore;
    }
    public void setComScore(int comScore) {
        this.comScore = comScore;
    }
    

/**
 * 玩家选择电脑角色
 * @param com
 * @return
 */
    public String Name(int com){
        switch(com){
        case 1: 
            comName="王权富贵 ";
            break;
        case 2:
            comName="东方月初";
            break;
        case 3:
            comName="涂山苏苏";
            break;
        }
        return comName;
    }

/**
 * 电脑生成自己的出拳方式
 * @return
 */
    public String cType(){
        int cTypeNum = (int)(Math.random()*3+1);
        switch (cTypeNum) {
        case 1:
            comType="石头";
            break;
        case 2:
            comType="剪刀";
            break;
        case 3:
            comType="布";
            break;
        }
        
        return comType;
    }
    
}
**********************************************************************************

、********************************************************************

package com.neusoft.data20180717;

import java.util.Scanner;

/**
 * 人机猜拳
 * @author 郭浩
 *
 */
public class HumanCompareComputer {

    public static void main(String[] args) {
        
        String ifnext=null;
        System.out.println("******************欢 迎 来 到 人 机 大 战*****************");
        System.out.println("**************************************************");
        System.out.println("********************1.开始    2.退出******************");
        System.out.println("**************************************************");
        
        Scanner input = new Scanner(System.in);//接受输入,判断是否开始
        int st = input.nextInt();
        //判断开始
        if(st==1){//如果输入等于1,那么开始游戏
            System.out.println("开始游戏");
            System.out.println("请输入你的姓名:");
            Player player = new Player();//实例一个玩家
            Computer computer = new Computer();//实例一个电脑对手
            //1.接受玩家添加的姓名
            player.playName=input.next();//接受用户从界面创建的角色名
            System.out.println(player.playName);
            
            //2.选择电脑玩家
            System.out.println("请选择你的电脑对手名字:");
            System.out.println("1.王权富贵  2.东方月初  3.涂山苏苏");
            
            //2.1玩家选择对象
            Scanner choose = new Scanner(System.in);
            int com = choose.nextInt();
            
            //2.2调用Computer类中Name方法,为电脑命名
            computer.Name(com);            
            System.out.println("你好,你的对手是"+computer.comName);
            
            do{
            System.out.println("游戏开始,选择你的出拳方式:");
            System.out.println("1.石头  2.剪刀  3.布");
            
            //3.玩家开始选择自己出拳方式
            Scanner playT  = new Scanner(System.in);
            int playChoose = playT.nextInt();
            player.playTypeChooose(playChoose);
            System.out.println("你的出拳是"+player.playType);
            
            //4.电脑随机生成自己的出拳方式
            computer.cType();
            System.out.println("电脑的出拳是"+computer.comType);
            
            //5.比较玩家和电脑的胜负
            //5.1先判断平局
            
            if(player.playType.equals(computer.comType)){
                System.out.println("平局");
                
                //5.2下面判断玩家石头的胜负
            }else if(player.playType=="石头"){
                if(computer.comType=="剪刀"){
                    System.out.println("玩家胜利!");
                    player.playScore=player.playScore+1;
                    
                }else if(computer.comType=="布"){
                    System.out.println("电脑胜利!");
                    computer.comScore=computer.comScore+1;                    
                }
                //玩家剪刀的胜负
            }else if(player.playType=="剪刀"){
                if(computer.comType=="布"){
                    System.out.println("玩家胜利!");
                    player.playScore=player.playScore+1;                    
                }else if(computer.comType=="石头"){
                    System.out.println("电脑胜利!");
                    computer.comScore=computer.comScore+1;                    
                }
                //玩家布胜负
            }else if (player.playType=="布") {
                if(computer.comType=="石头"){
                    System.out.println("玩家胜利!");
                    player.playScore=player.playScore+1;                    
                }else if (computer.comType=="剪刀") {
                    System.out.println("电脑胜利!");
                    computer.comScore=computer.comScore+1;                
                }                
            }
            //6.是否继续
            System.out.println("是否继续比赛(y/n)");
            Scanner next1 = new Scanner(System.in);
            ifnext=next1.next();
            }while("y".equals(ifnext));
            //6.
            //打印比赛结果
            System.out.println(player.playName+"的分数为"+player.playScore+"...\n"+computer.comName+"的分数为"+computer.comScore);
            
            
        }else if(st ==2){//如果输入等于2,那么退出游戏
            System.out.println("欢迎再次使用人机猜拳游戏!");
            System.exit(0);
        }
        
        

    }

}
 

你可能感兴趣的:(Java)