All Downloads are FREE. Search and download functionalities are using the official Maven repository.

stanford.karel.SuperKarel Maven / Gradle / Ivy

Go to download

This the original Stanford Karel for Java, packaged for Maven. ACM Library is included. See also https://cs.stanford.edu/people/eroberts/karel-the-robot-learns-java.pdf

The newest version!
/*
 * File: SuperKarel.java
 * ---------------------
 * This class implements an extension of the basic Karel class that
 * supports more operations.
 */

package stanford.karel;

import acm.util.*;
import java.awt.*;

/* Class: SuperKarel */
/**
 * Extended Karel class that supports the following features:
 *
 * 

 

    *
  • Built-in implementations of the turnRight * and turnAround methods. *

     

    *
  • New methods to support adding color to corners (the * command paintCorner and the predicate * cornerColorIs). *

     

    *
  • A predicate method random(p) which returns * true with probability p. If * p is omitted, random returns * true 50 percent of the time. *

     

    *
  • A method pause(ms), which delays processing * for the specified number of milliseconds. This method * makes it possible to control animation much more * effectively. *
*/ public class SuperKarel extends Karel { public static final Color BLACK = Color.BLACK; public static final Color BLUE = Color.BLUE; public static final Color CYAN = Color.CYAN; public static final Color DARK_GRAY = Color.DARK_GRAY; public static final Color GRAY = Color.GRAY; public static final Color GREEN = Color.GREEN; public static final Color LIGHT_GRAY = Color.LIGHT_GRAY; public static final Color MAGENTA = Color.MAGENTA; public static final Color ORANGE = Color.ORANGE; public static final Color PINK = Color.PINK; public static final Color RED = Color.RED; public static final Color WHITE = Color.WHITE; public static final Color YELLOW = Color.YELLOW; /** * Creates a new SuperKarel object. You will * not ordinarily need to call this method explicitly; it is * the default constructor and will be executed automatically * if you write a SuperKarel program. */ public SuperKarel() { /* Empty */ } /** * Specifies the code for this SuperKarel program. * You need to provide a new implementation of this method * whenever you extend SuperKarel. */ public void run() { /* Empty */ } /** * Causes Karel to turn right without moving from the current * corner. This version of turnRight is implemented * as a built-in primitive and does not actually turn left three * times as is necessary if you write this method yourself. */ public void turnRight() { checkWorld("turnRight"); setDirection(KarelWorld.rightFrom(getDirection())); getWorld().trace(); } /** * Causes Karel to turn around without moving from the current * corner. This version of turnRight is implemented * as a built-in primitive and does not actually turn left twice * as is necessary if you write this method yourself. */ public void turnAround() { checkWorld("turnAround"); setDirection(KarelWorld.oppositeDirection(getDirection())); getWorld().trace(); } /** * Paints the current corner in the specified color, which should * be one of the constants defined in the SuperKarel: * BLACK, BLUE, CYAN * DARK_GRAY, GRAY, GREEN * LIGHT_GRAY, MAGENTA, ORANGE * PINK, RED, WHITE, * YELLOW. * * @param color The new color for the corner. */ public void paintCorner(Color color) { KarelWorld world = getWorld(); Point pt = getLocation(); checkWorld("paintCorner"); world.setCornerColor(pt.x, pt.y, color); world.trace(); } /** * Returns true if the current corner is painted * in the specified color. * * @param color The color you're testing for * @return true if the corner has that color */ public boolean cornerColorIs(Color color) { KarelWorld world = getWorld(); Point pt = getLocation(); checkWorld("cornerColorIs"); if (color == null) { return world.getCornerColor(pt.x, pt.y) == null; } else { return (color.equals(world.getCornerColor(pt.x, pt.y))); } } /** * Returns true roughly 50 percent of the time, * as if Karel flips a coin. * * @return true with probablity 0.5 */ public boolean random() { checkWorld("random"); return random(0.5); } /** * Returns true with probability p, * where p is a number between 0 (never) and * 1 (always). * * @param p The probability of returning true * @return true with probablity p */ public boolean random(double p) { checkWorld("random"); return Math.random() < p; } /** * Pauses Karel for the specified number of milliseconds. * This method makes it possible to write much better Karel * animations. * * @param ms The number of milliseconds to pause */ public void pause(double ms) { checkWorld("pause"); getWorld().trace(); JTFTools.pause(ms); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy