• Java Game quick fix
    6 replies, posted
So I made this simple java game to test what I have learned in class (simple project) but there is a small bug in my code, please help problem is with the sword import javax.swing.*; import java.lang.Math; import java.util.*; import java.util.Scanner; public class TroubleInTheField { public static void main(String args[]) { Intro(); WalkForest(); SwordFind(); Walk(); Dragon(); DragonFight(); DragonFightRoll(); GoldFind(); GoldUse(); Extro1(); Extro2(); Extro3(); StopWorld(); Death(); } private static void Intro() { System.out.println ("You wake up in an unfamiliar room \n Must have been knocked out by that thieve running out of that house \n *A man dressed in dirty clothing approaches you* \n 'Are you alright Son?' asks the man \n You tell him your fine and get dressed \n While getting dressed the man explains that he is very poor and just got robbed, and that you tried to fight the thieve but he was too strong for you \n you accept what has happened and leave the old mans house"); System.out.println (" \n Leaving the house you notice that you are in a big city \n Still disturbed by the current events you leave the City Gates and head for the forest"); } private static void WalkForest() { System.out.println (" Arriving at the forest you see Elven Hieroglyphs engraved into the tree that divides the road \n Do you 1. Go Left 2.Go Right 3.Translate The Elven Hieroglyphs"); Scanner input = new Scanner(System.in); int ForestChoice = input.nextInt(); if (ForestChoice ==1){ Death();} else if (ForestChoice == 2) { SwordFind();} else if (ForestChoice == 3) { System.out.println("You translate the glyphs '' You must go the 'rightway' '' Pesky elfs , they're always writing in riddles... You also see some in black ink just under the main body. You Read It To Be '' KLIS IS BEAST'' 'RAWR'"); } } private static void SwordFind() { System.out.println("You take the right path and after a few minutes come by a dead person, blood still fresh from his stabwound \n You can 1.Leave him be 2.Loot his corpse"); Scanner input2 = new Scanner(System.in); int DeadPersonChoice = input2.nextInt(); if (DeadPersonChoice ==1){System.out.println("You Bury the man with his belongings"); int Sword=0;} else if (DeadPersonChoice == 2) { System.out.println("You find out that this is the man that hit you over the head...'The thieve' \n You also find out that he wasn't stabed, he was bit. \n But what creature takes a mans wealth? "); int Sword=1;} //chu help need to get a var for sword Walk(); } private static void Walk() { System.out.println("walking walking wlaking walking wlaking and see a dark form in the sky ");} private static void Dragon() { System.out.println("Suddenly the Dark Form lands infront of you and lets out a RAWR! \n *you notice that it is a western dragon and that it is holding a bag of holding with its large talons \n You Can 1. Fight the creature 2. Run "); Scanner input3 = new Scanner(System.in); int DragonEncounter = input3.nextInt(); if (DragonEncounter ==1){System.out.println(" You decide to fight the creature "); DragonFight();} //chu need help with var and this set up else if (DragonEncounter == 2) { Death();} } private static void DragonFight() { if (SwordFind(Sword=1)){ System.out.println(" You Raise Your sword "); DragonFightRoll();} else if (SwordFind(Sword=0)) {Death();} } private static void DragonFightRoll() { System.out.println (" You Swing Your Sword , You need to roll higher then a 60 to kill the dragon "); int KillDragon = (int)(Math.random()* 100 ); if ( KillDragon <=60){System.out.println(" You Kill the Dragon "); GoldFind();} else if (KillDragon >=60) System.out.println(" You Missed ");{ DragonFightRoll();} } private static void GoldFind() { System.out.println(" You can 1.Leave 2.Loot the corpse"); Scanner input3 = new Scanner(System.in); int DeadDragonChoice = input3.nextInt(); if (DeadDragonChoice ==1){System.out.println("You Leave");} else if (DeadDragonChoice == 2) { System.out.println("You find that the dragon has the belongings of the city man \n You decide to bring them back to the man "); GoldUse(); } } private static void GoldUse() {System.out.println(" You bring the belongings back to the man in the city \n He is grateful and Awards you"); Extro2(); } private static void Extro1() {System.out.println(" You Won The Game ... I guess "); } private static void Extro2() {System.out.println(" You have Won the game !!! SWEET !!! "); } private static void Extro3() { } private static void Death() { System.out.println(" \n \n \n \n Oh Noes you died, You have died from thugs if you went left // I recommand you translate the glyphs', or maybe you died from the dragon later on, doesn't really matter... your died now");} private static void StopWorld() { } } thanks again, also if there is a code/ code thing please tell me how to do it
[noparse][code]Put your code in here[/code][/noparse]
First off why all the statics? Second what the hell does..(on line 62) [code] if (SwordFind(Sword=1)) [/code] mean? Third you should initialize sword before your main so the scope of the variable is the whole class and don't make it a static. This is how statics are used. [URL="http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html"]http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html[/URL] yeah..
[QUOTE=Guru-guru;21359260]First off why all the statics? [/QUOTE] he's coding procedural java :/
Trouble in the Field? :smug: [editline]06:19AM[/editline] I have problems.
From what i can tell, the sword integer doesn't leave the SwordFind method because it's declared inside it. so you can't access the Sword integer unless you make it a static variable before all of your methods. or you could write it with multiple classes and make things simpler.
[QUOTE=Firo;21399885]From what i can tell, the sword integer doesn't leave the SwordFind method because it's declared inside it. so you can't access the Sword integer unless you make it a static variable before all of your methods. or you could write it with multiple classes and make things simpler.[/QUOTE] This.
Sorry, you need to Log In to post a reply to this thread.