The Artima Developer Community
Sponsored Link

Java Answers Forum
One last thing... counter doest work!!

6 replies on 1 page. Most recent reply: Jul 29, 2003 6:02 AM by K. Hodel

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

One last thing... counter doest work!! Posted: Jul 28, 2003 7:55 PM
Reply to this message Reply
Advertisement
First of all I want to thank everyone who helped me..
Dave, Greg, and K.

Ok guys.. I have one last problem then I can finally hand this thing in.

I dont think this is even a programming problem, more of a trouble shooting problem. but here goes.

I would like a counter that incraments the number of INCORRECT guesses. I can incrament all guesses right now but I have no way of incramenting the incorrect ones. Here is my code.. please let me know if it required a complete re write or just a minor adjustment.

import york.*;
/*
This class plays a game of Hang Man
*/

public class HangManGame
{
public static void main (String[] args)
{
char sentinel = 'y';
while (sentinel !='n')
{
York.println ("Please enter a secret word.");
String secretWord = York.readLine();
char guess;
String prevGuess = " ";
int i = secretWord.length();
int l;
int j;
String answer = "*";
for (l=1; l<i ;l++)
{
answer = answer.concat("*");
}
York.println(answer);
York.println("Please choose a letter");
while (i!=0 && !answer.equals(secretWord))//I cant get it to recognize this word as being equal.
{
//counts the number of guesses
York.println();
guess = York.readChar();
for (j=0; j<secretWord.length(); j++)
{
if (guess==secretWord.charAt(j))// I could not get the equalsIgnoreCase to work here.
answer = answer.substring(0,j)+guess+answer.substring(j+1);
}

York.println();
York.println(answer);
prevGuess = prevGuess+" "+(guess);
York.println("You guessed "+guess+ ".");
York.println("Your previous guesses include "+prevGuess+" .");
York.println("You have " +(i)+ " guesses left");
}
York.println("Game Over");
York.println("Do you want to play another game?");
sentinel = York.readChar();
York.println("Please enter y or n.");
}
}
}


K. Hodel

Posts: 8
Nickname: hodeka104
Registered: Jul, 2003

Re: One last thing... counter doest work!! Posted: Jul 28, 2003 10:02 PM
Reply to this message Reply
Since you're already testing to see if the character matches, just change your counter when it doesn't. And regarding your comment following that if statement, String's equalIgnoreCase method takes a String argument; since you want to compare two chars rather than two Strings, you might try something like
if ( Character.toUpperCase( guess ) == Character.toUpperCase( secretWord.charAt(j) ) ) {
  answer = ...
}
else {
  --i;
}

One note - I think your code would be simpler if you'd followed David's advice about using a StringBuffer (you wouldn't need those substrings anymore, and your initialization of answer could then be improved).

Hope you can get it working.

K. Hodel

Posts: 8
Nickname: hodeka104
Registered: Jul, 2003

Re: One last thing... counter doest work!! Posted: Jul 28, 2003 10:34 PM
Reply to this message Reply
Oops. I just realized my counter suggestion makes no sense. Try this instead (perhaps before your for loop).
if ( secretWord.indexOf( Character.toString( guess ) ) < 0 ) {
  --i;
}

You could probably refactor your check loop a bit to better integrate this, but anyway...

Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Re: One last thing... counter doest work!! Posted: Jul 28, 2003 11:24 PM
Reply to this message Reply
Just want to thank K.Hodel.. got it working great.. everything orks...

Thanks alot

Philip Pompili

Posts: 12
Nickname: ppompili
Registered: Jul, 2003

Re: One last thing... counter doest work!! Posted: Jul 28, 2003 11:35 PM
Reply to this message Reply
Wait K.. I spoke too soon.. for some reason the program will compile on my home machine, but I get one error on the machine at school(Via Telnet)..I get an error with this lin
if ( secretWord.indexOf(Character.toString(guess))<0)
I dont get the error at home.. in fact at home the program works perfectly...
can anyone explain this..

I have initialized the Java enviroment at school as well

source /javainit

but that doesnt help...

Mr Plow

Posts: 18
Nickname: mrplow
Registered: Jun, 2003

Re: One last thing... counter doest work!! Posted: Jul 28, 2003 11:50 PM
Reply to this message Reply
Philip -

Below I have posted an alternative way to tackle this problem. Main difference in the program below is that it tries to be more true to OOP by separating the concepts of the word to guess and the game of hangman into separate classes. (The HangMan class uses an instance of the Word class via composition).

I wrote it quickly, so I am sure that there are plently of areas for improvement in logic and constructs.

I always find that it is a good learning experience to look at how others solved problems after I have solved them myself. Perhaps you will find the same.

import java.util.*;
import javax.swing.*;
 
class Word
{
  private String word;
  private StringBuffer displayWord = new StringBuffer();
 
  Word(String s)
  {
    word = s;
	int wordLength = s.length();
	for(int i = 0 ; i < wordLength; i++)
	{
	  displayWord.append("*");
	}
  }//end of constructor
 
  public String getWord ()
  {
    return displayWord.toString();
  }
 
  public boolean checkChar (char c)
  {
    int place = word.indexOf(c);
	if(place == -1)
	{
	  return false;
	} else {
	    char[] array = word.toCharArray();
		ArrayList list = new ArrayList();
		for(int i = 0 ; i < array.length; i++)
		{
		  if(array[i] == c)
		    list.add(new Integer(i));
		}
		Object[] objArray = list.toArray();
		for(int i = 0; i < objArray.length; i++)
		{
		  int currentPlace = ((Integer)objArray[i]).intValue();
		  displayWord.setCharAt(currentPlace, c);
		}
	  
	  return true;
	}
  }//end of checkChar method
 
  public boolean wordGuessed()
  {
    int i = displayWord.indexOf("*");
	if(i < 0)
	  return true;
	return false;
  }//end of wordGuessed method
 
};//end of class word
 
 
 
public class Hangman
{
  static int correctGuesses = 0;
  static int incorrectGuesses = 0;
  static int totalGuesses = 0;
  static Word word;
  static char[] guessedAlready = new char[26];
  static int guessedAlreadyCounter = 0;
 
  public static void main(String[] args)
  {
    try
    {
    //Get secret word from user and create "Word" object with it.
    String input = JOptionPane.showInputDialog("Enter the secret word: ");
    word = new Word(input);
 
    //Use a while loop to keep having the user enter guesses until the word is complete
	while(!word.wordGuessed())
	{
	  input = JOptionPane.showInputDialog("Guess a letter: ");
	  
	  char guess = input.charAt(0);
      
	  if(alreadyGuessed(guess))
	  {
	    System.out.println("You already guessed the character: " + guess + "\n");
		continue;
	  }
      totalGuesses++;
	  if(word.checkChar(guess))
	  {
	    correctGuesses++;
		System.out.println("LETTER MATCHED");
	  } else {
	    incorrectGuesses++;
		System.out.println("NOT A MATCH");
	  }
 
	  printState();
	
	}
	
	System.exit(0);
    } catch (Exception e) {
      System.exit(1);
    }//end of try/catch block... used to catch errors related to the use of SWING components
  }//end of main method
 
  static boolean alreadyGuessed(char c)
  {
    for(int i = 0 ; i < guessedAlready.length; i++)
	{
	  if(guessedAlready[i] == c)
	    return true;
	}
	guessedAlready[guessedAlreadyCounter++] = c;
	return false;
  }
 
  static void printState()
  {
    System.out.println("Current state of unknown word: " + word.getWord() );
	System.out.println("Total guesses: " + totalGuesses);
	System.out.println("Correct guesses: " + correctGuesses );
	System.out.println("Incorrect guesses: " + incorrectGuesses );
	System.out.println("");
  }
 
};

K. Hodel

Posts: 8
Nickname: hodeka104
Registered: Jul, 2003

Re: One last thing... counter doest work!! Posted: Jul 29, 2003 6:02 AM
Reply to this message Reply
I'm guessing the problem is a difference in the versions of java being used. The static toString method of Character looks like it was introduced in 1.4, so try this instead:
if ( secretWord.indexOf( ( new Character( guess ) ).toString() ) < 0 )

Flat View: This topic has 6 replies on 1 page
Topic: newbie. Need Help on Combo Box Previous Topic   Next Topic Topic: Does not Equal Operationq

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use