Reputation: 21
I'm new to OOP and I am making a fishing game. Right now I have a object called boat which can travel a certain distance depending on its fuel. However if it runs out of fuel and is in the middle of the ocean the game should end. My problem is that for some reason rather than ending the game since it is not in a landmark and fuel would be empty, it say's the boat traveled the certain distance.
public class Boat
{
int fuelCapacity;
int fuelLevel;
int fishCapacity;
int location;
public Boat()
{
fuelCapacity=10;
fuelLevel=10;
fishCapacity=10;
location=0;
}
public int getFuelCapacity()
{
return this.fuelCapacity;
}
public int getFuelLevel()
{
return this.fuelLevel;
}
public int getLocation()
{
return this.location;
}
//start the process of travel and determine what happens
////
public int Travel(int Fuel,int Location,int FuelCapacity) throws IOException
{
int FuelConsumed=0;
//Enable IO
BufferedReader MyInput=new BufferedReader (new InputStreamReader(System.in));
System.out.println("How many meters do you want to travel");
System.out.println("Please enter only in increments of 10m");
System.out.println("Enter in negative number to go west");
System.out.println("Please in positive number to go east");
int distance=Integer.parseInt(MyInput.readLine());
//if they go east
if (distance<0)
{
for (int x=0;x<=(distance/10);x++)
{
FuelConsumed++;
}
}
//if they go west
if (distance>0)
{
for (int x=0;x<=((distance*(-1))/10);x++)
{
FuelConsumed++;
}
}
Location=Location+distance;
int FuelLeft=Fuel-FuelConsumed;
//Checks which sceanrio it falls under and gives apporpiate output
if (FuelLeft==0 && Location!=100) //End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=0) ///End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=200) ///End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=300) ///End game
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (FuelLeft==0 && Location!=400) //stuck in ocean
{
System.out.println("Oh No your stuck in the middle of ocean with no fuel");
System.out.println("Hope you can survive the middle of ocean by youself");
System.out.println("GAME OVER");
distance=9999;
return distance;
}
else if (Location>400) //error catching (out of bounds)
{
System.out.println("You cant go out of bounds! Its restricted waters");
distance=0;
return distance;
}
else if (Location<0) //error catching (out of bounds)
{
System.out.println("You cant go out of bounds! Its restricted waters");
distance=0;
return distance;
}
else if (FuelLeft<(distance/10)) //error catching (trying togo father then the amount of fuel allows them too)
{
System.out.println("You dont have engough fuel to go there");
return distance;
}
else if (FuelLeft<((distance*(-1))/10)) //error catching (trying togo father then the amount of fuel allows them too)
{
System.out.println("You dont have engough fuel to go there");
return distance;
}
else
{
System.out.println("You travelled " + distance + "m");
return distance;
}
}
}
Boat boat= new Boat();
System.out.println("You have " +boat.getFuelLevel());
System.out.println("1 fuel goes 10m ");
System.out.println("The map");
System.out.println("Legend:");
System.out.println("- eligable fishing spot and is 10m each ");
System.out.println("# is a town");
System.out.println("East: ->");
System.out.println("West: <-");
System.out.println(" 1 2 3 4 5");
System.out.println(" |----------|----------|----------|----------| ");
System.out.println(" 0m 100m 200m 300m 400m");
boat.fuelLevel=5;
boat.location=100;
boat.fuelCapacity=10;
boat.Travel(boat.getFuelLevel(), boat.getLocation(), boat.getFuelCapacity());
Upvotes: 0
Views: 59
Reputation: 86
If the Fuel is not equal to 0 (ex. FuelLeft==0 && Location!=100
) then it will not trigger the end game event. You should change it so that it looks like this.
if (FuelLeft <= 0 && Location !=100)
This will make it so that you have the ability to run out of fuel and it know about it. When you are checking for the distance:
if (distance<0) //Change to if (distance <= 0)
and
if(distance>0) //Change to if (distance >= 0)
This should allow for the end GameOver to happen.
-EDIT-
I have looked at your code some more, and found that your problem lies in your input. Your input never affect your boat object. You will need to change your input method. Maybe by getting the input in from the main function instead of the boat object.
You also need to add a loop that will allow you repeat the process of "fishing" or "moving" because every time that you would run out of fuel, your "error checkers" are working because it will say that you do not have enough fuel to do that. Because it only runs one time, it is impossible for you to meet the required specifications because you cannot run out of fuel.
Last, you set the fuel to 5 already in your code, so your fuel will always be at 5 when you run the program. Here is what I would suggest doing:
make the input in the main function.
change your boat class to do something when you receive a certain value. (right now it does nothing if you were to JUST change the input into the boat object, you would have to add an argument or something so that it could track how far you want to move. You could do this by making a class variable that stores the values of each, and then the Boat object would return the values after it has moved.)
Break your code down into more pieces. It looks like you are trying to do too many things at once. Make it so that you have separate functions for each individual things. For Example, Travel should not necessarily be an int because you want it to move the boat. You also don't need all of those arguments in the Travel because they are already being stored in the boat variable.
Make sure that you understand that each time that you hit the run button, everything resets. That could also be why you always have the same amount of fuel. Make a loop around the input part (That would presumably be in the main function) and then again, make the boat functions do things based off of that.
I would show examples of all of my testing (that I did), but for me to solve your problem, I would have to redo pretty much the whole program of input and output, which would take too much time. But here is a suggestion to get you started:
public void Move(int distance) throws IOException
{
Travel( fuelLevel, location, fuelCapacity, distance); //distance is the distance
}
You could add this to get the input in your main function:
Scanner keyboard = new Scanner(System.in);
int distance = keyboard.nextInt();
boat.Move(distance);
Where the move function (above) would then move the boat, and change the class variable values to the right values. Then you could make a
do{
//display where the boat is with your picture
//get user input
}while(!gameOver);
Good luck in your game! and I hope that this helps!
Upvotes: 1
Reputation: 31
Is simple, in your code... boat.fuelLevel=5; boat.location=100; boat.fuelCapacity=10; This values are reasigned in consrructor with statics values.
Upvotes: 0