14 Replies - 2981 Views - Last Post: 18 May 2008 - 08:41 PM Rate Topic: -----

#1 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Help with Text-Based Java Game

Post icon  Posted 14 May 2008 - 06:16 PM

Hello. I'm tryin to develop a small text-based fighting game and I'm using BlueJ to help me out. I have two classes a Hero and Villian.

Hero Code so far:


/**
* Hero class for text-based game
*
* @author Owen Hartley
* @version 5-14-08
*/
public class Hero
{
// instance variables - replace the example below with your own
private int Health;
private int Punch;
private int Kick;
private int Special;
/**
* Constructor for objects of class Hero
*/
public Hero()
{
// initialise instance variables
Health = 100;
Punch = -30;
Kick = -25;
Special = -55;
}


Villian Code so far:


/**
* Villian class for Text-Based game
*
* @author Owen Hartley
* @version 5-14-08
*/
public class Villian
{
// instance variables - replace the example below with your own
private int Health;
private int Punch;
private int Kick;
private int Special;

/**
* Constructor for objects of class Villian
*/
public Villian()
{
// initialise instance variables
Health = 100;
Punch = -25;
Kick = -30;
Special = -50;
}


I want to make it turn-based as well so that when the hero attacks, it's the villian's turn. But I'm having trouble trying to construct a suitable method for the attacks. Can someone please help me with this?

I have also attached my program if those willing to help me would like a better view.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Help with Text-Based Java Game

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with Text-Based Java Game

Posted 14 May 2008 - 06:29 PM

Your two classes looks a lot alike.
Why do you use 2 different classes ?
Doesn't look like a good start ...
Was This Post Helpful? 0
  • +
  • -

#3 stdunbar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 17-December 04

Re: Help with Text-Based Java Game

Posted 14 May 2008 - 06:42 PM

You're going to need something that acts as a "driver" for the two classes. The pseudo code would be something on the order of:

Hero hero = new Hero();
Villain villain = new Villain();

java.util.Random random = new java.util.Random();

// isAlive is a new method that determines if a player is "alive"
while( hero.isAlive() && villain.isAlive() ) {
// generate some random "hits" - another new method

	System.out.println( "the villain is being attacked" );

	villain.hit( random.nextInt() );

	 if( !villain.isAlive() ) {
		System.out.println( "the villain has been vanquished!" );
		break;
	}

	System.out.println( "the herois being attacked" );

	hero.hit( random.nextInt() );

	 if( !hero.isAlive() ) {
		System.out.println( "Doh! the hero is toast!" );
		break;
	}
}



The key here is for you to define what "alive" means for your characters and how they get "hit". I sent a random integer to the method but maybe it is dependent on who is doing the "hitting". Maybe instead you could pass the hitter and a "strength of hit" parameter to the hit() method. It would look into the overall health of the hitter and how hard they are hitting the other character.

These are just some ideas. You'll need to decide things like how is life determined and how one opponent attacks another one.

Additionally, you're likely to want a common base class such as Competitor to contain similar functionality between the two competitors.

This post has been edited by stdunbar: 14 May 2008 - 06:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Re: Help with Text-Based Java Game

Posted 16 May 2008 - 04:54 AM

View Postpbl, on 14 May, 2008 - 06:29 PM, said:

Your two classes looks a lot alike.
Why do you use 2 different classes ?
Doesn't look like a good start ...


Well can you help me get off to a better start. I talked to some other people and they said it would be good to add a fight class and make the attack functions as int variables.

I know what I want to do it's just trying to figure out the coding in order to do it. I'm not too handy with the dot.notation. Lol

Feel free to contact me at zabobula@hotmail.com
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with Text-Based Java Game

Posted 16 May 2008 - 12:52 PM

At least something like:


class Character {
	private int Health;
	private int Punch;
	private int Kick; 
	private int Special; 

	// constructor
	Character(int punch, int kick, int special) {
		Health = 100;
		Punch = punch;
		Kick = kick;
		Special = special;
	}
}

class Vilian extends Character {
	
	// constructor
	Vilian() {
		super(-15, -30, -50);
	}
}

class Hero extends Character {
	// constructor
	Hero() {
		super(-30, -25, -55);
	}
}



At least now you can put common methods like addHealth() to your Character class
and the 2 other will share:

class Character {
	private int Health;
	private int Punch;
	private int Kick; 
	private int Special; 

	// constructor
	Character(int punch, int kick, int special) {
		Health = 100;
		Punch = punch;
		Kick = kick;
		Special = special;
	}
	
	// to add/subtract health
	void addHealth(int nb) {
		Health += nb;
	}
}



and now you can do:

Hero h = new Hero();
Vilian v = new Vilian();
....
h.addHealth(10);
v.addHealth(-1);


Was This Post Helpful? 0
  • +
  • -

#6 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Re: Help with Text-Based Java Game

Posted 16 May 2008 - 03:46 PM

I just want this to be a one user program where the villian has prefined attacks against the user. Like if the hero(user) attacks with a punch, the villian attacks with a kick.

How would I set that up in an if statement?

This post has been edited by Zab the Man: 17 May 2008 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#7 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Re: Help with Text-Based Java Game

Posted 17 May 2008 - 10:59 AM

View PostZab the Man, on 16 May, 2008 - 03:46 PM, said:

I just want this to be a one user program where the villian has prefined attacks against the user. Like if the hero(user) attacks with a punch, the villian attacks with a kick.

How would I set that up in an if statement?


Sorry for dping but I still need help with this and my above question still stands. How do I set up that if statement?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with Text-Based Java Game

Posted 17 May 2008 - 10:00 PM

View PostZab the Man, on 17 May, 2008 - 10:59 AM, said:

View PostZab the Man, on 16 May, 2008 - 03:46 PM, said:

I just want this to be a one user program where the villian has prefined attacks against the user. Like if the hero(user) attacks with a punch, the villian attacks with a kick.

How would I set that up in an if statement?


Sorry for dping but I still need help with this and my above question still stands. How do I set up that if statement?


Don't really understand your question (I know nothing about text game) but if you want an if in the Character class you can always do:

if(this instanceof Hero) {
   // do something for hero
}
else if(this instanceof Vilian) {
   // then do something special for Vilian
}



Or your class Vilian and class Hero might have a method (with the same name) that do something specific
Was This Post Helpful? 0
  • +
  • -

#9 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Re: Help with Text-Based Java Game

Posted 18 May 2008 - 06:12 AM

Basically, I want it so that if the hero attacks the villian, the villian will attack back.
Was This Post Helpful? 0
  • +
  • -

#10 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Re: Help with Text-Based Java Game

Posted 18 May 2008 - 09:37 AM

View PostZab the Man, on 18 May, 2008 - 06:12 AM, said:

Basically, I want it so that if the hero attacks the villian, the villian will attack back.

Once again I apologize for dping. (I gotta bad habit of thinking the thread will just get overlooked)

Here is the code I've put together so far But there is a problem...

import java.util.Random;

/**
* This is the fight class for both Sam and Butch.
*
* @author Owen Hartley
* @version 5-14-08
*/
public class Fight
{
private Random randomGenerator;
private int Health;
private int Punch;
private int Kick;
private int Special;
private int isAlive;


/**
* Character constructor for hero and villian
*/
public Fight (int punch, int kick, int special)
{
Health = 100;
Punch = punch;
Kick = kick;
Special = special;
Hero hero = new Hero();
Villain villian = new Villain();
randomGenerator = new Random();
}

// to add/subtract health
public void addHealth(int nb, int h, int v)
{
Health += nb;
}

public class Villian extends Character
{
Villian()
{
super(-15, -30, -50);
}
}

public class Hero extends Character
{
// constructor
Hero()
{
super(-30, -25, -55);
}
}

/**
* isAlive is a method that determines if a player is "alive"
*/
public void getisAlive

if(Sam.isAlive() && Butch.isAlive()) Right here I keep getting a "')' Expected error". CAN SOMEONE SHOW ME WHAT IS WRONG WITH THE CODING?
{
System.out.println("Sam is attacking Butch!");
Butch.hit( random.nextInt() );
}

if(!Butch.isAlive())
{
System.out.println("Butch has been defeated!");
break;
System.out.println("Butch is attacking Sam!");
Sam.hit(random.nextInt() );
}
if(!Sam.isAlive())
{
System.out.println("Sam has been beaten by Big Bully Butch.");
break;
}
}


}

}
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with Text-Based Java Game

Posted 18 May 2008 - 01:16 PM

public void getisAlive <------ the () are missing
Was This Post Helpful? 0
  • +
  • -

#12 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Re: Help with Text-Based Java Game

Posted 18 May 2008 - 07:14 PM

This is what my code looks like now....

import java.util.Random;

/**
* This is the fight class for both Sam and Butch.
*
* @author Owen Hartley
* @version 5-14-08
*/
public class Fight
{
private Random randomGenerator;
private int Health;
private int Punch;
private int Kick;
private int Special;
private int isAlive;


/**
* Character constructor for hero and villian
*/
public Fight (int punch, int kick, int special)
{
Health = 100;
Punch = punch;
Kick = kick;
Special = special;
Hero hero = new Hero();
Villain villian = new Villain();
randomGenerator = new Random();
}

// to add/subtract health
public void addHealth(int nb, int h, int v)
{
Health += nb;
}

public class Villian extends Character It says here "cannot inherit from final java.lang.Character"
{
Villian()
{
super(-15, -30, -50);
}
}

public class Hero extends Character
{
// constructor
Hero()
{
super(-30, -25, -55);
}
}

/**
* isAlive is a method that determines if a player is "alive"
*/
public void getisAlive()
{
if(Sam.isAlive() && Butch.isAlive())
{
System.out.println("Sam is attacking Butch!");
Butch.hit( random.nextInt() );
}

if(!Butch.isAlive())
{
System.out.println("Butch has been defeated!");
break;
System.out.println("Butch is attacking Sam!");
Sam.hit(random.nextInt() );
}
if(!Sam.isAlive())
{
System.out.println("Sam has been beaten by Big Bully Butch.");
break;
}
}


}

And here are my Sam(hero/USER) and Butch(Villian) classes

/**
* Sam-Hero of TB-Fighting Game
*
* @author Owen Hartley
* @version 5-14-08
*/
public class Sam
{
// instance variables - replace the example below with your own
private int Health;
private int Punch;
private int Kick;
private int Special;


/**
* Constructor for objects of class h
*/
public Sam(int punch, int kick, int special)
{
// initialise instance variables
Health = 100;
Punch = punch;
Kick = kick;
Special = special;

}

/**
* Sam's punch attack method
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getPunch(int punch) Cannot find symbol - method isAlive()
{
if(Sam.isAlive() && Butch.isAlive())
{
System.out.println("Sam is attacking Butch!");
Butch.hit( random.nextInt() );
}
}
}



/**
* Butch-Villian of TB-Fighting Game
*
* @author Owen Hartley
* @version 5-14-08
*/
public class Butch
{
// instance variables - replace the example below with your own
private int Health;
private int Punch;
private int Kick;
private int Special;

/**
* Constructor for objects of class h
*/
public Butch(int punch, int kick, int special)
{
// initialise instance variables
Health = 100;
Punch = punch;
Kick = kick;
Special = special;

}

/**
* Butch's punch attack method
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int getPunch(int punch)
{
if(Butch.isAlive() && Sam.isAlive())
{
System.out.println("Butch is attacking Sam!");
Sam.hit( random.nextInt() );
}
}
}


I know there is something wrong with my Sam and Butch classes but I don't know what it is that's missing. What should I add?

This post has been edited by Zab the Man: 18 May 2008 - 07:17 PM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with Text-Based Java Game

Posted 18 May 2008 - 07:21 PM

You decided to rename Fight the class I had named Character

so your new classes extens Fight
Was This Post Helpful? 0
  • +
  • -

#14 Zab the Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-May 08

Re: Help with Text-Based Java Game

Posted 18 May 2008 - 07:29 PM

Now it says

import java.util.Random;

/**
* This is the fight class for both Sam and Butch.
*
* @author Owen Hartley
* @version 5-14-08
*/
public class Fight
{
private Random randomGenerator;
private int Health;
private int Punch;
private int Kick;
private int Special;
private int isAlive;


/**
* Character constructor for hero and villian
*/
public Fight (int punch, int kick, int special)
{
Health = 100;
Punch = punch;
Kick = kick;
Special = special;
Sam hero = new Sam();
Butch villian = new Butch();
randomGenerator = new Random();
}

// to add/subtract health
public void addHealth(int nb, int h, int v)
{
Health += nb;
}

public class Butch extends Fight
{
Butch()
{
super(-15, -30, -50);
}
}

public class Sam extends Fight
{
// constructor
Sam()
{
super(-30, -25, -55);
}
}

/**
* isAlive is a method that determines if a player is "alive"
*/
public void getisAlive()
{
if(Sam.isAlive() && Butch.isAlive()) Now it says here "cannot find symbol - method isAlive(). It says the same thing in the Butch class in the getPunch method
{
System.out.println("Sam is attacking Butch!");
Butch.hit(random.nextInt());
}

if(!Butch.isAlive())
{
System.out.println("Butch has been defeated!");
break;
System.out.println("Butch is attacking Sam!");
Sam.hit(random.nextInt());
}
if(!Sam.isAlive())
{
System.out.println("Sam has been beaten by Big Bully Butch.");
break;
}
}


}
Was This Post Helpful? 0
  • +
  • -

#15 cutegrrl  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 77
  • Joined: 12-May 08

Re: Help with Text-Based Java Game

Posted 18 May 2008 - 08:41 PM

import java.util.*;

public class Character {

	// instance variables
	private String name;
	private int health;
	private ArrayList<Attack> atkList;

	
	// default constructor
	public Character(){
		this.name = null;
		health = 0;
		atkList = new ArrayList<Attack>();
		// set primary attacks
		atkList.add(new Attack("Punch", 0));
		atkList.add(new Attack("Kick", 0));
		atkList.add(new Attack("Special", 0));
	}
	
	public Character(String name, int hp, int punch, int kick, int special){
		this.name = name;
		health = hp;
		atkList = new ArrayList<Attack>();
		// set primary attacks
		atkList.add(new Attack("Punch", punch));
		atkList.add(new Attack("Kick", kick));
		atkList.add(new Attack("Special", special));
	}
	
	public void setName(String n){
		name = n;
	}
	
	public void setHP(int hp){
		health = hp;
	}
	
	public void setPunch(int p){
		atkList.get(0).setAtkValue(p);
	}
	
	public void setKick(int k){
		atkList.get(1).setAtkValue(k);
	}
	
	public void setSpecial(int s){
		atkList.get(2).setAtkValue(s);
	}
	
	public String getName(){
		return name;
	}
	public int getHP(){
		return health;
	}
	
	public int getPunch(){
		return atkList.get(0).getAtkValue();
	}
	
	public int getKick(){
		return atkList.get(1).getAtkValue();
	}
	
	public int getSpecial(){
		return atkList.get(2).getAtkValue();

	}

	public ArrayList<Attack> getAtkList(){
		return atkList;
	}

 public void addAttack(String atkName, int atkValue){
		atkList.add(new Attack(atkName, atkValue));
	}
	
	public boolean isAlive(){
		if(health > 0){
			return true;
		}
		return false;
	}
}




public class Attack {
	
	private String attackName;
	private int value;
	
	public Attack(){
		attackName = null;
		value = 0;
	}
	
	public Attack(String attackName, int attackValue){
		this.attackName = attackName;
		this.value = attackValue;
	}
	
	public void setAtkName(String attackName){
		this.attackName = attackName;
	}
	
	public void setAtkValue(int attackValue){
		this.value = attackValue;
	}
	
	public String getName(){
		return attackName;
	}
	
	public int getAtkValue(){
		return value;
	}
	
	public String display(){
		return attackName + " (" + value + ")";
	}
}



import java.io.*;


public class TBFlightDriver {

	// Create a single shared BufferedReader for keyboard input
	static BufferedReader stdbr = new BufferedReader(new InputStreamReader(System.in));

	public static void main(String[] args) throws IOException{
		boolean continuePlay = false;
		do{
			System.out.print("Enter your character's name: ");
			String name = stdbr.readLine();

			// Create new characters
			Character hero = new Character(name, 100, 30, 25, 55);
			Character villain = new Character("Villian", 100, 25, 30, 50);

			System.out.println("\nVillain wants to fight " + name + "! \n\nFIGHT!");

			String choice = "";
			do{
				choice = getChoice(hero);
				if(Integer.parseInt(choice)-1 < 0 || Integer.parseInt(choice)-1 >= hero.getAtkList().size()){
					System.out.println("Invalid choice.");
				}

			}while(Integer.parseInt(choice)-1 < 0 || Integer.parseInt(choice)-1 >= hero.getAtkList().size());

			while(!choice.equals("quit")){
				int action = Integer.parseInt(choice)-1;
				System.out.println("\n" + name + " uses " + hero.getAtkList().get(action).display() + " against " + villain.getName() + ".");

				int atkPoints = hero.getAtkList().get(action).getAtkValue();
				villain.setHP(villain.getHP() - atkPoints);

				// check status of villain
				if(!villain.isAlive()){
					System.out.println(villain.getName() + " has been vanquished!");
					break;
				}else{
					System.out.println(villain.getName() + " loses " + atkPoints + " health points.");
				}

				System.out.println();

				// generate random attack for villain
				java.util.Random random = new java.util.Random();
				int randomAttack = random.nextInt(villain.getAtkList().size());

				System.out.println(villain.getName() + " uses " + villain.getAtkList().get(randomAttack).display() + " against " + hero.getName() + ".");

				atkPoints = villain.getAtkList().get(randomAttack).getAtkValue();
				hero.setHP(hero.getHP() - atkPoints);

				// check status of hero
				if(!hero.isAlive()){
					System.out.println("Doh! " + hero.getName() + " is toast!");
					break;
				}else{
					System.out.println(hero.getName() + " loses " + atkPoints + " health points.");
				}

				System.out.println();
				do{
					choice = getChoice(hero);
					if(Integer.parseInt(choice)-1 < 0 || Integer.parseInt(choice)-1 >= hero.getAtkList().size()){
						System.out.println("Invalid choice.");
					}
				}while(Integer.parseInt(choice)-1 < 0 || Integer.parseInt(choice)-1 >= hero.getAtkList().size());
			}

			do{
				System.out.print("\nPlay again? (Y/N) ");
				choice = stdbr.readLine().toLowerCase().trim();
				if(choice.equals("y")){
					continuePlay = true;
				}else if(choice.equals("n")){
					continuePlay = false;
				}else{
					System.out.println("Invalid input.");
				}
			}while((!choice.equals("y")) && (!choice.equals("n")));

			System.out.println();
		}while(continuePlay);

	}

	private static String getChoice(Character hero) throws IOException{
		System.out.println("\nYou currently have " + hero.getHP() + " health points.");
		System.out.println("\nChoose an attack or type 'quit' to end the game");
		for(int i = 0; i < hero.getAtkList().size(); i++){
			System.out.println(i+1 + ". " + hero.getAtkList().get(i).display());
		}

		System.out.print("\n> ");

		return stdbr.readLine().toLowerCase().trim();
	}

}



Posted Image

This post has been edited by cutegrrl: 18 May 2008 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1