{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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.*/
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern