CodeRallyで遊ぼう


SoftwareDesignにCodeRallyが紹介されていたので、入れてみた。googleに聞いてみたらいくつかサンプルコードがあったので、そのままcopy&pasteしてみたら、結構いい感じでサンプルカーに勝てるんですねぇ。で、いまこれをベースにいじっています。拡張すると挙動不審だったりするんですが、デバッグはどうしたらいいんでしょうか…。printf使えないし(^^;
とりあえず、メモをつくった。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

コメント

  1. niche diary より:

    RallyCar

    現実逃避中…とりあえず、チェックポイントを順番に巡るところまで。Stateパターン適用はちょっと冗長かしらん?import com.ibm.rally.Car;import com.ibm.rally.ICar;import com.ibm.rally.IObject;/** * This is the class that you must implement to enable your car within * the CodeRally track. Adding code to these methods will give your car * it’s personality and allow it to compete. */public class RallyCar extends Car { private StateManeger stateManeger = null; private StateAction action = null; /** * @see com.ibm.rally.Car#getName() */ public String getName() { return “NicheCar”; } /** * @see com.ibm.rally.Car#getSchoolName() */ public String getSchoolName() { return “OLUG”; } /** * @see com.ibm.rally.Car#getColor() */ public byte getColor() { return CAR_BLUE; } /** * @see com.ibm.rally.Car#initialize() */ public void initialize() { stateManeger = new StateManeger(this); action = new GoCheckPointAction(this); } /** * @see com.ibm.rally.Car#move(int, boolean, ICar, ICar) * Put the car in reverse for a few moves if you collide with another car. * Go toward the first gas depot. */ public void move( int lastMoveTime, boolean hitWall, ICar collidedWithCar, ICar hitBySpareTire) { try { Context context = new Context( lastMoveTime, hitWall, collidedWithCar, hitBySpareTire); action = stateManeger.getNext(context, action); action.turn(); action.ahead(); } catch (Throwable t) { t.printStackTrace(); } }class Context { private int lastMoveTime; private boolean hitWall; private ICar collidedWithCar; private ICar hitBySpareTire; public Context( int lastMoveTime, boolean hitWall, ICar collidedWithCar, ICar hitBySpareTire) { this.lastMoveTime = lastMoveTime; this.hitWall = hitWall; this.collidedWithCar = collidedWithCar; this.hitBySpareTire = hitBySpareTire; } /** * @return */ public ICar getCollidedWithCar() { return collidedWithCar; } /** * @return */ public ICar getHitBySpareTire() { return hitBySpareTire; } /** * @return */ public boolean isHitWall() { return hitWall; } /** * @return */ public int getLastMoveTime() { return lastMoveTime; }// 状態管理class StateManeger { private StateAction goCheckPointAction = null; private StateAction backAction = null; public StateManeger(Car car) { goCheckPointAction = new GoCheckPointAction(car); backAction = new BackAction(car); } // どの状態にするかを選択する。 public StateAction getNext(Context context, StateAction nowState) { if (!nowState.canChange()) { return nowState; } if (context.getCollidedWithCar() != null) { return backAction; } return goCheckPointAction; }// 状態抽象クラスabstract class StateAction { private Car car = null; private IObject checkPoints[]; private IObject fuelPoints[]; private IObject tirePoints[]; private ICar rallyCars[]; public StateAction(Car car) { this.car = car; checkPoints = car.getCheckpoints(); fuelPoints = car.getFuelDepots(); tirePoints = car.getSpareTireDepot(); rallyCars = car.getOpponents(); } abstract void ahead(); abstract void turn(); // 次の状態へ遷移可能か? public boolean canChange() { return true; } /** * @return */ public Car getCar() { return car; } /** * @return */ public IObject[] getCheckPoints() { return checkPoints; } /** * @return */ public IObject[] getFuelPoints() { return fuelPoints; } /** * @return */ public ICar[] getRallyCars() { return rallyCars; } /** * @return */ public IObject[] getTirePoints() { return tirePoints; }// チェックポイントを狙う状態class GoCheckPointAction extends StateAction { private IObject target; private double distance; public GoCheckPointAction(Car car) { super(car); } void ahead() { getNextCheckPointInfo(); getCar().setThrottle(Car.MAX_THROTTLE / adjustment()); } void turn() { getNextCheckPointInfo(); // 目標までの角度を計算 int iHeadingTo = getCar().getHeadingTo(target) – getCar().getHeading(); if (iHeadingTo > 180) { iHeadingTo = iHeadingTo – 360; } else if (iHeadingTo Car.MAX_STEER_RIGHT) { iHeadingTo = Car.MAX_STEER_RIGHT / adjustment(); } else if (iHeadingTo 100) { return 1; } else if (distance > 50) { return 2; } else if (distance > 25) { return 4; } else { return 8; } } private void getNextCheckPointInfo() { int iPrevPointNo = getCar().getPreviousCheckpoint(); iPrevPointNo = iPrevPointNo + 1; // 前回通過の次のチェックポイント if (iPrevPointNo >= getCheckPoints().length) { iPrevPointNo = 0; } target = getCheckPoints()[iPrevPointNo]; distance = getCar().getDistanceTo(target); }// 後ろに下がる状態class BackAction extends StateAction { int backTime = 10; public BackAction(Car car) { super(car); } void ahead() { getCar().setThrottle(Car.MIN_THROTTLE); } void turn() { getCar().setSteeringSetting(Car.MAX_STEER_LEFT); } public boolean canChange() { if (backTime–

  2. t-doi より:

    ほほう。これは面白そうですねぇ。
    早速現実逃避に活用しなくては。(w

  3. t-doi より:

    System.out.println(“ほげ”);で<デバッグ

  4. koj より:

    ほー。あとでやって見ます。 > System.out.println()
    わくわく。
    どっかにサーバがほしい今日この頃。