Word Guessing Game in Java
A long long time ago I used to prefer Java over Python or any other language for quick work, and I was also a member of the Computing Club at Grand Rapids Community College. From time to time they'd choose challenges from Reddit for fun, but I was the only one who even approached the "medium" or "hard" difficulty challenges. The conclusion they reached was that you can be a ridiculous programmer without a degree, and that being a hobbyist like me is a red flag. If you don't mind I'd like to take you down memory lane, and walk through the text game I quickly put together.
The biggest difference between Java and Python is that every program needs a defined class. The name of the class you use must be the same as the name of the filename. The easiest way I have come to think of a class over the years is as like a filing cabinet. You can define the characteristics or "fields" of a class, which are just variables you believe will be important for the class. For example, if your class was a human you could define its hair color, eye color, height, weight, and so on. Classes also let you define the methods associated with that class. For example, since humans can run you may want some method that lets humans move a little faster than walking. The reason you would use classes is that if you are working with a large project, like a video game, you don't have to write code for each individual item (e.g. gun, human, car, building). Unlike Python, Java also requires a main method.
Main methods are pretty common in programming languages, and they are basically a trigger for the compiler. I have heard conflicting ideas over the years regarding best practice for using main methods, but in general I would probably try to keep code within the main method to a minimum. A lot of the code I write for this web site is just sloppily put together coding solutions and proof of concepts, so it's fine. If you're really trying to show off with beautiful code though I highly recommend using more functions than I did. My main function uses Java Scanners to let the user both choose the difficulty of the word they want to attempt, and record their attempts. They get four tries regardless of attempt, and the difficulty of the word is determined by word length. There's only like five words per difficulty, and it's basically randomly chosen.
This next code segment uses a switch-case statement instead of an if-statement. In nature a switch-case statement is basically an if-statement that can only determine if two things are equal. You start out by telling the switch statement what variable you want to be the primary value being compared, and then by telling it what values it could equate to and what to do if it equates to those values. In the case of what to do when your variable equals a certain value is the case part. The benefit of a switch-case statement is that they are much faster than if-statements thanks to the hard work of Computer Engineers. From what I recall from college with switch-statements the computer goes straight to the relevant case instead of trying to compare your value against every possible branch, which is the behavior with if-statements.
The final portion of the code includes a dynamically sized static array. Doesn't make sense right? I know. One time an author of a book on C++ had the audacity to tell me that you cannot set or change the sizes of static arrays. You would think people would learn by now. A professor at an old university also once told me that Linux does not support RDP. I found an app on my Chromebook from Microsoft that suggested differently, and I'd remote into the servers on campus even though performance was bad just to prove a point. Anyway, if you wait long enough before you declare your array you can make sure it's large enough for your needs. Dynamic arrays are not always needed if you do not know the size of an array right off the bat.
The while loop is set to run while you have at least one guess left. It tells you how many letters you've gotten right and how many guesses you have left. Then it lets you make your guess, iterates through the word comparing each letter in your word with the word it chose for you to guess. It was supposed to only expose letters when they match up perfectly, but I think it's backwards. Once you have guessed all the letters in the word it tells you congratulations or tells you the word if you fail.
That is basically the breakdown of the code I gave the club 4 years ago. If I were to refactor this code I would write several functions within the class, and make it its own class. That way in the main function all I would have to do is use a class constructor, and maybe a while loop or something. The main method would definitely be far more compact and readable. Unfortunately, the best I can do is let you see the source code on Github, and a video demonstration of the app running below. Please keep in mind any bugs present are because we had a week to get through three challenges of varying difficulties.
I did my best to get through all three. I would offer a direct download for the script itself, but compiling Java applications are kind of a pain if you're not actively developing in Java. I only ever used it to prototype C++ applications or to write sloppy code quickly since memory management would not be a concern (unlike in OOP style C++).