{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Walker

# Walker - Walker should move randomly based on how many...

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

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.*/

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

View Full Document
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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern