Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2012, 09:42 PM   PM User | #1
Staceadam
New Coder

 
Join Date: Nov 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Staceadam is an unknown quantity at this point
Buddy Game

Starting a game for my Java class but having trouble getting it going. Any code or help would be awesome.

Buddy Caretaker
Create a game that instantiates a buddy that may be a pet, creature, car, plant, etc. You need to select a type of buddy and at least five properties of at least three different types. The game must include at least three different activities that you can do with your buddy (example: Cat – feed, pet, clean litter box). There needs to a point system that determines the well-being of your buddy. (example: 5 – points for feeding, 1 – for petting and negative 5 – for cleaning the litter box) The needs to be a method to determine the wellbeing of your buddy (example: Good if points are between -3 and 3, poor if the points are between -6 and 6 and not Good, and dead if less than -6 or more then 6.)

The game should continuously ask the user which activity the user wants to perform. The activities must be listed including a sentinel value. After each activity, the program needs to display the wellbeing of the buddy. This should continue until the buddy dies or the sentinel value is entered.

Example:
Buddy Caretaker
What is your buddies name: Ily
Do you want to (F)eed Ily, (P)et Ily, (C)lean Ily’s litter box, or (G)o to bed? F
Buddy is Feeling Poor
Do you want to (F)eed Ily, (P)et Ily, (C)lean Ily’s litter box, or (G)o to bed? C
Buddy is Feeling Good
Do you want to (F)eed Ily, (P)et Ily, (C)lean Ily’s litter box, or (G)o to bed? C
Buddy is Feeling Poor
Do you want to (F)eed Ily, (P)et Ily, (C)lean Ily’s litter box, or (G)o to bed? C
Buddy is Feeling Dead
Staceadam is offline   Reply With Quote
Old 11-07-2012, 10:15 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This one will be somewhat tricky to give advice since its a school assignment and I don't know what it is that you know.

Now does the instructions indicate that you are allowed/have to to make multiple different types of buddies? Can you choose between a cat, a car, a shovel, etc? Or is it just one type? If you are to make multiple that the user can select, I'd suggest that Buddy be an interface for simplicity. If its only the one, then you can simply create a single class for Buddy.
Next, you have a class Activity. This class will contain a field for the condition change amount as well as a description of it. Set these during the constructor and allow it the accessor methods it needs. Mutators are optional.

Buddy properties will include: name, condition, and activities. Activities can be an Activity[], a Collection<Activity>, or individual properties (activity1, activity2, etc). If you can't think up any other properties to give it, separate activities will fulfill the requirements of 5 properties and 3 different datatypes (string, int, activity, activity, activity), but does complicate the code when needing to check on stuff. I'd suggest a collection or array for that, and make up some other stuff. Easy if you can only create one type of buddy, you can give your cat a fur colour, a breed, eye colour, whatever. But if you can take many different types, its a bit trickier since there is no real way to enforce it (my shovel will have a brand, a type/design, a material, etc).
Come back if you run into problems with making these.

btw, right now I suggest you make a shovel buddy. Seriously. Points for creativity.
Fou-Lu is offline   Reply With Quote
Old 11-07-2012, 11:14 PM   PM User | #3
Staceadam
New Coder

 
Join Date: Nov 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Staceadam is an unknown quantity at this point
hahaha. Loving the shovel idea! Also thank you for spending the time to reply. The assignment only calls for one type buddy and its only a first semester course so we haven't gotten into arrays yet. I'll start tinkering with some code and post what I come up with tomorrow.
Staceadam is offline   Reply With Quote
Old 11-08-2012, 04:27 PM   PM User | #4
Staceadam
New Coder

 
Join Date: Nov 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Staceadam is an unknown quantity at this point
Ok. I got some of the basic code done but I am having trouble with some of the harder concepts. 1. How am I going to make it so when the user enters the first letter of the corresponding activity it will run that activity. 2. How to associate a points system that returns the correct condition of the buddy. I apologize, my java skills are quite noob.

package shovelbuddy;
import java.util.Scanner;
/**
*
* @author SKelnhofer
*/
public class ShovelBuddy {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter a name for you shovel");
String name = keyboard.nextLine();

System.out.println("Do you want to" + Pet + name);

Activity theActivity = new Activity(Pet, Sleep, Dig, Feed, Clean);
}
}



package shovelbuddy;

/**
*
* @author SKelnhofer
*/
public class Activity {
private String Pet;
private String Sleep;
private String Dig;
private String Feed;
private String Clean;
private int Health;

public Activity(String Pet, String Sleep, String Dig, String Feed, String Clean, int Health) {
this.Pet = Pet;
this.Sleep = Sleep;
this.Dig = Dig;
this.Feed = Feed;
this.Clean = Clean;
this.Health = Health;
}


public String getPet() {
return Pet;
}


public void setPet(String Pet) {
this.Pet = Pet;
}


public String getSleep() {
return Sleep;
}


public void setSleep(String Sleep) {
this.Sleep = Sleep;
}


public String getDig() {
return Dig;
}


public void setDig(String Dig) {
this.Dig = Dig;
}


public String getFeed() {
return Feed;
}


public void setFeed(String Feed) {
this.Feed = Feed;
}


public String getClean() {
return Clean;
}


public void setClean(String Clean) {
this.Clean = Clean;
}
}
Staceadam is offline   Reply With Quote
Old 11-08-2012, 06:04 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Activity base definition like so:
PHP Code:
public class Activity
{
    private 
String sActivityName;
    private 
int iChangeConditionBy;

    
// add constructors and base methods.  Then implement:
    
public void performActivity(Buddy b); // give Buddy object a method to modifyConditionBy(int)
    // OR
    
public int modifyConditionBy(); // handled in Buddy object instead

The main class should be responsible for assigning the menus itself. Whether it be numbers or textual on first letter based; checking the Activity and pulling the activity name out will give you what you need. Start with numbers as its easier, then do the string (which is why you should let the main deal with this).

Also, in the future please wrap code with [php][/php] or [code][/code] tags as it preserves the formatting.

Activity wise, I wouldn't pet or sleep my shovel. I'd store and. . . stroke it. lol.

Edit:
Actually, noting the phrases used here for the activities, make activity like so:
PHP Code:
public class Activity
{
    private 
String sActivityname;
    private 
int iModifyCondition;
    private 
Buddy buddy;

    
//. . .
    
public void modifyCondition();

You'll need Buddy's name for a part of the activity name.

Last edited by Fou-Lu; 11-08-2012 at 07:03 PM..
Fou-Lu is offline   Reply With Quote
Old 11-13-2012, 02:10 AM   PM User | #6
Staceadam
New Coder

 
Join Date: Nov 2012
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Staceadam is an unknown quantity at this point
Still messing with this. What I am trying to do is start health off at 3 then based on the users input subtract or add the corresponding number. Then check to see what range health is in and display its condition. health< 0 = dead, health 1-3 sick, health 3-5 ok, health 5-7 too good, health>7 dead.

Code:
public class ShovelBuddy {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Please enter a name for you shovel");
        String name =  keyboard.nextLine();
        
       
       
         
        Activity healthy = new Activity();
        
        for(int i=1;i<10;i++){
            System.out.println("Press 1 to Pet " + name + ", 2 to Store " + name +
                ", 3 to Dig with " + name + ", 4 to Clean " + name);
        
        int selection = keyboard.nextInt();
        switch(selection){
            case 1: 
                System.out.println(name + " is feeling " + healthy.getPet()  );break;
            case 2: 
                System.out.println(name + " is feeling " + healthy.getStore() );break;
            case 3: 
                System.out.println(name + " is feeling " + healthy.getDig() );break;
             case 4: 
                System.out.println(name + " is feeling " + healthy.getClean() );break;
        }
            
        }
       
        
    }
}


package shovelbuddy;

public class Activity
{
    private int health = 3;
    private int pet = health +2;
    private int store = health -1;
    private int dig = health +1;
    private int clean = health -2;

    public Activity() {
    }

    /**
     * @return the health
     */
    public int getHealth() {
        return health;
    }

    /**
     * @param health the health to set
     */
    public void setHealth(int health) {
        this.health = health;
    }

    
    public int getPet() {
        return pet;
    }

   
    public void setPet(int pet) {
        this.pet = pet;
    }

   
    public int getStore() {
        return store;
    }

   
    public void setStore(int store) {
        this.store = store;
    }


    public int getDig() {
        return dig;
    }


    public void setDig(int dig) {
        this.dig = dig;
    }

  
    public int getClean() {
        return clean;
    }


    public void setClean(int clean) {
        this.clean = clean;
    }

}
Staceadam is offline   Reply With Quote
Old 11-13-2012, 01:33 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No, this isn't correct. Look back at the requirements; the buddy requires at least 5 properties and at least three datatypes. Buddy would contain a name, condition and Activity items.
Activity itself should just be a shell. I can think of a dozen ways to accomplish this task, but the absolute minimum for Activity would be a name of the activity and the amount it modifies condition by. The name would include any specific buddy information during construction such as the name and passed to the constructor for Activity. Take all the weight off of Activity and put it into buddy.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:29 AM.


Advertisement
Log in to turn off these ads.