在 java 中实现一个简单的文字冒险游戏(使用界面)
Implementing a simple text adventure game in java (working with interfaces)
我正在阅读 ECS/Composition over inheritance 并决定修改我目前正在开发的文字冒险游戏以更好地理解这个概念。
我决定在游戏中使用特定武器进行尝试。
现在,在我的游戏中,逻辑是每个武器都被视为一个 IGameObject。考虑到所有这些,我想到了以下内容:
武器界面:
public interface IWeapon {
int getWeaponDamage();
}
武器class:
public class Weapon implements IWeapon {
private int damage;
public Weapon(int damage)
{
this.damage = damage;
}
@Override
public int getWeaponDamage() {
return this.damage;
}
IGameObject 接口:
public interface IGameObject {
String getGameObjectID();
String getGameObjectName();
String getGameObjectDescription();
}
游戏对象class:
public class GameObject implements IGameObject {
private String ID;
private String name;
private String Desc;
public GameObject(String ID, String name, String Desc)
{
this.ID = ID;
this.name = name;
this.Desc = Desc;
}
使用这一切的摘要class:
public abstract class GameGun implements IGameObject, IWeapon {
IGameObject gameObj;
IWeapon weaponObj;
//Left out getters to keep it simple and easy to read.
public GameGun(IGameObject game, IWeapon weapon)
{
this.gameObj = game;
this.weaponObj = weapon;
}
public void fire()
{
System.out.println("Shot Fired");
}
public void reload()
{
//implementation for reload method
}
综合起来:
主要方法:
GameObject sleep = new GameObject("SLP-1","Sleep Weapon","A Custom Made Gun");
Weapon sleepDamage = new Weapon(10);
GameGun sleepWeapon = new SleepWeapon(sleep,sleepDamage);
问题:
- 我只是想知道我的实现是否正确?
- 如果不是,我该如何更正?
我一直在纠正许多问题并使其正常运行。你现在可以玩了:D
请抓住要点以获得所有工作代码。
控制台
Revolver Gun:Gun reloaded
Revolver Gun:Gun shots => 10 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Revolver Gun:Gun shots => 10 damage(s)
代码
package com.rizze.test.labs.sof.gamegun;
import org.junit.Test;
public class GameGunTest {
@Test
public void betterCode2() {
Game sleep = new Game("SLP-1","Sleep Weapon","A Custom Made Gun");
Gun gun = new Gun();
Sword sword = new Sword();
gun.hit();
sword.hit();
sword.hit();
sword.hit();
gun.hit();
}
public class Gun implements IWeapon {
private int damage;
private String name;
public Gun(int damage)
{
this.damage = damage;
this.reload();
}
public Gun()
{
this.damage = 10;
this.name= "Revolver Gun";
this.reload();
}
@Override
public int getDamages() {
return this.damage;
}
/**
*
* @return damages
*/
@Override
public int hit() {
System.out.println( this.name + ":Gun shots => "+damage +" damage(s)");
return this.damage;
}
@Override
public void reload() {
System.out.println(this.name+":Gun reloaded");
}
}
public class Sword implements IWeapon {
private String name;
private int damage;
public Sword(String name){
this.damage = 5;
this.name=name;
}
public Sword(){
this.damage = 5;
this.name="Basic Sword";
}
@Override
public int getDamages() {
return this.damage;
}
/**
*
* @return damages
*/
@Override
public int hit() {
System.out.println( name+ ":Sword cuts => "+damage +" damage(s)");
return damage;
}
@Override
public void reload() {
System.out.println( name+ ":Sword - skipped reload" );
}
}
public class Game implements IGame {
private String id;
private String name;
private String desc;
public Game(String ID, String name, String Desc)
{
this.id = ID;
this.name = name;
this.desc = Desc;
}
@Override
public String getId() {
return this.id;
}
@Override
public String getName() {
return this.getName();
}
@Override
public String getDesc() {
return this.desc;
}
}
}
//IGAME
public interface IGame {
String getId();
String getName();
String getDesc();
}
//IWEAPON
package com.rizze.test.labs.sof.gamegun;
public interface IWeapon {
int getDamages();
int hit();
void reload();
}
GIST
这是完整的 GIST :https://gist.github.com/jeorfevre/92d093853bfd3fc5c666b915b309abf1
我正在阅读 ECS/Composition over inheritance 并决定修改我目前正在开发的文字冒险游戏以更好地理解这个概念。
我决定在游戏中使用特定武器进行尝试。
现在,在我的游戏中,逻辑是每个武器都被视为一个 IGameObject。考虑到所有这些,我想到了以下内容:
武器界面:
public interface IWeapon {
int getWeaponDamage();
}
武器class:
public class Weapon implements IWeapon {
private int damage;
public Weapon(int damage)
{
this.damage = damage;
}
@Override
public int getWeaponDamage() {
return this.damage;
}
IGameObject 接口:
public interface IGameObject {
String getGameObjectID();
String getGameObjectName();
String getGameObjectDescription();
}
游戏对象class:
public class GameObject implements IGameObject {
private String ID;
private String name;
private String Desc;
public GameObject(String ID, String name, String Desc)
{
this.ID = ID;
this.name = name;
this.Desc = Desc;
}
使用这一切的摘要class:
public abstract class GameGun implements IGameObject, IWeapon {
IGameObject gameObj;
IWeapon weaponObj;
//Left out getters to keep it simple and easy to read.
public GameGun(IGameObject game, IWeapon weapon)
{
this.gameObj = game;
this.weaponObj = weapon;
}
public void fire()
{
System.out.println("Shot Fired");
}
public void reload()
{
//implementation for reload method
}
综合起来:
主要方法:
GameObject sleep = new GameObject("SLP-1","Sleep Weapon","A Custom Made Gun");
Weapon sleepDamage = new Weapon(10);
GameGun sleepWeapon = new SleepWeapon(sleep,sleepDamage);
问题:
- 我只是想知道我的实现是否正确?
- 如果不是,我该如何更正?
我一直在纠正许多问题并使其正常运行。你现在可以玩了:D 请抓住要点以获得所有工作代码。
控制台
Revolver Gun:Gun reloaded
Revolver Gun:Gun shots => 10 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Basic Sword:Sword cuts => 5 damage(s)
Revolver Gun:Gun shots => 10 damage(s)
代码
package com.rizze.test.labs.sof.gamegun;
import org.junit.Test;
public class GameGunTest {
@Test
public void betterCode2() {
Game sleep = new Game("SLP-1","Sleep Weapon","A Custom Made Gun");
Gun gun = new Gun();
Sword sword = new Sword();
gun.hit();
sword.hit();
sword.hit();
sword.hit();
gun.hit();
}
public class Gun implements IWeapon {
private int damage;
private String name;
public Gun(int damage)
{
this.damage = damage;
this.reload();
}
public Gun()
{
this.damage = 10;
this.name= "Revolver Gun";
this.reload();
}
@Override
public int getDamages() {
return this.damage;
}
/**
*
* @return damages
*/
@Override
public int hit() {
System.out.println( this.name + ":Gun shots => "+damage +" damage(s)");
return this.damage;
}
@Override
public void reload() {
System.out.println(this.name+":Gun reloaded");
}
}
public class Sword implements IWeapon {
private String name;
private int damage;
public Sword(String name){
this.damage = 5;
this.name=name;
}
public Sword(){
this.damage = 5;
this.name="Basic Sword";
}
@Override
public int getDamages() {
return this.damage;
}
/**
*
* @return damages
*/
@Override
public int hit() {
System.out.println( name+ ":Sword cuts => "+damage +" damage(s)");
return damage;
}
@Override
public void reload() {
System.out.println( name+ ":Sword - skipped reload" );
}
}
public class Game implements IGame {
private String id;
private String name;
private String desc;
public Game(String ID, String name, String Desc)
{
this.id = ID;
this.name = name;
this.desc = Desc;
}
@Override
public String getId() {
return this.id;
}
@Override
public String getName() {
return this.getName();
}
@Override
public String getDesc() {
return this.desc;
}
}
}
//IGAME
public interface IGame {
String getId();
String getName();
String getDesc();
}
//IWEAPON
package com.rizze.test.labs.sof.gamegun;
public interface IWeapon {
int getDamages();
int hit();
void reload();
}
GIST 这是完整的 GIST :https://gist.github.com/jeorfevre/92d093853bfd3fc5c666b915b309abf1