Religious Studies > SOPHIA Milestone > College of William & Mary - CS 1114 Food.java. (All)
import sofia.micro.*; import sofia.util.Random; */ public class Food extends SimpleActor { //~ Constructor ........................................................... // ------------------------... ---------------------------------- /** * Creates a new Food object. */ public Food() { super(); } //~ Methods ............................................................... // ---------------------------------------------------------- /** * Take one step, either forward or after turning left or right. */ public void act() { int random = Random.generator().nextInt(4); if (random == 0) { turn(LEFT); } else if (random == 1) { turn(RIGHT); } // stops the food from going outside the world. avoidEdges(); // is so the food can move around within the world. if ((this.getGridX() != 0) || (this.getGridY() != 0) && this.getGridX() != 11 || this.getGridY() != 11) { move(); } } /** * checks that the food doesn't go outside the world. */ public void avoidEdges() { while (this.getGridX() == 0 && (getDirection() == WEST) || this.getGridY() == 0 && (getDirection() == NORTH) || this.getGridX() == 11 && (getDirection() == EAST) || this.getGridY() == 11 && (getDirection() == SOUTH)) { https://www.coursehero.com/file/57230390/Foodjava/ This study resource was shared via CourseHero.comturn(RIGHT); [Show More]
Last updated: 1 year ago
Preview 1 out of 2 pages
Connected school, study & course
About the document
Uploaded On
Mar 20, 2021
Number of pages
2
Written in
This document has been written for:
Uploaded
Mar 20, 2021
Downloads
0
Views
53
In Browsegrades, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Browsegrades · High quality services·