Question: I currently am working on a program, i had most of the code done except one part, and then new changed were made to the

I currently am working on a program, i had most of the code done except one part, and then new changed were made to the guidelines and i spent the last 8 hours trying to debug this program and write it correctly that i ruined it even more. I bought a chegg subscription solely because of this program. Please help me.

This is the guidelines for the program.

The Game:

The game you will implement is very simple and found in newspapers across the country. Your program will read in words from a file, scramble up the letters and then show the scrambled letters to the player. The player will then have 3 chances to guess each correct unscrambled word. If he/she succeeds, he/she wins the round - otherwise he/she loses and the program shows him/her the actual word. For each guess the program will show which (if any) letters in the player's guess that were in the correct locations. Play will continue until the player elects to quit or until no more words remain in the file. Statistics will be kept on the overall words attempted (by all players) and how many were correctly solved. These statistics will be shown to each player at the end of a game.

Details and Requirements:

Scramble Class:

Words will be accessed by the main program through a class called Scramble. This class is required to have the following:

- A constructor that takes the name of a text file as an argument. The file will be used as the source of the words, and should have one word per line. The constructor should open the file for reading but not actually read anything - that will be handled by the methods below.

- A String method called getRealWord(). This method will have 3 possible return results:

1) The next word in the file. This will occur if a word is left in the file and getScrambledWord() has been called since the most recent call to getRealWord().

2) The same word returned as in 1) above. This will occur if getRealWord() has already been called and then is called again before a call to getScrambledWord().

3) null. This will occur if 1) would apply except that no words remain in the file.

- A String method called getScrambledWord(). This method will have 2 possible return results:

1) A scrambled version of the most recent word returned by getRealWord(). It can be inferred from this requirement that two successive calls to getScrambledWord() will return the same scrambled word. The letters in the scrambled word must have been altered in some random way utilizing the Random class.

2) null. This will occur if no call to getRealWord() has ever been made on this object and any time after 3) has occurred for getRealWord() as explained above.

In order to demonstrate the correct functionality of your Scramble class I have provided a test program, ScramTest.java. This program should execute as is with your Scramble class and the output should be the same as that shown in file scramtest.txt (with the exception that the actual scrambles of the words may be different). Submit the ScramTest.java file with your other files so that your TA may test your Scramble class.

Results Class:

Results will be maintained by the main program through a class called Results. This class is required to have the following:

- A constructor that takes the name of a text file as an argument. The file will be used to store the results, and should have the following format:

ROUNDS

WINS

LOSSES

Each value is a single integer stored on a separate line in the file. ROUNDS is the total number of rounds played by all players of the scramble game. WINS is the total rounds won and LOSSES is the total rounds lost. WINS plus LOSSES should always equal ROUNDS. When the constructor is called these values should be read in and stored in instance variables.

- A void method called won() which increments the number of wins but does not update the file.

- A void method called lost() which increments the number of losses but does not update the file.

- A void method called save() which saves the data in the object back to the file.

- A String method called toString() which returns a nicely formatted String containing the current information in the Results object.

Main Program (Assig2):

Your main program must be called Assig2 and it should proceed in the following way:

- Welcome the player and ask his/her name (reading it in).

- Create the Scramble object using file "words.txt" and create the Results object using the file "results.txt".

- Play the game with the user as specified above, updating the Results object at the end of each round. For each round, your program should obtain both the real word (to test the user's answer) and the scrambled word (to show the user) as specified in the Scramble class above.

- Guesses should NOT be case sensitive, so, for example, a guess of "piRAtE" would match the word "pirate".

- Once the player quits or runs out of words, show him/her the overall results and save them back to the file.

Issues and Hints:

- You may not use any arrays (or ArrayLists or other similar types) in this program. They are not necessary and I want you to think about how to implement the program without using them. Some places you think you may need arrays (ex: in the Scramble class) do not actually require them (hint: use a StringBuilder)

- You will need to use files in several places in this program. Some of these files may have to be opened / re-opened several times in the course of a single execution of the program - this is fine. You can read from files in Java using the Scanner class - you simply need to pass a File into the constructor. You can write to files using a PrintWriter. However, there are some quirks to reading/writing text files in Java (related to exceptions that must be dealt with). See ex11.java, FileTest.java, FileTest2.java and Section 4.10 in your text for some help with file I/O in Java. In particular, note how IOException is dealt with in these handouts - you will need to deal with this exception in some way in your Scramble and Results classes.

- The words.txt file will be provided for you. Note, however, that this file could be changed and/or a different version of it could be used to test your programs. Your program should work for any properly formatted "words.txt" file.

- Create the "results.txt" file yourself using any text editor. Initialize it to three 0s, each on a separate line. When you submit your assignment, the "results.txt" file can be arbitrary, as long as it is correct.

Test your program thoroughly before handing it in. Make sure it functions as required in all cases. I have generated some sample output that may help you with this. See: a2out.txt. I may also put additional files online so be sure to check back regularly for updates.

Note that the words are obtained from the Scramble object in the order that they are stored in the file. This is fine and expected. Later we may consider improving this to randomize the word selection.

Don't forget your Assignment Information Sheet -- you will lose points without it. You do not have to use my .html file. If you prefer simply copy the contents into a .txt file.

Don't forget to comment your code - you will lose points without them.

I know at this point its probably reaching but can you also assist me in doing the extra credit?

Extra Credit Option (up to extra 10 points total):

Rather than keeping one results.txt file for all users, keep separate results for each user. Note that in order to do this the users must log in to the program in some way.

Assig2:

import java.io.*; import java.util.*;

public class Assig2 {

/* * @param args the command line arguments * @throws java.io.FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException, IOException { int rounds = 0, wins = 0, losses = 0, guesscount = 0; int realcount = 0, scramcount = 0; int looper = 0; char compscram, compguess; String realWord = ""; String filestuff = ""; char choice; Scanner inScan = new Scanner(System.in); System.out.println("Welcome to the Scrambler! Please enter your name: "); String name = inScan.next(); File scramfile = new File("words.txt"); Scanner scram = new Scanner(scramfile); //File resFile = new File("results.txt"); //Scanner result = new Scanner(resFile); //FileWriter fwriter = new FileWriter("results.txt", true); Scramble theScramble = new Scramble(); PrintWriter OutputFile = new PrintWriter(new BufferedWriter(new FileWriter("results.txt"))); Results obj2 = new Results(); do{ System.out.print( "You have three guesses to get the scramble Good Luck! "); realWord = theScramble.getRealWord(realcount, scramcount); String ScrambledWord = theScramble.getScrambledWord(realWord); scramcount++; System.out.print("Scrambled: " + ScrambledWord+" "); System.out.print("Your guess: "); String guess = inScan.next(); while(guesscount < 2) { if(guess.equalsIgnoreCase(realWord)) { System.out.println("Great job " + name + " You got it!"); obj2.won(wins, rounds); break; } else { System.out.print("Sorry, " + name + " that is not correct. "); System.out.print(" Here are the letters you got correct! "); int len = realWord.length(); if (len > guess.length()) len = guess.length(); for(int counter = 0; counter < len; counter++ ) { compscram = realWord.charAt(counter); compguess = guess.charAt(counter);

if(compscram != compguess) { System.out.print("_"); } else { System.out.print(compscram); System.out.print(""); } } guesscount++; System.out.print(" You have "+ (3-guesscount) +" guesses remaining "); System.out.print("Scrambled: " + ScrambledWord+" "); System.out.print("Your guess: "); guess = inScan.next(); } } if(!(guess.equalsIgnoreCase(realWord))) { obj2.lost(losses, rounds); } guesscount = 0; if(scram.hasNext()) { System.out.print("Would you like to play another round?"); choice = inScan.next(".").charAt(0); if(choice == 'n' || choice == 'N') { looper = 1; } rounds++; } else { obj2.save(wins, losses, OutputFile, rounds); filestuff = obj2.toString(filestuff); }

}while(looper != 1); filestuff = obj2.toString(filestuff); System.out.printf(filestuff); } }

Scramble:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Assig2; import java.io.*; import java.util.*;

/** * * @author j2956 */ public class Scramble {

public String getRealWord(int realcount, int scramcount) throws FileNotFoundException { File theScramble = new File("words.txt"); Scanner scram = new Scanner(theScramble); if(scram.hasNext()) { String realWord = (scram.nextLine());

return realWord; } else if(realcount > scramcount) { String realWord = scram.nextLine(); return realWord; } else return null; } public String getScrambledWord(String realWord) { if(realWord == null) return null; else { if (realWord.length()<=1) return realWord; int scramblifier=realWord.length()/2;

String temp1=getScrambledWord(realWord.substring(0,scramblifier)); String temp2=getScrambledWord(realWord.substring(scramblifier));

if (Math.random() > 0.5) return temp1 + temp2; else return temp2 + temp1; } }

}

Results:

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */

import java.io.*; import java.util.*;

/** * * @author j2956 */ public class Results { public void won(int wins, int rounds) { wins++; rounds++; } public void lost(int losses, int rounds) { losses++; rounds++; } public void save(int wins, int losses, PrintWriter outputfile, int rounds) { outputfile.println(rounds); outputfile.println(wins); outputfile.println(losses); outputfile.close(); } public String toString(String filestuff) throws FileNotFoundException { filestuff = new Scanner(new File("results.txt")).useDelimiter("\\Z").next(); System.out.printf("Rounds Tried: Rounds Won: Rounds lost:"); return(filestuff); } }

ScramTest:

import java.io.*; import java.util.*; public class ScramTest { public static void main(String[] args) throws IOException, FileNotFoundException { System.out.println("Testing normal use... "); Scramble theScramble = new Scramble("words.txt"); // Create Scramble object using the file name as an argument. Note // that this constructor does not actually read in any words. // Sentinel controlled loop. Keep getting words and showing them until returned // word is null.. String word = theScramble.getRealWord(); while (word != null) { System.out.println("Real word is : " + word); System.out.println("Scrambled word is: " + theScramble.getScrambledWord()); word = theScramble.getRealWord(); } System.out.println(" Testing special cases... "); theScramble = new Scramble("words.txt"); System.out.println("Initial call to getScrambledWord(): "); word = theScramble.getScrambledWord(); if (word == null) System.out.println("No scrambled word yet"); String word1 = theScramble.getRealWord(); String word2 = theScramble.getRealWord(); System.out.println(" Two calls to getRealWord(): "); System.out.println("Word 1: " + word1); System.out.println("Word 2: " + word2); System.out.println(" Two calls to getScrambledWord(): "); word1 = theScramble.getScrambledWord(); word2 = theScramble.getScrambledWord(); System.out.println("Scram word 1: " + word1); System.out.println("Scram word 2: " + word2); System.out.println(" Now a call to each: " ); word1 = theScramble.getRealWord(); word2 = theScramble.getScrambledWord(); System.out.println("Word: " + word1); System.out.println("Scram word: " + word2); } }

My main difficulties have been getting the strings to append to the document and print formatted correctly, and having scramtest cooperate correctly with the scramble class. Im sorry this is a long post and appreciate any help given.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!