CS计算机代考程序代写 package comp1110.ass1;

package comp1110.ass1;

public class Piece {
/** Location of the dog */
private Location dogLoc = new Location();
/** Location of the owner, this field is meaningless if the dog does not have an owner */
private Location ownerLoc = new Location();
/** The leash length of this piece */
private final int leashLength;

/**
* An array of length (leashLength + 1) containing the locations of all
* squares the leash is occupying, ordered from the dog to the owner
* (ie the first element will be the dogs location and the final element
* the owners location and intermediate locations should connect these two)
*/
private Location[] leash;

public Piece(int leashLength) {
this.leashLength = leashLength;
this.leash = new Location[this.leashLength + 1];
for (int i = 0; i <= this.leashLength; i++) this.leash[i] = new Location(); } public int getLeashLength() { return this.leashLength; } public Location getDogLoc(){ return this.dogLoc; } public Location getOwnerLoc() { return this.ownerLoc; } public Location[] getLeash() {return this.leash;} public void setDogLoc(Location newDogLoc){ this.dogLoc = newDogLoc; } public void setOwnerLoc(Location newOwnerLoc){ this.ownerLoc = newOwnerLoc; } public void setLeash(Location[] leash) {this.leash = leash;} /** * @return True if the current piece is placed on the board, False otherwise */ public boolean onBoard(){ if (this.leashLength != 0 && this.ownerLoc.offBoard()) return false; return !this.dogLoc.offBoard(); } /** * Return placement string of current piece * If the piece is not currently placed the empty string should be returned * @return The placement string of the current piece */ public String getPlacement() { if (!this.onBoard()) return ""; if (leashLength == 0) return this.dogLoc.getX() + "" + this.dogLoc.getY(); return this.dogLoc.getX() + "" + this.dogLoc.getY() + this.ownerLoc.getX() + this.ownerLoc.getY(); } /** * Assuming a valid placement, update the board state and current piece * with new placement. * * @param board The WalkTheDog board to place the Piece on * @param placement The valid placement string */ public void placePiece(WalkTheDog board, String placement) { this.dogLoc = new Location(placement.substring(0, 2)); board.setState(this.dogLoc, State.DOG); if (placement.length() == 4) { this.ownerLoc = new Location(placement.substring(2, 4)); board.setState(this.ownerLoc, State.OWNER); } this.leash = WalkTheDog.findLeash(placement, board.getTree()); } /** * Update the board state and piece to remove piece from board. * If the piece is not on the board do nothing. * * @param board The WalkTheDog to remove the piece from */ public void removePiece(WalkTheDog board) { if (this.onBoard()) { board.setState(this.getDogLoc(), State.EMPTY); if (!this.ownerLoc.offBoard()) board.setState(this.ownerLoc, State.EMPTY); this.setDogLoc(new Location()); this.setOwnerLoc(new Location()); for (int i = 0; i <= this.leashLength; i++) this.leash[i] = new Location(); } } }