-
Notifications
You must be signed in to change notification settings - Fork 4
fix/spell_delay #21
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
base: menu
Are you sure you want to change the base?
fix/spell_delay #21
Changes from all commits
8b002b9
764dce8
e2d5fcd
ddb7f17
e582ae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| public class ConsoleOutputHandler{ | ||
|
|
||
| private static final long SPELL_DELAY = 15; | ||
| private Object ConsoleOutputHandler; | ||
|
|
||
| private void ConsoleOutputHandler(){ | ||
| //this class is intended to be static and not instanced. | ||
|
|
@@ -13,7 +14,13 @@ private void ConsoleOutputHandler(){ | |
| /** | ||
| * output the provided string in one line, character by character | ||
| */ | ||
| public static void spell(String input) { | ||
|
|
||
| public ConsoleOutputHandler newOutput(){ | ||
| ConsoleOutputHandler = new ConsoleOutputHandler(); | ||
| return this; | ||
|
|
||
| } | ||
| public ConsoleOutputHandler spell(String input) { | ||
| for(int i = 0; i < input.length(); i++) { | ||
| System.out.print(input.charAt(i)); | ||
| try { | ||
|
|
@@ -24,17 +31,28 @@ public static void spell(String input) { | |
| Thread.currentThread().interrupt(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think that interrupting here is correct? What happened with the SPELL_DELAY? I can't see it being used anywhere.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused. I didn't write 'Thread.currentThread().interrupt();', it was already there. Is the SPELL-DELAY not used when the 'spell' method is called in the New Game View?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is the idea. The delay needs to be used in the spell method, to delay every character that is being displayed, to simulate a typing effect. Interrupt() there is a mistake, you need to replace it with something that will achieve that typing effect. Read a little bit about what threads are in programming, and how to do some basic operations with them. Hint: delaying execution of code.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok thanks, I'll look into it. I'm just letting you know that I did not put Interrupt() there. I did not write any of that code, so i didn't want to go about deleting it.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I can see that you didn't do it. I'm not sure why it's there though, it doesn't make much sense. So we should probably fix it.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey this hasn't changed since we last reviewed it? It's there to handle the Interrupted exception if thrown by the .sleep above.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh my god thanks for making me look into it. Github had folded the code for me and I totally missed the sleep. @AdamPhilipSmith You don't have to do anything about this, it's working, I'm just blind. @liam027 How does does interrupting the thread handle the exception? Isn't the exception happening because the thread was already interrupted?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my limited understanding of exception handling and threads, it goes like this: if Interrupted while sleeping the thread gets an Interrupted flag and throws the exception. Calling interrupt() on it actually clears (in this case I think toggles) the flag and it is nolonger set to "interrupted" and will just continue what it was doing.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your understanding is correct. But isn't that the same as just ignoring the exception? I'm not sure that anything changes, with or without that line of code. I don't think that flag is ever checked internally, if I recall correctly. The interrupt flag is there for people to use it to handle such interruptions. For example, if you're saving the game in a save file, and the thread gets interrupted, you should probably delete the half completed file before closing the thread, if the file is gonna be useless without it being fully finished. Basically, the interruption mechanism is there to allow you to gracefully finish your threads halfway through, to avoid the risk of your code breaking in weird ways. |
||
| } | ||
| } | ||
| return this; | ||
| } | ||
| /** | ||
| * output the provided string in one line | ||
| */ | ||
| public static void post(String input) { | ||
| System.out.println(input); | ||
| public ConsoleOutputHandler post(String input) { | ||
| System.out.println(input); | ||
| return this; | ||
| } | ||
| /** | ||
| * output a line break | ||
| */ | ||
| public static void lineBreak() { | ||
| System.out.println(); | ||
| public ConsoleOutputHandler lineBreak() { | ||
| System.out.println(); | ||
| return this; | ||
| } | ||
|
|
||
| public void print(){ | ||
| try { | ||
| finalize(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a slight misunderstanding. :D Calling the method finalize() is in most cases is very bad. You can read about it here. https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html#finalize() What I meant in general is to do any final steps, if there are any, in the process. I dunno what that could be in this particular case. But for example, you could store all of the strings sent through post() in one variable, and on print() you can print it all at once, to sliiightly improve performance, because system.out.println can be considered a slower method than others. Think about it first though! ;) This is a very cool coding exercise, because whenever spell() is called, it will mess up the order of the messages, if you store all of the posts and print them at once in the end. |
||
| } catch (Throwable throwable) { | ||
| throwable.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,12 +12,16 @@ public NewGameView(NewGameViewModel viewModel) { | |
| public void renderNewGameScreen() { | ||
| String startGamePrompt = String.format("* Start Game - %s ", NewGameCommandHandler.COMMAND_START_NEW_GAME); | ||
| String backPrompt = String.format("* Back - %s ", NewGameCommandHandler.COMMAND_BACK); | ||
| ConsoleOutputHandler.lineBreak(); | ||
| ConsoleOutputHandler.spell("-- GAME SETUP --"); | ||
| ConsoleOutputHandler.lineBreak(); | ||
| ConsoleOutputHandler.post(startGamePrompt); | ||
| ConsoleOutputHandler.post(backPrompt); | ||
| ConsoleOutputHandler.lineBreak(); | ||
| ConsoleOutputHandler.post("Please enter your command:"); | ||
|
|
||
| ConsoleOutputHandler console = new ConsoleOutputHandler(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you try to make it so that you don't have to write the keyword
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so yeah, this is what I meant when I said I know some of this stuff I did was kind of a work around haha. I'm yet to fully get my head around static and non-static in Java. I cannot seem to do what you suggest without making the newOutput() method static. However, if I do make it static then I can no longer use ConsoleOutputhundler as a return value in it. This was the only way I could find to get it to work...
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's nothing wrong with making the method static. That is what we have to do, to get that fluent interface. The starting method has to be static, otherwise it can't be called without making an object. What is stopping you from using ConsoleOutputhundler as a return value?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As soon as I make the newOutput() method static it comes up with an error on the return value saying it cannot be referenced. I think I've fixed it though by creating a new ConsoleOutputHandler inside the method.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, that's the goal! |
||
| console.newOutput() | ||
| .lineBreak() | ||
| .spell("-- GAME SETUP --") | ||
| .lineBreak() | ||
| .post(startGamePrompt) | ||
| .post(backPrompt) | ||
| .lineBreak() | ||
| .post("Please enter your command:") | ||
| .print(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.