人机猜拳小游戏(面向对象的方法)

/**
 * Created by Administrator on 2017/4/12.
 */
public class Main {
    public static void main(String[] args) {
        Game game = new Game();
        game.init();
        game.ready();
        //开始游戏
        game.start();
    }
}
 * Created by Administrator on 2017/4/12.
 */
public class Computer {
    public String name;
    int Random;

    public void hit() {
        Random =(int) (Math.random()*3);

    }
}
/**
 * Created by Administrator on 2017/4/12.
 */
public class User {
    public String name;

    //
    public void hit() {

    }
}
import java.awt.*;
import java.util.Random;
import java.util.Scanner;

/**
 *
 * Created by Administrator on 2017/4/12.
 */
public class Game {
    int count = 0;
    int userAscore=0;
    int userBscore=0;
    User userA;//属性是一个对象用的。
    Computer userB;
    String[] option;
    String[] computerName;
    public void init() {
        userA = new User();
        userB = new Computer();
        option = new String[]{"剪刀", "石头", "布"};
        computerName = new String[]{"刘备", "孙权", "曹操"};
    }

    public void showWelcome() {
        System.out.println("\t\t\t--------------------------------欢迎进入游戏世界--------------------------------");
        System.out.println("\t\t\t********************************************************************************");
        System.out.println("\t\t\t**\t\t\t\t\t\t\t\t猜拳,开始\t\t\t\t\t\t\t\t\t**");
        System.out.println("\t\t\t********************************************************************************");
        System.out.print("出拳规则");
        for (int i = 0; i < option.length; i++) {
            System.out.print((i+1)+"."+option[i]);
        }
    }

    public void inputComputerName() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择对方的角色(");
        for (int i = 0; i < computerName.length;i++) {
            System.out.print((i+1)+":"+computerName[i]);
        }
        System.out.print(")");
        int i =  sc.nextInt();
        userB.name =   computerName[i-1];
    }

    public void inputName() {
        System.out.print("请输入你的名字:");
        Scanner sc = new Scanner(System.in);
        userA.name= sc.next();
        System.out.println( userA.name+" vs "+userB.name +"对战");
    }

    public void start() {
        System.out.print("要开始吗(y/n)");
        Scanner sc = new Scanner(System.in);
        String choose= sc.next();
        if(choose.equals("y")){
            userA.hit();
            userB.hit();
            calcGame();
            showGameResult();
        }
    }

    private void showGameResult() {
        Scanner scan = new Scanner(System.in);
        System.out.println("是否开始下一轮(y/n):");
        System.out.println("------------------------------------------------------------------");
        String a = scan.next();
        if (a.equals("y")){
            calcGame();
            showGameResult();
        }else{
            System.out.println(userA.name+" vs "+userB.name +"对战");
            System.out.println("对战次数:"+count);
            System.out.println();
            System.out.println("姓名"+"\t\t得分");
            System.out.println(userA.name+"\t\t"+userAscore);
            System.out.println(userB.name+"\t\t"+userBscore);
        }

    }

    private void calcGame(){
        Scanner scan = new Scanner(System.in);
        System.out.print("请出拳:");
        for (int i = 0; i < option.length; i++) {
            System.out.print((i+1)+"."+option[i]+"  ");
        }
        System.out.print("(输入相应数字):");
        int a = scan.nextInt();
        System.out.print("你出拳:"+option[a-1]);
        System.out.println();
        System.out.print(userB.name+"出拳:"+option[userB.Random]);
        System.out.println();
        if (option[a-1]==option[userB.Random]){
            System.out.println("结果:平局");
        }else if ((a==1&&userB.Random==2)||(a==2&&userB.Random==3)||(a==3&&userB.Random==1)){
            System.out.println("结果:"+userB.name+"获胜");
            userBscore++;
        }else {
            System.out.println("结果:你获胜");
            userAscore++;
        }
        count++;
    }

    public void ready() {
        //显示欢迎界面
        showWelcome();
        //输入电脑名字
        inputComputerName();
        //输入玩家名字
        inputName();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值