making my code look better

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1220 Views - Last Post: 17 May 2020 - 03:35 AM Rate Topic: -----

#1 jimbob2504   User is offline

  • New D.I.C Head

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

making my code look better

Posted 14 May 2020 - 11:24 PM

Hello I have started to learn java about a week ago, I am using https://www.onlinegdb.com. I started to make a text based adventure game and my code is very long and getting harder and harder to go through it and add stuff.
the questions I have is,
1: Is it possible to have a diifrent pages,one for the script and the other for the main program?
2: If so how?
3: Is there any ways other ways I can shorten my code?
4: any ways of making it look better as well

any help will be apricated
code is not finished yet but it is playable
import java.util.Scanner;

public class Main
{

  Scanner myScanner = new Scanner (System.in);
  Scanner enterScanner = new Scanner (System.in);
  int delay = 2000;
  String spaceFront;
  String spaceBack;
  int gap = 24;
  int goUp;
  int gameOverCount = 10;
  int playerHP;
  String playerName;
  String playerWeapon;
  int choice;
  int bearHP;
  int rope;
  int lanturn;
  int bearIsDead;
  int meetWizard;
  int bearClaws;
  int bearHide;
  int bearTeath;
  int harvestBear;
  int payToll;
  int gold;
  int talkGuard;
  int talkedToVillager;
  int talkedToLumberJack;
  int talkToLumberJack;
  int gotWood;
  int gotLogs;
  int food;

  String villagerName = "the villager";
  String guard = "the Guard";
  String villageName = "Falconworth";
  String cityName = "Palinka";
  String lumberjackName = "Paul";
  
  //Possible weapons: knife, machete, Long Sword, crossbow



  public static void main (String[]args)
  {
    Main dublin;
      dublin = new Main ();

      dublin.playerSetUp ();
  }

  public void playerSetUp ()
  {
    playerHP = 100;
    bearHP = 1500;

    System.out.println ("");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    System.out.println("      Your HP: "+playerHP                                                 );
    System.out.println("      Your Gold: "+gold                                                   );
    System.out.println("      Your Food: "+food                                                   );
    System.out.println("                                                                         ");
    System.out.println("      Please enter your name:"                                            );
    System.out.println("                                                                         ");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    playerName = myScanner.nextLine ();
    System.out.println("                                                                         ");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    System.out.println("      Hello "+playerName+", let's start the Adventure!                   ");
    System.out.println("                                                                         ");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    addDelay();
    System.out.println("      You are on an adventure for lost tresure. You arive in maginanea.  ");
    addDelay();
    System.out.println("      You have been travling for days you decide to make a camp and rest ");
    System.out.println("      inside the dark forrest.                                            ");
    delay = 5500;
    addDelay();
    delay = 2000;
    System.out.println("      Its Morning now . You wake up and you pack up your camp.           ");
    addDelay();
    darkForrest();
  }
    
  public void darkForrest()
  {
    System.out.println("                                                                         ");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    System.out.println("      You are stood in the middle of the dark forrest.                   ");
    addDelay();
    System.out.println("                                                                         ");
    System.out.println("      What do you want to do?                                            ");
    addDelay();
    System.out.println("                                                                         ");
    System.out.println("      1: Follow the path deeper into the forrest.                        ");
    System.out.println("      2: Follow the path out of the forrest.                             ");
    System.out.println("                                                                         ");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    choice = myScanner.nextInt ();
    if (choice==1)
    {
      lookAroundForrest();
    }
    else if (choice == 2)
    {
      System.out.println("                                                                         ");
      System.out.println("      ------------------------------------------------------------------ ");
      System.out.println("                                                                         ");
      System.out.println("      You started to wonder deeper and deeper into the forrest.");
      System.out.println("      ...");
      delay = 5500;
      addDelay();
      delay = 2000;
      deepDarkForrest ();
    }
    else if (choice == 3)
    {
      System.out.println("                                                                         ");
      System.out.println("      ------------------------------------------------------------------ ");
      System.out.println("                                                                         ");
      System.out.println("      You follow the path out of the forrest.                            ");
      addDelay();
      crossroad ();
    }
    else
    {
      darkForrest();
    }
  }

  public void lookAroundForrest ()
  {
    System.out.println("                                                                         ");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    System.out.println("      You look around ...                                                ");
    delay = 6000;
    addDelay();
    delay = 2000;
    System.out.println("      The only thing you see is your smoldering campfire                 ");
    darkForrest();
  }

  public void deepDarkForrest ()
  {
    System.out.println("                                                                         ");
    System.out.println("      ------------------------------------------------------------------ ");
    System.out.println("                                                                         ");
    if (lanturn != 1)
    {
      System.out.println("      You're to deep into the forrest it's to dark now you need a lanturn.");
      System.out.println("      You cannot go any further you turn around and follow the path back.");
      darkForrest ();
    }
    else if (gotLogs == 0)
    {
      System.out.println("      You enter the deep dark Forrest.                                   ");
      System.out.println("      You find the logs " + lumberjackName + " was talking about.");
      System.out.println("      You load up the cart with the logs and turn back");
	darkForrest ();
      }
    else
      {
	System.out.println
	  ("     you have collected the logs theres nothing left");
	darkForrest ();
      }
  }

  public void crossroad ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println
      ("     You are stood in the midle of 4 diffrent paths theres a signpost in front of you.");
    System.out.println ("");
    System.out.println ("     What Way do you want to go?");
    System.out.println ("");
    System.out.println ("     1: North - To the City");
    System.out.println ("     2: East  - To the river");
    System.out.println ("     3: South - To the Dark Forrest");
    System.out.println ("     4: West  - To the Village");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();

    if (choice == 1)
      {
	cityGate ();
      }
    else if (choice == 2)
      {
	river ();
      }
    else if (choice == 3)
      {
	darkForrest ();
      }
    else if (choice == 4)
      {
	villageEntrance ();
      }
    else
      {
	crossroad ();
      }
  }

  public void cityGate ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println
      ("     You approach the city. you notice a toll gate ahead!");
    System.out.println ("");
    System.out.println ("     what do you want to do?");
    System.out.println ("");
    System.out.println ("     1: Approach " + guard + ".");
    System.out.println ("     2: Go into the City.");
    System.out.println ("     3: Go back to the Crossroad.");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();

    if (choice == 1)
      {
	approachGuard ();

      }
    else if (choice == 2)
      {
	if (payToll == 1)
	  {
	    city ();
	  }
	else
	  {
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");
	    System.out.println ("");
	    System.out.println ("     " + guard +
				" gets up and stands in your way!.");
	    approachGuard ();
	  }
      }
    else if (choice == 3)
      {
	crossroad ();
      }
    else
      {
	cityGate ();
      }
  }

  public void approachGuard ()
  {
    if (talkGuard == 0)
      {
	System.out.println ("");
	System.out.println
	  ("\n------------------------------------------------------------------\n");
	System.out.println ("");
	System.out.println ("     " + guard +
			    ": Welcome traveler to the City " + cityName +
			    ".");
	System.out.println ("     " + guard +
			    ": My name is Hamish. What is your name?");
	guard = "Hamish";
	System.out.println ("     You: Hello my name is " + playerName +
			    ". I would like to enter the city.");
	System.out.println ("     " + guard +
			    ": Ha! you cant just enter the city without paying me first.");
	System.out.println ("     " + guard +
			    ": The toll to enter these gates is 25 gold");
	System.out.println ("     Your Gold: " + gold + ".");
	if (gold >= 25)
	  {
	    System.out.println ("     You have enouth gold.");
	    System.out.println ("");
	    System.out.println ("     what do you want to");
	    System.out.println ("");
	    System.out.println ("     1: Pay the Guard.");
	    System.out.println ("     2: Go back to the Crossroads.");
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");

	    choice = myScanner.nextInt ();

	    if (choice == 1)
	      {
		gold = gold - 25;
		System.out.println ("     You pay " + guard + " 25 Gold.");
		System.out.println ("     Your Gold: " + gold + ".");
		System.out.println ("     " + guard +
				    ": Thank you, You may enter or leave as you like.");
		System.out.println ("");
		payToll = 1;
		talkGuard = 1;
		cityGate ();
	      }
	    else if (choice == 2)
	      {
		crossroad ();
	      }
	  }
	else
	  {
	    System.out.println ("     " + guard +
				": you dont have enouth Gold to pay me.");
	    System.out.println ("     " + guard +
				": Return when you have enouth.");
	    cityGate ();
	  }
      }
    else
      {
	System.out.println ("     " + guard + ": Welcome " + playerName +
			    " to Palinka. I hope you are enjoying yourself.");
	cityGate ();
      }
  }

  public void city ()
  {
    System.out.println
      ("     There is no City yet. We are still building it come back later.");
    cityGate ();
  }

  public void river ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println ("     You are at The River.");
    System.out.println ("");
    System.out.println ("     what do you want to do?");
    System.out.println ("");
    System.out.println ("     1: Follow the River down");
    System.out.println ("     2: Follow the River up");
    System.out.println ("     3: Try to look in the River");
    System.out.println ("     4: Go to the Crossroad");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();

    if (choice == 1)
      {
	downRiver ();
      }
    else if (choice == 2)
      {
	upRiver ();
      }
    else if (choice == 3)
      {
	catchSalmon ();
      }
    else if (choice == 4)
      {
	crossroad ();
      }
    else
      {
	river ();
      }
  }

  public void villageEntrance ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println
      ("     You walk to the Village. You see a cheery villager walking around.");
    System.out.println ("     " + villagerName + " is walking your way");
    System.out.println ("");
    System.out.println ("     what do you want to do?");
    System.out.println ("");
    System.out.println ("     1: Talk to " + villagerName + ".");
    System.out.println ("     2: Look around the village");
    System.out.println ("     3: Go back to the Crossroads.");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();

    if (choice == 1)
      {
	talkVillager ();
      }
    else if (choice == 2)
      {
	village ();
      }
    else if (choice == 3)
      {
	crossroad ();
      }
    else
      {
	villageEntrance ();
      }
  }

  public void village ()
  {
    System.out.println ("There is no village yet we are still building it");
    villageEntrance ();
  }

  public void talkVillager ()
  {
    if (gotWood == 0)
      {
	if (talkedToVillager == 0)
	  {
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");
	    System.out.println ("");
	    System.out.println ("     " + villagerName +
				": Welcome traveler to our small village " +
				villageName + ".");
	    System.out.println ("     " + villagerName +
				": My name is Hoel. What is your name?");
	    villagerName = "Hoel";
	    System.out.println ("     You: Hello my name is " + playerName +
				". I would like to enter the village.");
	    System.out.println ("     " + villagerName +
				": You can enter our village but can you help us first?");
	    System.out.println ("     " + villagerName +
				": We are trying to build a town hall and we need lumber from the Mill.");
	    System.out.println ("     " + villagerName +
				": I can give you gold if you can get the lumber for us");
	    System.out.println ("");
	    System.out.println ("     what do you want to");
	    System.out.println ("");
	    System.out.println ("     1: Help " + villagerName + ".");
	    System.out.println ("     2: Walk away.");
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");
	    choice = myScanner.nextInt ();
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");
	    System.out.println ("");
	    if (choice == 1)
	      {
		System.out.println ("     " + villagerName +
				    ": Thank you, You can take my horse and cart to the lumber mill");
		System.out.println ("     " + villagerName +
				    ": Just go back to the crossroad and follow it to the river then follow it down");
		System.out.println ("     " + villagerName +
				    ": Just remember dont follow the river up as there have been sightings of a Bear");
		System.out.println ("     " + villagerName +
				    ": Paul the Lumberjack is there he is expecting someone to get the wood");
		System.out.
		  println ("     You start to go back to the Crossroads");
		talkedToVillager = 1;
		crossroad ();
	      }
	    else if (choice == 2)
	      {
		System.out.println ("     You: sorry im busy right now");
		villageEntrance ();
	      }
	    else
	      {
		talkVillager ();
	      }
	  }
	else if (talkedToVillager == 1)
	  {
	    System.out.println ("     " + villagerName + ": Hello again " +
				playerName +
				" we still need the timber from the mill");
	    villageEntrance ();
	  }
	else if (talkedToVillager == 2)
	  {
	    System.out.println ("     " + villagerName +
				": Thank you for your help.");
	    System.out.println ("     " + villagerName +
				": I do not have any more tasks for you yet");
	    villageEntrance ();
	  }

      }
    else
      {
	System.out.println ("     " + villagerName +
			    ": Thank you for getting the timber for us");
	System.out.println ("     " + villagerName +
			    ": Hear is 25 Gold for your troubles");
	gold = gold + 25;
	System.out.println ("     Your Gold: " + gold + ".");
	talkedToVillager = 2;
	gotWood = 0;
	villageEntrance ();
      }
  }

  public void upRiver ()
  {
    if (bearIsDead == 1)
      {
	upRiver2 ();
      }
    else
      {
	upRiver1 ();
      }
  }

  public void upRiver1 ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println
      ("     You start following the River up. you notice a Bear ahead!");
    System.out.println ("");
    System.out.println ("     what do you want to do");
    System.out.println ("");
    System.out.println ("     1: slowly go back down the River");
    System.out.println ("     2: run back down the River");
    System.out.println ("     3: Attack the bear");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();
    int playerChance = 0;
    playerChance = new java.util.Random ().nextInt (10);
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    if (choice == 1)
      {
	System.
	  out.println ("     You try and slowly head back down the river.");
	if (playerChance >= 2)
	  {
	    System.out.println ("     The bear did not see you.");
	    System.out.println ("     player chance" + playerChance +
				" more than or 2");
	    river ();
	  }
	else
	  {
	    System.
	      out.println ("     The bear saw you, You need to attack it.");
	    System.out.println ("     player chance" + playerChance +
				" less than 2");
	    attackBear ();
	  }
      }
    else if (choice == 2)
      {
	System.out.println ("     You try and run back down the river.");
	if (playerChance >= 8)
	  {
	    System.out.println ("     The bear did not see you.");
	    System.out.println ("     player chance" + playerChance +
				" more than or 8");
	    river ();
	  }
	else
	  {
	    System.
	      out.println ("     The bear saw you, You need to attack it.");
	    System.out.println ("     player chance" + playerChance +
				" less than 8");
	    attackBear ();
	  }
      }
    else if (choice == 3)
      {
	System.out.println
	  ("     You try and slowly head to the bear and attack it.");
	if (playerChance >= 8)
	  {
	    System.out.println ("     The bear did not see you.");
	    System.out.println ("     player chance" + playerChance +
				" more than or 8");
	    System.
	      out.println ("     you sneeked up to the bear and stabed it");
	    System.out.println ("     Bear's HP - 75");
	    bearHP = bearHP - 75;
	    System.out.println ("     Bear HP: " + bearHP);
	    System.
	      out.println ("     The bear turned around and clawed you.");
	    System.out.println ("     Your HP - 25");
	    playerHP = playerHP - 25;
	    System.out.println ("     Your HP: " + playerHP);
	    attackBear ();
	  }
	else
	  {
	    System.
	      out.println ("     The sneeked upto the bear and it saw you.");
	    System.out.println ("     player chance" + playerChance +
				" less than 8");
	    System.
	      out.println ("     The bear turned around and clawed you.");
	    System.out.println ("     Your HP - 25");
	    playerHP = playerHP - 25;
	    System.out.println ("     Your HP: " + playerHP);
	    attackBear ();
	  }
      }
    else
      upRiver1 ();
  }

  public void upRiver2 ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println
      ("     You have followed the River up. The only thing here is the bear you killed");
    System.out.println ("");
    System.out.println ("     what do you want to do");
    System.out.println ("");
    System.out.println ("     1: Inspect the bear");
    System.out.println ("     2: Go back down the river");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    if (choice == 1)
      {
	if (meetWizard == 1)
	  {
	    if (harvestBear == 0)
	      {
		System.out.println
		  ("     You gather the Bear claws, Teath and Hide for the Wizard.");
		System.out.println
		  ("     Its getting dark, You make a camp and a fire and harvest ");
		System.out.println
		  ("     the Bears meat and cook it to regenate your health");
		bearClaws = 1;
		bearTeath = 1;
		bearHide = 1;
		playerHP = 150;
		System.out.println ("     Your HP: " + playerHP);
		System.
		  out.println ("     Its morning now you feel well rested");
		upRiver2 ();
	      }
	    else
	      {
		System.out.println
		  ("     You have already Harvested the Bear there is nothing left of use");
		upRiver2 ();
	      }
	  }
	else
	  {
	    System.out.println
	      ("     You look at the bear theres nothing you need yet");
	    upRiver2 ();
	  }
      }
    else if (choice == 2)
      {
	System.out.println ("     you go back down the river");
	river ();
      }
    else
      {
	upRiver2 ();
      }
  }

  public void downRiver ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println
      ("     You follow the river down. You see a Water Mill ahead.");
    System.out.println ("");
    System.out.println ("     what do you want to do?");
    System.out.println ("");
    System.out.println ("     1: Go to the Water Mill.");
    System.out.println ("     2: Look around.");
    System.out.println ("     3: Go back up the river.");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();

    if (choice == 1)
      {
	waterMill ();
      }
    else if (choice == 2)
      {
	aroundMill ();
      }
    else if (choice == 3)
      {
	river ();
      }
    else
      {
	downRiver ();
      }
  }

  public void waterMill ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println ("     You walk to the door of the Water Mill.");
    System.out.println ("");
    System.out.println ("     what do you want to do?");
    System.out.println ("");
    System.out.println ("     1: Knock on the door.");
    System.out.println ("     2: Go back to the path.");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");

    choice = myScanner.nextInt ();

    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    if (choice == 1)
      {
	if (talkedToVillager == 0)
	  {
	    System.out.println ("     You go to knock on the door ");
	    System.out.
	      println ("     You see a notice on the door it reads:");
	    System.out.
	      println ("     No vistors today!. very busy. come back later!");
	    System.out.
	      println ("     You deside to turn around and come back later");
	    waterMill ();
	  }
	else
	  talkToLumberJack ();
      }
    else if (choice == 2)
      {
	downRiver ();
      }
    else
      {
	waterMill ();
      }
  }

  public void talkToLumberJack ()
  {
    if (gotLogs == 0)
      {
	if (talkedToLumberJack == 0)
	  {
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");
	    System.out.println ("");
	    System.out.
	      println ("     You knock on the door and a man answers");
	    System.out.println ("     " + lumberjackName +
				": What do you want, I am busy");
	    System.out.println ("     You: Im sorry I was sent by " +
				villagerName + ".");
	    System.out.println ("     " + lumberjackName +
				": Ok, im Paul by the way.");
	    System.out.println ("     " + lumberjackName +
				": What is your name?");
	    System.out.println ("     You: My name is " + playerName + ".");
	    System.out.println ("     You: " + villagerName +
				" asked me to get some timber from you.");
	    System.out.println ("     " + lumberjackName +
				": yhea, I have been having troubles with my Mill");
	    System.out.println ("     " + lumberjackName +
				": I can fix it but I wont have time to get the lumber");
	    System.out.println ("     " + lumberjackName +
				": Can you get the lumber for me so I can fix the mill?");
	    System.out.println ("");
	    System.out.println ("     What do you want to do?");
	    System.out.println ("");
	    System.out.println ("     1: Help " + lumberjackName + ".");
	    System.out.println ("     2: Go back to the path.");
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");

	    choice = myScanner.nextInt ();
	    System.out.println ("");
	    System.out.println
	      ("\n------------------------------------------------------------------\n");
	    System.out.println ("");

	    if (choice == 1)
	      {
		System.out.println ("     " + lumberjackName +
				    ": Thank you there should be logs stacked up in the dark forrest");
		System.out.println ("     " + lumberjackName +
				    ": Just use your horse and cart to bring it all here");
		System.out.println ("     " + lumberjackName +
				    ": Here is a lanturn so you can see your way");
		lanturn = 1;
		talkedToLumberJack = 1;
		downRiver ();
	      }
	    else if (choice == 2)
	      {
		System.out.println ("     You: sorry im busy at the moment");
		downRiver ();
	      }
	    else
	      {
		talkToLumberJack ();
	      }
	  }
	else
	  {
	    System.out.println ("     " + lumberjackName +
				": Im busy just get me the logs");
	    System.out.println ("     " + lumberjackName +
				": only come back when you have them");
	    downRiver ();
	  }
      }
    else
      {
	System.out.println ("     " + lumberjackName +
			    ": Just in time, I have finished fixing my Mill");
	System.out.println ("     " + lumberjackName +
			    ": let me just mill these logs for " +
			    villagerName);
	System.out.println ("     ...");
	System.out.println ("     " + lumberjackName +
			    ": Ok done, I have loaded up your cart for you");
	System.out.println ("     " + lumberjackName +
			    ": take the lumber back to " + villagerName +
			    " for me");
	System.out.println ("     " + lumberjackName +
			    ": Hear have this crossbow I have made");
	playerWeapon = "longSword";
	downRiver ();
      }
  }

  public void aroundMill ()
  {
    System.out.println ("     You look around the Mill");
    System.out.println ("     Theres not much here there");
    downRiver ();
  }

  public void catchSalmon ()
  {

  }

  public void fightBear ()
  {
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    System.out.println ("     Your Food: " + food);
    System.out.println ("     Your HP: " + playerHP);
    System.out.println ("     Bear HP: " + bearHP);
    System.out.println ("     1: Attack");
    System.out.println ("     2: Eat");
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    choice = myScanner.nextInt ();
    if (choice == 1)
      {
	attackBear ();
      }
    else if (choice == 2)
      {
	if (food >= 5)
	  {
	    System.out.println ("     You eat some of your Food.");
	    food = food - 5;
	    playerHP = playerHP + 50;
	    bearAttack ();
	  }
	else
	  {
	    System.out.println ("     You dont have enouth Food");
	    fightBear ();
	  }
      }
    else
      {
	fightBear ();
      }

  }
  
  public void attackBear ()
  {
    int playerDamage = 0;
    choice = myScanner.nextInt ();
    System.out.println ("");
    System.out.println
      ("\n------------------------------------------------------------------\n");
    System.out.println ("");
    if (playerWeapon.equals ("knife"))
    {
      playerDamage = new java.util.Random ().nextInt (6) + 10;	//between 10 and 15
    }
    else if (playerWeapon.equals ("mechete"))
    {
      playerDamage = new java.util.Random ().nextInt (11) + 25;	// between 25 and 35
    }
    else if (playerWeapon.equals ("longSword"))
    {
      playerDamage = new java.util.Random ().nextInt (11) + 20;	//between 20 and 30
    }
    else if (playerWeapon.equals ("crossbow"))
    {
      playerDamage = new java.util.Random ().nextInt (21) + 20;	//between 20 and 40 
    }
    
    System.out.println ("     You attaced the Bear and gave " +
    playerDamage + " damage.");
    bearHP = bearHP - playerDamage;
    System.out.println ("     Bear HP: " + bearHP);
    if (bearHP < 1)
    {
      bearDied ();
    }
    else if (bearHP > 0)
    {
      bearAttack();
    }
  }

  public void bearAttack ()
  {
    int bearDamage;
    bearDamage = new java.util.Random ().nextInt (10);
    System.out.println ("     The Bear attacked you and gave " +
			bearDamage + " damage.");
		playerHP = playerHP - bearDamage;
    System.out.println ("     Your HP: " + playerHP);
    if (playerHP < 1)
    {
      gameOver ();
    }
    else if (playerHP > 0)
    {
      fightBear ();
    }
	}

  public void bearDied ()
  {
      System.out.println ("");
      System.out.println
	("\n------------------------------------------------------------------\n");
      System.out.println ("");
      System.out.println ("     You killed the bear!");
      upRiver2 ();
    }
  
  public void spacebar()
  {
    if (gap==24)
    {
      spaceFront = "                        ";
      spaceBack = "                                 ";
    }
    else if (gap==25)
    {
      spaceFront = "                         ";
      spaceBack = "                                ";
    }
    else if (gap==26)
    {
      spaceFront = "                          ";
      spaceBack = "                               ";
    }
    else if (gap==27)
      {
      spaceFront = "                           ";
      spaceBack = "                              ";
    }
    else if (gap==28)
    {
      spaceFront = "                            ";
      spaceBack = "                             ";
    }
    else if (gap==29)
    {
      spaceFront = "                             ";
      spaceBack = "                            ";
    }
    else if (gap==30)
    {
      spaceFront = "                              ";
      spaceBack = "                           ";
    }
    else if (gap==31)
    {
      spaceFront = "                               ";
      spaceBack = "                          ";
    }
    else if (gap==32)
    {
      spaceFront = "                                ";
      spaceBack = "                         ";
    }
    else if (gap==33)
    {
      spaceFront = "                                 ";
      spaceBack = "                        ";
    }
    else if (gap==34)
    {
      spaceFront = "                                  ";
      spaceBack = "                       ";
    }
  }
  
  public void gameOver()
  {
    addDelay();
    if (gameOverCount>=1)
    if (goUp==0)
    {
      if (gap<=34)
      {
        spacebar();
        gameOver2();
        gap = gap + 1;
        gameOver();
      }
      else
      {
        goUp = 1;
        gameOverCount = gameOverCount - 1;
        gameOver();
      }
    }
    else
    {
      if (gap>=25)
      {
        spacebar();
        gameOver2();
        gap = gap - 1;
        gameOver();
      }
      else
      {
        goUp = 0;
        gameOverCount = gameOverCount - 1;
        gameOver();
      }
    }
  }

  public void addDelay()
  {
    for (int i = 1; i <= 1; i++)
    {
      try
      {
        Thread.sleep(delay);
      }
      catch (InterruptedException ie)
      {
        ie.printStackTrace();
      }
    }
  }

  public void gameOver2()
  {
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("      ------------------------------------------------------------------ ");
    System.out.println ("                                                                         ");
    System.out.println ("      "+spaceFront+"You Died"+spaceBack+"  ");
    System.out.println ("                                                                         ");
    System.out.println ("      "+spaceFront+"GAMEOVER"+spaceBack+"  ");
    System.out.println ("                                                                         ");
    System.out.println ("      ------------------------------------------------------------------ ");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
    System.out.println ("");
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: making my code look better

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: making my code look better

Posted 15 May 2020 - 05:01 AM

One change that would make the code easier to read and understand would be to declare a long, multi-line String for each set of lines and then use one print statement to print all those lines instead of having so many print statements in the code.
For example the Strings printed on lines 60 to 70 could be put into a single String and a single print statement used to print it instead of the 11 lines of code now used.
Likewise for lines 181 to 196 and many more.

This post has been edited by NormR: 15 May 2020 - 05:05 AM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6901
  • View blog
  • Posts: 28,559
  • Joined: 12-December 12

Re: making my code look better

Posted 15 May 2020 - 05:13 AM

With all those spaceFront and spaceBack you could probably take base strings of a min width and add additional spaces to them.

Java is an OOP language so you could move towards encapsulating global variables (within main) into an object.

You can get rid of enterScanner as well, you only need one Scanner.
Was This Post Helpful? 1
  • +
  • -

#4 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3702
  • View blog
  • Posts: 16,962
  • Joined: 20-September 08

Re: making my code look better

Posted 15 May 2020 - 05:41 AM

Quote

One change that would make the code easier to read and understand would be to declare a long, multi-line String for each set of lines and then use one print statement to print all those lines instead of having so many print statements in the code.

Good idea. Even better, prepare for if and when you write professional quality Java by using ResourceBundle to store your strings. Even if you don't use internationalization, it's good practice and will clear up your code enormously
Was This Post Helpful? 2
  • +
  • -

#5 jimbob2504   User is offline

  • New D.I.C Head

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

Re: making my code look better

Posted 15 May 2020 - 11:48 AM

Thank you guys for getting back so quickly.
I have only started to learn 1 week ago so I'm not to sure what you mean g00se. I will search up tomorrow and see what I can find.
Andrewsw not to sure what you mean either I will look up tomorrow
Thank you NormR that's a great idea didn't think of that
Once again thank you so much.
One other thing I'm using onlinegbd.com and there's a main.java tab and if you can open a new tab. is there a way to put the dialogue on one and call it from the other if so is it worth it? (Java)
Was This Post Helpful? 0
  • +
  • -

#6 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1027
  • View blog
  • Posts: 3,951
  • Joined: 13-June 14

Re: making my code look better

Posted 16 May 2020 - 12:04 AM

A couple of things:

1. Your indentation style in some places is very odd and makes the code harder to read. For example, lines 200-203: having the call to cityGate() at the same level of indentation as the if means that when you're reading, it's harder to see that it's actually inside the if. Traditional Java style would have this as

if (choice == 1) {
    cityGate ();
}


2. There are other data types besides integers and strings. For example, on line 164, you test the variable gotLogs being equal to 0 to mean that the player hasn't picked up the logs. If you care about only the possibilities of something happening or not, use a boolean type, for which the only possible values are true and false.
Was This Post Helpful? 1
  • +
  • -

#7 jimbob2504   User is offline

  • New D.I.C Head

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

Re: making my code look better

Posted 16 May 2020 - 02:18 AM

Thanks ndc85430,
I was laying the coad out like that when I started but the website I was using had a beautify button which I pressed and for got to undo I am in the process of changing it back as I go. Also thanks about the Boolean I forgot about it I'll change it now thank you
Was This Post Helpful? 0
  • +
  • -

#8 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: making my code look better

Posted 16 May 2020 - 10:27 AM

One guiding principle would be to look for repeated or similar code and find ways to eliminate it.

For example, it looks like you often display messages between likes of -----. You could make a method along the lines of this:

public void displayMessage(String message) {
    String separator = "\n------------------------------------------------------------------\n";
    String decoratedMessage = separator + "\n" + message + "\n" + separator;
    System.out.println(decoratedMessage);
}


Then you could call it like this:

displayMessage("      Hello "+ playerName +", let's start the Adventure!                   ");


One advantage of this is that if you ever want to change the way messages are displayed, you can edit this one method and change them all.

Another place where I think this would work well for you is in the damage mechanics. Your code looks like this:

		if (playerWeapon.equals("knife"))
		{
			playerDamage = new java.util.Random().nextInt(6) + 10; // between 10 and 15
		} 
		else if (playerWeapon.equals("mechete"))
		{
			playerDamage = new java.util.Random().nextInt(11) + 25; // between 25 and 35
		} 
		else if (playerWeapon.equals("longSword")) 
		{
			playerDamage = new java.util.Random().nextInt(11) + 20; // between 20 and 30
		} 
		else if (playerWeapon.equals("crossbow"))
		{
			playerDamage = new java.util.Random().nextInt(21) + 20; // between 20 and 40
		}


So there is some repetition but I also notice all the comments. When I see comments like this, I know that the standard library isn't written the way the programmer wants it. How about this:

	public int randomDamageBetween(int minInclusive, int maxInclusive) {
		int randomRange = maxInclusive - minInclusive + 1;
		return new java.util.Random().nextInt(randomRange) + minInclusive;
	}


and your main code becomes:

		if (playerWeapon.equals("knife")) {
			playerDamage = randomDamageBetwee(10, 15);
		} else if (playerWeapon.equals("mechete")) {
			playerDamage = randomDamageBetween(25, 35);
		} else if (playerWeapon.equals("longSword")) {
			playerDamage = randomDamageBetween(20, 30);
		} else if (playerWeapon.equals("crossbow")) {
			playerDamage = randomDamageBetween(20, 40);
		}



I don't know about you but I think this code is clearer.


(As an aside, there is another problem with this code. For almost all applications, you should only make one new java.util.Random at the start and use the same one throughout. The same goes for Scanners reading from System.in.)
Was This Post Helpful? 3
  • +
  • -

#9 jimbob2504   User is offline

  • New D.I.C Head

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

Re: making my code look better

Posted 16 May 2020 - 10:51 AM

o wow thank you so much cfoley, I was doing the ---- a different way after the comment from NormR but yours is much better just one question what is the "\n" for?
and thank you for the random values that will be a great help. can I use that in any part of the code as well I have tried to understand it and if I do understand it then I believe I can but I just want to check as I have ideas for the game that calls for random numbers?

thank you all again I am learning so much from you guys hopefully I get this game done then I can try one with graphics
Was This Post Helpful? 0
  • +
  • -

#10 jimbob2504   User is offline

  • New D.I.C Head

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

Re: making my code look better

Posted 16 May 2020 - 11:22 AM

cfoley, also what do u mean by the scanners sorry not sure how they work all I know is I copied "Scanner myScanner = new Scanner (System.in);" and "Scanner enterScanner = new Scanner (System.in);" from somewhere else and then call it with "choice = myScanner.nextInt ();" and I can type a number in the console and it reads it besides that not shore how it works yet
Was This Post Helpful? 0
  • +
  • -

#11 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3702
  • View blog
  • Posts: 16,962
  • Joined: 20-September 08

Re: making my code look better

Posted 16 May 2020 - 11:25 AM

Quote

NormR but yours is much better just one question what is the "\n" for?

Just as a point of practice, it's not good to hardcode line separators ('\n' is a newline character) as they are OS-specific. The point of Java is to be OS-agnostic so you want to do something like

final String SEPARATOR = "%n----------------%n";
String result = String.format(SEPARATOR + "%s" + SEPARATOR, message);
return result;

which will give you the correct line separator for your platform

This post has been edited by g00se: 16 May 2020 - 11:26 AM
Reason for edit:: Clarification

Was This Post Helpful? 2
  • +
  • -

#12 jimbob2504   User is offline

  • New D.I.C Head

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

Re: making my code look better

Posted 16 May 2020 - 11:46 AM

thank you g00se so i should use %n insted of \n. i just found out what \n is for just before i came back on.
final String SEPARATOR = "%n----------------%n";
String result = String.format(SEPARATOR + "%s" + SEPARATOR, message);
return result;


also whats "%s" and "final" for?

also cfoley i tryed your code
public void displayMessage(String message) {
    String separator = "\n------------------------------------------------------------------\n";
    String decoratedMessage = separator + "\n" + message + "\n" + separator;
    System.out.println(decoratedMessage);
}


with
display("You are stood in the middle of the dark forrest.\n\nWhat do you want to do?\n\n1: Look around the Forrest.\n2: Follow the path deeper into the forrest.\n3: Follow the path out of the forrest.");

it looks the way i wanted it to look thank you
Was This Post Helpful? 0
  • +
  • -

#13 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2410
  • View blog
  • Posts: 5,050
  • Joined: 11-December 07

Re: making my code look better

Posted 16 May 2020 - 06:09 PM

Quote

I was doing the ---- a different way after the comment from NormR but yours is much better


I was actually thinking you could combine my comment with NormrR's. In your code, there are lots of examples of multi-line menus. These could be single strings that NormR was talking about. My method can add the decoration (i.e. the lines of hyphens).

Quote

what is the "\n" for?


I copied the \n from your code. You tell me! :P/>

As g00se explained, it'a newline character. He also explained a more correct way of inserting newlines into Java.

Quote

thank you for the random values that will be a great help. can I use that in any part of the code as well I have tried to understand it and if I do understand it then I believe I can but I just want to check as I have ideas for the game that calls for random numbers?


You can only use it if you understand it. ;)/>

One great thing about programming is that you don't have to ask if something will work. You can try it and see! In fact, this is a great way of learning through experimentation.

One scary thing about programming is if you copy and paste code from the internet without understanding it, then you have no idea what it will do. It may be incorrect. It may even be malicious! Now, I intend no harm and am trying to help but in general, it's good to look for help on the internet but bad to copy and paste without understanding. In this case, you could verify by looking up what the Random method does, or how methods work if that's what is tripping you up. In general, ask for help but verify. :)/>

Quote

what do u mean by the scanners


Near the top of your code, you have this:

Scanner myScanner = new Scanner (System.in);
Scanner enterScanner = new Scanner (System.in);


A scanner can be used to get input from various sources. In this case it it getting the input from System.in, also known as standard input. By default, standard input comes form the keyboard (although it is possible to change that).

The problem is that you have declared two scanners that read from standard input. So, if one of them reads some input, and the other reads some other input then I don't know if there are any guarantees about synchronisation between the two.

Instead, you should just declare one scanner and use that for all your keyboard input.

This post has been edited by cfoley: 16 May 2020 - 06:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 jimbob2504   User is offline

  • New D.I.C Head

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

Re: making my code look better

Posted 17 May 2020 - 12:19 AM

View Postcfoley, on 16 May 2020 - 06:09 PM, said:

A scanner can be used to get input from various sources. In this case it it getting the input from System.in, also known as standard input. By default, standard input comes form the keyboard (although it is possible to change that).


so how would i get it so i can ask for your name at the start then ask for numbers after for the choices ?
ive tryed
String choice;

public void choice()
  {
    choice = myScanner.nextString ();
  }

then call
choice();


but its not working for the numbers i will be trying diffrent way after posting this so i might have the solution.
Was This Post Helpful? 0
  • +
  • -

#15 jimbob2504   User is offline

  • New D.I.C Head

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

Re: making my code look better

Posted 17 May 2020 - 12:29 AM

dont worry about the last post i fixed it

for the name
String choice;

public void choice()
  {
    choice = myScanner.nextLine ();
  }

then call

choice();




and then for the choices i put

public void makeAChoice()
  if (choice=="1")
  {
    [choice 1]
  }
  else if (choice=="2")
  {
    [choice 2]
  }  
  else
  {
    makeAChoice();
  }


it seems to be working the way i wanted.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2