Jump to content
  • Advertisement
Sign in to follow this  
metal_kid43

good start? (java text game)

This topic is 4156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, another guy from my java class and myself are working on creating an online text game. I just wanted to show people what we have so far (which isnt much) and get people's comments on it to make sure we arent making any mistakes which are going to hurt us down the road. I think we're going to store player stats in a text file eventually and load the file up when the game starts. Written in Java. Also, what steps would need to be taken in order to get this beast online when the time comes?? Thank you much. Here's the main player class, all the other characters in the game will be subclasses of this...
public class Player{
	private int exp;
	private int strength;
	private int intelligence;
	private int hp;
	private int mp;
	
	//player constructor
	Player(int theExp, int theStrength, int theIntelligence, int theHp, int theMp){
		exp = theExp;
		strength = theStrength;
		intelligence = theIntelligence;
		hp = theHp;
		mp = theMp;
	}

	public void setExp(int temp){
		exp = temp;
	}

	public void setStrength(int temp){
		strength = temp;
	}

	public void setIntelligence(int temp){
		intelligence = temp;
	}

	public void setHp(int temp){
		hp = temp;
	}

	public void setMp(int temp){
		mp = temp;
	}

	public int getExp(){
		return exp;
	}

	public int getStrength(){
		return strength;
	}

	public int getIntelligence(){
		return intelligence;
	}

	public int getHp(){
		return hp;
	}

	public int getMp(){
		return mp;
	}

	public String toString(){
		return "   Experience:  " + exp + "   Strength:  " + strength + "   Intelligence:  " + intelligence + "   Hp:  " + hp + "   Mp:  " + mp;
	}
}


Here is one of the races that the player will be able to choose from...
public class Cyborg extends Player{
	private final String RACE = "Cyborg";
	private String name;

	Cyborg(String theName){	
		super(0, 10, 5, 10, 5);
		name = theName;
	}
	
	int theExp = getExp();
	int theIntelligence = getIntelligence();
	int theStrength = getStrength();
	int theHp = getHp();
	int theMp = getMp();
	
	public String toString(){
		return "Name:  " + name + "\n" + "Race:  " + RACE + "\n" + "Experience:  " + theExp + "\n" + "Strength:  " + theStrength + "\n" + "Intelligence:  " + theIntelligence + "\n" + "HP:  " + theHp + "\n" + "MP:  " + theMp;
	}
}

And now the obligatory main method...
import javax.swing.JOptionPane;
public class Main{
	public static void main(String args[]){
		
		String name = "";
		String choice = "";
		
		name = JOptionPane.showInputDialog("Please enter your name");
		System.out.println("Please select your race...");
		System.out.println("1.  Cyborg\n2.  Mutant\n3.  Human\n\n");
		
		choice = JOptionPane.showInputDialog("Select 1 for Cyborg, 2 for Mutant, or 3 for Human");
		
		while(choice.equals("1") == false && choice.equals("2") == false && choice.equals("3") == false){
			choice = JOptionPane.showInputDialog("Choose 1 for Cyborg, 2 for Mutant, or 3 for Human");
		}

		if(choice.equals("1")){
			Cyborg cy1 = new Cyborg(name);
			System.out.println(cy1);
		}
		else if(choice.equals("2")){
			//create new mutant
		}
		else if(choice.equals("3")){
			//create new human
		}
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
int theExp = getExp();
int theIntelligence = getIntelligence();
int theStrength = getStrength();
int theHp = getHp();
int theMp = getMp();

This totally defeats the purpose of using accessor methods. If you're going to have get/set methods, you should always access the values through them whenever possible.

Share this post


Link to post
Share on other sites
You're off to a good start if what you're trying to do is just a simple game. Im still relatively new in this stuff but I've been messing with Java and C # and I can tell that thats just more or less a concept what you have there. Keep at it and keep hacking away at it and let us see what you guys can crack out.

Share this post


Link to post
Share on other sites
Hi,

First of all, this is indeed quite wrong (said before I know):


int theExp = getExp();
int theIntelligence = getIntelligence();
int theStrength = getStrength();
int theHp = getHp();
int theMp = getMp();



Second I would separate the statistics from the player into a seperate class.
This way you won't have to recode the player class if you want to read statistics from file or a database.

I would also place 'name' in the player class, because no matter what a player is(Cyborg, Human, whatever), they always wants to have a name, right. :)

Personally, I would move away from JOptionPane when you're feeling up to it and move to a JDialog instead (not that hard). With this you make a list and let the player choose from it so they won't have to type, 1, 2, 3, rather, they choose Human, Cyborg, etc from a combobox. If you want this here's a good link Using JDialog

The example uses Object[] possibilities = {"ham", "spam", "yam"}; as input, I don't think it will be that hard to change it into Object[] possibilities = {"Cyborg", "Human", "ZY-009"}!

Hope this was helpfull.

Share this post


Link to post
Share on other sites
Hi, I have looked at your code and came up with the following.

Like Starik said I moved the Statistics into a new class away from the Player class. I also moved race and name to the Player class from the Cyborg class. I also made the player class abstract because you shouldn't be able to create a Player object by using the new operator.

public abstract class Player{

protected int race;
protected String name;
protected Statistics stats;

public static final int CYBORG = 1;
public static final int MUTANT = 2;
public static final int HUMAN = 3;

public String getRace()
{
switch(race)
{
case CYBORG:
return "Cyborg";
case MUTANT:
return "Mutant";
case HUMAN:
return "Human";
default:
return "";
}
}

public String getName()
{
return name;
}

public Statistics getStats()
{
return stats;
}

public String toString(){
return "PLAYER DETAILS\n--------------\nName: " + getName() + "\n" + "Race: " + getRace() + "\n" + getStats();
}
}




public class Statistics
{
protected int exp;
protected int strength;
protected int intelligence;
protected int hp;
protected int mp;

Statistics(int theExp, int theStrength, int theIntelligence, int theHp, int theMp){
exp = theExp;
strength = theStrength;
intelligence = theIntelligence;
hp = theHp;
mp = theMp;
}

public void setExp(int temp){
exp = temp;
}

public void setStrength(int temp){
strength = temp;
}

public void setIntelligence(int temp){
intelligence = temp;
}

public void setHp(int temp){
hp = temp;
}

public void setMp(int temp){
mp = temp;
}

public int getExp(){
return exp;
}

public int getStrength(){
return strength;
}

public int getIntelligence(){
return intelligence;
}

public int getHp(){
return hp;
}

public int getMp(){
return mp;
}

public String toString(){
return "SATATISTICS\n--------------\nExperience: " + getExp() + "\n" + "Strength: " + getStrength() + "\n" + "Intelligence: " + getIntelligence() + "\n" + "HP: " + getHp() + "\n" + "MP: " + getMp();
}
}



As you can see the Cyborg class has been cut back;


public class Cyborg extends Player
{
Cyborg(String theName)
{
stats = new Statistics(0, 10, 5, 10, 5);
name = theName;
race = CYBORG;
}
}




I created a factory class to handle the creation of the Player object;


public class PlayerFactory
{
public static Player getPlayer(int c, String name)
{
switch(c)
{
case Player.CYBORG:
return new Cyborg(name);
case Player.MUTANT:
// return new Mutant(name);
case Player.HUMAN:
// return new Human(name);
default:
return null;
}
}
}



And finally the main class has been cut back.


import javax.swing.JOptionPane;
public class Main{

public static void main(String args[]){

String name = "";
int choice;
Player player;
name = JOptionPane.showInputDialog("Please enter your name");
System.out.println("Please select your race...");
System.out.println("1. Cyborg\n2. Mutant\n3. Human\n\n");

choice = Integer.parseInt(JOptionPane.showInputDialog("Select 1 for Cyborg, 2 for Mutant, or 3 for Human"));

while(choice != 1 && choice != 2 && choice != 3)
choice = Integer.parseInt(JOptionPane.showInputDialog("Choose 1 for Cyborg, 2 for Mutant, or 3 for Human"));

player = PlayerFactory.getPlayer(choice, name);
System.out.println(player);
}
}




There are a few powerful oop techniques in there so hopefully you will learn something from this.

Share this post


Link to post
Share on other sites
Wow, thanks a lot guys for your help with this and getting us off to a good start. . .especially clearz! We will definitely keep you all posted with our progress!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!