7 Replies - 1942 Views - Last Post: 10 March 2013 - 03:43 PM Rate Topic: -----

#1 Matthias55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 13

Word Unscrambler game...Figuring out the Statements

Posted 09 March 2013 - 03:29 PM

Hey everyone! So I am very new to the Java language, and I am trying to figure out how to design and implement a word unscrambler game that reads a word from a text file, scramble it, and allow the user to switch the character indexes as they please...Here is my code.

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

public class Unscrambler
{
	//-----------------------------------------------------------------------------------------
	// This program reads a random word from a file created separately called words.txt that 
	// has been provided. The program then scrambles the word by swapping random letters a 
	// random number of times. The scrambled word is displayed with character indexes on top. 
	// The user selects 1 to swap a pair of letters, 2 to print the word unscrambled and quit, 
	// and 3 to simply quit. When the word is scrambled correctly, the computer congratulates 
	// the user, repeats the unscrambled word, and the number of steps it took to solve. An 
	// error message appears if invalid indexes are input by the user. 
	//-----------------------------------------------------------------------------------------
	
	public static void main (String[] args) throws IOException
	{
		//Setting Up Variables and Scanners to read from User and words.txt
		String words;
		Scanner fileScan, wordsScan, optionScan;
		int option; 
		boolean done = false;
		Random generator = new Random();
		
		fileScan = new Scanner (new File("words.txt"));
		words = filescan.nextLine(generator.nextInt(22);
		optionScan = new Scanner (System.in);
		
		while (!done)
		{
			//Menu
			System.out.prinln ("Welcome Computational Unscrambling!");
			//read from User
			option = optionScan.nextInt(3) + 1;
			
			if (option == 1)
			{
				//Allow user to switch letters between index; 
				//If User unscrambles word, done == true; 
			}
			else
			{
				if (option == 2)
				{
					//Print the unscrambled word and quit
				}
			}
			else
			{
				if (option == 3)
				{
					//Just quit the program
				}
			}
		}
	}
}	



Basically I think I have the structure set up....but I don't know how to scramble the actual words....I know I should use the random generator to switch the characters but I dont really have any clues as what else to do. I included the comments to enhance readability. If anyone could offer some suggestions that would be appreciated.Thanks everyone!

This post has been edited by baavgai: 10 March 2013 - 03:08 PM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: Word Unscrambler game...Figuring out the Statements

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Word Unscrambler game...Figuring out the Statements

Posted 09 March 2013 - 03:36 PM

You can scramble a word by putting it into a String[] array with String.split and calling Collections.shuffle(Arrays.asList(yourStringArray));

Investigate those classes and try to implement that.
Was This Post Helpful? 0
  • +
  • -

#3 Matthias55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 13

Re: Word Unscrambler game...Figuring out the Statements

Posted 09 March 2013 - 03:44 PM

View Postfarrell2k, on 09 March 2013 - 03:36 PM, said:

You can scramble a word by putting it into a String[] array with String.split and calling Collections.shuffle(Arrays.asList(yourStringArray));

Investigate those classes and try to implement that.


Unfortunately we have not learned arrays yet, although I do think they would solve this problem much easier. I only have been taught loops, conditionals, and the basics of the String class. Sorry for forgetting to mention that!
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,620
  • Joined: 29-July 11

Re: Word Unscrambler game...Figuring out the Statements

Posted 09 March 2013 - 04:10 PM

Then take the initiative and learn more. It's entirely too complicated without arrays.
Was This Post Helpful? 1
  • +
  • -

#5 Matthias55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 13

Re: Word Unscrambler game...Figuring out the Statements

Posted 10 March 2013 - 11:57 AM

I dont wish to bother anyone, but if anyone else could offer a suggestion, that would be very much appreciated. Its due tonight, and I think I might be able to figure it out if anyone could give me a hint.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Word Unscrambler game...Figuring out the Statements

Posted 10 March 2013 - 01:28 PM

optionScan.nextInt(3) won't even compile
Fix your compilation errors first
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,806
  • Joined: 16-October 07

Re: Word Unscrambler game...Figuring out the Statements

Posted 10 March 2013 - 03:15 PM

Use methods!

You can actually choose a random word from a file without an array. It's a little abstract...

You read your first word from the file. The chance of choosing that word is 1 in 1. The next word the chance of choosing it is 1 in 2, then 1 in 3, and so on. You call random based on the current count and if you get a zero, it's the winner. Read all the words in the file, applying this logic.

Each menu option should have a method.
Was This Post Helpful? 1
  • +
  • -

#8 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2066
  • View blog
  • Posts: 4,299
  • Joined: 11-December 07

Re: Word Unscrambler game...Figuring out the Statements

Posted 10 March 2013 - 03:43 PM

It sounds like an important part of your program is swapping letters. You can swap the letters of a String without using arrays, although it takes a few lines of code. Write a method with this signature:

public static String swapLetters(String jumbled, int indexOne, int indexTwo) {
    // Your code here
}


It takes the jumbled string and the two indexes and returns anohter string with the two letters swapped.

Write the code to do that in there and test it separately. Once you have done that, you will be able to use it in your overall program.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1