Question
For this lab you will fill in the skeleton of a Java program that will run a Dragon Trainers. The rules of the game are
For this lab you will fill in the skeleton of a Java program that will run a Dragon Trainers. The rules of the game are simple: each player has three dragons that they have trained, and each dragon is tied to a particular element. Each player has a Fire Dragon, a Water Dragon and a Plant Dragon. Each player picks one dragon to put out in front of them to fight the other player's dragon. Both players choose separately without knowing what the other player has selected. Fire Dragons always beat Plant Dragons, Plant Dragons always beat Water Dragons, and Water Dragons always beat Fire Dragons. If both players choose the same kind of dragon, they tie.
Your program will run a series of games of one player against the computer. First the player will be prompted for a random seed and then a number of matches to play. For each match, the computer will pick a random dragon type and the player will enter their choice as a single letter - 'F' for fire, 'W' for water, or 'P' for plant (the program should accept either uppercase or lowercase, and should only care about the first letter of the user's input). The program should then report both choices as well as who the winner is. This process repeats until all of the matches have been played - at that point the program will report a summary of the matches and decide who won the tournament overall. See the transcripts below for how your output should be formatted.
NOTE if the player enters an invalid choice, the computer wins by default.
NOTE To generate a random choice, this code uses the Random object as we have used in the previous labs. A value of 0 will be Plant, 1 will be Water and 2 will be Fire. Note that you MUST use these assignments of numbers to types for your submission to be able to pass the test cases!
NOTE You MUST only declare and instantiate one single Random object to be able to match the test cases. And this must be done outside of the main game loop and in the main method. Your methods should NOT create their own Random object. Note that this is typically how Random objects are used - you create them once and then use the same Random object for your entire program, rather than creating new ones every time you need a new random number.
NOTE You MUST only declare and instantiation on single Scanner object to be able to run the test cases. And this must be done outside of the main game loop and in the main method. Your methods should NOT create another Scanner object but instead should use the one passed in as a parameter. Note that this is also typically how Scanner objects get used - you create them once and then use the same Scanner object for your entire program.
Create a new project in your workspace and a new class named DragonTrainers. Then paste the code below into the class. Note that this "skeleton" of code includes a main method that has already been written for you. Read the main method and make sure you understand what it is doing and how it is calling the other methods in the program. Comments are included to help you understand what each piece of the code is supposed to be accomplishing. DO NOT CHANGE THE CODE IN THE MAIN METHOD TO GET YOUR SOLUTION TO WORK! Full credit for the assignment will only be earned if you can get the code to work without changing the main method provided.
/** * YOUR DESCRIPTION OF THIS PROGRAM HERE * @author YOUR NAME HERE * @version DATE HERE */ import java.util.Random; import java.util.Scanner; public class DragonTrainers { /** * Constant array to hold the types of the dragon in order. 0 - Plant, 1 - * Water, 2 - Fire. Make sure your code does not change these values! */ private static final String[] DRAGONS = { "Plant", "Water", "Fire" }; /** * Prompts the user with the message: "How many matches will we play? " and * takes in an integer as input. If the user enters a value that is 0 or * negative, displays the error message: "ERROR - number of matches must * be positive!" and asks again. Continues looping until the user enters a * positive number. * * @param input * - Scanner to read values from the keyboard * @return - an integer value strictly larger than zero. */ public static int getNumberOfMatches(Scanner input) { // TODO - complete this method // TODO - the following line is only here to allow this program to // compile. Replace it and remove this comment when you complete // this method. return 0; } /** * Prompts the user with the message "Please select a dragon * [Plant/Water/Fire]: " and waits for user input. If the user enters a * blank line, prints the error message "ERROR - Dragon prompt cannot be * empty" and asks the user again. Repeats until the user enters a non-blank * line. * * @param input * - Scanner to read values from the keyboard * @return A non-empty String entered by the user */ public static String promptForDragon(Scanner input) { // TODO - complete this method // TODO - the following line is only here to allow this program to // compile. Replace it and remove this comment when you complete // this method. return ""; } /** * Takes a single UPPERCASE character. If it is a 'W','P' or 'F' returns the * appropriate numeric value given the dragon types in the array DRAGON * (i.e. 0 for 'P', 1 for 'W', 2 for 'F'). If it is none of these returns a * -1 to represent an invalid value. * * @param dragon * - the UPPERCASE character to look up a value for. * @return 0 if dragon is 'P', 1 if dragon is 'W', 2 if dragon is 'F', -1 * otherwise */ public static int dragonToNumber(char dragon) { // TODO - complete this method // TODO - the following line is only here to allow this program to // compile. Replace it and remove this comment when you complete // this method. return 0; } /** * Takes a number representing the player's choice and another representing * the computer's choice. Returns a 0 if they tie, a 1 if the player wins, * and a -1 if the player loses. Note that the values map to the indexes of * the array DRAGONS above (0 is a Plant dragon, 1 is a Water dragon, 2 is a * Fire Dragon). * * @param player * - value 0-2 representing player dragon choice * @param cpu * - value 0-2 representing computer dragon choice * @return 1 if the player wins, -1 if the computer wins, 0 if they tie */ public static int determineWinner(int player, int cpu) { // TODO - complete this method // TODO - the following line is only here to allow this program to // compile. Replace it and remove this comment when you complete // this method. return 0; } /** * Takes a number representing the player's choice and another number * representing the computer's choice, and a third number that is positive * if the player is the winner, negative if the computer is the winner, and * 0 if they tied. Then displays the appropriate player defeats computer or * computer defeats player or tie message as given in the project * description. * * @param player * index into the DRAGONS array representing the player choice * @param cpu * index into the DRAGONS array representing the computer's * choice * @param winner * 0 for a tie, positive for a player win, negative for a * computer win */ public static void displayMatchResult(int player, int cpu, int winner) { // TODO - complete this method } /** * Takes the number of wins, losses and ties and displays the final message * and summary statistics as given in the project description. * * @param wins * number of total wins for the player * @param losses * number of total losses for the player * @param ties * number of ties */ public static void displayFinalResult(int wins, int losses, int ties) { // TODO - complete this method } /** * NOTE: The main method has been completed for you. If you correctly * complete the methods above, the main method will "just work" and produce * the correct output. */ public static void main(String[] args) { // Prompt for a random number seed Scanner keyboard = new Scanner(System.in); System.out.print("Enter a random seed: "); int seed = Integer.parseInt(keyboard.nextLine()); // Create a Random instance with the seed Random rnd = new Random(seed); // Prompt for number of matches to play int totalMatches = getNumberOfMatches(keyboard); // Start with wins, losses and ties at 0. // Repeat until all matches have been played (use the sum of the // results so we don't need another variable) int wins = 0, losses = 0, ties = 0; while ((wins + losses + ties) 0) { wins++; } else if (winnerYou MUST use the skeleton for your code. Your solution must include the implementation and use of all of the methods defined in this skeleton - think about what each method does and how it relates to the program overall. You MUST NOT make any changes to the mainmethod - violating any of these requirements will cause a deduction in the points earned. Remember - a goal for this assignment is to practice writing methods that conform to a particular specification! The test cases for this program are written to let you test the individual methods as well as the final output. Test incrementally! Write one method at a time and then test it rather than waiting until you've written the whole program and testing it all at once! Taking an incremental approach will help you keep the whole assignment manageable by tackling it in pieces. Note also that you can complete the methods in any order
NOTE The template includes a private constant array named DRAGONS. The order of the Strings in the array DRAGONS matches the mapping of ints to dragon types required by this assignment. Since DRAGONS is declared at the class level - outside of any method - it is in scope for all of the methods in the class. You should use this array in your solutions, but do NOT change the values in this array when you run your solution.
NOTE 2: It is easy to write an overly complex determineWinner method if you think about it as checking every possible combination of choices for the player and the computer. If you think about the order required and given in the array in the code, you can see that it has been purposefully set up so that they are in a particular order. The dragon type at position 1 is beaten by the type at position 0, the type at position 2 is beaten the type at position 1, and the type at position 0 is beaten by the type at position 2. Notice that this is NOT strictly "the bigger index is beaten by the opponent", nor is it exactly "you beat your opponent if your index is exactly 1 smaller than their index", but there is a fairly elegant way to the comparisons without just "brute forcing" all of the possible combinations. Think about how to write a determineWinner method that makes use of this ordering instead of just brute forcing all of the possible combinations in a giant if-else-if-else statement. HINT One approach involves the fact that the winner of the pair is exactly one smaller than the loser in 2 of the 3 cases.
Examples of user input:
Enter a random seed: 33 How many matches will we play? 3 Please select a dragon [Plant/Water/Fire]: F I chose: Plant dragon You chose: Fire dragon. Fire defeats Plant - you win! Please select a dragon [Plant/Water/Fire]: W I chose: Plant dragon You chose: Water dragon Plant defeats Water - you lose! Please select a dragon [Plant/Water/Fire]: P I chose: Fire dragon You chose: Plant dragon Fire defeats Plant - you lose! The tournament is over! We tied O matches. I won 2 matches. You won 1 matches I am the winner! Here is another example of the game in action. Note that if the user enters an invalid response, the computer wins by default. But also note that only the first character of the input matters when determining whether or not the input is valid: Enter a random seed: 24 How many matches will we play? 3 Please select a dragon [Plant/Water/Fire]: f I chose: Water dragon You chose: Fire dragon Water defeats Fire - you lose! Please select a dragon [Plant/Water/Fire]: alsdf I chose: Water dragon You don't have the alsdf dragorn So no dragon fights for you I win by defaultl Please select a dragon [Plant/Water/Fire]: pfjslkd I chose: Plant dragon You chose: Plant dragon. A Tiel The tournament is over! We tied 1 matches. I won 2 matches You won matches. I am the winner! If there is a tie, the final message should indicate that neither player is the winner as in the transcript below Enter a random seed: 33 How many matches will we play? 1 Please select a dragon [Plant/Water/Fire]: P I chose: Plant dragon. You chose: Plant dragon A Tie! The tournament is over e tied 1 matches I won matches You won 0 matches Neither of us can claim victory here! Note that if the user enters a non-positive value for the number of matches, the program should reject it and make them enter a positive value, and if the user enters a blank line for the dragon the program should reject it and make them enter some non-empty value: Enter a random seed: 33 How many matches will we play? e ERROR number of matches must be positive! How many matches will we play? -1 ERROR number of matches must be positive! How many matches will we play? 1 Please select a dragon [Plant/Water/Fire]: ERROR Dragon prompt cannot be empty Please select a dragon [Plant/Water/Fire]: P I chose: Plant dragon You chose: Plant dragon A Tie! The tournament is over! We tied 1 matches I won matches. You won 0 matches Neither of us can claim victory here! Enter a random seed: 33 How many matches will we play? 3 Please select a dragon [Plant/Water/Fire]: F I chose: Plant dragon You chose: Fire dragon. Fire defeats Plant - you win! Please select a dragon [Plant/Water/Fire]: W I chose: Plant dragon You chose: Water dragon Plant defeats Water - you lose! Please select a dragon [Plant/Water/Fire]: P I chose: Fire dragon You chose: Plant dragon Fire defeats Plant - you lose! The tournament is over! We tied O matches. I won 2 matches. You won 1 matches I am the winner! Here is another example of the game in action. Note that if the user enters an invalid response, the computer wins by default. But also note that only the first character of the input matters when determining whether or not the input is valid: Enter a random seed: 24 How many matches will we play? 3 Please select a dragon [Plant/Water/Fire]: f I chose: Water dragon You chose: Fire dragon Water defeats Fire - you lose! Please select a dragon [Plant/Water/Fire]: alsdf I chose: Water dragon You don't have the alsdf dragorn So no dragon fights for you I win by defaultl Please select a dragon [Plant/Water/Fire]: pfjslkd I chose: Plant dragon You chose: Plant dragon. A Tiel The tournament is over! We tied 1 matches. I won 2 matches You won matches. I am the winner! If there is a tie, the final message should indicate that neither player is the winner as in the transcript below Enter a random seed: 33 How many matches will we play? 1 Please select a dragon [Plant/Water/Fire]: P I chose: Plant dragon. You chose: Plant dragon A Tie! The tournament is over e tied 1 matches I won matches You won 0 matches Neither of us can claim victory here! Note that if the user enters a non-positive value for the number of matches, the program should reject it and make them enter a positive value, and if the user enters a blank line for the dragon the program should reject it and make them enter some non-empty value: Enter a random seed: 33 How many matches will we play? e ERROR number of matches must be positive! How many matches will we play? -1 ERROR number of matches must be positive! How many matches will we play? 1 Please select a dragon [Plant/Water/Fire]: ERROR Dragon prompt cannot be empty Please select a dragon [Plant/Water/Fire]: P I chose: Plant dragon You chose: Plant dragon A Tie! The tournament is over! We tied 1 matches I won matches. You won 0 matches Neither of us can claim victory here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started