Java实现控制台版CS
也可以把这四个类直接复制下去放在一个包下面
父类
package Cs1_6;
public abstract class Persion {
public abstract void Attack(Persion p);
public abstract boolean Shan();
public abstract boolean Baoji();
public Persion() {
}
public Persion(int hp, int atk) {
this.hp = hp;
this.atk = atk;
}
private int hp;
public int getHp() {
return hp;
}
public void setHp(int hp) {
this.hp = hp;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
private int atk;
}
匪徒类继承人类
package Cs1_6;
public class Fei extends Persion {
public static int shantemp=0;
public Fei() {
}
public Fei(int hp, int atk) {
super(hp, atk);
}
@Override
public String toString() {
return String.format("匪徒的生命为%d,攻击为%d",this.getHp(),this.getAtk());
}
@Override
public void Attack(Persion p) {
if (p instanceof Police){
Police pp = (Police)p;
if (Baoji()){
System.out.println("匪徒达成暴击");
pp.setHp(0);
return;
}
if(Shan()){
if(++shantemp==3){
System.out.println("获得闪避之神称号");
System.out.println("每次收到伤害可抵挡5点");
}
System.out.println("警察已闪避");
System.out.println("闪避成功获得10点回复效果");
pp.setHp(pp.getHp()+5);
return;
}
if(shantemp>=3){
pp.setHp(pp.getHp()-pp.getAtk()+5);
System.out.println("闪避之神抵挡5点伤害");
return;
}
pp.setHp(pp.getHp()-pp.getAtk());
}
}
@Override
public boolean Shan() {
if (Math.random()>0.7) return true;
return false;
}
@Override
public boolean Baoji() {
if (Math.random()>=0.9) return true;
return false;
}
}
警察类继承人类
package Cs1_6;
public class Police extends Persion {
public static int shantemp=0;
public Police() {
}
public Police(int hp, int atk) {
super(hp, atk);
}
public String toString() {
return String.format("警察的生命为%d,攻击为%d",this.getHp(),this.getAtk());
}
@Override
public void Attack(Persion p) {
if (p instanceof Fei){
Fei ff = (Fei)p;
if (Baoji()){
System.out.println("警察达成暴击");
ff.setHp(0);
return;
}
if(Shan()) {
if(++shantemp==3){
System.out.println("获得闪避之神称号");
System.out.println("每次收到伤害可抵挡5点");
}
System.out.println("匪徒已闪避");
System.out.println("闪避成功获得10点回复效果");
ff.setHp(ff.getHp()+10);
return;
}
if(shantemp>=3){
ff.setHp(ff.getHp()-ff.getAtk()+5);
System.out.println("闪避之神抵挡5点伤害");
return;
}
ff.setHp(ff.getHp()-ff.getAtk());
}
}
public boolean Shan() {
if (Math.random()>0.7) return true;
return false;
}
public boolean Baoji() {
if (Math.random()>=0.9) return true;
return false;
}
}
开始游戏的类
package Cs1_6;
public class Game {
public static void Da(Persion p1,Persion f1){
Police p;
Fei f;
if (p1 instanceof Police){
p=(Police) p1;
f=(Fei) f1;
}
else{
p=(Police) f1;
f=(Fei) p1;
}
System.out.println("警察开了枪");
p.Attack(f);
System.out.println(f.toString());
while (p.getHp()>0 && f.getHp()>0){
System.out.println("匪徒开了枪");
f.Attack(p);
System.out.println(p.toString());
System.out.println();
if(p.getHp()<=0){
break;
}
System.out.println("警察开了枪");
p.Attack(f);
System.out.println(f.toString());
}}
public static void IsLive(Persion p1,Persion f1){
Police p;
Fei f;
if (p1 instanceof Police){
p=(Police) p1;
f=(Fei) f1;
}
else{
p=(Police) f1;
f=(Fei) p1;
}
if (p.getHp()<=0){
System.out.println("警察阵亡");
}
else if(f.getHp()<=0){
System.out.println("匪徒阵亡");
}
}
public static void main(String[] args) {
//创建
Police p = new Police(100,(int)(Math.random()*20+1));
Fei f = new Fei(100,(int)(Math.random()*20+1));
//谁先打
// boolean b = Math.random()>=0.5;
//警察先打
if(Math.random()>=0.5){
Da(p,f);
// System.out.println("警察开了枪");
// p.Attack(f);
// System.out.println(f.toString());
// while (p.getHp()>0 && f.getHp()>0){
//
//
//
// System.out.println("匪徒开了枪");
// f.Attack(p);
// System.out.println(p.toString());
// System.out.println();
// if(p.getHp()<=0){
// break;
// }
// System.out.println("警察开了枪");
// p.Attack(f);
// System.out.println(f.toString());
// }
}
//匪徒先打
else{
Da(f,p);
// System.out.println("匪徒开了枪");
// f.Attack(p);
// System.out.println(p.toString());
// while (p.getHp()>0 && f.getHp()>0){
//
// System.out.println("警察开了枪");
// p.Attack(f);
// System.out.println(f.toString());
// System.out.println();
// if(f.getHp()<=0){
// break;
// }
// System.out.println("匪徒开了枪");
// f.Attack(p);
// System.out.println(p.toString());
// }
}
// while (p.getHp()>0 && f.getHp()>0){
//
// System.out.println("警察开了枪");
// p.Attack(f);
// System.out.println(f.toString());
// if(f.getHp()<=0){
// break;
// }
// System.out.println("匪徒开了枪");
// f.Attack(p);
// System.out.println(p.toString());
// }
//是否活着
IsLive(p,f);
// if (p.getHp()<=0){
// System.out.println("警察阵亡");
// }
// else if(f.getHp()<=0){
// System.out.println("匪徒阵亡");
// }
}
}