DOC PREVIEW
OSU CSE 1223 - PRO12A

This preview shows page 1 out of 3 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

project12a A program that plays simple word guessing game In this game the user provides a list of words to the program The program randomly selects one of the words to be guessed from this list The player then guesses letters in an attempt to figure out what the hidden word might be The number of guesses that the user takes are tracked and reported at the end of the game See the write up for Project 12 for more details author package project12 import java io File import java io FileNotFoundException import java util ArrayList import java util List import java util Random import java util Scanner public class Project12 public static void main String args throws FileNotFoundException Scanner scanner new Scanner System in System out print Enter a filename containing your wordlist File file new File scanner next Scanner fileScanner new Scanner file Scanner inScanner new Scanner System in ArrayList String list new ArrayList ArrayList String historyList new ArrayList list getList fileScanner System out println Read list size words from the file n while true String solution getRandomWord list String currentGuess starWord solution char guessChar boolean flag true while flag System out println nThe word to guess is modifyGuess guessChar solution currentGuess System out println Previous characters guessed historyList toString guessChar getCharacterGuess new Scanner System in historyList add Character toString guessChar ArrayList Character charList new ArrayList Character for char c solution toCharArray charList add c if checkInList guessChar charList currentGuess modifyGuess guessChar solution currentGuess System out println nThe word to guess is now currentGuess System out print Enter your guess if checkWord inScanner next solution currentGuess solution break else System out println That is not the correct word if guesssComplete currentGuess flag false continue System out println Congratulations solution toUpperCase is the correct word System out println You achieved the correct answer in historyList size guesses System out print Would you like a rematch y n inScanner new Scanner System in if inScanner next equals y historyList new ArrayList continue else break System out println nThanks for playing Goodbye private static ArrayList String getList Scanner inScanner ArrayList String list new ArrayList String while inScanner hasNext list add inScanner nextLine toLowerCase return list private static boolean checkWord String guess String solution guess guess toLowerCase if guess equals solution return true else return false private static String getRandomWord ArrayList String inList Random ran new Random return inList get ran nextInt inList size 1 private static char getCharacterGuess Scanner inScanner while true System out print Enter a character to guess String input inScanner next input input toLowerCase if input length 1 return input charAt 0 else System out println Error Enter a single character private static boolean checkInList char inChar ArrayList Character inList for int i 0 i inList size i if inList get i equals inChar return true return false private static String starWord String inWord StringBuilder outputBuffer new StringBuilder inWord length for int i 0 i inWord length i outputBuffer append return outputBuffer toString private static int checkChar char guessChar String guessWord int count 0 for int i 0 i guessWord length i if guessWord charAt i guessChar count return count private static String modifyGuess char inChar String word String currentGuess StringBuilder newWord new StringBuilder currentGuess for int i 0 i word length i if word charAt i inChar newWord setCharAt i inChar return newWord toString private static boolean guesssComplete String word for int i 0 i word length i if word charAt i return false return true


View Full Document

OSU CSE 1223 - PRO12A

Documents in this Course
PRO11A

PRO11A

4 pages

PRO10A

PRO10A

3 pages

PRO9A

PRO9A

3 pages

PRO8A

PRO8A

3 pages

PRO7A

PRO7A

2 pages

PRO6A

PRO6A

2 pages

PRO4A

PRO4A

3 pages

PRO3A

PRO3A

3 pages

CL13A

CL13A

2 pages

CL11B

CL11B

2 pages

CL11A

CL11A

2 pages

CL10A

CL10A

2 pages

CL5A

CL5A

2 pages

Load more
Download PRO12A
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view PRO12A and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view PRO12A and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?