Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added farms (partial fix of the issue #86) #140

Merged
merged 9 commits into from
Feb 3, 2022
Merged

Conversation

R1ndT
Copy link
Contributor

@R1ndT R1ndT commented Dec 27, 2021

Hello,

I am adding farms with this pull request. I've got other types of properties - passive income properties, to be exact - in mind but I'll leave those for later. Please focus on the two comments in the code which I added. I'll try to highlight them using code comments.

Thanks in advance,
RindT

(#86)

Ui.print("(" + ((seed == null) ? "No seeds planted)" : seed.getName() + ") - "));
Ui.println((seed == null) ? "" : ((this.getRemainingTime() > 0) ? this.getRemainingTime() + " fights remaining until the harvest." : "Ready for harvest"));
Ui.println();
// Can you think of any way I could possibly make this work? I wanted to make the menu dynamic - display the harvest option only if the crops are ready for harvest - but I just can't come up with a universal and scalable way to solve this other than bunch of if-else statements, which is rather bulky and doesn't meet either specified requirement.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the first comment.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I couldn't think of a clean way of doing this without getting into some much more advanced stuff (a seperate helper class that can take an array of functions/items, and dynamically display them in order without any hard-coded number). I think a few if statements is good enough in this case. This is what I got (similar to yours): ``` boolean readyToHarvest = false;

print("1) Plant Seeds");
if (readyToHarvest) print("2) Harvest");
print(readyToHarvest ? "3" : "2" + ") Back");

switch (getValidInt()) {
  case 1:
    plant();
    return;
  case 2:
    if (readyToHarvest) {
      harvest();
      return;
    } else {
      print("quit!");
      return;
    }
  case 3:
    if (readyToHarvest) {
      print("quit!");
      return;
    }
  default:
    print("fallback");
}```

(Done in a sandbox test environment, would need tweaking to work in this codebase)


For the situation where there are an unknown amount of extra options, theres an example of how this could be done here:

for (int i = 0; i < Weapon.getWeapons().size(); i++) {

although its not really a clean solution either.

If there was to be some nice automated menu-builder classs, that should be done on a seperate PR/issue since it would be a pretty big project & would include converting all the old menus to use the new helper class

println("-".repeat(hrLength));
}

// This function will reliably centre solely strings of an even length. I don't know of a way to fix this without varying hrLength. Do you? Couldn't varying hrLength solve the issue reliably as well?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the second one.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is a way to solve this. A variable hr length could work in theory, but would probably be more of a hassle than its worth, as the printhr function would need to know the string length as well, which would be weird. it isn't really noticeable anyways so I wouldn't worry about it

@hhaslam11
Copy link
Owner

I'll take a look at this over the weekend! been busy over the holidays :) Both are interesting problems, but I think I can come up with solutions

@R1ndT
Copy link
Contributor Author

R1ndT commented Dec 31, 2021

Alright. Thanks. :)

Copy link
Owner

@hhaslam11 hhaslam11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty good! I'll loop back on those comments in the next day or two, will require a bit more testing, definitely not super trivial

import java.util.ArrayList;

public class Crop {
public static final ArrayList<Crop> cropArrayList = new ArrayList<Crop>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is implicit and not actually needed

Suggested change
public static final ArrayList<Crop> cropArrayList = new ArrayList<Crop>();
public static final ArrayList<Crop> cropArrayList = new ArrayList<>();

public class Crop {
public static final ArrayList<Crop> cropArrayList = new ArrayList<Crop>();

private String name;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these three variables can be final

}

if (firstInit) {
this.owned = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already set as 0 in the declaration on line 18

}
}

public void sell(int piecesSold){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void sell(int piecesSold){
public void sell(int piecesSold) {

Coins.set(totalPrice, true);
Stats.coinsGainedOnCrops += totalPrice;
this.owned -= piecesSold;
Ui.println("You have sold " + this.getName() + " (" + piecesSold + " bundles)" + " for " + totalPrice + " coins.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to do it this way, but an alternative could be

Ui.println(String.format("You have sold %s (%s bundles) for %s coins.", this.getName(), piecesSold, totalPrice));

}

if (firstInit) {
this.owned = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already set to 0 on declaration

@@ -1,11 +1,15 @@
package com.hotmail.kalebmarc.textfighter.main;

import com.hotmail.kalebmarc.textfighter.farm.Farm;
import com.hotmail.kalebmarc.textfighter.farm.Field;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

import com.hotmail.kalebmarc.textfighter.farm.Farm;
import com.hotmail.kalebmarc.textfighter.farm.Field;
import com.hotmail.kalebmarc.textfighter.farm.Seed;
import com.hotmail.kalebmarc.textfighter.farm.Crop;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

if (menuItem <= suitableFarms.size()) {
suitableFarms.get(menuItem - 1).menu();
}
return;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this return doesn't do anything, can be removed

@@ -112,6 +112,16 @@ public static void println() {
print("\n");
}

private static int hrLength = 66;
public static void printhr() {
println("-".repeat(hrLength));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.repeat requires java 11+, which is fine, but could you add a note in the README stating java 11+ is required? :) It's common for java developers to use much older versions like java 8, 9, etc

@hhaslam11 hhaslam11 merged commit 9088590 into hhaslam11:master Feb 3, 2022
@hhaslam11
Copy link
Owner

i accidently merged then unmerged this branch lol. If youre still interesting in having it merged, please fix the above comments and reopen this PR :) sorry about that

@R1ndT
Copy link
Contributor Author

R1ndT commented Feb 4, 2022

Hey, sorry. I've been quite busy with school stuff since the New Year. I'll take a look at it as soon as I have a bit of time.

@hhaslam11
Copy link
Owner

yep, no worries. No rush!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants