Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help!how to get a graphical display of this game

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help!how to get a graphical display of this game

    Hello everybody,
    I'm new to java & I'm still in the process of learning. Can anyone tell me what I need to do, so that I can ineract with the game (Hangman) in graphical mode? I would appreciate it, if somebody gives me a hand.
    thanks
    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();
      }
    }
     
    class HangmanSession {
      private Player player;
      private HiddenKeyword hiddenKeyword;
      private LetterBox letterBox;
      private int triesNumber = 7;
     
      public HangmanSession() {
        player = new Player();
        player.askName();
        hiddenKeyword = new HiddenKeyword();
        letterBox = new LetterBox();
      }
     
      private void printState() {
        letterBox.print();
        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();
          char ch = player.takeGuess();
          if( letterBox.contains( ch ) ) {
            System.out.println( "Try again, you've already used letter " + ch );
            bool = false;
          }
          if( bool ) {
            if( hiddenKeyword.guess( ch ) ) {
              System.out.println( "Success, 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 a banana!" );
        System.exit( 0 );
      }
     
      public void gameOver() {
        System.out.println( "Sorry " + player + ", this time you lose!" );
        System.exit( 0 );
      }
    }
     
    class HiddenKeyword {
      private String fValue;
      private StringBuffer pValue;
      private int lfoundNumber = 0;
     
      public HiddenKeyword() {
        fValue = new String( "banana" );
        pValue = new StringBuffer( "-------" );
      }
     
      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 );
      }
     
    }
     
     
    class Player {
      private String fName = "";
     
      public void askName() {
        System.out.print( "\nPlayer, enter your name:" );
        fName = receiveInput();
      }
     
      public char takeGuess() {
        return receiveInput().charAt( 0 );
      }
     
      private String receiveInput() {
        String str = " ";
        BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
        try {
          str = br.readLine();
        }
        catch( IOException ex ) {
          ex.printStackTrace();
        }
        return str;
      }
     
      public String toString() {
        return fName;
      }
     
    }
     
    class LetterBox {
      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( "" );
      }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help!how to get a graphical display of this game

    Hello makarov,

    You need to look into Swing. Java Swing is for creating GUIs.

    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Job offers to program Hobo Wars
    By MooncakeZ in forum Paid Java Projects
    Replies: 7
    Last Post: September 17th, 2009, 09:41 PM
  2. Java program to display single and plural words
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 10th, 2009, 11:09 PM
  3. How to display sound signal waveforms continously?
    By ces_31 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 10th, 2009, 09:43 AM
  4. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 03:15 AM
  5. How to display the contents of my queue?
    By rocafella5007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2009, 11:46 AM