Jump to content
  • Advertisement
Sign in to follow this  
dajiandbun

optimizing code for first game

This topic is 2163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im in the progress of working on a crappy little fight simulator as my first game in java. Ive got most of it done, but whenever i fight the slime i get an infinite loop. can somebody point out to me what i did wrong?

also any tips on making my code smoother and easier to read will be appreciated.

[source lang="java"]import java.io.*;
import java.util.*;

public class HelloWorld {

static String name;
static boolean gameStart = true;
static Scanner sc = new Scanner(System.in);

static int hero_health = 100;
static int hero_strength = 10;
static int hero_defense = 8;
static int hero_agility = 6;
static int hero_exp = 0;

static int s_health = 25;
static int s_strength = 3;
static int s_defense = 3;
static int s_agility = 1;


public static void main(String args[]){
System.out.println("What is your name?");
name = sc.next();
System.out.println("Welcome " + name);
System.out.println("Your stats: ");
System.out.println(hero_health + " health");
System.out.println(hero_strength + " strength");
System.out.println(hero_defense + " defense");
System.out.println(hero_agility + " agility");

startGame();
}
public static void startGame()
{
Scanner sc = new Scanner(System.in);

System.out.println("You awake in a tavern. You walk outside to hear commotion in the streets. " +
"From what you can gather, the princess of the kingdom has been kidnapped and has been taken to a " +
"nearby castle.");
System.out.println("You wander around the village asking where the castle is, hoping to make a few quick bucks."
+ "After a good hour, you learn that the castle is just outside the village, but it is guarded by a dragon");
System.out.println("Even though you are weak, you have slain dragons before. All you need is to train back to your normal level and fight the dragon");
System.out.println("Do you want to go outside of the village to train? Or would you rather relax at the tavern?");
System.out.println("Type '1' to rest or '2' to go fight monsters");
int start = sc.nextInt();

switch(start)
{
case 1:
System.out.println("You decide to return to the tavern to rest.");
hero_health = 100;
System.out.println("HEALTH RESTORED.");
break;
case 2:
System.out.println("You wander out of the town looking for a fight");
battleSlime();
break;
}
}

public static void fightTech()
{
boolean fight_tech = true;
int basedamage = hero_strength - s_defense;
final int LOWF = 1;
int damage = (int)(Math.random() * (basedamage - s_defense + 1)) + LOWF;
final int HIGHF = 20;
int ROLLF = (int)(Math.random() * 20) + 1;
int crithit = basedamage * 2;

while (fight_tech)
{
if (ROLLF <= 13)
{
System.out.println("You hit!");
s_health = s_health - damage;
System.out.println("You dealt " + damage + " damage to slime.");
System.out.println("Slime has " + s_health + " health left");
if (s_health == 0 || s_health < 0)
{
fight_tech = false;
Victory();
}
slimeAttack();
}
else if (ROLLF >= 14 && ROLLF <= 18)
{
System.out.println("Aww you missed!");
System.out.println("Slime has " + s_health + " health left");
slimeAttack();
}
else if (ROLLF >=19)
{
System.out.println("Critical hit!!!!!");
s_health = s_health - crithit;
System.out.println("Slime has " + s_health + " health left");
slimeAttack();
}
System.out.println("Slime has " + s_health + " health left");
}
}
public static void defendTech()
{
final int LOWD = 1;
final int HIGHD = 10;
final int ROLLD = (int)(Math.random() * (HIGHD - LOWD + 1)) + LOWD;
if (ROLLD <=3)
{
System.out.println("You avoided half the damage by blocking");

}
else if (ROLLD >=4 && ROLLD < 10)
{
System.out.println("Close but not close enough. You only blocked 25% of damage.");
}
else if (ROLLD == 10)
{
System.out.println("You managed to roll out of the way. Took no damage");
}
}
public static void runTech()
{
final int LOWR = 1;
final int HIGHR = 10;
final int ROLLR = (int)(Math.random() * (HIGHR - LOWR +1)) + LOWR;
if (ROLLR <= 10)
{
System.out.println("You successfully escaped");
}
else
{
System.out.println("Couldn't escape");
}
}
public static void battleSlime()
{
boolean battle_slime = true;

System.out.println("You've encountered a slime!!");
while (battle_slime)
{
System.out.println("Type 'F' to fight, 'D' to defend, or 'R' to run away); ");
switch('F')
{
case 'F':
fightTech();
break;
case 'D':
defendTech();
break;
case 'R':
break;
}
}
}
public static void slimeAttack()
{
int low = 1;
int high = 10;
int roll = (int)(Math.random() * 10);
int damage = hero_defense - s_strength;
if (roll <= 5)
{
hero_health = hero_health - damage;
System.out.println("Slime dealt " + damage + " damage.");
System.out.println("You have " + hero_health + " health left");
fightTech();
}
else
{
System.out.println("Slime missed!");
System.out.println("You have " + hero_health + " health left");
fightTech();
}
}
public static void slimeDead()
{

}
public static void Victory()
{
System.out.println("You've defeated the slime!");
}
}[/source]

Share this post


Link to post
Share on other sites
Advertisement
huh. didnt show all the code


if (ROLLR <= 10)
{
System.out.println("You successfully escaped");
}
else
{
System.out.println("Couldn't escape");
}
}
public static void battleSlime()
{
boolean battle_slime = true;

System.out.println("You've encountered a slime!!");
while (battle_slime)
{
System.out.println("Type 'F' to fight, 'D' to defend, or 'R' to run away); ");
switch('F')
{
case 'F':
fightTech();
break;
case 'D':
defendTech();
break;
case 'R':
break;
}
}
}
public static void slimeAttack()
{
int low = 1;
int high = 10;
int roll = (int)(Math.random() * 10);
int damage = hero_defense - s_strength;
if (roll <= 5)
{
hero_health = hero_health - damage;
System.out.println("Slime dealt " + damage + " damage.");
System.out.println("You have " + hero_health + " health left");
fightTech();
}
else
{
System.out.println("Slime missed!");
System.out.println("You have " + hero_health + " health left");
fightTech();
}
}
public static void slimeDead()
{

}
public static void Victory()
{
System.out.println("You've defeated the slime!");
}
}
Edited by dajiandbun

Share this post


Link to post
Share on other sites
public static void battleSlime()
{
boolean battle_slime = true;

System.out.println("You've encountered a slime!!");
while (battle_slime)
{
System.out.println("Type 'F' to fight, 'D' to defend, or 'R' to run away); ");
switch('F')
{
case 'F':
fightTech();
break;
case 'D':
defendTech();
break;
case 'R':
break;
}
}
}


In this section, you never set battle_slime to anything but true, so the loop will never exit.

Share this post


Link to post
Share on other sites
when i set battle_slime to false after its health reaches 0 it still gives the infinite loop

public static void battleSlime()
{
boolean battle_slime = true;


while (battle_slime)
{
System.out.println("Type 'F' to fight, 'D' to defend, or 'R' to run away");
switch('F')
{
case 'F':
fightTech();
break;
case 'D':
defendTech();
break;
case 'R':
break;
}
if (s_health == 0 || s_health < 0)
{
battle_slime = false;
Victory();
}
}
}

Share this post


Link to post
Share on other sites
Your while(fight_tech) loop has the same problem. However, it would be very helpful if you provided a bit more description of the actual problem, such as what is looping infinitely.

One other thing that is important: = is not the same as ==. If you want to be checking if a variable equals some value, use ==. If you want to set a variable to a specific value, use =.

Share this post


Link to post
Share on other sites
it was a problem with my switch in battleSlime(). but now it wont let me enter a response, it just terminates after i battleSlime starts

do i even need the while loops with the fight_tech if im just calling the method again from slimeAttack? i feel like i already have a loop just by repeatedly calling the methods back and forth until the slime dies Edited by dajiandbun

Share this post


Link to post
Share on other sites
public static void fightTech()
{
boolean fight_tech = true;
int basedamage = hero_strength - s_defense;
final int LOWF = 1;
int damage = (int)(Math.random() * (basedamage - s_defense + 1)) + LOWF;
final int HIGHF = 20;
int ROLLF = (int)(Math.random() * 20) + 1;
int crithit = basedamage * 2;

while (fight_tech)
{
if (ROLLF = 14 && ROLLF =19)
{
System.out.println("Critical hit!!!!!");
s_health = s_health - crithit;
System.out.println("Slime has " + s_health + " health left");
slimeAttack();
}
System.out.println("Slime has " + s_health + " health left");
}
}
public static void defendTech()
{
final int LOWD = 1;
final int HIGHD = 10;
final int ROLLD = (int)(Math.random() * (HIGHD - LOWD + 1)) + LOWD;
if (ROLLD =4 && ROLLD < 10)
{
System.out.println("Close but not close enough. You only blocked 25% of damage.");
}
else if (ROLLD == 10)
{
System.out.println("You managed to roll out of the way. Took no damage");
}
}

a few things i saw:
In fight_tech() :
the fight_tech boolean is never set to false.
You have an if check that sets ROLLF 14 and 19 every iteration

in defense_tech();
simialr situation you have an if check that sets ROLLD to 4 every iteration

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!