Hei, holder på med en oppgave som går ut på å lage et enkelt RPG. Foreløbig er klassene Player, Item og Game opprettet. Holder på med en ArrayList i klassen Player som skal kunne inneholde items og bl.a. ha en metode kalt buyItem som legger inn et item i listen og reduserer spillerens gullbeholdning med gjenstandens verdi, kun hvis totalvekten av Items etter å legge til en ny Item ikke overskrider spillerens kapasitet og spillerens gullbeholdning er større eller lik verdien til gjenstanden. Tror jeg har et greit grunnlag for buyItem men sitter fast og er på utkikk etter tips som kan peke meg i riktig retning.
Item :
Player :
Item :
Kode
/** * A class for defining and creating various items. * * @Trygve * @24.02.2013 */ public class Item { // fields private String name; private String description; private int value; private int weight; private String action; /** * @param setName the items name * @param setDescription the items description * @param setValue the items value in gold * @param setWeight the items weight in kg * @param itemAction the items action */ public Item(String name, String description, int value, int weight, String itemAction) { setName(name); setDescription(description); setValue(value); setWeight(weight); action = itemAction; } /** * Prints info about the item */ public void print() { System.out.println("Name: " + name + " Description: " + description + " Value: " + value + " Weight: " + weight); } /** * @return the item name */ public String getName() { return name; } /** * @param name */ public void setName(String newName) { if (newName == "") { name = "UNSPECIFIED"; } else { name = newName; } } /** * @return the item description */ public String getDescription() { return description; } /** * @param description */ public void setDescription(String newDescription) { if (newDescription == "") { description = "UNSPECIFIED"; } else { description = newDescription; } } /** * @return the value */ public int getValue() { return value; } /** * @param value */ public void setValue(int newValue) { if (newValue >= 0) { value = newValue; } else { value = 0; System.out.println("ERROR! Value can not be less than 0"); } } /** * @return the weight */ public int getWeight() { return weight; } /** * @param weight */ public void setWeight(int newWeight) { if (newWeight >= 0) { weight = newWeight; } else { weight = 0; System.out.println("ERROR! Weight can not be less than 0"); } } /** * @return the items action */ public String getAction() { return action; } /** * @param action */ public void setAction(String newAction) { if (newAction == "") { action = "UNSPECIFIED"; } else { action = newAction; } } }
Kode
import java.util.ArrayList; /** * Oblig 2 * This class represents a Player in a role-playing game. * * @author Trygve * @version 0.2 * 25.02.13 * */ public class Player { //fields private ArrayList<Item> item; private String nick; private String type; private int health; private int balance; /** * @param playerNick the players nick * @param setType the players type(valid types are "warrior", "rogue", "mage" and "ranger", lower-case) * @param setBalance the amount of gold a player has(can not be a negative number) */ public Player(String playerNick, String type, int balance) { item = new ArrayList<Item>(); nick = playerNick; setType(type); health = 100; setBalance(balance); } /** * Add an item to the player * @param name for the item to be added */ public void buyItem(Item itemToBuy) { int value = item.getValue(); int weight = item.getWeight(); if(value <= balance && weight <= capacity) { item.add(itemToBuy); balance = balance - value; capacity = capacity - weight; } else { System.out.prinln("Insufficient Funds/Not enough capacity"); } } /** * @return true if the player is alive */ public boolean isAlive() { if(health > 0 && health <= 100) { return true; } else { return false; } } /** * Changes the players health * The health can not be less than 0 or exceed 100. * @param healthPoints */ public void changeHealth(int healthPoints) { int temp = health + healthPoints; if(temp > 100) { health = 100; } else if (temp <= 0) { health = 0; } else { health = temp; } } /** * Prints info about the player */ public void print() { System.out.println("Nick: " + nick + " Type: " + type); System.out.println("Items: " + item); if(isAlive()){ System.out.println("Is alive with health: " + health); } else { System.out.println("Is dead. Game Over"); } } /** * @return the players nick */ public String getNick() { return nick; } /** * @param nick */ public void setNick(String newNick) { if (newNick == "") { nick = "UNSPECIFIED"; } else { nick = newNick; } } /** * @return the players type */ public String getType() { return type; } /** * @param new type(valid types are "warrior", "rogue", "mage" and "ranger", lower-case) */ public void setType(String newType) { if(newType == "warrior") { type = newType; } else if (newType == "rogue") { type = newType; } else if (newType == "mage") { type = newType; } else if (newType == "ranger") { type = newType; } else { type = "UNSPECIFIED"; } } /** * @return the players health */ public int getHealth() { return health; } /** * @return the balance */ public int getBalance() { return balance; } /** * @param set balance */ public void setBalance(int newBalance) { if (newBalance >= 0) { balance = newBalance; } else { balance = 0; } } }