仿恶魔轮盘(俄罗斯转盘道具版)单人版以及双人版Java实现

目录

游戏介绍

玩家和Ai

父类

玩家

Ai

木仓

道具

knife

smoke

drink

glass

glassOfAi

glassOfPlayer

游戏初始化界面


一个Java新人写的练习项目,和原版差距很大,轻喷

游戏介绍

规则如下:
每回合每人获得2个道具(最多四个)
每轮子弹随机,由玩家先开始
当对自己射击,无论是否中枪,都继续自己的回合\n当对对方射击无论是否命中,都结束回合
道具及其功能如下:
1、knife:下一发子弹伤害翻倍
2、smoke:回一滴血:
3、glass:看到下一发子弹是什么
4、drink:退掉一发子弹

玩家和Ai

父类

玩家和Ai有很多相同的地方,而且玩家和Ai同属于这个类方便开火等函数的实现。

package demo.People;

import demo.Gun;
import demo.Props.Prop;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class People {
    //名字
    private String name;

    public People(String name) {
        this.name = name;
    }
    //每个人初始4滴血
    private  int heart=4;
    //道具栏
    private ArrayList<Prop> props =new ArrayList<>();
    public People() {
    }

    public String getName() {
        return name;
    }

    //随机数
    public Random random=new Random();
    //开火
    public void fire(People people, Gun gun, int hurt){
        //判断有没有子弹
        if(gun.getBullets().isEmpty()){
            System.out.println("没子弹了!下一轮!");
            return;
        }
        int index=0;
        //实弹
        if(gun.getBullets().get(index)==1){
            System.out.println("是真子弹!");
            //人扣血
            people.Hurt(hurt);
            //死没死
            if(people.isDead()){
                return;
            }
            //子弹减少
            gun.bulletReduce(index,true);

        }else {//虚弹
            System.out.println("空包弹!");
            //子弹减少
            gun.bulletReduce(index,false);
        }
    }
    //判断是否死亡
    public boolean isDead(){
        if(this.getHeart()==0){
            return true;
        }else {
            return false;
        }
    }
    public void Hurt(int hurt){
        //人扣血
        this.setHeart(this.getHeart()-hurt);
        System.out.println(this.getName()+"还剩"+this.getHeart()+"滴血!");
    }
    public int getHeart() {
        return heart;
    }

    public void setHeart(int heart) {
        this.heart = heart;
    }

    public ArrayList<Prop> getProps() {
        return props;
    }

    //获得道具
    public void haveProp(ArrayList<Prop>propList) throws InterruptedException {
        if(this.props.size()==4){
            Thread.sleep(1000);
            System.out.println("道具栏满了");
            return;
        }
        //获得两个道具
        Prop temp;
        for (int i=0;i<2;i++) {
            temp=propList.get(random.nextInt(0,4));
            Thread.sleep(300);
            System.out.println("获得"+temp.getName());
            this.props.add(temp);
        }
    }
    //展示道具,方便使用
    public void showProps() throws InterruptedException {
       ArrayList<Prop> Props = getProps();
        Thread.sleep(1000);
        System.out.println("当前拥有道具如下:");
        for (int i = 0; i < Props.size(); i++) {
            Thread.sleep(500);
            System.out.println((i+1)+"、"+Props.get(i).getName());
        }
    }
    Scanner sc=new Scanner(System.in);

}

玩家

package demo.People;

import demo.Gun;
import demo.People.People;
import demo.Props.Prop;

import java.util.ArrayList;
import java.util.Scanner;

public class Player extends People {
    public Player(String name) {
        super(name);
    }
    Scanner sc=new Scanner(System.in);
    @Override
    public void showProps(){
        ArrayList<Prop> Props = getProps();
        System.out.println(this.getName()+"当前拥有道具如下:");
        for (int i = 0; i < Props.size(); i++) {
            System.out.println((i+1)+"、"+Props.get(i).getName());
        }
    }
    public Prop useProps(String nameOfProp){
        Prop temp=new Prop();
        Prop no=new Prop("no");
        int i = 0;
        switch (nameOfProp) {
            case "knife":
                //如果有刀
                for (i = 0; i < this.getProps().size(); i++) {
                    if(this.getProps().get(i).getName().equals("knife")){
                        temp=this.getProps().get(i);
                        this.getProps().remove(i);
                        return temp;
                    }
                }
                //没刀
                System.out.println("没有该道具!");
                return null;
            case "smoke":
                //如果有烟
                for (i = 0; i < this.getProps().size(); i++) {
                    if(this.getProps().get(i).getName().equals("smoke")){
                        temp=this.getProps().get(i);
                        this.getProps().remove(i);
                        return temp;
                    }
                }
                //没烟
                System.out.println("没有该道具!");
                return null;
            case "drink":
                //如果有饮料
                for (i = 0; i < this.getProps().size(); i++) {
                    if(this.getProps().get(i).getName().equals("drink")){
                        temp=this.getProps().get(i);
                        this.getProps().remove(i);
                        return temp;
                    }
                }
                //没刀
                System.out.println("没有该道具!");
                return no;
            case "glass":
                //如果有刀
                for (i = 0; i < this.getProps().size(); i++) {
                    if(this.getProps().get(i).getName().equals("glass")){
                        temp=this.getProps().get(i);
                        this.getProps().remove(i);
                        return temp;
                    }
                }
                //没放大镜
                System.out.println("没有该道具!");
                return no;
            default:
                System.out.println("没有该道具");
                return no;
        }
    }
    public void playerFire(People people,  Gun gun, int hurt ) throws InterruptedException {
        if(gun.getBullets().isEmpty()){
            return;
        }
        Thread.sleep(1000);
        System.out.println(this.getName()+"开火!");
        String s;
        while(true){
            if(gun.getBullets().isEmpty()){
                return;
            }
            Thread.sleep(1000);
            System.out.println(this.getName()+"剩余血量:"+this.getHeart());
            System.out.println(people.getName()+"剩余血量:"+people.getHeart());
            if(this.isDead()){
                return;
            }
            //玩家回合
            Thread.sleep(1000);
            System.out.println("输入A、a(Ai、other_player)或P、p(player、youself)进行开火:");
            s=sc.next();
            if(s.equals("A")||s.equals("a")){
                System.out.println(this.getName()+"朝"+people.getName()+"开枪!");
                this.fire(people,gun,hurt);
                break;
            } else if (s.equals("P")||s.equals("p")) {
                System.out.println(this.getName()+"朝"+this.getName()+"开枪!");
                this.fire(this,gun,hurt);
                continue;
            }else {
                System.out.println("输入错误");
                continue;
            }
        }
    }
}

Ai

不懂真正的ai,只是穷举情况,按照合适的方式执行指令。

package demo.People;

import demo.Gun;
import demo.People.People;
import demo.Props.Prop;

import java.util.ArrayList;
import java.util.Random;

public class Ai extends People {
    public Ai(String name) {
        super(name);
    }
    @Override
    public void showProps() throws InterruptedException {
        ArrayList<Prop> Props = getProps();
        Thread.sleep(1000);
        System.out.println(this.getName()+"当前拥有道具如下:");
        for (int i = 0; i < Props.size(); i++) {
            Thread.sleep(500);
            System.out.println((i+1)+"、"+Props.get(i).getName());
        }
    }
    public boolean aiFire(People player, Gun gun, int hurt,int isKnow) throws InterruptedException {
        if(gun.getBullets().isEmpty()){
            return true;
        }
        Thread.sleep(1000);
        System.out.println(this.getName()+"开火!");
        if(gun.getBullets().isEmpty()){
            return true;
        }
        //0不知道
        //1实弹
        //2虚弹
        boolean toWho=true;
        switch (isKnow){
            case 0://不知道
                toWho=dontKnow(this, player, gun,hurt);
                break;
            case 1://实弹
                this.fire(player,gun,hurt);
                break;
            case 2://虚弹
                this.fire(this,gun,hurt);
                break;
            }
            return toWho;
        }
    private boolean dontKnow(People ai, People player, Gun gun, int hurt) throws InterruptedException {
        //朝玩家
        if(gun.getRealBullet()>=gun.getFakeBullet()){//实弹多或一样多
            Thread.sleep(1000);
            System.out.println("Ai朝玩家"+player.getName()+"开枪!");
            this.fire(player,gun,hurt);//朝玩家开枪
            return true;
        }else {//虚弹多或等于2发朝自己开枪,多一发随机
            // 虚弹多两发及以上
            if(gun.getFakeBullet()- gun.getRealBullet()>=2){
                Thread.sleep(1000);
                System.out.println("Ai朝自己开枪!");
                this.fire(ai,gun,hurt);//朝自己开枪
                return false;
            }else{
                //只多一发,随机
                Random random=new Random();
                int r=random.nextInt(2);//0-1
                if(r==1){//打玩家
                    Thread.sleep(1000);
                    System.out.println("Ai朝玩家"+player.getName()+"开枪!");
                    this.fire(player,gun,hurt);
                    return true;
                }else {//打自己
                    Thread.sleep(1000);
                    System.out.println("Ai朝自己开枪!");
                    this.fire(ai,gun,hurt);
                    return false;
                }
            }
        }
    }
}

木仓

实现随机上弹(有时会全实弹或全假单,未解决),退弹等操作。

package demo;

import java.util.ArrayList;
import java.util.Random;

public class Gun {
    private int realBullet;
    private int fakeBullet;
    private int totalNumberOfBullet;
    private ArrayList<Integer> bullets=new ArrayList();

    public ArrayList<Integer> getBullets() {
        return bullets;
    }

    public void setBullets(ArrayList<Integer> bullets) {
        this.bullets = bullets;
    }

    public int getTotalNumberOfBullet() {
        return totalNumberOfBullet;
    }

    public void setTotalNumberOfBullet(int totalNumberOfBullet) {
        this.totalNumberOfBullet = totalNumberOfBullet;
    }

    private Random random=new Random();

    public Gun() {
    }
    //上弹
    public void setBullet() {
        System.out.println("上弹!");
        totalNumberOfBullet=random.nextInt(5)+2;//2到6发
        if(bullets.size()>0){
            for (int i = 0; i < bullets.size(); i++) {
                bullets.remove(i);
            }
        }
        for (int i = 0; i < totalNumberOfBullet; i++) {
            if(random.nextInt()%2==0){
                bullets.add(1);
                realBullet++;
            }else {
                bullets.add(0);
                fakeBullet++;
            }
        }
        System.out.println("实弹"+realBullet+"发,虚弹"+fakeBullet+"发");
    }
    public void bulletReduce(int index,boolean flag){
        //子弹减少
        if (flag) {//实弹
            this.setRealBullet(this.getRealBullet()-1);
            this.setTotalNumberOfBullet(this.getTotalNumberOfBullet()-1);
            this.getBullets().remove(index);//退弹
            System.out.println("实弹"+this.getRealBullet()+"发,虚弹"+this.getFakeBullet()+"发!");
        } else {//虚弹
            this.setFakeBullet(this.getFakeBullet()-1);
            this.setTotalNumberOfBullet(this.getTotalNumberOfBullet()-1);
            this.getBullets().remove(index);//退弹
            System.out.println("实弹"+this.getRealBullet()+"发,虚弹"+this.getFakeBullet()+"发!");
        }
    }

    public int getRealBullet() {
        return realBullet;
    }

    public int getFakeBullet() {
        return fakeBullet;
    }

    public void setRealBullet(int realBullet) {
        this.realBullet = realBullet;
    }

    public void setFakeBullet(int fakeBullet) {
        this.fakeBullet = fakeBullet;
    }
}

道具

道具分为刀,烟,放大镜(glass)以及饮料,效果在游戏介绍已经写了,不在赘述。

注意,这些道具的use方法有些没有真正使用,而是在调用后续,这是因为技术力不够hhh

package demo.Props;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Prop {
    public String name;
    public Random random=new Random();
    public Scanner sc=new Scanner(System.in);

    public Prop() {
    }

    public Prop(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

knife

没有在这里实现伤害翻倍,而是在调用后续。

package demo.Props;

import demo.People.Ai;
import demo.Gun;
import demo.People.Player;

public class Knife extends Prop {
    public Knife(String name) {
        this.name=name;
    }
    public void use(){
        System.out.println("使用knife:下一发子弹伤害翻倍");
    }
}

smoke

package demo.Props;

import demo.People.People;

public class Smoke extends Prop {
    public Smoke(String name) {
        this.name=name;
    }
    public void use(People people){
        System.out.println("smoke:回一滴血");
        if(people.getHeart()<4){
        people.setHeart(people.getHeart()+1);
        System.out.println(people.getName()+"还剩"+people.getHeart()+"滴血!");
        }else {
            System.out.println("现在满血!");
        }
    }
}

drink

package demo.Props;

import demo.Gun;

public class Drink extends Prop {
    public Drink(String name) {
        this.name=name;
    }
    public void use(Gun gun){
        System.out.println("[drink:退掉一发子弹]");
        int index=0;
        if (gun.getBullets().get(index)==1) {
            System.out.println("退了一发实弹!");
            //子弹减少
            gun.bulletReduce(index,true);
        }else {
            System.out.println("退了一发虚弹!");
            //子弹减少
            gun.bulletReduce(index,false);
        }
    }
}

glass

这个比较特殊,因为玩家使用时会得到下一发子弹的真假,可是如果ai也是用同一个方法的话,ai的下一发也会告诉玩家,这不合理(多人时也会互相告知,可以解决但是相对麻烦,如果有大佬有好方法望告知),所有我写了两版

glassOfAi

返回真假以告知Ai。

package demo.Props;

import demo.People.Ai;
import demo.Gun;
import demo.People.Player;

public class GlassOfAi extends Prop {
    public GlassOfAi(String name) {
        this.name = name;
    }

    public int use(Gun gun) {
        //System.out.println("使用glass:看到下一发子弹是什么");
        int index = 0;
        int theBullet = gun.getBullets().get(index);
        if (theBullet == 1) {
            return 1;//1实弹
        } else {
            return 2;//2虚弹
        }
    }
}

glassOfPlayer

package demo.Props;

import demo.People.Ai;
import demo.Gun;
import demo.People.Player;

public class GlassOfPlayer extends Prop {
    public GlassOfPlayer(String name) {
        this.name = name;
    }

    public void use(Gun gun) {
        System.out.println("glass:看到下一发子弹是什么");
        int index = 0;
        int theBullet = gun.getBullets().get(index);
        if (theBullet == 1) {
            System.out.println("这发子弹是实弹!");
        } else {
            System.out.println("这发子弹是虚弹!");
        }
    }
}

游戏初始化界面

package demo;

import demo.People.Ai;
import demo.People.People;
import demo.People.Player;
import demo.Props.*;

import java.util.ArrayList;
import java.util.Scanner;

public class Game {
    public Game() {
        //初始化玩家道具
        propListOfPlayer.add(knife);
        propListOfPlayer.add(drink);
        propListOfPlayer.add(smoke);
        propListOfPlayer.add(glassOfPlayer);
        //初始化Ai道具
        propListOfAi.add(knife);
        propListOfAi.add(drink);
        propListOfAi.add(smoke);
        propListOfAi.add(glassOfAi);
        System.out.println("======欢迎来到俄罗斯轮盘=====");
        System.out.println("规则如下:");
        System.out.println("每回合每人获得2个道具(最多四个)\n每轮子弹随机,由玩家先开始");
        System.out.println("当对自己射击,无论是否中枪,都继续自己的回合\n当对对方射击无论是否命中,都结束回合");
        System.out.println("道具及其功能如下:");
        System.out.println("1、knife:下一发子弹伤害翻倍");
        System.out.println("2、smoke:回一滴血:");
        System.out.println("3、glass:看到下一发子弹是什么");
        System.out.println("4、drink:退掉一发子弹");
    }

    //初始化道具
    private final ArrayList<Prop> propListOfPlayer = new ArrayList<>();
    private final ArrayList<Prop> propListOfAi = new ArrayList<>();
    private final Knife knife = new Knife("knife");
    private final Drink drink = new Drink("drink");
    private final Smoke smoke = new Smoke("smoke");
    private final GlassOfPlayer glassOfPlayer = new GlassOfPlayer("glass");
    private final GlassOfAi glassOfAi = new GlassOfAi("glass");
    private  int round = 0;//轮数
    private int hurt = 1;//默认伤害

    public Scanner sc = new Scanner(System.in);

    public void start1() throws InterruptedException {
        System.out.println("输入你的名字,准备赌上你的性命:");
        //初始化玩家
        Player player=new Player(sc.next());
        //初始化Ai
        Ai ai=new Ai("Ai");
        //初始化枪
        Gun gun=new Gun();
        while(true){
            //是否死
            if(player.isDead()){
                System.out.println(player.getName()+"已经死啦死啦滴!");
                return;
            }
            if(ai.isDead()){
                System.out.println(ai.getName()+"已经死啦死啦滴!");
                return;
            }
            Game.showPlayers(ai,player);
            System.out.println("===========第"+(++round)+"回合========");
            gun.setBullet();//上弹
            //玩家回合
            this.playerRound(ai,player,gun);
            //Ai回合
            this.aiRound(ai,player,gun);
            if(gun.getTotalNumberOfBullet()==0){
                continue;
            }
        }
    }
    public void start2() throws InterruptedException {
        //初始化玩家1
        System.out.println("请输入玩家1姓名:");
        Player player1=new Player(sc.next());
        //初始化玩家2
        System.out.println("请输入玩家2姓名:");
        Player player2=new Player(sc.next());
        //初始化枪
        Gun gun=new Gun();
        while(true){
            //是否死
            if(player1.isDead()){
                System.out.println(player1.getName()+"已经死啦死啦滴!");
                return;
            }
            if(player2.isDead()){
                System.out.println(player2.getName()+"已经死啦死啦滴!");
                return;
            }
            Game.showPlayers(player1,player2);
            System.out.println("===========第"+(++round)+"回合========");
            gun.setBullet();//上弹
            //玩家1回合
            this.playerRound(player2,player1,gun);//函数定义时第一个参数仍是ai,因为一开始写的时候是单人模式,一样用
            //玩家2回合
            this.playerRound(player1,player2,gun);
            if(gun.getTotalNumberOfBullet()==0){
                continue;
            }
        }
    }

    public static void showPlayers(People player1, People player2) {
        System.out.println(player1.getName()+"血量:" + player1.getHeart());
        System.out.println(player2.getName() + "血量:" + player2.getHeart());
    }

    //玩家回合
    public void playerRound(People ai, Player player, Gun gun) throws InterruptedException {
        showPlayers(player,ai);
        System.out.println("=====" + player.getName() + "回合=====");
        player.haveProp(propListOfPlayer);
        player.showProps();
        Prop prop = new Prop();
        String nameOfProp;
        //使用道具
        while (true) {
            System.out.println("输入道具名使用道具(不想使用输入no)");
            nameOfProp = sc.next();
            if (nameOfProp.equals("no")) {
                break;
            }
            prop = player.useProps(nameOfProp);
            switch (prop.getName()) {
                case "knife":
                    knife.use();
                    hurt = 2;
                    break;
                case "drink":
                    drink.use(gun);
                    break;
                case "smoke":
                    smoke.use(player);
                    break;
                case "glass":
                    glassOfPlayer.use(gun);
                    break;
                case "no"://没有
                    break;
            }
            player.showProps();
            if (player.getProps().isEmpty()) {
                break;
            }
        }
        player.playerFire(ai, gun, hurt);
        hurt = 1;//重置hurt
        return;
    }
    //Ai回合
    public void aiRound(Ai ai, Player player, Gun gun) throws InterruptedException {
        //每回合显示双方剩余血量
        showPlayers(ai,player);
        Thread.sleep(1000);
        System.out.println("=====" + ai.getName() + "回合=====");
        if (gun.getBullets().isEmpty()) {
            System.out.println("没有子弹,回合结束!");
            return;
        }
        Thread.sleep(1000);
        ai.haveProp(propListOfAi);
        ai.showProps();
        String nameOfProp;
        Prop prop = new Prop();//空道具对象
        while (true) {
            int isKown = 0;//0不知道,默认
            for (int i = 0; i < ai.getProps().size(); i++) {
                prop = ai.getProps().get(i);//空道具作为暂时容器
                if (prop.getName().equals("knife")) {
                    Thread.sleep(1000);
                    knife.use();
                    ai.getProps().remove(knife);
                    hurt = 2;//伤害翻倍
                } else if (prop.getName().equals("drink")) {
                    Thread.sleep(1000);
                    drink.use(gun);
                    ai.getProps().remove(drink);
                } else if (prop.getName().equals("smoke")) {
                    Thread.sleep(1000);
                    if (ai.getHeart() < 4) {
                        smoke.use(ai);
                        ai.getProps().remove(smoke);
                    }
                } else if (prop.getName().equals("glass")) {
                    Thread.sleep(1000);
                    isKown = glassOfAi.use(gun);
                    ai.getProps().remove(glassOfAi);
                } else if (prop.getName().equals("glass")) {
                    Thread.sleep(1000);
                    isKown = glassOfAi.use(gun);
                    ai.getProps().remove(glassOfAi);
                } else {
                    System.out.println("Ai没有道具!");
                }
            }
            boolean flag = true;
            flag = ai.aiFire(player, gun, hurt, isKown);
            hurt = 1;//重置hurt
            if (flag) {//如果对玩家开枪,退出回合
                break;
            }
            ai.showProps();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值