oop - Chess board and piece design in java -


i'm working on assignment. chess design(ai, gui not required), have piece class. piece class has 2 variables: color , name. far have "move method" in class. `

  public void move(piece piece,int x,int y)   {  int a=0;  int b=0;    for(int i=0;i<board.grid.length;i++) {    for(int r=0;r<board.grid[i].length;r++) {    if(board.grid[i][r]==piece)    a=i;    b=r;    if(board.getisnull(x, y)){    board.grid[a][b]=null;    board.grid[x][y]=piece;    }   }  } board.grid[u][t]=null; } ` 

in code, want find index old index of piece want move, moving setting old index null not working. can see name on screen not color. also, old index not set null. how it? started think using object(piece) array how?

just small hint:

//finds piece equal name , color of our piece.so if(board.grid[i][y].equals(name) || piece.color.equals(color))

this doesn't fit together, since check "equal name or equal color". think want change to: if(board.grid[i][y].equals(name) && piece.color.equals(color))

piece.setcolor(color);//set piece's color

huh? doing for? shouln't piece keep color time?

if(board.getisnull(x, y)==true)

you're redefining y in loop y not parameter passed in method.

basically, i'd redefine method (to keep close op possible, note oop wise there better design, subclassing piece etc.):

//board.grid[][] should 'piece[8][8]';   //edit: rename parameters clarity public void move( piece piece,int targetx,int targety)  {   if( board.getisnull(targetx, targety) )//method in other class checks indexes if null   {     //remove piece field/index     board.grid[piece.x][piece.y]=null;      //add piece target field , update position     piece.x = targetx;     piece.y = targety;     board.grid[targetx][targety]=piece;    }   else   {     //handle case, e.g. throwing exeption   } } 

now you'd piece want move (which knows index), calculate target position , call move(piece, targetx, targety);


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -