10 Replies - 952 Views - Last Post: 03 February 2010 - 12:43 AM Rate Topic: -----

#1 justin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Hangman type of game

Posted 02 February 2010 - 09:29 PM

import javax.swing.*;
import java.util.*;
class Chapter9_23{
	public static void main(String []args){
		String secretWord = JOptionPane.showInputDialog(null, "\tPlayer One\nInput Phrase For Player Two: ");
		
		
		String shuffleSecret = Shuffle.shuffle(secretWord);
		System.out.println("Hint : " + shuffleSecret);	
		
		int hint = JOptionPane.showConfirmDialog(null, "Player One\nDo You Want To Give A Hint?");
		if (hint == 0){
			String hintYes = JOptionPane.showInputDialog(null, "Player One\nInput Second Hint: ");
			System.out.print("Hint #2 : " + hintYes);
		}
		
		String playerTwo = JOptionPane.showInputDialog(null, "Player Two\nYou Have One Chance To Guess Correctly.\nInput Guess : \n\n*NOTE: CASE SENSITIVE!");
		if (playerTwo.equals(secretWord)){ 
			JOptionPane.showMessageDialog(null, "CONGRADULATIONS! YOU WON!");
		}
			else {
				JOptionPane.showMessageDialog(null, "GAME OVER!\nCorrect Answer Was : " + secretWord);
			}

		System.exit(0);
	}
}

import javax.swing.*;
import java.util.*;
class Shuffle{
static String shuffle(String secretWord){
	int wordAmt = secretWord.length();
	if (wordAmt <= 1){
	    return secretWord;
	}
	
	int arraySecret[] = new int[wordAmt];
	for (int i = 0; i < arraySecret.length; i++){
		arraySecret[i] = Math.random();
		}
	}
}



I'm struggling with my array. I need to come up with a game that allows a player to guess a secret word that has been shuffled or randomized. My problem is that i'm not sure how to randomize my secret word. My main problem lies in the for loop of the shuffle class. Any suggestions on how to finish my shuffling?

Is This A Good Question/Topic? 0
  • +

Replies To: Hangman type of game

#2 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman type of game

Posted 02 February 2010 - 11:08 PM

my suggestion is for every character in the string, generate a random number between 0 and the length of the string. that random number will be an index, so you can swap the character with the character in that random index..
Was This Post Helpful? 0
  • +
  • -

#3 justin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Re: Hangman type of game

Posted 02 February 2010 - 11:15 PM

yeah thats what i had in mind too, but im struggling with generating the random number in my for loop
is
arraySecret[i] = Math.random();
that correct?
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman type of game

Posted 02 February 2010 - 11:39 PM

View Postjustin23, on 03 February 2010 - 08:15 AM, said:

yeah thats what i had in mind too, but im struggling with generating the random number in my for loop
is
arraySecret[i] = Math.random();
that correct?


not exactly, well first of all, why do you have an array of ints??, what do you want to do with that?..

as for generating the random number..if you are using the random() method from the Math class
int randomInt = (int)(Math.random()*secretWord.length());



or you can just use the Random class which is easier to use
Random random = new Random();
int randomInt = random.nextInt(secretWord.length());



dont forget to import java.util.Random first though, if you are going to use the Random class..

did you understand how i generated the random integer?

This post has been edited by mostyfriedman: 02 February 2010 - 11:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 justin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Re: Hangman type of game

Posted 02 February 2010 - 11:43 PM

wouldnt I need the ints in order to swap them out with the characters in my string?
and thanks for the random number help
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman type of game

Posted 02 February 2010 - 11:45 PM

just to elaborate more..the way you were generating the random integers is not wrong but you are generating the random integers in the wrong range..Math.random() returns a random "double" between 0 and 1.0 so you had to do some extra work to make it generate random ints in the range you wanted
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman type of game

Posted 02 February 2010 - 11:53 PM

View Postjustin23, on 03 February 2010 - 08:43 AM, said:

wouldnt I need the ints in order to swap them out with the characters in my string?
and thanks for the random number help


not really, i don't really see it..you can swap the characters as you pass by them..my idea was to turn your string into a char array and swap the characters as you loop through the array, like this.

static String shuffle(String secretWord){
        int wordAmt = secretWord.length();
        if (wordAmt <= 1){
            return secretWord;
        }
        Random random = new Random();
       

        char arraySecret[] = secretWord.toCharArray();//toCharArray() is a method in the string class that turns a string into a character array
        for (int i = 0; i < arraySecret.length; i++){
            int randomInt = random.nextInt(secretWord.length());
            char temp = arraySecret[i];
            arraySecret[i] = arraySecret[randomInt];
            arraySecret[randomInt] = temp;         
        }
        return (new String(arraySecret));
}



Edit: forgot the return statement
this should do it now :)

This post has been edited by mostyfriedman: 02 February 2010 - 11:58 PM

Was This Post Helpful? 0
  • +
  • -

#8 justin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Re: Hangman type of game

Posted 03 February 2010 - 12:12 AM

oh i didnt know about the toCharArray method
thanks! but i got a "reached end of file while parsing" error
i've never seen this error before, any tips on how to fix it?
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman type of game

Posted 03 February 2010 - 12:19 AM

check out your braces, you probably have a missing brace or something like that
Was This Post Helpful? 0
  • +
  • -

#10 justin23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Re: Hangman type of game

Posted 03 February 2010 - 12:32 AM

haha wow that was really simple
thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman type of game

Posted 03 February 2010 - 12:43 AM

no problem, glad that i helped :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1