Walker - /** Walker should move randomly based on how many...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
import java.awt.*; /** An instance of this class is a J*Man. There should at most one J*Man in a game board at a time*/ public class Walker extends Piece { /** Constructor: a new Walker at position (x, y) on Map m with color red if c = 0, green if c = 1, and yellow if c = 2. */ public Walker(int x, int y, int c, Map m){ super(Piece.WALKER, m); setX(x); setY(y); if (c==0){ setColor(Color.RED);} if (c==1){ setColor(Color.GREEN);} if (c==2){ setColor(Color.YELLOW);} } /** Constructor: a new Walker at position (x, y) on Map m with color c. Precondition: c is one of Color.RED, Color.GREEN, and Color.YELLOW.*/ public Walker(int x, int y, Color c, Map m){ super(Piece.WALKER, m); setX(x); setY(y); setColor(c); } /** Initialize variables for number of times J*Man moves and random colors*/ private int r; /** number of times J*Man moves*/ private int rPos; /** random position.*/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: /** Walker should move randomly based on how many times J*Man moves. */ public void act(){ r = rand (1,3); if (r==1 && !(hasActed())){ rPos= rand(0,3); if (rPos==0 && getMap().isEmpty(getX(), (getY()-1))){ getMap().move(getX(), getY(), getX(), getY()-1); } if (rPos==1 && getMap().isEmpty(getX(), (getY()+1))){ getMap().move(getX(), getY(), getX(), getY()+1); } if (rPos==2 && getMap().isEmpty((getX()-1), getY())){ getMap().move(getX(), getY(), getX()-1, getY()); } if (rPos == 3 && getMap().isEmpty((getX()+1), getY())){ getMap().move(getX(), getY(), getX()+1, getY()); } } setActed(true); } /** = representation of this piece */ public String toString() { String color= ""; return getColorWord() + " Walker at (" + getX() + ", " + getY() + ")"; } }...
View Full Document

Page1 / 2

Walker - /** Walker should move randomly based on how many...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online