In: Computer Science
Write this program using an IDE. Comment and style the code according to the CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct.
Monster collector is a game of chance, where the user tries to collect monsters by guessing the correct numbers between 1 and 5. If the user doesn't guess the incorrect number, you catch the monster, otherwise, it gets away!
Example output:
Welcome to Monster Collector, collect 2 monsters to win! A wild pikamoo appears! Guess a number between 1 and 5 5 You almost had it, but the monster escaped. A wild bulbaroar appears! Guess a number between 1 and 5 1 Congratulations, you caught bulbaroar! There are no more monsters to encounter! You caught 1 monsters of 2 Keep training to be the very best!
Welcome to Monster Collector, collect 2 monsters to win! A wild pikamoo appears! Guess a number between 1 and 5 3 Congratulations, you caught pikamoo! A wild bulbaroar appears! Guess a number between 1 and 5 1 Congratulations, you caught bulbaroar! There are no more monsters to encounter! You caught 2 monsters of 2 You're the monster collector master!
A more detailed explanation of the requirements for each method will be in the method header comments - please follow these closely. Suggested order of completion: getMonster(), catchMonster(), printResult() then main().
Config.java contains an array of monsters, and the seed for your random number generator.
Implementation in JAVA;
// import scanner class
import java.util.Scanner;
//class config.java
public class Config {
// main method
public static void main(String[] args) {
System.out.println();
// String monster array
String monster[]= {"wild
bulbaroa","Werewolf Mask","wild pikamoo","Giant
Squid","Chupacabra."};
// generating random numbers in
range of 1 to 5
// num is number of cases
int num = (int) (Math.random() * (5
- 1 + 1) + 1);
System.out.println("Welcome to
Monster collector , collect "+num+" monsters to win !");
System.out.println();
// call get monster method
// it return the results by the help
of catchmonster() method
int
result=get_monster(monster,num);
System.out.println();
// print result
print_result(num,result);
}
// get monster method
// it return the results by the help of catchmonster()
method
public static int get_monster(String monster[],int
num) {
int result=0;
// will run no. of cases
while(num>0)
{
//
generate random number for monster
int mon= (int) (Math.random() * (5 - 1 + 1) +
1);
String monst = monster[mon-1];
System.out.println("A "+monst+" appers! Guess a
number between 1 and 5 ");
//
call catchmonster method if catch result will
increase by 1
if(catchMonster(monster,mon)) {
result++ ;
}
System.out.println();
num--;
}
System.out.println("There are no more monsters to
encounter!");
return result;
}
// boolean catchMonster method will return true if
the user entered guess
// and the argumrnted num are equal i.e user guess
correctly
// else return false
public static boolean catchMonster(String monster[],
int num) {
Scanner s= new
Scanner(System.in);
int guess=s.nextInt();
if(guess==num) {
System.out.println("Congratulations, you caught
"+monster[num-1]+"!");
return
true;
}
else {
System.out.println("You almost had it, but the monster
escaped.");
return
false;
}
}
// print the result of game
public static void print_result(int num, int result)
{
if(num==result) {
System.out.println("You caught "+result+" monsters of "+num);
System.out.println("You're the monster collector master!");
}
else {
System.out.println("You caught "+result+" monsters of "+num);
System.out.println("Keep training to be the very best!");
}
}
}
// NOTE: As per mentioned in question monsters are set by using random number generated by random generator with in given range. we only check the guess (correct / incorrect).
SAMPLE OUTPUT:
1.
2.
3.
// PLEASE THUMBS-UP AND RATE POSITIVELY
If you have any doubt regarding this question please ask me in
commnets
// THANK YOU:-)