Part 2: DoomGame

reconstructed the whole thing, working out its problems

Page 1 of 1

9 Replies - 1268 Views - Last Post: 11 January 2011 - 02:53 PM Rate Topic: -----

#1 Zekorov   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Part 2: DoomGame

Posted 20 December 2010 - 07:56 PM

Ok so my problem is now hopefully not my design. I think i have set up this thing completely the way i should set up a program. but.... here it is. my problem in it at the moment is that when i exit out of the battle while loop, and then go back to battling, my health automatically goes back up to ten where it is set at inside the DoomControl constructor class. I'm wondering, how can i make it be what it was before i exited out of the while loop in the battle?
here's my code:
 
public class DoomMain {

    public static void main(String[] args)
    {
		DoomControl runner = new DoomControl( );
		runner.doomRunner( );
    }
}


import java.util.Random;
import java.util.Scanner;
import static java.lang.System.*;

public class DoomGame {

	public void battleSequence()
	{
		DoomControl game = new DoomControl();

		game.doomGameOutput();
	}

}


//DoomControl is my brain for the whole program. 

import static java.lang.System.*;
import java.util.Scanner;
import java.util.Random;

public class DoomControl {

	private int health, inihealth, inimonsterhealth, monsterhealth, attack, iniattack, monsterattack, option,
	 randomattack, exp, experience, givehealth, level, nextlevel;

	DoomInput input = new DoomInput();
	DoomOutput output = new DoomOutput();
	DoomGame doom = new DoomGame();

    public DoomControl()
    {
    	health = 10;
    	inihealth = 10;
		monsterhealth = 0;
		inimonsterhealth = 0;
		attack = 5;
		iniattack = 5;
		monsterattack = 0;
		experience = 0;
		exp = 0;
		givehealth = 0;
		randomattack = 0;
		level = 1;
    }

	//reads in the difficulty selected and gets ready to send out to Output
	public void doomDiffReader()
	{
		input.doomDifficulty();
		input.getDiffChoice();
		out.println("You have chosen the difficulty level " + input.getDiffChoice() + ".");

		switch (input.getDiffChoice())
		{
			case 1:
			{
				monsterhealth = 10;
				inimonsterhealth = 10;
				monsterattack = 2;
				break;
			}
			case 2:
			{
				monsterhealth = 25;
				inimonsterhealth = 25;
				monsterattack = 6;
				break;
			}
			case 3:
			{
				monsterhealth = 40;
				inimonsterhealth = 40;
				monsterattack = 10;
				break;
			}
		}

		out.println("The monster's health will start at " + monsterhealth + ".");
		out.println("The monster's attack will start at " + monsterattack + ".");
	}

	//reads in the main battle and sends it to output from here
	public void doomBattleReader()
	{
		Random generator = new Random();

		input.doomBattle();
		input.getBattleChoice();
		while (input.getBattleChoice() == 2)
		{
			//you attack the monster
			randomattack = generator.nextInt(2);
			if(randomattack == 1)
			{
				monsterhealth = monsterhealth - attack;
			}

			//the monster attacks you
			randomattack = generator.nextInt(2);
			if(randomattack == 1)
			{
				health = health - monsterattack;
			}

			//if the monster dies, do this
			if(monsterhealth <= 0)
			{
				out.println("You win the battle!");
				out.println("You still have " + health + " health left.");

				input.doomBattle();
				input.getBattleChoice();
			}

			//if you die, game ends, end of line lolz
			if(health <= 0)
			{
				out.println("You have died. Game Over. End of Line");
				break;
			}
		}
	}

	public void doomGameOutput()
	{
		output.doomGameO();
	}




	//helps keep the whole game running unless you opt to quit
	public void doomRunner()
	{
		out.println("What do you want to do?");
		out.println("Go to the Store, Battle, or Quit? (1 = Store, 2 = Battle, 3 = Quit)");
		Scanner scanner = new Scanner(in);
		option = scanner.nextInt( );

		while (option == 1 || option == 2)
		{
			//you go to the store(for now, nothing really happens)
			if (option == 1)
			{
				out.println("Now what do you want to do?");
				out.println("Go to the Store, Battle, or Quit? (1 = Store, 2 = Battle, 3 = Quit)");
				option = scanner.nextInt( );
			}

			//the battle will commence if you press 2
			if (option == 2)
			{
				doom.battleSequence();

				//if the player dies in battle, whole game stops
				if (health <= 0)
				{
					break;
				}

				out.println("Now what do you want to do?");
				out.println("Go to the Store, Battle, or Quit? (1 = Store, 2 = Battle, 3 = Quit)");
				option = scanner.nextInt( );
			}
		}
	}

}

import static java.lang.System.*;
import java.util.Scanner;

public class DoomInput {

	private int difficulty, battle;

	//the next two methods cover selecting and returning the value for difficulty
    public void doomDifficulty()
    {
		Scanner scanner = new Scanner(in);

		out.println("Choose your difficulty: 1, 2, or 3");
		difficulty = scanner.nextInt();
    }

    public int getDiffChoice()
    {
    	return difficulty;
    }



	//these two methods cover selecting and returning the choice to battle
    public void doomBattle()
    {
    	Scanner scanner = new Scanner(in);

    	out.println("You must press 2 to battle.");
    	battle = scanner.nextInt();
    }

    public int getBattleChoice()
    {
    	return battle;
    }




	//for monsters that will be added later
    public void doomMonsters( )
    {

    }
}

import static java.lang.System.*;

public class DoomOutput {



	//simply prints out everything from the input in DoomController
    public void doomGameO()
    {
    	DoomControl outtie = new DoomControl();

    	outtie.doomDiffReader();
    	outtie.doomBattleReader();
    }

}



ok that's all of it now. :P I would also like to know if this is designed well again, just like the last time. it seems like it is. but because of numerous problems i keep seeming to have, i'm not so sure.
But yes, how can i keep health from going back to ten and staying what it was before exiting out of DoomControl's while loop for the main battle?

Is This A Good Question/Topic? 0
  • +

Replies To: Part 2: DoomGame

#2 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Part 2: DoomGame

Posted 20 December 2010 - 09:00 PM

Okay here's your problem:

public void battleSequence()
	{
		DoomControl game = new DoomControl();

		game.doomGameOutput();
	}



Every time you call the battleSequence method, you are creating a new DoomControl object. When you do this, you use the DoomControl constructor. The problem is here:
 public DoomControl()
    {
    	health = 10;
    



Every time you call this method health gets set back to 10. And you call this method, and create the new object, every time you get into battle.

Hope this helps you see the error and can fix it. If you come across problems fixing it, let us know!
Was This Post Helpful? 2
  • +
  • -

#3 Zekorov   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: Part 2: DoomGame

Posted 20 December 2010 - 09:11 PM

well, fixing it is the problem. and thanks for letting me know more specifically what the problem was. i knew something was making it renew every time but i didn't know exactly how. hmmmm.... if you have any suggestions on how to fix that, they're more than welcome. :) meanwhile, i'll continue to think about it myself.
Was This Post Helpful? 0
  • +
  • -

#4 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Part 2: DoomGame

Posted 20 December 2010 - 09:17 PM

Well you're the designer, but...

is there a reason you have to create a new object every time there is a battle?

I guess I just don't understand the logic behind that, but you're the designer, I'm just thinking out loud :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Part 2: DoomGame

Posted 21 December 2010 - 08:12 AM

Another negative side to it is that you are holding all the monster variable IN GameControl. Instead, consider using Monster objects and manipulate those instead.
Was This Post Helpful? 0
  • +
  • -

#6 Zekorov   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: Part 2: DoomGame

Posted 21 December 2010 - 09:53 AM

hmmmm... Dogstopper, do you mean like making Monsters with their own already set stats, instead of having variables like i do now? if that were the case, that would eliminate the need for a difficulty setting. and then i'd also have to totally rewrite a lot of my program again. lol so.... hmmm..... guess it's back to the drawing board. for the 4th time with this thing. :P Actually, i'm gonna do this. instead of wasting so much time trying to continually write the program, i'm going to plan it all out first, see if you all like that design, if it's at all good, and then write the code once i find a design that's easy to work with. Because, i know what i want it to do for now. I want the user to be able to battle several different kinds of monsters, level up, gain random health veils in battle, and eventually gain gold and things to buy weapons from a store. And with the attack stat and weapons working, when and if they do, it would be easy to do the same and add defense and armor. so.... hmmm... ok well i'm gonna plan this out a little better and think about it before trying again.
Was This Post Helpful? 0
  • +
  • -

#7 Zekorov   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: Part 2: DoomGame

Posted 21 December 2010 - 10:03 AM

hmmm i was thinking, and looking at some tutorials on inheritance. Would inheritance be useful in my game? honestly, i'm thinking it would be very useful. But, i never learned about it until now because my old programming class didn't cover it until Programming II. Yay DIC Java Tutorials! :D haha
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Part 2: DoomGame

Posted 21 December 2010 - 10:57 AM

Absolutely. There is certainly a limit at which too much OO can slow down progress, but a well-developed system needs to be made for good game. Generally, if there is a different "tangible" object (like Monster, Game, Simulator, Level) that CONTAINS other values, like stats, it is a good idea to make it an object or a series of objects.
Was This Post Helpful? 0
  • +
  • -

#9 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Part 2: DoomGame

Posted 21 December 2010 - 01:10 PM

Utilizing inheritance makes perfect sense for a game like this.

For an example of how it would pan out just think in terms of Monsters at first. You'd obviously have a Monster parent class that a bunch of other specific types of Monsters inherit from such as Ogre, Cyclops, Rat, etc. It would probably make even more sense to have Monster be an abstract class. It wouldn't make a lot of sense for the Monster class to actually implement the attack method for example, but of course you are going to want to have a fully implemented attack method for each specific type of monster.

Just a thought. :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#10 Zekorov   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: Part 2: DoomGame

Posted 11 January 2011 - 02:53 PM

Ok well i've been working on DoomGame and i modified it's name to AlienDoom and I now have a MainStats class so that the main stats of the user, and all monsters can be extended down to a "User" class, and then an "Aliens" class. From the Aliens class, there are so far two open ended classes that are specific kinds of aliens that the user will be able to choose to fight. I have eliminated AlienGame (which ran the battle sequence and would have eventually ran the store sequence) simply because it kept creating a problem by creating a new object every time i tried to battle, thus would not "remember" the stats that were in place according to the last battle the player was in. The battle sequence is now in the AlienControl class, where the main mechanism that makes the game run is also located. it is just a nested While loop with some if statements in there for all the logic and battling. Furthermore, in my Aliens class, i have experience and eventually will have gold located in that class because for each alien killed, the user gains a certain level of experience and gold from the alien upon its death (so it makes sense to have experience gained in the Aliens class). And i have the level variable in the User class for now. it may change, but to me it makes sense for the user to have the level in its own class. Anyways, Just letting you all know the thoughts i have so far and that my code, when a little more complete, will be posted once more to look for problems i may be experiencing and fixing them. Thanks everyone for your support in my continuing journey to learn good design. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1