All pastes #1010142 Raw Edit

Something

public text v1 · immutable
#1010142 ·published 2008-05-07 05:20 UTC
rendered paste body
/** Vung Nguyen
	 Section AG
	 Brice Johnson
	 5-6-08
 *  Represents a text base game of Hangman
 */

import java.util.*;

public class Hangman {
	public static void main (String[] args) {
		Scanner console = new Scanner(System.in);	
		System.out.println("Let's play Hangman");	
		System.out.println();
		String answer = "cAt";
		answer = answer.toUpperCase();
		String rightAnswer = checkAnswer(answer);
		int gamesPlayed = 1;
		boolean nextGame = true;
		while (nextGame) {
			intro(answer);
			playGame(console, answer, nextGame, rightAnswer);
			nextGame = newGame(console, gamesPlayed);
			System.out.println();
			gamesPlayed++;
		}	
	}
	
	//This method allow the player to guess letters as long as they missed less than 6 guesses
	//and if they chose to continue playing.
	public static String playGame (Scanner console, String answer, boolean done, String rightAnswer) {
		int maxGuess = 0;
		String numGuess = "";
		String misses = "";
		String guessWord =" ";
		while (maxGuess <=6 && done){        //allow user to guess as long as the missed guesses 
			String guess = console.next();	 //is less than 6 and if they want to keep playin
			guess = guess.toUpperCase();					
			int pos = answer.indexOf(guess);	
			int wordLength = answer.length();
			if (pos ==-1 && misses.indexOf(guess) == -1){  //keep tracks of how many missed gueses
				misses += guess;									 //and what they are
				maxGuess++;
			}				
			if (numGuess.indexOf(guess) != -1 && maxGuess <=6) {			//check if the letter 
				System.out.print ("Already Guessed! Guess a letter: ");	//has been guessed							
			} else {
				System.out.println();
				System.out.print("Word:   ");
				numGuess += guess;
				for (int i=0; i < wordLength; i++) {		
					if (guess.charAt(0) == answer.charAt(i)) {
						guessWord += answer.charAt(i);
					} else {
					}		
				}
			System.out.print(guessWord+ " ");			
			if(guessWord.equalsIgnoreCase(rightAnswer)){
				System.out.println();
				System.out.println("The answer was " +answer);
				System.out.println("You WIN!");
				break;
			}							
			System.out.println();	
			System.out.println("Misses: " + misses);	
			System.out.print("Guess a letter: ");
			}								
			if (maxGuess > 6) {           //the maximum of wrong guess cant be greater than 6
				System.out.println();	
				System.out.println("The answer was: " +answer);
				System.out.println("You LOSE!");
				break;
			}											
		}	
		return answer;	
	}
	
	//This is the intro to the first game being play.
	public static String intro(String answer) {	
		System.out.print("Word:   ");
		int wordLength = answer.length();
			for( int i=0; i < wordLength; i++){ 	
				System.out.print("_ ");		
		}		
		System.out.println();	
		System.out.println("Misses: ");
		System.out.print("Guess a letter: ");	
		return answer;			
	}	
	
	//This method ask whether the user wants to play again. 
	//anything that begins with y is yes and for n is no.
	public static boolean newGame(Scanner console, int games){
		System.out.print("Do you want to play again? ");
		String yesOrNo = console.next();
		yesOrNo = yesOrNo.toLowerCase();
		boolean done = false;
		if (yesOrNo.charAt(0) == 'y'){
			int maxGuess= 0;
			done = true;
		} else if(yesOrNo.charAt(0) == 'n'){
			System.out.print("You have played " + games +  " game(s). Goodbye!");
			done = false;	
		} 
		return done;		
	}	
	
	//This is use to create a string that we can compare to what the user has guesses
	//to see if the user has won.
	public static String checkAnswer (String answer) {
		int len = answer.length();
		String rightAnswer =" ";
		if (answer.length() >0) {
			for (int i=0; i< answer.length(); i++) {
				rightAnswer += answer.charAt(i) + "";
			}	
		}
		return rightAnswer;					 
	}	
}