1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Harrizon [31]
3 years ago
11

The purpose of this assignment is to practice with ArrayLists (and hopefully, you'll have some fun). As far as the user knows, p

lay is exactly as it would be for a normal game of hangman, but behind the scenes, the computer cheats by delaying settling on a mystery word for as long as possible, which forces the user to use up several (perhaps all) chances.
initialization

The program reads a list of possible dictionary words, but instead of choosing one, it only decides on the length of a mystery word, which you might call randLength, a value chosen at random within some interval RAND_MIN and RAND_MAX, which are constants that you define in your program. Instead of choosing a word of randLength, the program removes all words from the list that are not of this length. That is, instead of choosing a word of length randLength, it keeps a list of all the words from the dictionary that are randLength letters long.

cheat phase

During this phase of the game, the program delays choosing a real random word for as long as possible. As in a normal game of hangman, the user may during any given round make a guess which is a full word, or a single letter. If the user guesses a word, and that word appears in our list of possible words, it is removed. If the guess is a single letter, every word remaining in the list that contains the letter is removed. The purpose of the cheat is to force the player to eat through as many chances as possible, increasing the chances that the player loses.

Obviously, at some point this phase must end. Otherwise, the user will realize that the cheat has taken place. This phase is ended if either of two conditions is true:

The user has run out of guesses. In this case, the program prints a message telling the user that they've lost. It chooses some word at random from the remaining words in the list, and tells the user that this was the mystery word all along.
The computer can't cheat any more. If the user makes a guess, and removing a word or words as we've specified previously would result in an empty list, the computer would get caught cheating. Your program must ensure that this never happens. Instead of removing the words from the list, the program should settle on a mystery word, by choosing it at random from the list of remaining words, and play continues as it would have during a normal game of hangman.
non-cheat phase

Play continues just as it would have during a normal game of hangman, except of course, because of the cheats, the user has fewer chances remaining.

the dictionary

You may use this small dictionary (dic.txt).

be stealthy

Remember that the cheating is done behind the scenes. To the user, the program should look like a normal game of hangman.

ArrayList

During each round of the cheat phase, you'll be manipulating a collection of words and you won't know in advance how much the size will change as the game progresses. At the beginning of the program, you'll have a large number of words, but you won't know how large. At each stage, it'll be reduced, but you won't know how much. While this is possible with an array, it's clear that ArrayList, which can grow and shrink as needed, is more appropriate.
Computers and Technology
1 answer:
aleksandrvk [35]3 years ago
3 0

Answer:

See explaination

Explanation:

Program source 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;

}

}

You might be interested in
The term “frivolous” implies that the author
tiny-mole [99]
Thank you for posting your question here at brainly. I hope the answer will help you. Feel free to ask more questions.
believes that the average individual does not need a smart phone 
<span>firivolous mean not serious, light, unimportant.</span>

3 0
3 years ago
What would the following program print to the screen when run?
Lady bird [3.3K]

Answer:

The output will be:

B

o

n

d

0

0

7

Explanation:

Given code is of Python language

Let us look at the code line by line

The first line is:

my_list = [7, 0, 0, "d", "n", "o", "B"]

This line will create a list with the given elements.

my_list.reverse()

This line will reverse the sequence of the elements of the list

for thing in my_list:

print (thing)

These lines will simply print the reversed elements of the list on screen.

The output will be:

B

o

n

d

0

0

7

8 0
2 years ago
I have a computer teacher who made me lose points in an assignment and saying that the reference page should be on a separate pa
lukranit [14]

Answer:

Have a talk with your teacher. Ask why she doesn't believe you, and if it still doesn't work, have a talk with someone that can physically help you - a principle or another teacher may be the best solution.

4 0
3 years ago
A non technical kind of cyber intrusion that relies heavily on human interaction and often involve tricking people into breaking
lana66690 [7]
Social Engineering.

---------------------------
4 0
3 years ago
Which of the following statements about crane hand signal training are true? A. Both statements are true about crane hand signal
Alenkinab [10]
The correct option is A.
When using crane at a construction site, it is required that:
1.A poster should be posted at the job site with an illustration of the hand signals that every operator and personnel working with the crane and around the crane must know.
2. Hand signals for crane and derrick operators should be those set by the American National Standard institute customize for the type of crane in use.<span />
5 0
3 years ago
Other questions:
  • Peter is working on a project. He feels that the parameters need to be changed to meet the client specifications. First he must
    7·1 answer
  • If in your checkout cart right before you buy something there is an option saying "this order contains a gift." on amazon, what
    7·1 answer
  • How can an administrator make only the files and folders to which a user has at least Read permissions visible?
    13·1 answer
  • Which of the following occurs during data cleansing?
    9·1 answer
  • Which of the following is not true about network design?Group of answer choicesIn designing LAN networks, network designers tend
    11·1 answer
  • The blue section of the following Venn diagram could represent which of the following Boolean statements?
    14·1 answer
  • How do I install another part on campaign call of duty cold war? please help.(best answer will get branliest.)
    15·2 answers
  • State whether the given HTML coding is True or False. &lt;HR SIZE=5 COLOR=YELLOW ALIGN=RIGHT WIDTH=75%&gt;​
    9·1 answer
  • Imagine that your parents were starting a small business, and they wanted to upgrade their data storage. Would you recommend a f
    11·1 answer
  • If you use your smartphone as a hotspot to connect to the Internet on your tablet you are using a ________.quillet
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!