81.1k views
2 votes
Monster Collector

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 i 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.

User Pejvan
by
3.2k points

1 Answer

1 vote

Answer:

In java:

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int monsternum, usernum;

int count = 0;

Random rand = new Random();

String [] monsters = {"wild pikamoo","wild bulbaroar"};

for(int i =0;i<2;i++){

monsternum = rand.nextInt(5);

System.out.print("A "+monsters[i]+" appears! Guess a number between 1 and 5: ");

usernum = input.nextInt();

if(monsternum == usernum){ count++; System.out.println("Congratulations, you caught a "+monsters[i]+"!"); }

else{ System.out.println("You almost had it, but the monster escaped."); }

}

System.out.println("There are no more monsters to encounter!");

System.out.println("You caught "+count+" monsters of 2");

if(count!=2){ System.out.print("Keep training to be the very best!"); }

else{ System.out.print("You're the monster collector master!"); }

}

}

Step-by-step explanation:

This declares monster and user number as integers

int monsternum, usernum;

Initialize count to 0

int count = 0;

Call the random object

Random rand = new Random();

Create a string array to save the monster names

String [] monsters = {"wild pikamoo","wild bulbaroar"};

Iterate for the 2 monsters

for(int i =0;i<2;i++){

Generate a monster number

monsternum = rand.nextInt(5);

Prompt the user to take a guess

System.out.print("A "+monsters[i]+" appears! Guess a number between 1 and 5: ");

Get user guess

usernum = input.nextInt();

If monster number and user guess are equal, congratulate the user and increase count by 1

if(monsternum == usernum){ count++; System.out.println("Congratulations, you caught a "+monsters[i]+"!"); }

If otherwise, prompt the user to keep trying

else{ System.out.println("You almost had it, but the monster escaped."); }

}

Print no monsters again

System.out.println("There are no more monsters to encounter!");

Print number of monsters caught

System.out.println("You caught "+count+" monsters of 2");

Print user score

if(count!=2){ System.out.print("Keep training to be the very best!"); }

else{ System.out.print("You're the monster collector master!"); }

User Ross Peoples
by
3.4k points