I've been coding for around a month, learning through CodeHS and coding within their sandbox tool. I am currently working on a text-based explorer/RPG game in order to practice Object-Oriented Programming in Java.
I wanted to know if the current relationship between the Location
and the Explore
class is optimal, as I am not sure if accessing the instance variables of Location
within Explore
is considered to be bad practice. Also, my current method of determining whether or not a generated location is for example a stream seems inefficient, as it simply checks if it contains the strings "stream"
or "river"
. Any help with design is greatly appreciated as well.
The Explore
Class:
public class Explore{
public Location location;
public Player player;
//distances from the town originated and to next town
public int distanceFrom;
public int distanceTo;
public Explore(Player player){
this.player = player;
this.distanceFrom = 0;
this.distanceTo = RNG.range(3, 6);
this.location = new Location(player);
}
public int getDistanceFrom(){
return distanceFrom;
}
public int getDistanceTo(){
return distanceTo;
}
public boolean isStream(){
return location.isStream;
}
public boolean canDrink(){
return location.canDrink;
}
public void drink(){
location.canDrink = false;
}
public String getDesc(){
return location.toString() + " " + distanceFrom + " km away from your origin";
}
public Location getLocation(){
return location;
}
public void advance(){
if(distanceFrom <= 5){
player.incrementDev(RNG.range(1, 4));
}else{
player.incrementDev(RNG.range(3, 6));
}
this.location = new Location(player);
this.distanceFrom++;
this.distanceTo--;
}
public boolean checkNearbyTown(){
if(distanceTo <= 1 && distanceTo > -1){
return true;
}else{
return false;
}
}
}
The Location
Class:
public class Location
{
private Player player;
private String desc;
public boolean isStream = false;
public boolean isCave = false;
public boolean canDrink = false;
//add caves later
//public boolean canCave = false;
public Location(Player player){
this.player = player;
this.desc = RNG.locationDesc(player);
if((this.desc.indexOf("stream") != -1) || (this.desc.indexOf("river") != -1)){
this.isStream = true;
this.canDrink = true;
}
}
public String toString(){
return desc;
}
}
The RNG
Class, which handles my games random events:
import java.util.*;
public class RNG{
//Handles all random events such as pulling a number from a range or generating a name/stats
public static int roll(int sides){
int aRoll = (int)(Math.random() * sides + 1);
return aRoll;
}
public static int range(double min, double max){
return (int)(Math.random() * (max - min + 1) + min);
}
public static String locationDesc(Player player){
int dev = player.getDev();
//takes in a player input in order to determine their insanity, making a more twister are
String[] desc;
if(dev <= 75){
desc = new String[]{"an unassuming wooded area", "an open flower field",
"a steady stream", "the entrance of a small cave", "a small river"};
}else{
desc = new String[]{"Spookier areas to add later"};
}
int index = (int)(Math.random() * desc.length);
return desc[index];
}
}
Location
seems to cover your needs and makes more sense in 'explaining your classes' as well. \$\endgroup\$