Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Multiple Problems

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Multiple Problems

    There are quite a few errors in my code and I have no idea on how to go about fixing them.
    It is a game, and the list of errors is in the "Information" file of the included .zip file.
    If you have any advice on improving my coding practice, or an answer on how to fix the bugs, I would be VERY grateful if you would reply to this thread with your knowledge.

    Thanks in advance,
    ZeroLRS

    GameSource.zip

    P.S. I will be using this as a reference for the code of my game in the upcoming LD48. Anyone who helps me will have their name included in the credits


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Multiple Problems

    There are quite a few errors in my code
    Please copy and paste the full text of the error messages here.
    Also post your code here.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Multiple Problems

    There is ZeroLRS chance of me downloading your code and wading through it. What Norm said.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Problems

    In actuallty, there are no errors per-say. But it will do things like print null values, and not show variable changes.

    Here are all of the source files.

    GameD01:
    import java.util.Scanner;
     
    //TODO:
    //	Alot
    public class GameD01 {
    	public Boolean isBattle = false;
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
     
    		String operation;
    		System.out.println("What would you like to see?");
    		System.out.println("Stats, TestBattle, or SetName?");
    		System.out.println();
    		operation = input.nextLine();
    		GameD01 main = new GameD01();
    		locationTest locTest = new locationTest();
     
    		if(operation.equalsIgnoreCase("Stats")){
    			operation = null;
    			main.charStats();
    		}else if(operation.equalsIgnoreCase("TestBattle")){
    			try {
    				System.out.print("Initilizing battle system.");
    				Thread.sleep(500);
    				System.out.print(".");
    				Thread.sleep(500);
    				System.out.print(".");
    				Thread.sleep(500);
    				System.out.print(".");
    				Thread.sleep(500);
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    			operation = null;
    			locTest.enemySelect();
    		}else if(operation.equalsIgnoreCase("SetName")){
    			operation = null;
    			main.setName();
    		}else{
    			System.out.println("Please try again.");
    			operation = null;
    			main(null);
    		}
    	}
    	public void setName(){
    		Scanner name = new Scanner(System.in);
    		characterStats charStats = new characterStats();
    		System.out.println("What is your name?");
    		System.out.println();
    		charStats.name = name.nextLine();
    		System.out.println("You are now known as "+ charStats.name +"!");
    		System.out.println();
    		main(null);
    	}
    	public void charStats(){
    		characterStats charStats = new characterStats();
    		battleTest battleTest = new battleTest();
    		System.out.println("Your stats are: ");
    		System.out.println("HP: "+ charStats.health);
    		System.out.println("MP: "+ charStats.magic);
    		System.out.println("Str: "+ charStats.strength);
    		System.out.println("Def: "+ charStats.defense);
    		System.out.println();
    		if(isBattle == true){
    			battleTest.playerTurn();
    		}else{
    			main(null);
    		}
    	}
    }

    battleTest:
    import java.util.Scanner;
     
    //TODO:
    //	Set separate variables from enemy/player stats, see if  that fixes StatPrint. (Does it have to poll each time?)
    //	Set up enemy list.
    //	Code Flee.
     
    public class battleTest {
    	//Main setup material.
    	public Scanner bInput = new Scanner(System.in);
    	public itemTest TestHeal = new itemTest();
    	public GameD01 game = new GameD01();
    	public String choice;
     
    	//Gets character stats.
    	public characterStats player = new characterStats();
     
    	//Variables passed from location to set stats.
    	public String foeName;
    	public int foeHP;
    	public int foeMP;
    	public int foeSTR;
    	public int foeDEF;
     
    	//Temporary damage counters.
    	public int dmgFoe;
    	public int dmgPlr;
     
    	//Prepares main information.
    	public void battleMain(){
    		System.out.println();
    		game.isBattle = true;
    		playerTurn();
    		}
     
    	public void playerTurn(){
    		System.out.println("What would you like to do?");
    		System.out.println("Fight, Item, Flee, Info.");
    		battleTest battle = new battleTest();
     
    		battle.choice = battle.bInput.nextLine();
    		if (battle.choice.equalsIgnoreCase("Fight")){
    			battle.dmgFoe = battle.player.strength - battle.foeDEF;
    			battle.foeHP -= battle.dmgFoe;
    			System.out.println("You did "+ battle.dmgFoe +" damage to the enemy!");
    			if(battle.foeHP <= 0){
    				System.out.println("You have defeated "+battle.foeName+"!");
    				game.isBattle = false;
    				GameD01.main(null);
    			}
    			foeTurn();
    		}else if(battle.choice.equalsIgnoreCase("Item")){
    			//Place item array list here.
    			System.out.println("TestHeal or back?");
    			battle.choice = battle.bInput.nextLine();
    			if(battle.choice.equalsIgnoreCase("TestHeal")){
    				battle.TestHeal.i000();
    				foeTurn();
    			}else{
    				battleMain();
    			}
    			battleMain();
    		}else if(battle.choice.equalsIgnoreCase("Flee")){
    			//Place random gen based on LV's here.
    			//Also a bool check from enemy class for canEscape.
    			System.out.println();
    			System.out.println("You couldnt escape!");
    		}else if(battle.choice.equalsIgnoreCase("Info")){
    			System.out.println();
    			System.out.println("Info on who?");
    			System.out.println();
    			System.out.println("Player, Enemy, or Back.");
    			battle.choice = battle.bInput.nextLine();
    			if(battle.choice.equalsIgnoreCase("Player")){
    				System.out.println();
    				battle.game.charStats();
    				battleMain();
    			}else if(battle.choice.equalsIgnoreCase("Enemy")){
    				//Implement foestats.
    				System.out.println();
    				playerTurn();
    			}else{
    				System.out.println("Please make a valid selection.");
    				playerTurn();
    			}
    		}
    	}
    	public void foeTurn(){
    		//Add random that chooses spell or attack based on enemy.
    		battleTest battle = new battleTest();
    		System.out.println();
    		System.out.println(battle.foeName+" attacked!");
    		battle.dmgPlr = battle.foeSTR - battle.player.defense;
    		battle.player.health -= battle.dmgPlr;
    		System.out.println(battle.foeName+" has done "+ battle.dmgPlr +" damage!");
    		if(battle.player.health <= 0){
    			System.out.println();
    			System.out.println("You have died!");
    			GameD01.main(null);
    		}else{
    			System.out.println();
    			playerTurn();
    		}
    	}
    	public void foeStats(){
    		battleTest battle = new battleTest();
    		System.out.println(battle.foeName+"'s stats are: ");
    		System.out.println("HP: "+ battle.foeHP);
    		System.out.println("MP: "+ battle.foeMP);
    		System.out.println("Str: "+ battle.foeSTR);
    		System.out.println("Def: "+ battle.foeDEF);
    		System.out.println();
    		battle.playerTurn();
    	}
    }

    characterStats:
    public class characterStats {
    	public int health, magic, strength, defense; {
    		health = 100;
    		magic = 20;
    		strength = 20;
    		defense = 10;
    	}
    	public String name = "Joe";
    }

    enemyTest:
     
    public class enemyTest {
    	public int health, magic, strength, defense; {
    		health = 50;
    		magic = 25;
    		strength = 15;
    		defense = 5;
    	}
    	public String name = "Test";
    }

    enemyTest2:
     
    public class enemyTest2 {
    	public int health, magic, strength, defense; {
    		health = 100;
    		magic = 50;
    		strength = 30;
    		defense = 10;
    	}
    	public String name = "Test2";
    }

    itemTest:
    //Heals and adds 100 to str.
    public class itemTest {
    	public void i000(){
    		characterStats player = new characterStats();
    		player.health = 100;
    		player.strength += 100;
    	}
    	public String name = "TestHeal";
    }

    locationTest:
    import java.util.Random;
     
    public class locationTest {
    	String selectedEnemy;
    	public battleTest battle = new battleTest();
    	public GameD01 game = new GameD01();
    	public String[] enemyList = new String[2];{
    		enemyList[0] = "Test";
    		enemyList[1] = "Test2";
    	}
    	public void enemySelect(){
    		Random rand = new Random();
     
    		selectedEnemy = enemyList[rand.nextInt(enemyList.length)];
    		characterStats charStats = new characterStats();
    		System.out.println(charStats.name+" vs "+selectedEnemy+"!");
    		enemyParse();
    	}
    	public void enemyParse(){
    		if (selectedEnemy.equals(enemyList[0])){
    			enemyTest enemy1 = new enemyTest();
    			battle.foeName = enemy1.name;
    			battle.foeHP = enemy1.health;
    			battle.foeMP = enemy1.magic;
    			battle.foeSTR = enemy1.strength;
    			battle.foeDEF = enemy1.defense;
    		}else if(selectedEnemy.equals(enemyList[1])){
    			enemyTest2 enemy2 = new enemyTest2();
    			battle.foeName = enemy2.name;
    			battle.foeHP = enemy2.health;
    			battle.foeMP = enemy2.magic;
    			battle.foeSTR = enemy2.strength;
    			battle.foeDEF = enemy2.defense;
    		}
    		battle.battleMain();
    	}
    }

    And thats it, other than the Information file that reads:
    All data within coded by Lucas Spiker (aka ZeroLRS) for GeekFreedom.
     
    Thanks to the following for help with this project:
    	Norm of http://www.javaprogrammingforums.com/ for coding advice.
    	Matthew Estabrook formerly of GeekFreedom for telling me "WE GOTSTA GED DIS DONE GEUYS" and for temporarily loaning me a computer to host a MC Server.
    	<YOUR NAME HERE> if you help me with this damn code >.>
     
    Buglist:
    	All enemy stats equal null.
    	Can modify stats, but when accessed, return default values.
    	Character name applies under ^
     
    Want to change:
    	Method for getting enemy stats from location.
     
    Misc. Development information:
    	enemyTest and enemyTest2 are just separate to look at stats and to test multiple enemy valuesets.
     
    General TODO:
    	ALOT.


    You said you wanted me to post my source....

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Multiple Problems

    Still not going to wade through that much code especially since there is little information about what I'm looking for. You have 2 choices: create a SSCCE (google it) or debug your code by placing a bunch of print statements to see what is happening and narrow it down to a certain line/section of code.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Multiple Problems

    For the first major problem, the bug lies somewhere within locationTest and/or battleTest.
    In the second problem, which is only discoverable by either directly sending enemy data to battle test from enemyTest(1or2), and lies somewhere in the way I attempt to display stats in battleTest for enemies, and GameD01 for the player.
    I hope this helps.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Multiple Problems

    Execute the program and copy the console contents and paste it here. Be sure to add comments to what you paste here showing what the problem is and add comments to show what you want the output to look like.

    Here is a suggestion for how you can make it easier for others to test your code. Change the Scanner definition to automatically provide the answers that you want so that the program will execute and display the problem. Change this line:
    Scanner input = new Scanner(System.in);
    to something like this:
    Scanner input = new Scanner("first answer\nsecond\nthird\n ....for all answers");
    Then when someone executes the program, there is no need to know what to answer,
    you will have provided ALL of the answer needed to show the problem.

    Also change the Random constructor call by giving it a seed so that the random numbers generated will be the same for the testing.


    Have you tried debugging the code by adding printlns to show how the variables values change and how the execution flow goes? Since you know what your program is supposed to do, if you print out values, you should be able to see where there is a problem in the logic.
    Last edited by Norm; August 16th, 2011 at 07:39 AM.

Similar Threads

  1. help with using multiple classes
    By finalfantasyfreak15 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2011, 11:18 PM
  2. Multiple Multishuffles
    By dino2dy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2010, 12:36 PM
  3. multiple tcp connection?
    By nasser in forum Java Networking
    Replies: 4
    Last Post: July 31st, 2010, 07:35 AM
  4. Multiple JFrames
    By Scottj996 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2010, 05:24 AM
  5. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 01:28 PM