Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class WordGame {
- public static boolean isLetterInPlace(String answer, String guess, int position) {
- if (answer.charAt(position) == guess.charAt(position)) {
- //if the nth char of the "answer" string matches the nth char of the "guess" string, returns true, otherwise returns false
- return true;
- }
- else {
- return false;
- }
- }
- public static boolean isLetterInWord(String answer, char c) {
- int positionliw = 0;
- //position, liw is an abbreviation for letter in word
- final int LENGTH = answer.length();
- while (positionliw < LENGTH) {
- if (answer.charAt(positionliw) == c) {
- //if the char being checked in answer matches the char we are looking for from the guess, returns true
- return true;
- }
- positionliw++;
- //checks the next char of the answer for that same char
- }
- return false;
- //if it's nowhere to be found, returns false, the letter not being in the word at any position
- }
- public static int countLettersInPlace(String answer, String guess) {
- int position = 0;
- int lettersinplace = 0;
- final int LENGTH = guess.length();
- while (position < LENGTH) {
- if (isLetterInPlace(answer, guess, position)) {
- lettersinplace++;
- //adds +1 to the count of letters that are correctly placed
- }
- position++;
- //checks if the next letter is correctly placed
- }
- return lettersinplace;
- //returns number of correctly placed letters
- }
- public static int countLettersInWord(String answer, String guess) {
- int current = 0;
- //current char position
- int lettersinword = 0;
- final int LENGTH = guess.length();
- while (current < LENGTH) {
- char c = guess.charAt(current);
- //updates char every loop since we are changing the current position every loop (otherwise it would just check the same char for all 5 loops)
- if (isLetterInWord(answer, c)) {
- lettersinword++;
- //adds to count of correctly guessed (but possibly incorrectly placed) letters
- }
- current++;
- //checks next letter
- }
- return lettersinword;
- //returns number of correctly guessed (but possibly incorrectly placed) letters
- }
- public static boolean assessWord(String answer, String guess) {
- int lettersinplace = countLettersInPlace(answer, guess);
- //calls letter-in-place-counting method to check how many letters from answer and guess match up
- int lettersinword = countLettersInWord(answer, guess);
- //same thing only with letter-in-word-counting method
- System.out.println(guess +" has " +lettersinplace +" correctly placed letters");
- System.out.println(lettersinword +" of the letters in " +guess +" were also in the answer");
- if (lettersinplace == answer.length()) {
- return true;
- //lets runGame method know whether the player guessed the word correctly
- }
- else {
- return false;
- }
- }
- public static void runGame(String answer) {
- int loops = 0;
- Scanner reader = new Scanner(System.in);
- System.out.println("Type in your guess:");
- String guess = reader.next();
- boolean finishStatus = false;
- while (loops <= 12 && finishStatus == false) {
- //had to add a boolean to break the loop in the case of a successful guess
- if (assessWord(answer, guess)) {
- System.out.println("Congratulations, you guessed the word!");
- finishStatus = true;
- //changes boolean to "true" which makes it no longer eligible for the loop condition
- }
- else {
- loops++;
- System.out.println("Type in your guess:");
- guess = reader.next();
- //lets the user try again
- }
- }
- if (finishStatus == false)
- //if the user still hasn't guessed correctly in the last loop, prints out game over message
- {
- System.out.println("You lost :(");
- }
- }
- public static void main(String[]args) {
- Scanner reader = new Scanner(System.in);
- System.out.println("Please enter word");
- //input answer
- String answer = reader.next();
- runGame(answer);
- //run game based on this answer
- }
- }
Advertisement
Comments
-
- PLZ, do not write your Java code with an opening bracket on a new line.
- PLZ, do not write your Java code with an opening bracket on a new line.
- PLZ, do not write your Java code with an opening bracket on a new line.
- This may be a good practice in other languages like c#, but in Java, this is considered a GARBAGE CODE.
- This means that anyone who sees it will automatically close the file or even block you.
- Please do not pollute the code with mixed conventions. Either follow entirely the thrashy new line bracket pattern or just stick to the native Java approaches.
- But combining everything makes your code overrated even for a Thrash Can and it may be best to start learning the language anew from "A" to "Z", starting with how to write basic Java classes, methods, and variables.
- You may have to create the same class 100 times until your brain comprehends that brackets on new lines are not meant for Java.
- Repeat after me:
- 1. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
- 2. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
- 3. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
- 4. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
- You can continue yourself for homework later on - it looks like you need it considering the struggle to remember it.
- This is very serious and it is subject to penalties if it is being done on a production code. For example, I would have fired you immediately without even allowing you to say something - just GO AWAY!
-
- fixed it for you sweetheart
- check my recent assignment? :3
Add Comment
Please, Sign In to add comment
Advertisement