4 Replies - 207 Views - Last Post: 06 December 2012 - 08:09 PM Rate Topic: -----

#1 ess1223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 12

Error message: method guess cannot be applied to given class

Posted 06 December 2012 - 07:24 PM

class HangmanSession {
  private HiddenKeyword hiddenKeyword;
  private int triesNumber = 7;
  
  public HangmanSession() {
    hiddenKeyword = new HiddenKeyword();
  }

  private void printState() {
    System.out.print( "Hidden word : " );
    hiddenKeyword.print();
    System.out.print( "Tries left: " + triesNumber + "\n<guess letter:>" );
  }

  public void play() {
    boolean bool = true;
    while( true ) {
      bool = true;
      printState();
        System.out.println( "Try again, you've already used that letter " + char.class );
        bool = false;
      }
      if( bool ) {
        if( hiddenKeyword.guess( char.class ) ) {
          System.out.println( "YAY! you have found letter! " + ch );
        }
        else {
          triesNumber--;
        }
        if( triesNumber < 1 )
          gameOver();

        if( hiddenKeyword.found() )
          congratulations();
      }
    } //end of bool
  }


For the project I'm doing, I need to be able to guess letters. The fact that it is not letting me use "guess" is very problematic. What is the problem and how do I fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: Error message: method guess cannot be applied to given class

#2 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Error message: method guess cannot be applied to given class

Posted 06 December 2012 - 07:29 PM

How are you calling the guess function? Since it's a part of your HiddenKeyword class you need to

//Create an instance of HiddenKeyword
HiddenKeyword hk = new HiddenKeyword();
hk.guess(charToGuess);



My suggestion is you get rid of all of the miniature classes. All these methods are functionality of a simple Hangman class, splitting it up this much will just cause confusion and make situations like this to debug.
Was This Post Helpful? 1
  • +
  • -

#3 ess1223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 12

Re: Error message: method guess cannot be applied to given class

Posted 06 December 2012 - 07:34 PM

How would I go about about getting rid of the "miniature classes"? Sorry, I'm very new to programming and don't have the slightest clue as to what I'm doing.
Was This Post Helpful? 0
  • +
  • -

#4 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Error message: method guess cannot be applied to given class

Posted 06 December 2012 - 07:43 PM

Something like this

public class Hangman()
{
    public static void main(String[] args)
    { 
        Hangman hangman = new Hangman();
        hangman.play(); // You already have this set up
    }

    //play method
    //guess method
    //printstate method
    //gameover method
    //etc
}



Just put all the methods in the scope of one class
Was This Post Helpful? 0
  • +
  • -

#5 ess1223  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-December 12

Re: Error message: method guess cannot be applied to given class

Posted 06 December 2012 - 08:09 PM

Like this?

 import java.util.Random;
import java.util.Scanner;
import java.util.*; 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Hangman {
public static void main( String[] args ) {
    HangmanSession hangmanSession = new HangmanSession();
    hangmanSession.play();
    
    private HiddenKeyword hiddenKeyword;
    private int triesNumber = 7;
    hiddenKeyword hk = new HiddenKeyword();
    hk.guess(charToGuess);
    private void printState() {
    System.out.print( "Hidden word : " );
    hiddenKeyword.print();
    System.out.print( "Tries left: " + triesNumber + "\n<guess letter:>" );
  }

  public void play() {
    boolean bool = true;
    while( true ) {
      bool = true;
      printState();
        System.out.println( "Try again, you've already used that letter " + char.class );
        bool = false;
      }
      if( bool ) {
        if( hiddenKeyword.guess( char.class ) ) {
          System.out.println( "YAY! you have found letter! " + ch );
        }
        else {
          triesNumber--;
        }
        if( triesNumber < 1 )
          gameOver();

        if( hiddenKeyword.found() )
          congratulations();
      }
    } //end of bool
    public void congratulations() {
    System.out.println( "Congratulations " + player + ", you win!" );
    System.exit( 0 );
  }

  public void gameOver() {
    System.out.println( "Sorry " + player + ", you lost, but you can try again!" );
    System.exit( 0 );
  }
  private String fValue;
  private StringBuffer pValue;
  private int lfoundNumber = 0;
  public String words() {
        Random r = new Random();
        int random = r.nextInt(20)+1;
    }
    
public boolean found() {
    System.out.println( "Letters found:" + lfoundNumber + "/" + fValue.length() );
    return ( lfoundNumber == fValue.length() );
  }
      
public boolean guess( char c ) {
    int index = fValue.indexOf( c );
    if( index == -1 )
      return false;
    else {
      lfoundNumber = lfoundNumber + findOccurances( c );
      return true;
    }
  }
  
  private int findOccurances( char c ) {
    int idx = fValue.indexOf( c );
    pValue.setCharAt( idx, fValue.charAt( idx ) );
    int counter = 1;
    while( idx != -1 ) {
      int idxx = fValue.indexOf( c, idx + 1 );
      idx = idxx;
      if( idx != -1 ) {
        counter++;
        pValue.setCharAt( idx, fValue.charAt( idx ) );
      }
    }
    return counter;
  }

  public void print() {
    System.out.println( pValue );
  }
  private char[] lbox = new char[24];
  private int counter = 0;

  public boolean contains( char c ) {
    for( int i = 0; i < counter; i++ ) {
      if( lbox[i] == c )
        return true;
    }
    lbox[counter] = c;
    counter++;
    return false;
  }

  public void print() {
    System.out.print( "\nLetterBox:" );
    for( int i = 0; i < counter; i++ ) {
      System.out.print( lbox[i] );
    }
    System.out.println( "" );
  }
  public static void main(String[] args) {
    int words = 20;String wordsString;
 switch(random) {
            case 1:
            return "star";

            case 2:
            return "hooray";

            case 3:
            return "dog";

            case 4:
            return "improvise";

            case 5: 
            return "teacher";
            
            case 6:
            return "college";
            
            case 7:
            return "seahawks";
            
            case 8:
            return "read";
            
            case 9:
            return "computer";
            
            case 10:
            return "program";
            
            case 11:
            return "holiday";
            
            case 12:
            return "film";
            
            case 13:
            return "music";
            
            case 14:
            return "learn";
            
            case 15: 
            return "library";
            
            case 16:
            return "waterfall";
            
            case 17:
            return "book";
            
            case 18:
            return "semester";
            
            case 19:
            return "first";
            
            case 20:
            return "vacation";

            default:
            return"";
        }

    }


        
    }
}

  
  

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1