Reputation: 11
I'm making a racing game using Strings and I'm having trouble making them move to the right. How would I go about moving the Strings? Methods JamesMove() & KeithMove() are the same as SusanMove(), just with the name of the variable are different.
public class NameRacer {
public static void nameRaces() {
Scanner scan = new Scanner(System.in);
String response;
int SusanFront, SusanBack, JamesFront, JamesBack, KeithFront, KeithBacck;
boolean raceIsDone;
int move = (int)((Math.random() * 5) + 1);
introduction();
scan.nextLine();
do {
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
SusanMove();
JamesMove();
KeithMove();
System.out.println("Would you like to watch the race again?");
response=scan.next();
}
while(response.equalsIgnoreCase("yes"));
}
private static void introduction() {
System.out.println("This game races three names. The random number generator");
System.out.println("from the Math class repeatedly gives each name a random number");
System.out.println("between 1 and 5. As the names move these random distances");
System.out.println("we see which name gets to the finish line first.");
System.out.println("Press a key to begin the race.");
}
private static void SusanMove() {
String SusanFrontString="";
String SusanBackString="";
int SusanFront=50,SusanBack=0;
int move = (int)((Math.random() * 5) + 1);
SusanFront=SusanFront-move;
SusanBack=SusanBack+move;
for(int back = 0; back<SusanBack;back++) {
SusanBackString = SusanBackString+" ";
}
for(int front = 0; front<SusanFront;front++) {
SusanFrontString = SusanFrontString+" ";
}
String SusanRace = SusanBackString+"Susan"+SusanFrontString+"|";
System.out.println(SusanRace);
}
I expected an output something like this:
Susan |
James |
Keith |
Susan |
James |
Keith |
Susan |
James |
Keith |
But instead I got:
Susan |
James |
Keith |
Susan |
James |
Keith |
Susan |
James |
Keith |
Susan |
James |
Keith |
Upvotes: 1
Views: 364
Reputation: 548
You are not storing the position of the racers anywhere. From your code, every call to the *Move
method is like your racers restart everytime. Your *Front
and *Back
values are always re-initialized to 50 and 0 at each call.
To solve it, just store your default values in the NameRaces
class and remove the initialization of those variables in the *Move
methods, like so:
public class NameRacer {
private static int SusanFront = 50, JamesFront = 50, KeithFront = 50;
private static int SusanBack = 0, JamesBack = 0, KeithBack = 0;
public static void nameRaces() {
Scanner scan = new Scanner(System.in);
String response;
boolean raceIsDone;
int move = (int)((Math.random() * 5) + 1);
introduction();
scan.nextLine();
do {
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
SusanMove();
JamesMove();
KeithMove();
System.out.println();
System.out.println("Would you like to watch the race again?");
response=scan.next();
}
while(response.equalsIgnoreCase("yes"));
}
private static void introduction() {
System.out.println("This game races three names. The random number generator");
System.out.println("from the Math class repeatedly gives each name a random number");
System.out.println("between 1 and 5. As the names move these random distances");
System.out.println("we see which name gets to the finish line first.");
System.out.println("Press a key to begin the race.");
}
private static void SusanMove() {
String SusanFrontString="";
String SusanBackString="";
// Removed re-initialization of Front and Back values
int move = (int)((Math.random() * 5) + 1);
SusanFront=SusanFront-move;
SusanBack=SusanBack+move;
for(int back = 0; back<SusanBack;back++) {
SusanBackString = SusanBackString+" ";
}
for(int front = 0; front<SusanFront;front++) {
SusanFrontString = SusanFrontString+" ";
}
String SusanRace = SusanBackString+"Susan"+SusanFrontString+"|";
System.out.println(SusanRace);
}
Upvotes: 1