2011-06-25 2 views
0
class Bug { 

// An ant is represented by the coordinates of its location, 
// and the direction it is facing. 
Integer x; 
Integer y; 
Dir dir; 

enum Dir { E,W,N,S } 
} 

Bug(Integer x, Integer y, Dir dir) { 
    this.x = x; 
    this.y = y; 
    this.dir = dir; 
}} 

class BugWorld { 


Integer theBreadth, theHeight; 
Board board; 
Bug bug; 

BugWorld(Integer breadth, Integer height) { 
    board = new board(breadth, height); 
    bug = new Bug(breadth/2, height/2, Ant.Direction.Y); 
    theBreadth = breadth; 
    theHeight = height; 
} 

を組み合わせたJavaメソッドを取得:トラブル私はすでに次を持って一つのメインの方法で

Status status(Integer x, Integer y) { 
    return board[x][y]; 
} 


void update(Integer x, Integer y) { 
    board[x][y].next(); 
} 

私はいくつかの問題を抱えているところ、以下のこの部分は、次のとおりです。

/* Take the world of the bug to the next step. */ 
void step() { 
    // 1) Get the state at the present bug position. 
      //I've done the following (next line) so far. 
      Bug status(Integer x, Integer y); ...? 

    // 2) Change the 'status' at that position. 
      .............? 
} 

それはちょうど組み合わせていますこれらは私が問題を抱えている。

+0

欲しいものだと思いますか? – joostschouten

+0

@jootschoutenステータスは2つのEnum {Visited、NotVisited}で構成されます。達成したいのは、コードの最後のブロックで強調されています。 "//私はこれまでに次の行を行っています"というテキストのすぐ下の行。動かない。 – maclunian

+0

@jootschouten私がしたいのは、バグを 'board 'のある場所から別の場所に移動することです(長期的には)。ボードは座標(x、y)からなる。 'next()'は、どちらが初期であるかに応じて、ステータスを「NotVisited」から「Visited」に、またはその逆に変更します。 – maclunian

答えて

0

それはあなたが何をしようとしているかを理解するのは難しいが、私はここにあなたが達成するために何をしようとしているし、何が機能していないあなたは

void step(){ 

    //get the location of the bug 
    int x = bug.x; 
    int y = bug.y; 

    //gets the status of the location of the bug 
    Status s = status(x,y); 

    //move the bug, or do something based on the status of the place 

    //update the status of the location the bug was originally on 
    update(x,y) 
} 
+0

いいえ、新しいバグオブジェクトを作成しようとしているわけではありません。ボードのバグの現在の位置で、与えられた(x、y)座標で 'status'を最初に取得しようとしています。 2番目の部分は、その位置で 'ステータス'を変更します。上にBugコンストラクタがハイライトされています。 – maclunian

+0

私のコードはあなたが望むことをしないのですか?最初の行はバグの位置でステータスを取得し、次の行はそれをnoに変更します。 – jhlu87

+0

最初のものは、次のように書かなければならなかった。 ステータスstatus = board.status(bug.x、bug.y); – maclunian