Hi everyone,

I know you probably see a hundred of these during this time of year, but I have a project going at uni that requires us to create a string based hangman game from scratch. It's not anything fancy, and the game itself isn't too complicated, but where I'm having trouble is one, comparing a users input using a JOptionPane.showInputDialog. So the user enters a letter and this letter is compared with the String. If the user enters a letter that is in the String, the program replaces every "_" with the letter entered. I haven't really done all that much work with Strings or with any kind of loops, so I'm a little bit clueless.

Another problem I'm facing is that we are required to code 3 methods that the user has the option of running. These are:
removeDoubletons() //This method should remove any appearance of the same letter e.g. "Cool" would become "Cl"

randomMove() //This method returns a string where all characters in the source string are shifted to the right by a random amount between 2 and the length of the string.

removeVowels() //This goes through the string sequentially and every time a vowel is found, it is removed. I think I'll be ok with this method.

Here is a summary of my task

Player is asked to enter a string of text in a JOptionPane.showInputDialog

Once a string of appropriate length has been entered a dialog box should appear asking the user whether they want to run method1 (removeDoubletons)

If the user clicks Yes then the removeDoubletons method is performed on the input string of text.

Regardless of which button the user pressed another dialog box should then appear asking the user whether method 2, randomMove should be performed on the input string.

If the user clicks Yes then the randomMove method is performed on the input string of text.

Regardless of which button the user pressed another dialog box should then appear asking the user whether method 3, removeVowels should be performed on the input string.

Once the user has decided whether or not to run any of your string manipulation methods the game should run.

Basically, the rest of the task is to replace "-"s with correctly guessed characters and check any word guesses against the original string.

Now I know it probably looks like I'm asking you to do my homework, but I promise I'm not. I don't expect anyone to give me any solid code and a quick solution. What I would like is some sort of pointers, hints, classes to use that I can go ahead and research.

Really appreciate any help that you guys can provide me with!

Thanks,
Chris

Thanks, will definitely look in to those when I get a spare hour. I've looked at the java docs for the String class quite a bit, and it does help, but, being a complete novice, I find them hard to implement.

I'm a little way in to the removeDoubletons() method, and I've ran in to a problem. Here's my code below (I'm absolutely certain there are some ridiculous mistakes in there, sorry for that):

import javax.swing.*;

public class StringGame {
	private String wordInput;
	private String guessInput;
	
	public void removeDoubletons(String wordInput) {
		String newWordInput = null;
		for (int i=0; i<wordInput.length()-1; i++) {
			if (wordInput.charAt(i)==wordInput.charAt(i+1)) {
				newWordInput += wordInput.charAt(i);
					
				}
			}
		}
	
	public static void main(String[] args) {
	
	}

}

I don't understand a) whether my method will even work b) how to implement the method in to my main method.

I have still to create the dialog boxes in which the user will enter the String for wordInput.

Edited 6 Years Ago by ChrisXL: n/a

well it is almost correct. just put the newWordInput in an else statement like this

import javax.swing.*;

public class StringGame {
	private String wordInput;
	private String guessInput;
	
	public void removeDoubletons(String wordInput) {
		String newWordInput = null;
		for (int i=0; i<wordInput.length()-1; i++) {
			if (!(wordInput.charAt(i)==wordInput.charAt(i+1)||wordInput.charAt(i)==wordInput.charAt(i-1))) {			
				newWordInput += wordInput.charAt(i);
		}
	}
	
	public static void main(String[] args) {
	
	}

}

the if statement is what you need to understand, let's give an example. lets say we have the word cool.
when i=0: (cool.charAt(0)==cool.charAt(1)||cool.charAt(0)==cool.charAt(-1)) this would be false since c==o||c==null is false, but the ! is a not so it returns true and c is added to newWordInput.
when i=1; (cool.charAt(1)==cool.charAt(2)||cool.charAt(1)==cool.charAt(0)) this would be true since o==o||o==c is true, but because of the not it does not add it.
when i=2; (cool.charAt(2)==cool.charAt(3)||cool.charAt(2)==cool.charAt(1)) this would be true since o==l||o==o is true, but because of the not it does not add it.
when i=3; (cool.charAt(3)==cool.charAt(4)||cool.charAt(3)==cool.charAt(2)) this would be false since l==null||l==o is false, but because of the not it does add it.

the end result is cl.


and to implement it you just say removeDoubletons("cool");

Edited 6 Years Ago by Gerbiler: implementation

Cheers mate, that was really helpful. Will post an update when I've had time to work on it a bit more.

It sounds like you're asking for help on how to approach a problem of this sort - good man, that's the right sort of question to ask.

My usual advice for this sort of thing is to start by trying to perform the task on paper, and to come up with a set of steps that will do the work, assuming that the person doing the work has some serious memory damage, and can only remember the item they're looking at at the given moment, everything else must be written down. So for your remove doubletons method, write the word "balloon" at the top of a sheet of paper. On the left side of the paper, make a box for variables. Anything that you need to remember after you're not actually looking at it has to be entered here, with a name.
You'd start by looking at the first letter, 'b', and then what? I'd assume that it gets held somewhere while you look at the second letter, 'a'. Presumably, you compare them, and then what? What are the possible cases, and how do you respond to them? What do you do with the 'b' and the 'a' after the comparison? Move along in this manner, you'll sort it out.

For your shift right, it's a similar process, only you don't need to make so many decisions. You have to deal with the characters at the right end of the string - do they fall off the end, or do they wrap around? If they wrap, how do you do that? (Can you generalize this to shift by n characters? If so, do it - it's not actually a harder problem, and your professor will love it). You might want to allow yourself the possibility of not starting at the beginning of the source string - this might make it easier to build the target string.

(I'm not sure how either of these connects to the game of hangman, but they're worthwhile problems for learning to deal with Strings, in any case).

[EDIT - sorry, got called away for a meeting while typing this, and fell behind the thread, but maybe it's still useful, so I'll leave it]

Edited 6 Years Ago by jon.kiparsky: n/a

This article has been dead for over six months. Start a new discussion instead.