敏捷开发,我在agile java里面碰到的一个问题。

caikangying 2012-10-08 09:12:06
agile java里面的一个练习,感觉比较棘手,不知道该怎么做,有看过agile的,或者会做这道题的同学帮帮忙。帮我写出这个棋盘的代码,或者给点提示。最好给我这本书的练习答案。
(This exercise and Exercise 5 are closely related. You may suspend refactoring until you have completed Exercise 5.) When a client creates a Board object, they should be able to assume that the board is already initialized, with pieces in place. You will need to modify the Board tests and code accordingly. Start by changing the assertions on the number of pieces available at time of board creation: There should be 16. Delete testAddPawns; it is not useful in its current form.

Add an initialize method to Board. The initialize method should add pawns to create two ranks: a rank for white pawns (the second rank) and a rank for black pawns (the seventh rank). To store a rank, use an ArrayList whose contents are Pawn objects. You declare such a list as ArrayList<Pawn>.

Add an assertion to testCreate that the second rank looks like this: "pppppppp". Assert that the seventh rank looks like this: "PPPPPPPP". Use a StringBuilder and a for loop to gather the printable representation for the pieces in each rank.
国际象棋的棋盘,我不知道棋盘类board里面是不是需要一个rank变量,加上rank变量后又好像变得好复杂,似乎没这个必要。棋盘里面已经有了ArrayList<Pawn>属性了!

package chess;

import java.util.ArrayList;
import java.util.Iterator;

import pieces.Pawn;

public class Board {
private int pieces=16;
private ArrayList<Pawn> pawns=new ArrayList<Pawn>();
// private StringBuffer representation;
private int size=64;
private char[] ch=new char[size];

public Board() {
// System.out.println(ch.toString().charAt(0));
for (int i = 0; i < 8; i++) {
pawns.add(new Pawn());
}
for (int i = 0; i < 8; i++) {
pawns.add(new Pawn("black"));
}
// StringBuffer temp = new StringBuffer();
// for (int i = 0; i < 8; i++) {
// pawns.add(new Pawn());
// temp.append('p');
// }
// assemble(temp);
// for (int i = 0; i < 8; i++) {
// pawns.add(new Pawn("black"));
// temp.append('P');
// }
// assemble(temp);
}

public String print(int beginIndex,int endIndex) {
// TODO Auto-generated method stub
System.out.println(ch);
return this.ch.toString().substring(beginIndex, endIndex);
}

public void assemble(StringBuffer buffer) {
// TODO Auto-generated method stub
if(buffer.toString().equals("pppppppp"))
for (int i = 8; i < 16; i++) {
ch[i]=buffer.toString().charAt(i-8);
// System.out.println(ch.toString());
}
else if(buffer.toString().equals("PPPPPPPP"))
for (int i = 49; i < 57; i++) {
ch[i]=buffer.toString().charAt(i-49);
}
// for (int i = 0; i < buffer.length(); i++) {
// for (char c : buffer.toString().toCharArray()) {
// ch[i+8]=c;
// }
// }
}

public int getPieces() {
return pieces;
}

public void addPawn(Pawn pawn) {
// TODO Auto-generated method stub
this.pieces++;
pawns.add(pawn);
}

public ArrayList<Pawn> getPawns() {
// TODO Auto-generated method stub
return this.pawns;
}
}



...全文
121 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,503

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧