All pastes #48560 Raw Edit

Untitled

public text v1 · immutable
#48560 ·published 2006-04-07 20:30 UTC
rendered paste body
import java.io.*;

public class Hangman // super class. The program uses inheritance to link the 3 classes together
{

    private StringBuffer wordToPlayWith;
    // word selected - for computer's use
    private StringBuffer wordToGuess;
    // same as the above, but works on wordToPlayWith - shown to the user
    public char [] wordForComp;
    public char [] wordForDisp;
    String charChosen;
    String playerName;
    int score;
    int highScore;
    boolean use = false;
    public char [] used;
    private int numberOfTries; // number of chances user has.
    private boolean hasGuessed = false; // tests if user has correctly guessed the word.
    int tries = 0;
    char [] letterHasBeenUsed; // tests whether chosen char has been used or not.
    boolean alreadyGuessedChar = true; // boolean to switch if user has used char already.



    public Hangman (String wordToPlayWith) throws IOException
        /* implements almost all of the necessary testing variables. Uses wordToPlayWith (which is the random
        word used to play with) and extends it to also be used by wordToGuess - for user - and wordToPlayWithCharArray.
        guessedWordFullArray.
        */
    {

        this.wordToPlayWith = new StringBuffer (wordToPlayWith);
        wordToGuess = new StringBuffer (128);
        used = new char [wordToPlayWith.length () + 8];

        wordForComp = new char [wordToPlayWith.length ()];
        wordForDisp = new char [wordToPlayWith.length ()];


        for (int i = 0 ; i < wordToPlayWith.length () ; i++)
        {
            wordForComp [i] = wordToPlayWith.charAt (i);
            if (
                    wordToPlayWith.charAt (i) == ' ' | wordToPlayWith.charAt (i) == '!' | wordToPlayWith.charAt (i) == '*' |
                    wordToPlayWith.charAt (i) == '#' | wordToPlayWith.charAt (i) == '$' | wordToPlayWith.charAt (i) == '@' |
                    wordToPlayWith.charAt (i) == '%' | wordToPlayWith.charAt (i) == '^' | wordToPlayWith.charAt (i) == '/' |
                    wordToPlayWith.charAt (i) == '&' | wordToPlayWith.charAt (i) == '&' | wordToPlayWith.charAt (i) == '*' |
                    wordToPlayWith.charAt (i) == '(' | wordToPlayWith.charAt (i) == ')' | wordToPlayWith.charAt (i) == '-' |
                    wordToPlayWith.charAt (i) == '_' | wordToPlayWith.charAt (i) == '=' | wordToPlayWith.charAt (i) == '`' |
                    wordToPlayWith.charAt (i) == '~' | wordToPlayWith.charAt (i) == '<' | wordToPlayWith.charAt (i) == '>' |
                    wordToPlayWith.charAt (i) == '|' | wordToPlayWith.charAt (i) == '[' | wordToPlayWith.charAt (i) == ']' |
                    wordToPlayWith.charAt (i) == '.' | wordToPlayWith.charAt (i) == ',' | wordToPlayWith.charAt (i) == '+' |
                    wordToPlayWith.charAt (i) == '"' | wordToPlayWith.charAt (i) == '?'


                    )
            {

                wordForDisp [i] = wordToPlayWith.charAt (i);

            }
            else if (wordToPlayWith.charAt (i) == ' ')
            {
                wordForDisp [i] = ' ';
            }

            else
            {
                wordForDisp [i] = '-';
            }

        }
    }


    public void play () throws IOException
    {



        int wordToPlayWithLength = wordToPlayWith.length ();
        /* tests to determine the number of tries
        the user should get to guess. Tries = word.length +5. */

        //   System.out.println ("The word to guess has " + wordToPlayWithLength + " characters.");
        //can be erased if the user pleases.


        BufferedReader input = new BufferedReader (new InputStreamReader (System.in));



        for (int y = 0 ; y < used.length ; y++)
        {

            System.out.print ("The word is: ");
            for (int t = 0 ; t < wordForComp.length ; t++)
            {
                System.out.print (wordForComp [t]);
            }

            System.out.println ("\n");

            System.out.print ("The word to guess is: " + wordToGuess.toString ());
            for (int t = 0 ; t < wordForDisp.length ; t++)
            {
                System.out.print (wordForDisp [t]);
            }

            System.out.println ("\n");

            System.out.print ("The characters you have used are: ");
            for (int t = 0 ; t < used.length ; t++)
            {
                if (used [t] == ' ')
                {
                    break;
                }
                else
                {
                    System.out.print (used [t]);
                    System.out.print (" ");
                    tries++;
                }

            }
            // if (tries == used.length)
            // {
            //     System.out.println ("Game Over!");
            // }


            System.out.println ("\n");
            System.out.println ("Please enter a single character: ");
            charChosen = input.readLine (); // user chooses the character.


            while ((charChosen.length () > 1) || (charChosen.length () == 0))
            {
                System.out.println ("Enter only one character!");
                charChosen = input.readLine ();
            }
            use = testInput2 (charChosen.charAt (0));

            while (use == true)
            {

                while ((charChosen.length () > 1) || (charChosen.length () == 0))
                {
                    System.out.println ("Enter only one character!");
                    //charChosen = input.readLine ();
                }

                System.out.println ("You have already used " + charChosen);
                charChosen = input.readLine ();
                use = testInput2 (charChosen.charAt (0));
            }

            if (use == false)
            {

                for (int t = 0 ; t < wordForComp.length ; t++)
                {
                    if (wordForComp [t] == charChosen.charAt (0))
                    {
                        wordForDisp [t] = charChosen.charAt (0);
                    }
                }

                used [y] = charChosen.charAt (0);
                tries++;

            }
            // System.out.println (wordForComp.length  + ", " + wordForDisp.length );
            // if (tries == used.length)
            // {
            //     System.out.println ("Game over!");
            //     break;
            // }
            String wordForDispS = "";
            String wordForCompS = "";

            for (int t = 0 ; t < wordForComp.length ; t++)
            {
                wordForDispS = wordForDispS + wordForDisp [t];
                wordForCompS = wordForCompS + wordForComp [t];
            }
            if (wordForDispS.equals (wordForCompS))
            {
                System.out.println ("Well done!");
                score++;
                break;

            }
        }

        BufferedReader highScoreInput = new BufferedReader (new FileReader ("HangmanHighScore.txt"));
        highScore = Integer.parseInt (highScoreInput.readLine ());

        if (score > highScore)
        {


            PrintWriter writer = new PrintWriter (new FileWriter ("HangmanHighScore.txt"));
            for (int o = 0 ; o < 1 ; o++)
            {
                writer.write ("The current high score holder is: " + playerName + "with " + (score));
            }
            writer.close ();
        }
    }




    public boolean testInput2 (char u)
    {
        for (int i = 0 ; i < used.length ; i++) //
        {
            if (charChosen.charAt (0) == used [i])
            {
                //System.out.println ("You have used this char");
                return true;
                //       break;
            }
            if (i == used.length - 1)
            {
                return false;
            }
        }

        return false;
    }
}