Building on H11a ; 1. Delete the loop that prints out 10 words from the ArrayLis
ID: 3716071 • Letter: B
Question
Building on H11a ;
1. Delete the loop that prints out 10 words from the ArrayList..
2. Get one random word from the ArrayList.
3. Make a guess word consisting of underscores. It should be the same length as the word.
3. Ask your user to guess a character in the word. If it is there put the character in the correct spot in the guess word.
4. If it is not there, that is a strike against the user. (10 strikes and he loses).
5. If the user guesses the word, he wins.
Suppose you wanted to put the character ‘k’ at index 4 in string s.
You would say
s.substring(0,4)+’k’+s.substring(5)
because s.substring(0,4) starts at index 0 and goes up to BUT NOT INCLUDING index 4, then you put the 'k' and then s.substring(5) starts at index 5 and goes to the end of the string
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class H11A {
public static void main(String[] args) {
ArrayList<String> ArrayList = getInput();
System.out.println("First Word in the ArrayList: "+ ArrayList.get(0));//first word in the ArrayList
System.out.println("Last Word in the ArrayList: "+ ArrayList.get( ArrayList.size()-1));//last word in the ArrayList
System.out.println();
Random r = new Random();
int low = 0;
int high = 4580;
for (int i=0;i<10;i++) {
int randomNum = r.nextInt(high - low) + low;
System.out.println("The position " + randomNum + " is " + ArrayList.get(randomNum));
}
}
public static ArrayList<String> getInput() {
Scanner sacnnerRead = null; //Scanner that can Read
try {
File file = new File("words.txt"); //creat file object based on the file name "words.txt"
if (file.exists()) { //check if file exist or not
ArrayList<String> wordList = new ArrayList<String>(); //ArrayList of String
sacnnerRead = new Scanner(file);//Scanner Read object based on file object
while (sacnnerRead.hasNextLine()) { //Iterating each line in the File
wordList.add(sacnnerRead.nextLine().trim());
}
return wordList;//retirning arraylist
}
else {
System.out.println("The input File is " + "" + "not found");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (sacnnerRead != null) {
try {
sacnnerRead.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
}
Explanation / Answer
/*Building on H11a ;
1. Delete the loop that prints out 10 words from the ArrayList..
2. Get one random word from the ArrayList.
3. Make a guess word consisting of underscores. It should be the same length as the word.
4. Ask your user to guess a character in the word. If it is there put the character in the correct spot in the guess word.
5. If it is not there, that is a strike against the user. (10 strikes and he loses).
6. If the user guesses the word, he wins.
Suppose you wanted to put the character ‘k’ at index 4 in string s.
You would say
s.substring(0,4)+’k’+s.substring(5)
because s.substring(0,4) starts at index 0 and goes up to BUT NOT INCLUDING index 4, then you put the 'k' and then s.substring(5) starts at index 5 and goes to the end of the string
*/
//Please test with the words.txt file contating the list of words.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class H11A
{
public static void main(String[] args)
{
//Call the File and collect the words from file to arraylist
ArrayList<String> ArrayList = getInput();
System.out.println("First Word in the ArrayList: "+ ArrayList.get(0));//first word in the ArrayList
System.out.println("Last Word in the ArrayList: "+ ArrayList.get( ArrayList.size()-1));//last word in the ArrayList
System.out.println();
Random r = new Random();
int low = 0;
int high = 4580;
high = ArrayList.size();// here i have limited the high number to max words in arraylist.
//while (true)
//{
startGame(high, low, ArrayList);// This call the method to strt the game.
//can be placed under infinite loop of controlled loop for mupliple iteration.
//}
/*
//This loop is to be deleted as per requirement number 1
//below FOR loop iterates for 10 times and prints 10 random words from the list.
for (int i=0;i<10;i++)
{
int randomNum = r.nextInt(high - low) + low;
System.out.println("The position " + randomNum + " is " + ArrayList.get(randomNum));
}
*/
}
public static void startGame(int high, int low, ArrayList ArrayListWord)
{
//Take a object of Random
Random r = new Random();
//requirement number 2
//select a random word index from the file list scanned through getInput() , here high and low are the two bound index of the array.
int randomWordIndx = r.nextInt(high - low) + low;
//fetch the word form the collected random index
String word = (String) ArrayListWord.get(randomWordIndx);
//get the length of the word extracted - the legth will provide grip on level of difficulty to be adjusted in generating new words with underscore
int wordLength = word.length();
////System.out.println("Selected Word:"+word);
//set the level to half the length of word.
int charChangeLength = wordLength/2;
////String testWrd = word;
char[] selWrd = word.toCharArray();//original selected word
char[] testWrd = word.toCharArray();//new word string with underscores
int count = 0;
//generate new string with underscores using random places within the word
//requirement number 3
for (int i=0;i<charChangeLength;i++)
{
int randomChar = r.nextInt(wordLength);
if(testWrd[randomChar]!='_')
{
testWrd[randomChar] = '_';
count++;
}
}
//store the new word
String newWord = new String(testWrd);
//display on console the question/ new word for guessing
System.out.println("Please complete the Word:"+newWord);
// variable to moniter the wrong entries by user - when reaches 10 user LOSES.
int wrongEntries = 0;
//set the scanner for taking input from user- char by char
Scanner sc = new Scanner(System.in);;
while(true)
{// here we collect the input character from user and check if it matches with original string
if(wrongEntries>=10)
{
//requirement number 5
System.out.println("YOU LOSE...");
break;
}
//flag to monitor if user had made any correct attempt in guessing the word.
//requirement number 4
int flg=0;
System.out.println("Please complete the Word:"+newWord);
System.out.print("Enter Character: ");
char input = sc.next().toCharArray()[0];
//Loop to compare the user entry with the original word at the position of Underscore
for (int i = 0; i < wordLength; i++) {
if(testWrd[i]=='_')
{
if(input==word.charAt(i))
{
System.out.println("Correct Entry");
newWord = newWord.substring(0, i)+input+newWord.substring(i+1);
flg++;
}
}
}
if (flg<=0)
{// if no success display message for wrong entry and update the variable a
//requirement number 5
System.out.println("Wrong Entry...");
wrongEntries++;
}
else if (flg == count)
{// if all words are over display message to user that the guess is correct and he wins.
//requirement number 6
System.out.println("YOU WIN...");
break;
}
else
{// update the counter for remaining character to be guessed.
count = count - flg;
}
}
}
public static ArrayList<String> getInput()
{
Scanner sacnnerRead = null; //Scanner that can Read
try
{
File file = new File("C:\words.txt"); //creat file object based on the file name "words.txt"
// PLease update the patch as per the requirement
if (file.exists())
{ //check if file exist or not
ArrayList<String> wordList = new ArrayList<String>(); //ArrayList of String
sacnnerRead = new Scanner(file);//Scanner Read object based on file object
while (sacnnerRead.hasNextLine())
{ //Iterating each line in the File
wordList.add(sacnnerRead.nextLine().trim());
}
return wordList;//retirning arraylist
}
else
{
System.out.println("The input File is " + "" + "not found");
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (sacnnerRead != null)
{
try
{
sacnnerRead.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
return null;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.