I'm struggling with dealing of inventory scan for my game, it basically search for the user inventory if "Flying Broom" if present(it was collected in another method and upload the code is too long), if not it will run the method challengedragon() again; else, it will proceed to the next challenge if the item is present.I was think of inserting method as parameter but it is not possible. This is what I have now. :
public class Main {
String Flyingbroom = "Flying broom";
public static void main(String[] args) {
Player_inventory p = new Player_inventory();
challengedragon();
}
public void challengedragon() {
System.out.println("a Hungarian Horntail dragon! Let's start the battle! You have four options to beat the dragon: ");
System.out.println("1: Fly away with your broom");
System.out.println("2: Fight the dragon");
System.out.println("3: Just run to the egg and get it");
System.out.println("4: Hide behind a rock");
System.out.println("5: Go back to Hogwart");
System.out.println("Your choice is: ");
Scanner in = new Scanner(System.in);
int dragonfightchoice = in .nextInt();
if (dragonfightchoice == 1) {
{
p.Scanitem(Flyingbroom,
"Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg",
"You dont have the broom. Try to search for the broom",
playerHP);
proceedtonextchallengelake();
} else if (dragonfightchoice == 2) {
System.out.println("The Hungarian Horntail dragon fired you. - 70HP. ");
playerHP -= 70;
challengedragon();
} else if (dragonfightchoice == 3) {
System.out.println("Bad idea... You lose 100 HP");
playerHP -= 100;
challengedragon();
} else if (dragonfightchoice == 4) {
System.out.println("The dragon found you. You lose 30 HP");
playerHP -= 30;
challengedragon();
} else if (dragonfightchoice == 5) {
Hogwart();
} else {
invalid();
challengedragon();
}
}
For my inventory class:
public void Scanitem(String item, String trueouputext, String textifconditionisnotmet) {
if (inv.contains(item) == true) {
System.out.println(trueouputext);
} else if (inv.contains(item) == false) {
System.out.println(textifconditionisnotmet);
}
public static ArrayList<String> inv = new ArrayList<String>();
Do you guys have any recommendation?
Are there additional steps to populate the inventory (variable inv)?
Also, wouldn't you want ScanItem to answer true or false, depending on whether the item was found? Then you would have something like this:
public boolean scanitem(String item) {
return ( inv.contains(item) );
}
if ( p.scanItem(flyingBroom) ) {
System.out.println("Good choice! You managed to kill the Hungarian Horntail dragon and to get the golden egg");
} else {
System.out.println("You dont have the broom. Try to search for the broom");
}
That will get you closer to what you want. However, there are two other issues which you'll need to put into your code:
You will need a loop of some sort, instead of calling challengeDragon from inside of itself.
Somehow, the return value from scanItem must be used to decide whether to loop.
Currently, you do a nested call of a method each time the player does something, this means that sooner or later you'll run out of the stack. A better idea for the framework for your text-based adventure is to have some kind of a description of the current game's state. The state could be represented as an object that contains the following information:
where's the player currently at (on which step, at which "crossing" etc.)
the player's stats (HP, available skills etc.)
the contents of the player's inventory
some previously made choices affecting the game
Then, the code could be written as a simple loop that does the following:
process player's input
change the state according to the player's input
present the player with available options according to the new state
wait for the next input
repeat
Related
I'm really scratching my heard on this one. I'm new at java, and I'm having the strangest thing happen.
It's homework and I'm taking it one step at a time. My issue is the loop just keeps going and stops asking for input, just keeps looping until it terminates. My comments are largely for myself. I tried to extract what was causing my problem and post it here.
Look at the "hatColor" switch, you'll notice the way I'm making sure the user enter only from the options I have allotted. Should I be using a exception handler or something?
Anyway, in short, the problem is that if I enter something with spaces, the loop skips asking for my next input. Like, if I entered "y y y y y " to the scanner when first prompted, the program will terminate and not give me the chance to enter something else.
Please, anyone that understands this, I would really appreciate your help.
import java.util.Scanner;
public class Testing
{
static String hatColor;
public static void main(String[] args) {
gameStart();
}
public static void gameStart()
{
Scanner userInput = new Scanner(System.in);
boolean keepLooping = true;
int loopCounter = 0;
System.out.println("The game begins. You must choose between 3 different colored hats."
+ " You can type white, black, or gray.");
while (keepLooping == true)
{
hatColor = userInput.next();
switch(hatColor)
{
case "white":
System.out.println("You have chosen the path of well intentioned decisions.");
walletDrop();
//the two items below are only there in case the wallet drop somehow completes without calling another method
keepLooping = false; // stops the while loop from looping again.
break; // breaks out of the switch
case "gray":
System.out.println("You have chosen the path of free will.");
walletDrop();
keepLooping = false;
break;
case "black" :
System.out.println("You have chosen the path of personal gain.");
walletDrop();
keepLooping = false;
break;
default : //we could just copy this default chunk for later switch statements
if (loopCounter >= 3)//end program on them
{
System.exit(0);
}
System.out.println("You didn't enter a usable answer. Try again");
loopCounter++;
if (loopCounter == 3)
{
System.out.println("This program will self destruct if you enter another invalid response.");
}
}//end of switch
}//end of while
}//end of game start method
public static void walletDrop()
{
System.out.println("wallet drop successful");
}
}
So I have actually solved this right after posting. In case someone else needs to look here for help:
The issue I was experiencing was due to using the scanner method
variableToAssign = scannerName.next();
instead of
variableToAssign = scannerName.nextLine();
I've a little problem.
Myself and a few friends were playing poker yesterday but we didn't have chips so I decided to start writing a program for that [Without Cards, just Chips].
In my code I have two main variables in the Game Object.
private int id;
private long bank;
I have a different file called Aside in which I can do different calculations.
In the code below I am trying to compare all instance bank variables to see if all the banks matched [In this case this will mean a new card can be drawn, otherwise users will have to keep to either raise or fold].
Is there a way of writing this in an easier term:
package poker;
import java.util.ArrayList;
public class Aside
{
public boolean compareBanks(ArrayList<Game> x)
{
ArrayList<Game> players = new ArrayList(x);
if(players.get(0).getBank() == players.get(1).getBank() && players.get(0).getBank() == players.get(2).getBank()
&& players.get(1).getBank() == players.get(2).getBank())
{
return true;
}
return false;
}
}
Later I use this here:
while(aside.compareBanks(players))
But the loop keeps going.
I'm fairly intermediate in programming so go easy on me with mistakes.
Thank you for your time.
P.S: This is NOT a code dump.
while(aside.compareBanks(players))
{
for(Game x : players)
{
if(x.hasPayedBid() == true)
{
System.out.println("Player : " + x.getName() + " [Call, Raise, Fold]:");
action = in.nextLine();
if(action.equalsIgnoreCase("call"))
{
break;
}else if(action.equalsIgnoreCase("raise"))
{
System.out.println("How much are you raising? $");
int raise = in.nextInt();
table += raise;
x.raise(raise);
}else
{
x.fold();
}
}
}
in.nextLine();
for(Game x : players)
{
System.out.println(x.toString() + "\n");
}
}//End While
You can do it using java-8 Stream API something like this
return players.stream().allMatch(p -> p.getBlank().equals( players.get(0).getBalnk()))
However if you will use while(aside.compareBanks(players)) and all elements of the list have equal blank value, your while loop will never stop. It is the same as while(true). So in this case you probably need to use if(aside.compareBanks(players)) or in case of equal blank values change them.
Try this
long bankValue=0;
For(Game player: players){
bankValue+=player.getBank();
}
if(bankValue==(players.get(0).getBank()*players.size)){
return true;}
else return false;
I have a project for my computer science class and we're making battleship. Part of the program is that we have make sure that the piece the player puts down does not go off of the board.
I've made a method to check to see whether it goes off the board:
private static boolean test(String s, int row, int column,int spaces)
{
if(s.equals("right")&&column+5<=10)
{
return true;
}
if(s.equals("up")&&row-spaces>=0)
{
return true;
}
if(s.equals("left")&&column-spaces>=0)
{
return true;
}
if(s.equals("Down")&&row+spaces<=10)
{
return true;
}
return false;
}
But once I've gotten it to print out an error message, I'm not sure how to make it so that the program can re-recieve the new position for the piece, without putting an if statement in and if statement in an if statement (and on and on), because you need to check the new position to make sure it doesn't go off of the board.
Here is the part where I get the position of the playing piece (although I don't think you need it)
Scanner sonic= new Scanner(System.in);
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
int beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
int begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
String direction = sonic.next();
And here's one of the if statements that I use to check/place the pieces
if(direction.equals("left")&&test("left",beginrow,begincolumn,5))
{
for(int i = beginrow; i>beginrow-5; i--)
{
battleship[begincolumn-1][i-1] = ('a');
}
}
else if(!test("left",beginrow,begincolumn,5))
{
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
}
This may be a duplicate, but I didn't know how to reword my search to find what I wanted. (So if anyone could direct me to the right article, that'd be nice as well)
What you should do is split your code appropriately into methods and call that methods repeatedly until your program is satisfied with the outcome.
For example:
create a method startGame() which has the job call methods getting user input until satisfied
make a method to request the user to input all the different ships and other required data
That might look something like
public void startGame() {
// do some setup
while(!requestShipInput()) { // request ship data until the data is valid
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
}
// do some more ship setup
// get the actual playing started
}
public boolean requestShipInput() {
Scanner sonic= new Scanner(System.in);
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
int beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
int begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
String direction = sonic.next();
if(direction.equals("left")&&test("left",beginrow,begincolumn,5)) {
for(int i = beginrow; i>beginrow-5; i--) {
battleship[begincolumn-1][i-1] = ('a');
}
return true; // valid ship data
}
return false; // invalid ship data
}
As a first step, separate input validation from taking the action based on that input - you already have the validation logic in a separate function, so this is easy. Then figure out what needs to be done in case of invalid input - in your case, you need to ask for new input until you get a valid position:
do {
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
direction = sonic.next();
} while (!test(direction, beginrow, begincolumn, 5))
After that, you know you've got a valid position.
My next step would probably be to group the information required to describe a ship on the board (i.e. beginrow,begincolumn,direction, probably also size) in a separate Object - possibly named Ship.
I think you could pretty naturally use recursion here:
public void getInput() {
// scanner code to get input
if (!test("left",beginrow,begincolumn,5)) { // test failed
getInput()
return
}
// test succeeded, continue
}
You already have something to the limits of you board? If you execute the check first, you don't need to execute a cascade of if-else
if(!test(direction,beginrow,begincolumn,size))
{
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
} else {
// check for collision with already placed ships
}
Keep in mind that there is a chance to combine up/down and left/right. The calculation rules are nearly the same and you only have to decide if you have to look to the one or the other direction.
We are making a simple rpg game. Right now we have a class called battleground, where the player can fight monsters. We use a random generator to pick out a random monster when we start the fight. In the terminal/main method, we have commands like "attack" and "run", that will either do damage to the random monster, or make the player leave/quit the game. Right now, we are trying to add a command called "attack scariest", which will let the player fight against the hardest monster with the most damage(there are three to choose from in our main). We need a method to choose a specific object from the ArrayList monsters, based on damage. Does anyone have tips on how we do that?
This is our code in the Battleground class that starts the game:
public void startBattle() {
printWelcomeMessage();
boolean finished = false;
this.currentMonster = getRandomMonster();
if(this.currentMonster == null) {
return;
}
System.out.println("A random monster is chosen for you. Prepare to meet the mighty " + this.currentMonster.getName());
System.out.println("\n-------- Player Stats ---------");
System.out.println(this.player);
while(!finished && monsters.size() > 0) {
ArrayList<String> commands = reader.getInput();
if(isValidCommand(commands)) {
if(commands.contains("quit") && !this.currentMonster.isDead()){
System.out.println("You can't quit the game in the middle of a fight!");
}else if(commands.contains("quit") && this.currentMonster.isDead()) {
finished = true;
System.out.println();
printFinalStats();
System.out.println();
System.out.println("You have left the arena, and the game has ended.");
System.out.println();
}
if(commands.contains("run")) {
finished = true;
System.out.println("You are a coward and you lose 50 gold pieces...\n");
this.player.changeGold(-50);
printFinalStats();
System.out.println("\nThanks for playing...");
}else if(commands.contains("drink") && !this.currentMonster.isDead()){
Potion potion = (Potion) player.findItem(commands.get(1));
player.drinkPotion(potion);
}else if(commands.contains("equip") && !this.currentMonster.isDead()){
Weapons weapon = (Weapons) player.findItem(commands.get(1));
player.useWeapon(weapon);
} else if(commands.contains("attack") && !this.currentMonster.isDead()) {
if(this.player.attack(this.currentMonster)) {
playerWon();
if(this.monsters.size() > 0) {
System.out.println("\nThere are " + this.monsters.size() + " more monsters to beat\nType \"attack\" if you want to attack another monster, or \"quit\" if you want to end the game.");
} else {
System.out.println("\n\n#### Congratulations ####\nYou have beaten every single monster in the game. You are a true champion!");
printFinalStats();
finished = true;
}
} else if(this.currentMonster.attack(this.player)) {
printLosingMessage();
finished = true;
}
} else if(commands.contains("attack") && this.currentMonster.isDead() && this.monsters.size() > 0) {
this.currentMonster = getRandomMonster();
printContinueMessage(this.player, this.currentMonster);
this.player.changeHealth(50);
}
} else {
System.out.println("Please write a valid command. Valid commands are:");
printCommands();
}
}
}
This is the ArrayList of monsters in the main class:
Monster beelzebub = new Monster("Beelzebub", 60);
Monster witch = new Monster("Witch", 40);
Monster ogre = new Monster("Ogre", 80);
ArrayList<Monster> monsters = new ArrayList<>();
monsters.add(beelzebub);
monsters.add(witch);
monsters.add(ogre);
Battleground battleground = new Battleground(monsters, player, reader);
battleground.startBattle();
We appreciate any help!
Replace the ArrayList with a SortedSet or a PriorityQueue. Implement a Comparator for Monster class. Then just pick the first element of the monsters collection.
I can't see your Monster class but I'm assuming the second variable to the constructor is the 'damage level'? If not, ideally this should be something that belongs to each monster so you should be setting it somewhere.
So you have the array Monsters which has all the monsters with various levels of damage already. There are a lot of ways you could do this, but a simple way (given you only have 3 monsters) is to just iterate over your monsters and keep track of the monster with the highest damage, and then return that monster.
For example:
Monster findHardestMonster(ArrayList<Monster> monsters)
{
//Set to the first monster in the list ASSUMING you have
//at least one monster
Monster highestDamageMonster = monsters.get(0);
//Go through every monster in your array
for (Monster monster : monsters)
{
//record the highest so far
if(monster.getDamage() > highestDamageMonster.getDamage())
highestDamageMonster = monster;
}
}
//return it
return highestDamageMonster;
}
If you plan on removing monsters frequently (say, when they're defeated), it might be worth as another poster suggested to use a priority queue or some sort of ordered collection whereby the ordering is based on damage level instead. Then you won't have to iterate over the monsters every time to find the one with max damage.
for(int i=0;i<monsters.size()-1;i++){
Monster m=monsters.get(i);
Monster m2=monsters.get(i+1);
if(m.getDamage()<m2.getDamage()){
Monster rightMonster=m2;
}else{
Monster rightMonster=m;
}
}
This should work if i am not mistaken. i havent tried the code but maybe it gives you hints
The stream operations you can perform on the list will let you do this easily, by using max with a Comparator that ranks the monsters according to damage.
Comparator<Monster> orderByDamage =
(m1, m2) -> m1.getDamage() < m2.getDamage() ? -1
: m1.getDamage() == m2.getDamage() ? 0 : 1;
What this does is compare the damage of two monsters (m1 and m2). If the damage of m1 is less than that of m2, a negative number is returned. If they are equal, zero is returned and if m1 has more damage than m2, a positive number is returned.
Note: if getDamage() returns an Integer and not an int, replace m1.getDamage() == m2.getDamage() with Objects.equals(m1.getDamage(), m2.getDamage()).
Use this with your list to get the monster with the most damage:
Optional<Monster> scariest = monsters.stream().max(orderByDamage);
Note the return type, which is Optional<Monster>. If the list is not empty, the Optional will contain the scariest monster; if the list is empty, the optional will be empty - this will help avoid a NullPointerException if all monsters have been removed.
scariest.ifPresent(monster -> /* do something to the monster */);
You can also use this approach to get the monster with the least damage:
Optional<Monster> leastScary = monsters.stream().min(orderByDamage);
I'm create an air traffic control system, where it generates a random amount of fuel and also a random amount of planes. I have these two elements done, however the issue is, in the output box it all shows and works perfectly. My issue is that I'm telling it, if there is no planes coming in to call the string no planes and if there is a plane then KLM. I can't get it to write to the main class with all the front end.
I've edited out some of the coding in the screen as I'm using Netbeans drag and drop front end:
enter public void StartSimulation()
{
Start pointer = new Start();
Plane incoming = new Plane();
//Needs a condition in here that checks if the plane and fuel has been
//Generated and also loop to keep up with the constant generated planes and fuel
jTextArea1.setText(incoming.planeName);
I have tried where the condition thing is the following:
if (incoming.nextPlaneLanding != 167) this generates the first thing over and over again in the output box. I've also tried setting a boolean in the plane class but again, it has had no effect even with the following condition around it. if (incoming.completed = true)
This is the stuff I have in my plane class:
class Plane
extends TimerTask
{
public int nextPlaneLoop = 0;
public int planes;
public int fuel;
public String planeName;
#Override
public void run()
{
if(nextPlaneLoop <=167)
{
//Currently only running 1 or 0 planes...
planes = (int) (Math.random()*((2-1)+1));
System.out.println("Random generated plane amount: "+planes);
System.out.println("Method called, one whole day loop");
//Adds to the plane in the airspace loop
nextPlaneLoop++;
System.out.println("Loop incrementing: "+nextPlaneLoop);
if(planes == 0)
{
System.out.println("No fuel is required as no planes are coming in");
planeName = "No incoming plane";
System.out.println("Planes name is: "+planeName);
System.out.println(" ");
}
else
{
//Amount of fuel
fuel = 30 + (int)(Math.random()*((120-30)+1));
System.out.println("Random fuel: "+fuel);
planeName = "KLM AirFrance";
System.out.println("Planes name is: "+planeName);
System.out.println(" ");
}
}
else
{
this.cancel();
System.out.println("Not in loop any more. End of day");
}
}
}
Can anyone suggest have to how to get the names to display on to the screen so I can then try and add them into a queue in the actual airport class.
I think the Observer pattern might work well for you. In Java, this is implemented via Observer and Observable.