4 Replies - 14377 Views - Last Post: 06 May 2009 - 07:17 AM Rate Topic: -----

#1 Toral   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 09

Simple console based Hangman program

Post icon  Posted 02 May 2009 - 01:54 PM

I have been working on this Hangman game for a few weeks now and am burnt out. I need some help with my logic in the do, while loop in my main method.
I need it to check if a guessed letter is in the word and if it is to show it. I really need someone different to take a look at this and give me some ideas on where to go. All the code is starting to blur together on me. This is what I have so far:

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

public class testIt 
{
	public static void main(String[] args) throws Exception
	{
	String word = Word();
	String dashes = "------------------------------------------";
	String wordDashes=dashes.substring(0, word.length());	
	int count = 0;
	StringBuffer sb = new StringBuffer(word);
	char[] chArr = new char[word.length()];

	sb.getChars(0, word.length(), chArr, 0);
	
	System.out.println(sb);
	System.out.println(wordDashes);
	
	
		do
			{
			char x = word.charAt(count);
			char y = getChar();
	
	
			String s = Character.toString(x);
			String k = Character.toString(y);
	
			boolean n = s.equalsIgnoreCase(k);
	
				if (n){
				System.out.println("YAY! They match!");}
				else if (!n) {System.out.println("Try Again");}
			}
			while (count != word.length());
	}

	static char getChar()
	{
		Scanner s = new Scanner(System.in);
		
		return s.next().toLowerCase().charAt(0); 
	}

	static String Word() throws Exception
	{
				
		Scanner s = new Scanner(new FileReader("dictionary.txt"));
		
		String[] words = new String[10];
		
		for (int i=0; i< words.length; i++)
				words[i] = s.nextLine();
		
				
		int Index = (int)(Math.random() *words.length);
		
		String secretWord = words[Index];
		
		return secretWord;
	
	} 
} 

This post has been edited by Toral: 02 May 2009 - 01:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Simple console based Hangman program

#2 Toral   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 09

Re: Simple console based Hangman program

Posted 04 May 2009 - 11:18 AM

I am thinking I want to use a string buffer method to find if a letter appears in the hidden word. Any suggestions on finding the right method?
Was This Post Helpful? 0
  • +
  • -

#3 WolfCoder   User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 828
  • View blog
  • Posts: 7,696
  • Joined: 05-May 05

Re: Simple console based Hangman program

Posted 04 May 2009 - 11:43 AM

Just google JAVA String. No, this isn't a Let Me Google That For You post, it's actually a trick to search the API for things. If you want to see everything an object can do and don't want to keep downloading the docs, just enter JAVA and then the object name to get it's information. I installed docs anyway so I can just click on the word and click API Help in JCreator and it loads the page for that object.

Here's a good method of String that lets you know if a single character (letter) appears in the string.

http://java.sun.com/...ml#indexOf(int)

Nice starting point, write a new program and play with it if you have to before implementing it. Also, try to ensure it is case insensitive so that a and A are the same thing. Information on doing that is on the same page.
Was This Post Helpful? 0
  • +
  • -

#4 Toral   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-May 09

Re: Simple console based Hangman program

Posted 05 May 2009 - 02:06 PM

Well I got this far and it is finding and replacing the guessed letter if it is there. Now my biggest issue is getting it to keep the dashes and the correctly guessed letter. I know it is an issue with my do, while loop, but I have no idea what it is. Help please!

Oh and WolfCoder thanks for the string indexOf, that helped so much, as well as you made it easier for me to find other stuff in the API. Thanks!

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

public class RieckS3HWHangman
{
	public static void main(String[] args) throws Exception 
	{
		String word = Word();
		String dashes = "------------------------------------------";
		String wordDashes=dashes.substring(0, word.length());	
		char y;
		int x;
		final int MAX_GUESS = 8;
		String z = " ";
		int count = 0;
		int rightcount = 0;
		System.out.println(wordDashes);
		
		do
		{
		y = getChar();
		x = word.indexOf(y);
		
		if (x > -1){
		z = z + replaceCharAt(wordDashes, x, y);
		removeChar(z, '-');
		System.out.println(z); rightcount++; count++;}
		else {System.out.println("Try Again"); System.out.println(z); count++;}
		}
		while (rightcount != word.length() && count != MAX_GUESS);

	}	
	
	public static String replaceCharAt(String s, int pos, char c) 
	{
		return s.substring(0,pos) + c + s.substring(pos+1);
	}
	
	


	static char getChar()
	{
		Scanner s = new Scanner(System.in);
		
		return s.next().toLowerCase().charAt(0); 
	}
	
	static String Word() throws Exception
	{
				
		Scanner s = new Scanner(new FileReader("dictionary.txt"));
		
		String[] words = new String[10];
		
		for (int i=0; i< words.length; i++)
				words[i] = s.nextLine();
		
				
		int Index = (int)(Math.random() *words.length);
		
		String secretWord = words[Index];
		
		return secretWord;
	
	} 
	
	public static String removeChar(String s, char c) 
	{

   String r = "";

   for (int i = 0; i < s.length(); i ++) 
   {
	 if (s.charAt(i) != c) r += s.charAt(i);
   }
   return r;
   }
}

Was This Post Helpful? 0
  • +
  • -

#5 avinashsaniva   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 06

Re: Simple console based Hangman program

Posted 06 May 2009 - 07:17 AM

I have made a few changes in your do...while loop and added an end message.
But the code still has a bug. Try figuring it out. If not, I can help.

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

public class RieckS3HWHangman {
	
	public static void main(String[] args) throws Exception {
		String word = Word();
		String dashes = "------------------------------------------";
		String wordDashes = dashes.substring(0, word.length());	
		char y;
		int x;
		final int MAX_GUESS = 8;
		int count = 0;
		int rightcount = 0;
		System.out.println(word);
		System.out.println(wordDashes);

		do {
			y = getChar();
			x = word.indexOf(y);

			if (x > -1){
				wordDashes = replaceCharAt(wordDashes, x, y);
				removeChar(wordDashes, '-');
				System.out.println(wordDashes); rightcount++; count++;
			} else {
				System.out.println("Try Again"); System.out.println(wordDashes); count++;
			}

		} while (rightcount < word.length() && count < MAX_GUESS);
		
		if (word.equalsIgnoreCase(wordDashes)) {
			System.out.println("Right answer.");
		} else {
			System.out.println("Better luck next time.");
		}
	}	

	public static String replaceCharAt(String s, int pos, char c) {
		return s.substring(0,pos) + c + s.substring(pos+1);
	}

	static char getChar() {
		Scanner s = new Scanner(System.in);
		return s.next().toLowerCase().charAt(0);
	}

	static String Word() throws Exception {
		Scanner s = new Scanner(new FileReader("dictionary.txt"));
		String[] words = new String[10];
		
		for (int i=0; i< words.length; i++)
			words[i] = s.nextLine();

		int Index = (int)(Math.random() *words.length);
		String secretWord = words[Index];
		return secretWord;
	}

	public static String removeChar(String s, char c) {
		String r = "";

		for (int i = 0; i < s.length(); i ++) {
			if (s.charAt(i) != c) { 
				r += s.charAt(i);
			}
		}
		return r;
	}
}
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1