object oriented - Console-based game in Java to demonstrate Solid principles and abstractions - Code Review Stack Exchange
i've written console-based demo while learning solid principles. along way, i've become exceedingly paranoid whether i'm writing code or not.
here code have far:
public class entity { //base template game entities... private string name; public entity(string name) { this.name = name; displayentityname(); } public string getname() { return name; } public void setname(string newname) { name = newname; } private void displayentityname() { system.out.println(name + " entity created..."); } public class player extends entity { private string[] inventory; //player have inventory. private int itemsininventory; private static final random rand = new random(); public player(string name) { super(name); inventory = new string[25]; //default inventory size. } public void attack(enemy enemy) //player can attack enemy types. { system.out.println(super.getname() + " attacked enemy..."); } public void findlegendaryweapon() { inventory[0] = weaponconstants.legendary_weapons [rand.nextint(weaponconstants.legendary_weapons.length)]; itemsininventory++; } public void equiptweapon() { system.out.println(super.getname() + " equipted weapon..."); } public void searchchest() { system.out.println(super.getname() + " searched chest , found nothing..."); } } public enum weaponconstants{ //legendary weapon constants. bow_of_the_major_archer(), blood_letter, dawnfang, frozen_touch; public static final string[] legendary_weapons = {dawnfang.name(), blood_letter.name(), frozen_touch.name(), bow_of_the_major_archer.name()}; } public interface enemy { //enemy interface... void attack(player player); } public class ghoul extends entity implements enemy { //an example of standard enemy class. public ghoul(string name) { super(name); } @override public void attack(player player) { system.out.println(super.getname() + " attacked " + player.getname()); } }
as mentioned above, want know if i'm violating of solid principles. main concern player class. have quite few methods in class different types of things related player. in way violating srp principle?
also, wanted know if abstractions correct , well-designed. represent coding style when used develop more serious apps? have 1 year of experience, i'm trying solidify practices on. input appreciated.
thanks sharing code
i know isn't huge amount of code, i'm going @ assuming want able add additional features , expand upon code that's here.
redundant comments
comments should why you're doing something, not what you're doing. line
private string[] inventory; //player have inventory.
is example of particularly bad offender. if have player
class, , has inventory
member variable. know player
have inventory
, don't need write comment saying code telling us.
let's take @ entity
class. if in constructor, can see constructor causing side effects. in case, it's printing console. in general, when construct object, assume there no side effects or other unexpected behaviour. it's reasonable provide method (in case displayentityname
) not include in constructor.
consider making classes immutable possible
unless need provide getter , setter methods instance variable, should leave them out entirely , make variable final. think here name
candidate, getter seems reasonable, don't think you'd ever need setter. in standard rpg or game, player or enemy names don't change. can marked final.
private final string name;
see question https://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen
have @ arguments , against using getters , setters. won't recommend reading on benefits (and drawbacks) of writing immutable classes.
avoid using strings represent more complex objects
you're representing inventory array of strings. argue item
far more string. rather see item
class. super class or interface items in game derive / implement.
some class come mind be, weapon
, armour
, accessory
etc. if of these things represented single string, you're restricting can do. additionally, not use array situation. how inventory
class.
public class inventory { private final list<item> items = new arraylist<>(); public void additem(item item){ // can weapon, armour etc. items.add(item); } public int numitems(){ return items.size(); } ... }
now, inventory
class has single responsibility of representing , maintaining list of items.
and player
class can have inventory
private final inventory inventory;
or put in entity
class. way enemy
can have inventory too.
notice how used list
instead of array. can resized , has list.size()
method tell how many elements in it. no need keep track of stuff yourself!
this inventory
can become lot more useful array of strings.
your current player
class has two responsibilities jump out. 1 providing player
behaviour, it's maintaining inventory keeping track of it's size. inventory
class in charge of this.
this method signature
public void searchchest()
at moment method prints player
searching chest, why not provide option search chest
object.
public void searchchest(chest chest)
and chest
like
public class chest { private final list<item> contents; ... public list<item> open(){ return new arraylist<>(contents); } }
then in searchchest
method, items chest
can added inventory
.
similarly chest method, method
public void equiptweapon()
firstly, there's typo here. should
public void equipweapon()
secondly, why not able provide weapon
public void equipweapon(weapon weapon)
at moment in enemy interface have method.
void attack(player player);
what if want have enemies fight each other, different factions etc. there's no need limit potential targets, how about
void attack(entity entity);
some final points consider.
when attack
entity, equipped weapon
, other entity's equipped armour
, simple math based on stats
instead of displayentityname
method, override tostring
, print entity
directly in calling code.
before start coding anything, think objects involved in system/game in real world terms, start thinking how these implemented objects in code.
instead of weaponconstants
, why not make collection
of weapon objects.
hopefully review useful you.
keep up!
Comments
Post a Comment