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
What two things should you do before starting the design process
Charra [1.4K]

Answer: B and C

Explanation: Analyze the audience

                      Identify the problem

8 0
2 years ago
How to calculate 3 X (50 + 40) ÷ 5 on excel 2016
kozerog [31]

Answer:

I think this might help, don't know much of this

Explanation:

How do you calculate 3.5 increase in Excel?

How To Increase a Number By a Percentage. If want to calculate a percentage increase in Excel (i.e. increase a number by a specified percentage), this can be done by simply multiply the number by 1 + the percentage increase. - which gives the result 60.

8 0
3 years ago
Read 2 more answers
There are several possible reasons why a high percentage of IT projects are abandoned-the business strategy changed, technology
aleksklad [387]

Answer:

a. True

Explanation:

The above listed information are part of the reasons why so many IT projects are abandoned by the business entities after a given period of time frame.

5 0
3 years ago
Your customer, Maggie, wants to buy a gaming PC, and she specifically mentions that she wants an Intel CPU and intends to overcl
Ksenya-84 [330]

Answer:

Intel produces a series of unlocked CPU's that can be overclocked. These CPUs are from the "K" or "X" series. Example: Intel Core i9-9900K, Intel Core i9-10940X.

These are the few things that are to be kept in mind while overclocking:

-Motherboard: Motherboard should support overclocking. Example: Intel Z series, most AMD motherboards.

-Cooler: Boosting the clock speed increases the temperature. The cooler has to be upgraded to keep the temperatures low. Example: Water-cooled. Also, the heat sink has to be checked if it's working properly.

-Be ready to test your system in BIOS. Make sure the temperature, voltage, memory speed is stable for the set clock speed.

Explanation:

8 0
3 years ago
What can Strings store
horsena [70]
A string is generally considered as a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding. String may also denote more general arrays or other sequence (or list) data types and structures.
5 0
3 years ago
Read 2 more answers
Other questions:
  • trhy356<br>yjetyi46ui y j4yhnpug 2utg[ 2[ 24[ou [o24t
    15·1 answer
  • Users in a corporation currently authenticate with a username and password. A security administrator wishes to implement two-fac
    8·1 answer
  • What is this tool called?
    5·2 answers
  • What determines the keystroke to open a cmos editor? how can you find this information?
    15·1 answer
  • Online Book Merchants offers premium customers 1 free book with every purchase of 5 or more books and offers 2 free books with e
    11·1 answer
  • Suppose you have one particular application that is trying to send data on the Internet but none of the data is making it to the
    15·2 answers
  • Which among the following choices is correct based on the two statements listed below? Statement 1: When the lexical analyzer sc
    12·1 answer
  • You are an IT technician for your company. One of your employees has a computer that continually reboots when it is powered on.
    8·1 answer
  • Eric would like to have a callout text box that makes it look as if the character in an image is speaking. Which object should h
    12·2 answers
  • ___________ is some danger that can exploit a vulnerability.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!