General Description: You have been tasked to design an NPC class which will be u
ID: 3880156 • Letter: G
Question
General Description: You have been tasked to design an NPC class which will be used to create NPC objects for an upcoming video game. NPC stands for "non-player character" and will be used to create various random characters in the video game world. Some NPCs may tell a story when talked to, or may say random things when talked to. You are required to implement the NPC class as well as include a Main (driver, client, tester) class to test all functionality of your NPC class. Package Declaration and General Setup Place all files for this project in a package called "hwl". Deductions will be given for programs which do not follow this requirement. . Please include a multi-line comment at the top of each .java file with your name and a brief description of what that class does. - See the Triangle.java source code file for an example of this. The NPc Class: Data Fields name: A string to hold the name of the NPC. The default value is "NO NAME" race: A string to hold the race of the NPC, Races can be anything such a dragon, orc, dwart, eif, human, etc. etc. The default value is "NO RACE" · XPOSA noating-point value to hold the x-coordinate of where the NPC will be placed in the world This value cannot be set to a negative number by a client The default value is-999 0 " yPos A noating-point value to hold the y-coordinate of where the NPC will be placed in the world This value cannot be set to a negative number by a client The default value is-999 0 . dialogue: An array of text holding all of the dialogue that the NPC can speak Each sentence of the NPCs dialogue shall be stored one sentence per index in the array. Some NPCs will say random things, others will tell a sequential story if the text is sequential, each sentence or the dialogue should be listed sequentially in the array starting trom index 0 The default value is a one element array with thetext "PLACEHOLDER TEXT" as the first index in the aray. See below for an example of some text has SequentialDialogue: A boolean indicating whether or not the dialogue is sequential. If so, then the dialog should be displayed in the correct sequence every time the talk) method is called. The default value is talse nextDialogue: An integer indicating the index of the next piece of dialog that will display if the NPC is spoken to. Only used it hassequentialDialogue is set to true. This value is controlled internally. The default value is 0 Constructors Include a default constructor which initializes a default NPC with the required default values for each data tield Include an additional constructor which initializes the name, race, xPos, yPos, dialogue, and hassequentialDialogue of a new NPC object. NOTE: nextDialogue is only handled intenally, should be initialized to o and should not be accessed set by an outside client. GettersExplanation / Answer
Ans : )
//NPC class
import java.util.Random;
public class NPC {
private String name;
private String race;
private float xPos;
private float yPos;
private String dialogue[];
private boolean hasSequentialDialogue;
private int nextDialogue;
public NPC(){
name="NO NAME";
race="NO RACE";
xPos=999.0f;
yPos=999.0f;
dialogue=new String[1];
dialogue[0]="PLACE HOLDER TEXT";
hasSequentialDialogue=false;
nextDialogue=0;
}
public NPC(String name,String race,float xPos,float yPos,String dialogue[],boolean hasSequentialDialogue){
this.name=name;
this.race=race;
this.xPos=xPos;
this.yPos=yPos;
this.dialogue=dialogue;
this.hasSequentialDialogue=hasSequentialDialogue;
this.nextDialogue=nextDialogue+1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRace() {
return race;
}
public void setRace(String race) {
this.race = race;
}
public float getxPos() {
return xPos;
}
public void setxPos(float xPos) {
this.xPos = xPos;
}
public float getyPos() {
return yPos;
}
public void setyPos(float yPos) {
this.yPos = yPos;
}
public String[] getDialogue() {
return dialogue;
}
public void setDialogue(String[] dialogue) {
this.dialogue = dialogue;
}
public boolean isHasSequentialDialogue() {
return hasSequentialDialogue;
}
public void setHasSequentialDialogue(boolean hasSequentialDialogue) {
this.hasSequentialDialogue = hasSequentialDialogue;
}
public String talk(){
String talkStr=null;
Random ran = new Random();
int x = ran.nextInt(dialogue.length);
talkStr=dialogue[x];
return talkStr;
}
public void walk(String direction){
if(direction.equalsIgnoreCase("up"))
yPos=yPos+1;
else if(direction.equalsIgnoreCase("down"))
yPos=yPos-1;
else if(direction.equalsIgnoreCase("left"))
xPos=xPos-1;
else if(direction.equalsIgnoreCase("right"))
xPos=xPos+1;
}
public String toString(){
String output="Name : "+name+" ";
output=output+"Race : "+race+" ";
output=output+"Location : ("+xPos+","+yPos+") ";
output=output+"Sequential : "+hasSequentialDialogue+" ";
String dialogues="";
for(int i=0;i<dialogue.length;i++)
dialogues=dialogues+dialogue[i]+" ";
output=output+"Dialogue : "+dialogues+" ";
return output;
}
}
//NPCMain class
import java.util.Scanner;
public class NPCMain {
public static void main(String[] args) {
NPC npc1=new NPC();
String seqDialogue[]={"Once upon a time, i found sword","The sword was mighty and gave me great strength","a dragon attacked our village one day","I took the magic sword and killed the fearsome dragon!"};
NPC npc2=new NPC("Jim The Dragon Slayer", "Goblin", 22.3f, 10.9f, seqDialogue, true);
String randomDialogue[]={npc2.talk(),npc2.talk(),npc2.talk(),npc2.talk()};
NPC npc3=new NPC("The Dragon Jim", "Goblin Loc", 12.3f, 20.9f, seqDialogue, true);
Scanner sc=null;
while(true){
System.out.println("Choose An NPC :");
System.out.println("1. NO NAME");
System.out.println("2. Jim The Dragon Slayer");
System.out.println("3. The Dragon Jim");
System.out.println("4. Exit");
System.out.println("Enter The number choice(1-4) :");
sc=new Scanner(System.in);
int choice=sc.nextInt();
if(choice==1)
menu2(npc1);
else if(choice==2)
menu2(npc2);
else if(choice==3)
menu2(npc3);
else if(choice==4)
break;
else
System.out.println("invalid choice try again");
}
}
public static void menu2(NPC npc){
Scanner sc=null;
while(true){
System.out.println("Test Menu For :"+npc.getName());
System.out.println("1. Display NPC Information");
System.out.println("2. Talk to NPC");
System.out.println("3. Make NPC to walk up");
System.out.println("4. Make NPC to walk down");
System.out.println("5. Make NPC to walk left");
System.out.println("6. Make NPC to walk right");
System.out.println("7. Go Back to privous Menu");
System.out.println("Enter The number choice(1-7) :");
sc=new Scanner(System.in);
int choice=sc.nextInt();
if(choice==1)
System.out.println(npc.toString());
else if(choice==2)
System.out.println("NPC say Something : "+npc.talk());
else if(choice==3)
npc.walk("up");
else if(choice==4)
npc.walk("down");
else if(choice==5)
npc.walk("left");
else if(choice==6)
npc.walk("right");
else if(choice==7)
break;
else
System.out.println("invalid choice try again");
}
}
}
Explanation : check the code as per your requirements,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.