Help with a broken for loop?
Hello all,
I'm a new member and relatively new to Java; I took two years in high school but that was about four years ago. I'm making a turn based strategy game based on Final Fantasy Tactics and I'm trying to make a system that will read in the speeds for each character and then (for the time being) just spit it back out, with hopes that I might eventually have it process the speeds and create a fluid and variable system that allows for applying multipliers like haste/slow/stop and such.
Right now, though, the issue is that when you input a value in the main argument (the for loop after I instantiate "print") it reads several lines at a time and then outputs numbers that have nothing to do with those input. If that's not a good explanation, running it and throwing in some junk numbers will make the problem quickly apparent. My code looks solid to my atrophied debugging skills and I can't for the life of me figure out why the input is so broken... the code is below and anyone who can offer me any pointers (related to the specific problem or not) would make my day in doing so.
Code :
import java.io.*;
public class turnOrder {
public static int[] speeds = new int[14]; //the values for the speed of each character
public static int[] ct = new int[14]; //how close each character is to getting a turn
public static String[] names = {"Red Hume","Red Moogle", "Red Viera", "Red NuMou", "Red Bangaa", "Red Seeq", "Red Summon",
"Blue Hume", "Blue Moogle", "Blue Viera", "Blue Scamron" ,"Blue NuMou", "Blue Bangaa", "Blue Seeq", "Blue Summon"};
public static void main(String[] args) throws IOException {
int print; //for loop
for (print=0;print<12;print++){
System.out.println("Input speed for "+ names[print]);
speeds[print]=System.in.read();} //requests and reads in all the speeds for each character
speeds[12]=0; //this space reserved for beastmaster class summons
speeds[13]=0; //this space reserved for beastmaster class summons
for (print=0;print<12;print++)
System.out.println("Speed for "+ names[print] + " is " + speeds[print]);
}
public void haste (int multiplier, int index)
{
speeds[index]*=multiplier;
//needs a way to make the haste count down automatically? Perhaps instantiate a count variable and implement a for or while loop
// keep track of how many turns haste has left, and as long as haste has at least a turn left, keep the speeds[index]*=multiplier up
/*
* int count;
* while (count > 0){
* if (count = 0)
* xxxx
* }
*/
}
public void turns()
{
int a; //for loop
int turnindex=-1; //set to a positive when a unit reaches 1000CT, otherwise stays -1
for (a=0; a<15; a++) { //loop cycles through the indexes of ct and adds the corresponding speed values, just once
ct[a]+=speeds[a]; //will likely need to run 10-12 times before anyone gets a turn
System.out.println("Turn has ended");
if (ct[a]>999) //at the end of each run, if nothing printed (such as a unit getting a turn) run again
turnindex=a; //once a unit gets a turn, break and wait for a prompt to continue or to set a character's speed or CT
}
if (turnindex>0){
System.out.println(names[turnindex]+" gets a turn!");
ct[turnindex]=0;
turnindex=-1;}
}
public void summon(int speed, boolean blueteam)
{
int x=0;
if (blueteam = true)
x+=1;
ct[12+x]=0; //resets the summoned monster's CT
speeds[12+x]=speed; //sets index 12 or 13 to the given speed depending on summoned monster's allegiance
}
public void setSpeed(int speed, int index)
{
//reads from console to set one character's speed
}
public void setCT (int index)
{
ct[index]=0;
}
}
Re: Help with a broken for loop?
Quote:
then outputs numbers that have nothing to do with those input.
Can you copy the output and paste it here? Add some comments saying what is wrong with the output and show what the output should be.
You are reading directly from the OS and getting what it sends without any help. The Scanner class methods would help you get cleaner data.
The pressing the Enter key sends the characters entered plus the endline characters one at a time.
To see: write a simple program with a loop that reads with the above and prints out what is returned by read().
Re: Help with a broken for loop?
Quote:
Originally Posted by
Iantaylora
Code :
speeds[print]=System.in.read();
System.in.read() reads 1 byte from the standard-input. If you type '1', you get the ascii code of the character '1' which is 49. It is surely not what you want.
If you want to read numerical values, use java.util.Scanner.
Re: Help with a broken for loop?
By Java convention, class names begin with capital letters.
'print' is a lousy name for a variable.
Use an instance of Scanner to get the user's input from the keyboard. There are several examples of how to do this on the Internet. Search for and pick your favorite.
Once you've implemented using a Scanner instance for input, I think your problem will be solved. Then it'll be on to new ones.
Re: Help with a broken for loop?
Andbin, that makes perfect sense, thanks so much! It completely solved my problem. If I run into further issues, should I start a new thread or should I edit the OP for this one, or what is the etiquette here?
--- Update ---
The next problem is a little bit more vague. Using the speeds that were indexed at the start of the main argument, I want to increment values in an array until one hits 1000, at which point the method pauses, outputs who hits 1000 and thereby gets a turn, and waits for a value to subtract from that 1000 (getting a turn is -200, and then a further -400 for moving or acting). I figure the best way to do this is a for loop, like so:
Code :
Scanner scan=new Scanner(System.in);
int a; //for loop
int turnindex=-1; //set to a positive when a unit reaches 1000CT, otherwise stays -1
for (a=0; a<15; a++) { //loop cycles through the indexes of ct and adds the corresponding speed values, just once
ct[a]+=speeds[a]; //will likely need to run 10-12 times before anyone gets a turn
if (ct[a]>999) //at the end of each run, if nothing printed (such as a unit getting a turn) run again
turnindex=a; //once a unit gets a turn, break and wait for a prompt to continue or to set a character's speed or CT
}
if (turnindex>0){
System.out.println(names[turnindex]+" gets a turn! How much CT do they lose?");
ct[turnindex]-=scan.nextInt(); //subtracts appropriate CT
turnindex=-1;} //resets turnindex
}
The most obvious issues I can see with this is that it hiccups when two units with similar or identical speeds hit 1000 in the same run through the for loop. Is the best way to deal with this by making a method within the loop, or by having the loop "break" when a unit hits 1000, or by some other avenue entirely? Again, thank you all for your help.
--- Update ---
Okay, I think I like what I've got going here.
Code :
public void turns()
{
int a; //for loop
boolean b = true; //keeps while loop running
while(b=true)
for (a=0; a<15; a++) { //loop cycles through the indexes of ct and adds the corresponding speed values, just once
ct[a]+=speeds[a];
if(ct[a]>999)
turnBreak(a);} //will likely need to run 10-12 times before anyone gets a turn
}
public void turnBreak(int whoseTurn)
{
Scanner scan = new Scanner(System.in);
System.out.println(names[whoseTurn]+" gets a turn! How much CT do they lose?");
ct[whoseTurn]-=scan.nextInt(); //subtracts appropriate CT
System.out.println("Got it. Anything else to report?");
//filler space for applying death, haste, slow, etc.
}
Is there anything really tremendously stupid going on here? If not, then I think I only have one major problem left.