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
Alex’s family members live in different parts of the world. They would like to discuss the wedding plans of one of their distant
goldenfox [79]
The answer would be b
8 0
4 years ago
Read 2 more answers
A) What is the maximum value that can be represented as an unsigned n-bit binary integer?
My name is Ann [436]

Answer:

The maximum value that are represented as unsigned n -bit binary integer is 2^n-1. The unsigned binary integer refers to the fixed point of the system that does not contain any fractional digits.

The unsigned binary integer contain module system with the power 2. The number of student table in the class is the best example of the unsigned integer. The numbers can be represented by using the binary notation and bits in the computer system.

5 0
3 years ago
Both UDP and TCP use port numbers to identify the destination entity when delivering a message. Give at least one reason for why
raketka [301]

Answer:

Follows are the solution to this question:

Explanation:

The process ID is not static because this can't be used to identity, therefore, it includes excellent service providers like HTTP since it is allocated dynamically only to process whenever a process is initiated.

Sometimes its instance connectors are managed on numerous TSAPs. This can be implemented unless the process ID is being used as each procedure could have an identity.

4 0
3 years ago
Distinguish between engineering and architecture​
Liula [17]

difference between engineering and architecture are.

A engineer is a person whose job involves designing and building engines, machines, roads, bridges ,etc .

While architect design buildings only.

3 0
3 years ago
Describe the basic features of the relational data model and discuss their importance to the end user and the designer. Describe
Mandarinka [93]

Answer:

The answer to this question can be described as follows:

Explanation:

Relational data model:

The use of data tables to organize sets of entities into relationships requires a relational data model. this model work on the assumption, which is a primary key or code, that is included in each table configuration. The symbol for "relational" data links and information is used by other tables.

Model Design:

This model is used for database management, it consists of structure and language consistency. It is design in 1969.

Importance of data model:  

This provides a common standard for processing the potentially sound data in machines, that was usable on almost any one device.  

Big Data:

It moves to locate new and innovative ways to handle large volumes of authentication tokens and to gather business insights when offering high efficiency and usability at an affordable cost at the same time.

6 0
4 years ago
Other questions:
  • Please answer fast screenshot included - thanks in advance
    11·1 answer
  • Which extensions can help drive installs of your mobile app?
    12·1 answer
  • Where is the typical location of a touchpad inside of a laptop?
    13·1 answer
  • An overall indication of the dependability of data may be obtained by examining the ________, credibility, reputation, and _____
    15·2 answers
  • Write a function so that the main program below can be replaced by the simpler code that calls function mph_and_minutes_to_miles
    7·1 answer
  • What are the advantages to using a linked implementation as opposed to an array implementation?
    8·1 answer
  • "which type of network connects smart devices or consumer electronics within a range of about 30 feet (10 meters) and without th
    7·1 answer
  • Initialized the variable with the value 0
    11·2 answers
  • Vadik is creating a program where the user inputs their grade level and the program tells them which sports teams they are allow
    9·2 answers
  • What option in the zone aging/scavenging properties dialog box will prevent dns record time stamps from being updated too often?
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!