Hi,
I have a Uni assignment which requires me to make a Hangman program without using arrays.
I have done this so far:

//class Hangman

public class hangman
{

	private int maxTries=10; //variable to coutn amount of letter guessess
	private int wordLength;
	
	
	
	//Retrieves word from words.txt file
	
	Scanner fileScan =new Scanner(new FileInputStream("words.txt"));
	String secretWord = fileScan.next();
	
	//Creates a StrungBuffer for viewing the letters to be guessed
	
	StringBuffer wordLength =new StringBuffer();
	for(int i=0; i= <= secretWord.length(); i++)
		word.append("_");
		System.out.println(wordLength);
		
		
	//Creates a StringBuffer to display the guessed letters
	
	StringBuffer guessedLetters =new StringBuffer();
		System.out.print("Enter a character?");
		Scanner inScan = new Scanner(System.in);
		letter = inScan.next();
		guessedLetters.append(letter + " ");
		
	while(maxTries > 0)
	{
	System.out.println("The letters that you have guessed are: " + guessedLetters);
	if(secretWord.indexOf(letter) != (-1))
	{
		secretWord.indexOf(letter);
		System.out.println("correct");
	}
		else
			maxTries--;
			System.out.println("You have " + maxTries + "guesses to go.");
	}

But i'm struggling to display the guessed letters in terms of "_a__v____er" format. If anyone knows an easy way to do this or can post an old hangman assignment you've done i'd greatly appreciate it.
Thanks in advance

You could just create your own collection-type, such as a linked list or a tree, to store your information.

Edit: Also I think a StringBuffer is backed by an array for random-access character retrieval.

Sorry i'm a bit of an amateur at Java programming so i'm not exactly sure what your post meant. Is there a way i could do it using StringBuffer methods as that is one of my assignment requirements? i just need to somehow replace for example
_ _ _ _ _ _(street) and s is guess with
s _ _ _ _ _

Thanks for your speedy reply, appreciate it

Here's an example of how you can store things in your own collection type.

However, if your assignment REQUIRES you to use something else, that's another story.

In any case, consider the following example code--

public class HangMan{



	public static void main(String... args){

		HangMan hg = new HangMan();
		HangMan.ReverseList<String> myList = hg.new ReverseList<String>();

		myList.add("least");
		myList.add("not");
		myList.add("but");
		myList.add("Last,");

		System.out.println(myList);

		System.out.println(myList.atIndex(2));
		myList.remove(2);
		System.out.println(myList);
		System.out.println(myList.atIndex(2));
	}

	public class ReverseList<T>{

		public class Node<T>{
			Node<T> next = null;
			T data = null;

			Node(T type){ data = type; }
			Node(T type, Node<T> other){ data = type; next = other;}
			T getType() {return data;}
			void setType(T type) {data = type;}
			Node<T> getNext() {return next;}
			void setNext(Node<T> other) {next = other;}
			@Override public String toString() {return (data != null) ? data.toString() : "<null>";}
		}

		Node<T> root = null;

		void add(T data){ root = new Node<T>(data, root); }

		boolean remove(int index){
			boolean successful = false;
			if(index >= 0){
				int count = 0;
				Node<T> previous = lookup(index - 1);
				Node<T> future = lookup(index + 1);

				if(index != 0 && previous != null){
					previous.setNext(future);
					successful = true;
				}else if(index == 0 && root != null){
					root = root.getNext();
					successful = true;
				}
			}

			return successful;
		}

		private Node<T> lookup(int index){
			Node<T> temp = root;
			if(index >= 0){
				int count = 0;

				while(count != index && temp.next != null){
					temp = temp.getNext();
					count++;
				}
			}
			return temp;
		}

		T atIndex(int index){
			Node<T> temp = lookup(index);
			return (temp != null) ? temp.getType() : null;
		}

		@Override public String toString(){
			String temp = "";

			Node<T> temp2 = root;

			while(temp2 != null){
				temp += temp2.toString() + " ";
				temp2 = temp2.getNext();
			}

			return temp;
		}
	}
}

thanks for your reply again. my assignment is more focused towards writing a class and creating an object based on the class apposed to the origin of the actual word(it can just be hard coded or user inputed). I have worked on the class objects some more.

//class Hangman

public class hangman
{

	private int maxTries=10; //variable to count amount of letter guessess
	private int wordLength;
	private string hangWord="ready";
	private string blankLetter="";
	
	
	//Creates a StrungBuffer for viewing the letters to be guessed
	
	StringBuffer word =new StringBuffer();
	for(int i=0; i <= hangWord.length(); i++)
		blankLetter.append("_");
		System.out.println(blankLetter);
		
		
	//Creates a StringBuffer to display the guessed letters
	
	StringBuffer guessedLetters =new StringBuffer();
		System.out.print("Enter a character?");
		
		guessedLetters.append(letter + " ");
		
	while(maxTries > 0)
	{
	System.out.println("The letters that you have guessed are: " + guessedLetters);
	if(hangWord.indexOf(letter) != (0))
	{
		hangWord.indexOf(letter);
		System.out.println("correct");
	}
		else
			maxTries--;
			System.out.println("You have " + maxTries + "guesses to go.");
	}

I'm yet to compile it but doubt that i will display the guessed letter like i mensioned previously and am unsure how to create the main the run the objects. If you could help with any of this that would be great. Cheers

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