So the code I have below creates a 20 by 20 2D array. The array is then filled u
ID: 3839419 • Letter: S
Question
So the code I have below creates a 20 by 20 2D array. The array is then filled up with words made up in the program. The user then is given a random word inside of the 2D array and asked to guess its coordinates. Well after he manages to guess the coordinates of the first word he is given, the program doesnt start over and give him a new word. So could someone right a segment of code that after the user has found the coordinates to the first word then they would recieve a new word that was inside the 2D array and the program would repeat itself, asking the user to guess the coordinates of the new word given to them.
This segment of code would be located in the class WordSearch. I think all you need to do is create a if statement saying if the user enters the coordinates matching the word they were given then print out a new random word from the 2D array.
Thank you!
package wordsearch;
/**
*
* Things class defines any thing
*
*/
public class Things {
String thing;
/**
* Initializes thing to *
*/
public Things() {
thing = "*";
}
/**
* initializes thing to thing
* @param thing
*/
public Things(String thing) {
this.thing = thing;
}
/**
* returns the thing
* @return thing
*/
public String getThing()
{
return thing;
}
}
Fruits.java
package wordsearch;
/**
* This class represents fruit thing
*
*/
public class Fruits extends Things{
/**
* List of fruits to feed into the things
*/
static String fruits[] = {"Mango", "Apple", "Orange", "Pears", "Strawberry", "Raspberry", "Grape",
"Banana", "Blackberry", "Blueberry", "Papaya", "Kiwi", "Apricot", "Jack fruit", "Pomegranate",
"Guava", "Melon", "Dragon fruit", "Pineapple","Peach"};
/**
* initializes fruit thing to *
*/
public Fruits() {
super();
}
/**
* initializes fruit thing to fruit
* @param fruit
*/
public Fruits(String fruit) {
super(fruit);
}
}
Drinks.java
package wordsearch;
/**
* This class represents Drink thing
*
*/
public class Drinks extends Things{
/**
* initializes drink thing to *
*/
public Drinks() {
super();
}
/**
* initializes drink thing to fruit
* @param drink
*/
public Drinks(String drink) {
super(drink);
}
/**
* List of drinks to feed into the things
*/
static String drinks[] = {"Milkshake", "Soda", "Water", "Hot chocolate", "Tea", "Coffee",
"Green Tea", "Juice", "Coke", "Lemonade", "Cocktail", "Moctail",
"Beer", "Red Wine", "White Wine", "Champagne", "Rum", "Cider", "Whiskey", "Brandy"};
}
WordSearch.java
package wordsearch;
import java.util.Random;
import java.util.Scanner;
/**
* Word Search class to play the word search game
*
*/
public class WordSearch {
/**
* scanner object to read the coordinates
* Two dimensional thing array which stores fruits and drink in 20X20 array
*/
static Scanner keyboard;
static Things things[][];
/**
* gets the coordinate values from user
* exits the program if the input is -1
* if the input is less than zero or greater than 19, asks the user to enter the coordinate again
* returns the valid coordinate
* @return valid input for the coordinate
*/
public static int getInt()
{
int value;
while(true)
{
value = keyboard.nextInt();
if(value == -1)
{
System.out.println("Thank you for playing!");
System.exit(0);
}
else if(value<0 || value >=20)
{
System.out.println("Enter a coordinate between 0 and 19");
continue;
}
else break;
}
return value;
}
/**
* Games plays here by showing the random selected word to the user and asking the user to
* guess the coordinates of the thing
* calls getInt() to read the input
* Prints the word at the inputed coordinate if it is wrong
* otherwise tells the user that they've guessed it right and starts playing again.
* @param thing
*/
public static void play(String thing)
{
System.out.println(" The word is " + thing);
while(true)
{
System.out.println("what coordinate would you like to check first (-1 to quit)? ");
int row = getInt();
int column = getInt();
if(things[row][column].getThing().equalsIgnoreCase(thing))
{
System.out.println("You guessed it right!");
}
else
{
System.out.println("Try again!");
System.out.println("You guessed " + things[row][column].getThing());
}
}
}
/**
* Initializes scanner and things objects and fills the things array with random values
* starts playing the game by calling play()
*/
public static void main(String[] args) {
keyboard = new Scanner(System.in);
things = new Things[20][20];
Random rand = new Random();
//filling up the array
for(int i=0; i<20; i++)
{
things[i] = new Things[20];
for(int j=0; j<10; j++)
{
things[i][j] = new Fruits(Fruits.fruits[rand.nextInt(20)]);
}
for(int j=10; j<20; j++)
{
things[i][j] = new Drinks(Drinks.drinks[rand.nextInt(20)]);
}
}
while(true)
{
int row = rand.nextInt(20);
int column = rand.nextInt(20);
play(things[row][column].getThing());
}
}
}
OUTPUT:
The word is Beer
what coordinate would you like to check first (-1 to quit)?
1 2
Try again!
You guessed Melon
what coordinate would you like to check first (-1 to quit)?
3 5
Try again!
You guessed Peach
what coordinate would you like to check first (-1 to quit)?
8 9
Try again!
You guessed Guava
what coordinate would you like to check first (-1 to quit)?
12 43
Enter a coordinate between 0 and 19
12 17
Try again!
You guessed Moctail
what coordinate would you like to check first (-1 to quit)?
-1
Thank you for playing!
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Sam
*/
/**
*
* Things class defines any thing
*
*/
class Things {
String thing;
/**
* Initializes thing to *
*/
public Things() {
thing = "*";
}
/**
* initializes thing to thing
* @param thing
*/
public Things(String thing) {
this.thing = thing;
}
/**
* returns the thing
* @return thing
*/
public String getThing()
{
return thing;
}
}
/**
* This class represents fruit thing
*
*/
class Fruits extends Things{
/**
* List of fruits to feed into the things
*/
static String fruits[] = {"Mango", "Apple", "Orange", "Pears", "Strawberry", "Raspberry", "Grape",
"Banana", "Blackberry", "Blueberry", "Papaya", "Kiwi", "Apricot", "Jack fruit", "Pomegranate",
"Guava", "Melon", "Dragon fruit", "Pineapple","Peach"};
/**
* initializes fruit thing to *
*/
public Fruits() {
super();
}
/**
* initializes fruit thing to fruit
* @param fruit
*/
public Fruits(String fruit) {
super(fruit);
}
}
/**
* This class represents Drink thing
*
*/
class Drinks extends Things{
/**
* initializes drink thing to *
*/
public Drinks() {
super();
}
/**
* initializes drink thing to fruit
* @param drink
*/
public Drinks(String drink) {
super(drink);
}
/**
* List of drinks to feed into the things
*/
static String drinks[] = {"Milkshake", "Soda", "Water", "Hot chocolate", "Tea", "Coffee",
"Green Tea", "Juice", "Coke", "Lemonade", "Cocktail", "Moctail",
"Beer", "Red Wine", "White Wine", "Champagne", "Rum", "Cider", "Whiskey", "Brandy"};
}
/**
* Word Search class to play the word search game
*
*/
public class WordSearch {
/**
* scanner object to read the coordinates
* Two dimensional thing array which stores fruits and drink in 20X20 array
*/
static Scanner keyboard;
static Things things[][];
/**
* gets the coordinate values from user
* exits the program if the input is -1
* if the input is less than zero or greater than 19, asks the user to enter the coordinate again
* returns the valid coordinate
* @return valid input for the coordinate
*/
public static int getInt()
{
int value;
while(true)
{
value = keyboard.nextInt();
if(value == -1)
{
System.out.println("Thank you for playing!");
System.exit(0);
}
else if(value<0 || value >=20)
{
System.out.println("Enter a coordinate between 0 and 19");
continue;
}
else break;
}
return value;
}
/**
* Games plays here by showing the random selected word to the user and asking the user to
* guess the coordinates of the thing
* calls getInt() to read the input
* Prints the word at the inputed coordinate if it is wrong
* otherwise tells the user that they've guessed it right and starts playing again.
* @param thing
*/
public static void play(String thing)
{
System.out.println(" The word is " + thing);
while(true)
{
System.out.println("what coordinate would you like to check first (-1 to quit)? ");
int row = getInt();
int column = getInt();
if(things[row][column].getThing().equalsIgnoreCase(thing))
{
System.out.println("You guessed it right!");
}
else
{
System.out.println("Try again!");
System.out.println("You guessed " + things[row][column].getThing());
}
}
}
/**
* Initializes scanner and things objects and fills the things array with random values
* starts playing the game by calling play()
*/
public static void main(String[] args) {
keyboard = new Scanner(System.in);
things = new Things[20][20];
Random rand = new Random();
//filling up the array
for(int i=0; i<20; i++)
{
things[i] = new Things[20];
for(int j=0; j<10; j++)
{
things[i][j] = new Fruits(Fruits.fruits[rand.nextInt(20)]);
}
for(int j=10; j<20; j++)
{
things[i][j] = new Drinks(Drinks.drinks[rand.nextInt(20)]);
}
}
while(true)
{
int row = rand.nextInt(20);
int column = rand.nextInt(20);
play(things[row][column].getThing());
}
}
}
Let me know if this code satisfy you :P
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.