Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Robocode

Date:04/20/05
Author:Peter Strömberg
URL:n/a
Comments:0
Info:http://robowiki.dyndns.org/?PEZ
Score: (2.50 in 10 votes)
package wiki.beer;
import robocode.*;
import java.awt.geom.Point2D;

// BeerPoet, one way to go about writing the 99-bottles-of-beer poem.
// By Peter Strömberg, http://robowiki.dyndns.org/?PEZ
// For more info on the poem: http://99-bottles-of-beer.ls-la.net/
//
// Run this robot in Robocode.
// Put it against 99 SittingDuck and open its output window.

public class BeerPoet extends AdvancedRobot {
    private boolean hasStarted;

    public void run() {
        turnGunRightRadians(Double.POSITIVE_INFINITY); 
    }

    public void onScannedRobot(ScannedRobotEvent e) {
        if (!hasStarted) {
            printBottles();
            hasStarted = true;
        }
        setFire(3);
        setTurnGunRightRadians(0 - getGunTurnRemainingRadians());
    }

    public void onRobotDeath(RobotDeathEvent e) {
        printBottles();
    }

    private void printBottles() {
        out.println(bottles());
        if (getOthers() > 0) {
            out.println("Take " + (getOthers() == 1 ? "it" : "one") +
                " down, and pass it around");
            }
        else {
            out.println("Now they are all gone");
        }
    }

    private String bottles() {
        return bottles(getOthers()) + " on the wall, " + bottles(getOthers());
    }

    private String bottles(int bottles) {
        return (bottles == 0 ? "No" : "" + bottles) +
            " bottle" + (bottles == 1 ? "" : "s") + " of beer";
    }
}

Download Source | Write Comment

Alternative Versions

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: