Question

PLEASE DO IN JAVA

You can use any source for the dictionary or just a few words are fine!

description The purpose of this assignment is to practice with ArrayLists (and hopefully, youll have some fun). As far as th

non-cheat phase Play continues just as it would have during a normal game of hangman, except of course, because of the cheats

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Random;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class hangman {
public static void main(String args[]) {

//ArrayList to store the dictionary
ArrayList<String> dict = new ArrayList<String>();

//to read the dictionary into ArrayList
BufferedReader fileReader;
try {
//change path to your dict file
fileReader = new BufferedReader(new FileReader(
"/Users/username/Downloads/dict.txt"));
String line = fileReader.readLine();

//read line by line into ArrayList
while (line != null) {
dict.add(line);
line = fileReader.readLine();
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}

//OR COMMENT THE ABOVE READING FROM FILE, to test with this 6 words
// dict.add("volvo");
// dict.add("apple");
// dict.add("ball");
// dict.add("cat");
// dict.add("elephant");
// dict.add("zoo");


int RAND_MIN=3,RANDMAX=9;
int maxGuesses = 10;
String chosenWord = "", resWord="";
Boolean isChosen = false;

Random r = new Random();
int randLength = r.nextInt((RANDMAX - RAND_MIN) + 1) + RAND_MIN;
System.out.println("Random length = " + randLength );

for (int i = 0; i < dict.size(); i++) {
if(dict.get(i).length() != randLength)
{
dict.remove(i);
i--;
}
}


//uncomment the println lines below to see the working of the algorithm
Scanner in = new Scanner(System.in);
int guessCounter = maxGuesses;
while(guessCounter>0)
{

System.out.println( "\n\n\nwords left are: and chosen is: " + isChosen);
for (int i = 0; i < dict.size(); i++) {
System.out.println(dict.get(i) );
}


guessCounter--;
System.out.println( "\nEnter a letter:" );
String letter = in.nextLine();


if(isChosen==false){

//delaying choosing random word by removing words based on given letter
int matchCount=0;
for (int i = 0; i < dict.size(); i++) {
if(dict.get(i).indexOf(letter.charAt(0)) != -1)
{
matchCount++;
}
}

if(matchCount!=dict.size())
{
for (int i = 0; i < dict.size(); i++) {
if(dict.get(i).indexOf(letter.charAt(0)) != -1)
{
dict.remove(i);
i--;
}
}

}
//choosing the mystery word if there'll be no words left in ArrayList
else{
chosenWord = dict.get(0);
resWord = chosenWord;
isChosen = true;

chosenWord = chosenWord.replace(letter.charAt(0)+"","");
//System.out.println("remaining word: "+ chosenWord);
if(chosenWord=="")
{
System.out.println( "you've cracked the word: " + chosenWord + " in Guesses:" + (maxGuesses-guessCounter));
return;
}
}
}
//if guessed all letters of chosen word
else{
chosenWord = chosenWord.replace(letter.charAt(0)+"","");
//System.out.println("remaining worrd: "+ chosenWord + " : " + chosenWord.length());
if(chosenWord.length()==0)
{
System.out.println( "you've cracked the word: " + resWord + " in Guesses:" + (maxGuesses-guessCounter));
return;
}
}

}

//if ran out of guesses
System.out.println( "you've ran out of your guesses, the word is: " + resWord);
return;

}
}

(py36) abashabo-mac:Downloads anilkumarbashaboina$ javac hangman.java (py36)abashabo-mac:Downloads anilkumarbashaboina$ javaEnter a letter: e words left are: and chosen is: true exchange Enter a letter: X words left are: and chosen is: true exchangewords left are: and chosen is: true exchange Enter a letter: words left are: and chosen is: true exchange a letter: Enter a w

Add a comment
Know the answer?
Add Answer to:
PLEASE DO IN JAVA You can use any source for the dictionary or just a few...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • code block and c++ only please on Programs> Prog 4: Twisted Hangman & Sylabus ule nte...

    code block and c++ only please on Programs> Prog 4: Twisted Hangman & Sylabus ule nte zza ckboard ss Notes al Reference ference [Corrected sample output and rubric still to come ..] Write a program to play hangman where the computer chooses the word and the human user tries to guess the word. The twist is that the computer starts out with a dictionary of many words. After each human guess, the computer eliminates all words containing the letters guessed...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the...

    JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • PYTHON CODE DOCUMENT EVERY LINE * You cannot use any external Python libraries. You can, of...

    PYTHON CODE DOCUMENT EVERY LINE * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program should work is the same as...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • Code In Python Well Document Code Every Line Please * You cannot use any external Python...

    Code In Python Well Document Code Every Line Please * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program should work...

  • Code In Python Well Document Code Every Line Please 5 Stars * You cannot use any...

    Code In Python Well Document Code Every Line Please 5 Stars * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program...

  • Code In Python Well Document Code Every Line Please * You cannot use any external Python...

    Code In Python Well Document Code Every Line Please * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program should work...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT