Project 04 Description For this lab you will write a Java program that will play
ID: 3886838 • Letter: P
Question
Project 04 Description
For this lab you will write a Java program that will play a simple game of Dragon Trainers. The rules 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 generate a random choice of Fire, Plant or Water. It should then prompt the user for their choice. It should report both choices and indicate who wins the game. In addition, it should check the user's input and if the user inputs an invalid choice it should replace the user's choice with a valid default choice instead cause the user to lose the game by default (see the transcript examples below).
Following the instructions from Closed Lab 01, create a new folder named Project04 and a new Java program in that folder named Project04.java for this assignment.
Project 04 Sample Ouptut
This is a sample transcript of what your program should do. Items in bold are user input and should not be put on the screen by your program.
Please select one of your dragons [Fire/Plant/Water]: Fire
You chose: Fire dragon
I chose: Water dragon
Water defeats Fire - you lose!
Your code will behave differently based on the random value it selects and the choice taken by the user. Here is a second possible execution of this code:
Please select one of your dragons [Fire/Plant/Water]: Plant
You chose: Plant dragon
I chose: Water dragon
Plant defeats Water - you win!
In the case of a tie, your code should report a tie:
Please select one of your dragons [Fire/Plant/Water]: Water
You chose: Water dragon
I chose: Water dragon
A Tie!
If the user enters an invalid choice, your code should report an error and the human player loses the game:
Please select one of your dragons [Fire/Plant/Water]: q
You don't have a q dragon, so you choose no dragons.
I chose: Fire dragon
You lose by default!
Your code should work even if the player only puts in the first letter of their choice:
Please select one of your dragons [Fire/Plant/Water]: W
You chose: Water dragon
I chose: Fire dragon
Water defeats Fire - you win!
For this assignment you should allow the user to enter either capital or lowercase letters to make their choice:
Please select one of [Fire/Plant/Water]: plant
You chose: plant dragon
I chose: Fire dragon
Fire defeats plant - you lose!
Please select one of [Fire/Plant/Water]: PLANT
You chose: PLANT dragon
I chose: Fire dragon
Fire defeats PLANT - you lose!
NOTE: To generate a random choice, you will need to use the Math.random() function as used in Lab 03. You will need to modify it to select an integer value between 1 and 3 and assign Fire, Plant or Water according to the number generated (for example, you might decide that 1 is Fire, 2 is Plant and 3 is Water).
Project 04 Extra Credit
For up to two points of Extra Credit, modify the game to represent a "tournament" of dragon trainers. Instead of just playing one game and ending, play until one player has won two games, then show the final score as well as the total number of matches played. Note that you will need to include a loop in your code to make this work:
Please select one of your dragons [Fire/Plant/Water]: Fire
You chose: Fire dragon
I chose: Water dragon
Water defeats Fire - you lose!
Please select one of your dragons [Fire/Plant/Water]: Plant
You chose: Plant dragon
I chose: Water dragon
Plant defeats Water - you win!
Please select one of your dragons [Fire/Plant/Water]: Water
You chose: Water dragon
I chose: Water dragon
A Tie!
Please select one of your dragons [Fire/Plant/Water]: Water
You chose: Water dragon
I chose: Fire dragon
Water defeats Fire - you win!
Out of 4 matches you won 2, I won 1, and we tied 1.
Congratulations - you win the tournament!
Explanation / Answer
package javapractice;
import java.util.Random;
import java.util.Scanner;
public class Project04 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String dragons[]= {"Fire","Plant","Water"};
Random random = new Random();
int numberShowing = random.nextInt(3);
String randomChoice=dragons[numberShowing];
Scanner sc=new Scanner(System.in);
System.out.println("please select one of your dragons[Fire/Plant/Water]:");
String choice=sc.next();
if(choice.length()==1)
{
int i=0;
for(i=0;i<3;i++)
{
if(dragons[i].charAt(0)==choice.charAt(0))
{
choice=dragons[i];
break;
}
}
if(i==3)
{
System.out.println("you dont have a "+choice+ " dragon,so you choose no dragon");
System.out.println("I choose: "+randomChoice);
}
}
if(choice.equalsIgnoreCase(randomChoice))
{
System.out.println("you choose: "+choice);
System.out.println("I choose: "+randomChoice);
System.out.println("A Tie!");
}
else if(choice.equalsIgnoreCase("plant")&&randomChoice.equalsIgnoreCase("water"))
{
System.out.println("you choose: "+choice);
System.out.println("I choose: "+randomChoice);
System.out.println(choice+" defeats "+randomChoice+" -you won!");
}
else if(choice.equalsIgnoreCase("fire")&&randomChoice.equalsIgnoreCase("plant"))
{
System.out.println("you choose: "+choice);
System.out.println("I choose: "+randomChoice);
System.out.println(choice+" defeats "+randomChoice+" -you won!");
}
else if(choice.equalsIgnoreCase("water")&&randomChoice.equalsIgnoreCase("fire"))
{
System.out.println("you choose: "+choice);
System.out.println("I choose: "+randomChoice);
System.out.println(choice+" defeats "+randomChoice+" -you won!");
}
else
{
System.out.println("you choose: "+choice);
System.out.println("I choose: "+randomChoice);
System.out.println(randomChoice+" defeats "+choice+" -you lost!");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.