Java Assignment Example

Below you will find a java assignment sample done by our experts. We highly encourage you to share your Java homework details with our team, and we will provide you with custom solutions and detailed answers.

Assignment Details

You will be required to write code for a type of bug, a BattleBug, to fight, in a way similar to gladiators, in a grid. These BattleBugs each have three different attributes, Strength, Defense, and Speed that are incremented as a BattleBug either steps on a PowerUp or kills another BattleBug.

BattleBugs will do battle on a special grid with a referee bug in the center of type Regulator. The regulator has many functions that are: to make sure the rules are followed, to add rocks to the limits of the empty space, and to drop PowerUps. The rules are checked at the beginning of every turn, a border of rocks around the empty space will be created every (40) acts, and a maximum of (6) flowers are dropped every(10) turns.  (the numbers in () might be changed depending on how things go)

Strength and defense are used to calculate the results of the attack() method. If the attacking BattleBug’s Strength is at least three points higher than the defenders Defense, the defending bug is “killed” and removed from the grid, and in its place, a TombStone is left in its place. The attacking BattleBug has an attribute increased by one based on what the highest value attribute of the defending bug is.

For example: if the defending BattleBug’s stats are 5 Strength, 4 Defense, and 7 Speed, the attacking BattleBug’s speed is increased by one. However, if the defending bug has two attributes with the same highest value, a random attribute of the attacking BattleBug is incremented. In the case of an unsuccessful attack, if the Strength of the attacker is not three greater than the defenders defense, nothing happens.

Speed is the special attribute: Speed determines which bug acts first.  Also a Speed of ten or greater, a BattleBug has his move2() method enabled, and can move two spaces forward instead of one.

Java Code

import BattleBugs.BattleBug2012;
 import info.gridworld.actor.Actor;
 import info.gridworld.grid.Grid;
 import info.gridworld.grid.Location;
 import java.awt.Color;
 import java.util.ArrayList;
 public class BattleBug extends BattleBug2012{
 public BattleBug(int s, int d, int sp, String n, Color co)
 {
 super(s,d,sp,n,co);
 }
 public BattleBug(){
 super(-1,-1,-1,"",new Color());
 }
 private int battleBugNumAct;
 private int direction;
 public int getDirection() {
 return direction;
       }
 public void setDirection(int direction) {
 this.direction = direction;
 }
 public int getBattleBugNumAct() {
 return battleBugNumAct;
 }
public void setBattleBugNumAct(int battleBugNumAct) {
 this.battleBugNumAct = battleBugNumAct;
 }
 //Attack with a bug
 public void attack(BattleBug attacking){
 if(getStrength() > attacking.getStrength()+3)
 {
 System.out.println("Defended well, Attacking bug has been killed");
 if(getStrength()>getDefense()&&
 getStrength()>getSpeed())
 setStrength(getStrength()+1);
 else if(getDefense()>getSpeed())
 setDefense(getDefense()+1);
 else
 setSpeed(getSpeed+1);
 }
 if(attacking.getStrength() > getStrength()+3){
 removeSelfFromGrid();
 if(attacking.getStrength()>attacking.getDefense()
 &&attacking.getStrength()>attacking.getSpeed())
 attacking.setStrength(attacking.getStrength()+1);
 else if(attacking.getDefense>attacking.getSpeed() )
  attacking.setDefense(attacking.getDefense()+1);
 else
 attacking.setSpeed(attacking.getSpeed()+1);
 }
 }
public void act()
 {
 int[] dirs = {90,-90,45,-45,135,-135,0};
 if(getNumAct()%4==3)
 {
 int d = (int)(dirs.length*Math.random());
 turn(dirs[d]);
 if(getSpeed()<=20)
 {
 if(canMove())
 {
 move();
 BattleBug b = rockAway();
 if(b != null)
 attack(b);
 }
 }
 else
 {
 double r = Math.random();
 if(r<.75)
 {
 Location loc = getLocation();
 Location next = loc.getAdjacentLocation(getDirection()).getAdjacentLocation(getDirection());
 if(canMove2Away())
 move2();
 if(rock2Away())
 {
 BattleBug b = (BattleBug) r2Away();
 if(b != null)
 attack(b);
 }
 }
 else
 if(canMove())
 {
 move();
 BattleBug b = rockAway();
 if(b != null)
 attack(b);
 }
 }
 }
 }
public BattleBug rockAway()
 {
 Grid<Actor> gr = getGrid();
 if (gr == null) {
 return null;
 }
 Location loc = getLocation();
 Location next = loc.getAdjacentLocation(getDirection());
 if (!gr.isValid(next)) {
 return null;
 }
 BattleBug neighbor = gr.get(next);
 return neighbor ;
 // ok to move into empty location or onto PowerUp
 // not ok to move onto any other actor
 }
  public BattleBug r2Away()
 {
 Grid<Actor> gr = getGrid();
 if (gr == null) {
 return null;
 }
 Location loc = getLocation();
  Location next = loc.getAdjacentLocation(getDirection()).getAdjacentLocation(getDirection());
 if (!gr.isValid(next)) {
 return null;
 }
 BattleBug neighbor = gr.get(next);
 return neighbor ;
 // ok to move into empty location or onto PowerUp
 // not ok to move onto any other actor
 }
}