1、 玩家类
package specialForce;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/24 16:44
*@Author: Nyg
*
* ** 玩家类
*/
public class Player {
private String name;//昵称
private int blood = 100;//血量
private Gun gun;//机枪
public int getBlood() {
return blood;
}
public String getName() {
return name;
}
public Player(){}
public Player(String name){
this(name,100);
}
public Player(String name,int blood){
this.name = name;
this.blood = blood;
}
/**
* 持枪
* @param gun
*/
public void holdGun(Gun gun){
this.gun = gun;
}
/**
* 射击
* @param enemy 对手
*/
public void shootEnemy(Player enemy){
System.out.printf("%s向%s开了一枪\n",this.name,enemy.name);
if(gun == null){
System.out.println(">>>"+name+"没有枪,无法进行射击!");
return;
}
gun.shootEnemy(enemy);
}
/**
* 装弹夹
* @param clip
*/
public void loadClip(Clip clip){
if(gun == null){
System.out.println(">>>"+name+"没有枪,无法装弹夹!");
return;
}
gun.loadClip(clip);
}
//计算损失
public void damage(int hurt){
if(blood == 0){
System.out.println(">>>"+name+"已死亡,请勿鞭尸!");
return;
}
blood-=hurt;
if(blood<=0){
blood = 0;
System.out.println(">>>"+name+"已经死亡!");
}
}
public void showPlayer(){
boolean isHoldGun = gun!=null;
System.out.printf(">>>玩家信息: 姓名=%s,血量=%d,是否持枪=%b\n",name,blood,isHoldGun);
}
}
2、枪类
package specialForce;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/24 16:49
*@Author: Nyg
*
* ** 枪类
*/
public class Gun {
private Clip clip;//弹夹
public Gun() {
this(null);
}
public Gun(Clip clip) {
this.clip = clip;
}
/**
* 装弹夹
*
* @param clip
*/
public void loadClip(Clip clip) {
this.clip = clip;
}
/**
* 开枪
*
* @param enemy
*/
public void shootEnemy(Player enemy) {
if (clip == null) {
System.out.println(">>>枪械没有弹夹,放了一个空枪!");
return;
}
Bullet bullet = clip.popBullet();
if (bullet == null) {
System.out.println(">>>枪械的弹夹已空,放了一个空枪!");
return;
}
bullet.hitEnemy(enemy);
}
/**
* 显示枪械信息
*/
public void showGun() {
if (clip != null) {
System.out.println(">>>枪械信息:有弹夹");
} else {
System.out.println(">>>枪械信息:无弹夹");
}
}
}
3、弹夹类
package specialForce;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/24 16:49
*@Author: Nyg
*
* ** 弹夹类
*/
public class Clip {
private Bullet[] magazine;//弹仓
private int capacity = 30;//弹夹容量
private int surplus=0;//子弹盈余
public Clip(){
this(30);
}
public Clip(int capacity){
this.magazine = new Bullet[capacity];
this.surplus = 0;
}
//装子弹
public void pushBullet(Bullet bullet){
if(surplus == capacity){
System.out.println(">>>弹夹已满,请勿重复装弹!");
return;
}
magazine[surplus] = bullet;
surplus++;
}
//卸子弹
public Bullet popBullet(){
if(surplus == 0){
System.out.println(">>>弹夹已空,无法弹出子弹!");
return null;
}
Bullet bullet = magazine[surplus - 1];
magazine[surplus - 1] = null;
surplus--;
showClip();
return bullet;
}
//显示弹夹信息
public void showClip(){
System.out.printf(">>>弹夹状态:%d/%d\n",surplus,capacity);
}
}
4、子弹类
package specialForce;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/24 16:51
*@Author: Nyg
*
* ** 子弹类
*/
public class Bullet {
//private int hurt = 10;//子弹的伤害值
public Bullet(){
}
//子弹击中敌人
public void hitEnemy(Player enemy){
int hurt = (int)(Math.random()*15+1);
enemy.damage(hurt);
}
}
5、测试类
package specialForce;
import java.util.Scanner;
/*
* 我想休息
* 不,你没有钱!
*@ Time : 2023/7/24 16:51
*@Author: Nyg
*
* ** 测试类
*/
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("欢迎来到特种部队!");
System.out.println("********战前准备********");
Player p1 =new Player();
System.out.print("请录入昵称:");
String name = in.next();
System.out.print("1.优酸乳 2.AD钙 3.乳酸菌 请选择对战玩家(1~3):");
int num = in.nextInt();
switch(num){
case 1:
p1 = new Player("优酸乳",100);
break;
case 2:
p1 = new Player("AD钙",100);
break;
case 3:
p1 = new Player("乳酸菌",100);
break;
default:
System.out.println("您的输入有误!");
break;
}
Player p2 = new Player(name,100);
Gun gun1 = new Gun();
Gun gun2 = new Gun();
//玩家持枪
p1.holdGun(gun1);
p2.holdGun(gun2);
//装配弹夹、子弹
Clip clip1 = new Clip();
Clip clip2 = new Clip();
for(int i = 1;i <= 30;i++){
clip1.pushBullet(new Bullet());
clip2.pushBullet(new Bullet());
}
p1.loadClip(clip1);
p2.loadClip(clip2);
//显示信息
p1.showPlayer();
p2.showPlayer();
int i = 0; //回合数
System.out.println("********开始对战********");
boolean isExit = false; //是否退出游戏
while(true){
System.out.println("---第"+(i+1)+"回合---");
i++;
p1.shootEnemy(p2);
if(p2.getBlood()==0) {
System.out.println(p1.getName()+"获胜!");
System.out.println("退出江湖!");
return;
}
p2.shootEnemy(p1);
if(p1.getBlood()==0) {
System.out.println(p2.getName()+"获胜!");
System.out.println("退出江湖!");
return;
}
System.out.println();
p1.showPlayer();
p2.showPlayer();
System.out.print("是否继续对战(输入y继续,其他键退出)?");
if(!(in.next().equals("y"))){
System.out.println("退出江湖!");
return;
}
}
}
}