英文:
How to simplify this 4 loop functions into 1 function?
问题
我有一个练习。一个 int[8][8] 的国际象棋棋盘。我需要找出白方是否可以吃掉黑方。输入如下:(小写表示黑子,大写表示白子)
tc.drf.t
ppp.pppp
...p...c
.....f..
..C..P..
..P.D.P.
PP.....P
T.F.RFCT
对于皇后/车,我使用了一个循环来检查每个方向(上/下/左/右),现在有四个类似的简单循环函数。
我想只使用一个函数,但是不知道怎么做。有什么想法吗?
static boolean attackRowRt(String[] board, int y, int fromX){
for(int x=fromX+1; x<=7; x++){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackRowLt(String[] board, int y, int fromX){
for(int x=fromX-1; x>=0; x--){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackColBtm(String[] board, int x, int fromY){
for(int y=fromY+1; y<=7; y++){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackColTop(String[] board, int x, int fromY){
for(int y=fromY-1; y>=0; y--){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
英文:
I have an exercise. An int[8][8] chess grid. I have to find out if white can take a black.
The input is: (lowercaps = black, upper = white)
tc.drf.t
ppp.pppp
...p...c
.....f..
..C..P..
..P.D.P.
PP.....P
T.F.RFCT
For the qeen/towers, I used a loop to check in each direction (top/bottom/left/right) and now have 4 simple loop functions that looks more or less the same.
I would like to have only one function but can't manage to find how.
Any idea?
static boolean attackRowRt(String[] board, int y, int fromX){
for(int x=fromX+1; x<=7; x++){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackRowLt(String[] board, int y, int fromX){
for(int x=fromX-1; x>=0; x--){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackColBtm(String[] board, int x, int fromY){
for(int y=fromY+1; y<=7; y++){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
static boolean attackColTop(String[] board, int x, int fromY){
for(int y=fromY-1; y>=0; y--){
char attacked = board[y].charAt(x);
if(attacked == '.') continue;
return Character.isLowerCase(attacked);
}
return false;
}
答案1
得分: 2
你的四个方法共享了三行代码,这些代码可以提取到一个单独的方法中,然后你可以从当前的方法中调用它(如果你将与“attacked”比较的部分改为与“attacked”相反,则可以将其简化为两行代码)。所以基本上要有一个独立的方法来执行以下操作,并从你的方法中调用它:
char attacked = board[y].charAt(x);
if(attacked != '.') {
return Character.isLowerCase(attacked);
}
此外,你的方法是成对相等的:attackColTop()
和 attackRowLt
是相同的,另外两个方法也是如此。如果两个方法唯一的区别是传递给方法的参数值,那么就没有必要有两个做相同事情的方法:你可以将这两个方法合并为一个,然后根据需要传递适当的值进行调用。
英文:
All four your methods share three lines of code that can be extracted to a separate method that you can call from your current methods (and you can simplify it to two lines of code if you invert the comparison with attacked
). So basically have a separate method that does the following and call it from your methods:
char attacked = board[y].charAt(x);
if(attacked != '.') {
return Character.isLowerCase(attacked);
}
Furthermore, your methods are equal to each other in pairs: attackColTop()
and attackRowLt
are the same, and so are the other two methods. You don't need to have two methods that do the same if the only difference is the values of the parameters that you pass to the method: you can subsume both methods into one and just call it with the appropriate values.
答案2
得分: 1
以下是翻译好的代码部分:
每个方法中的逻辑都是相同的,只是行走的方向不同。因此,通过将此作为参数传递,您可以重用相同的方法来处理所有方向:
static boolean attackLine(String[] board, int fromY, int fromX, int deltaX, int deltaY) {
int x = fromX + deltaX;
int y = fromY + deltaY;
while (true) {
if (x < 0 || x > 7 || y < 0 || y > 7) {
// 超出棋盘,这是终点
return false;
}
System.out.println(String.format("检查 (x, y): (%d, %d)", x, y));
char attacked = board[y].charAt(x);
if (attacked != '.')
{
System.out.println(String.format("在 (x, y): (%d, %d) 处找到棋子", x, y));
return Character.isLowerCase(attacked);
}
x += deltaX;
y += deltaY;
}
}
public static void main(String[] args) {
String[] board = new String[] { //
"tc.drf.t", //
"ppp.pppp", //
"...p...c", //
".....f..", //
"..C..P..", //
"..P.D.P.", //
"PP.....P", //
"T.F.RFCT" };
// 白色皇后向左上
System.out.println("" + attackLine(board, 7, 4, -1, -1));
// 白色皇后向右上
System.out.println("" + attackLine(board, 7, 4, 1, -1));
// 白色皇后向左下
System.out.println("" + attackLine(board, 7, 4, -1, 1));
// 白色皇后向右下
System.out.println("" + attackLine(board, 7, 4, 1, 1));
// 白色塔向上
System.out.println("" + attackLine(board, 7, 0, 0, -1));
}
英文:
The logic in every method is the same only the direction to walk differs. So by passing this as a parameter you can reuse the same method for all directions:
static boolean attackLine(String[] board, int fromY, int fromX, int deltaX, int deltaY) {
int x = fromX + deltaX;
int y = fromY + deltaY;
while (true) {
if (x <0 || x > 7 || y <0 || y > 7) {
// outside board, this is the end
return false;
}
System.out.println(String.format("checking (x,y):(%d,%d)", x, y));
char attacked = board[y].charAt(x);
if (attacked != '.')
{
System.out.println(String.format("piece found at (x,y):(%d,%d)", x, y));
return Character.isLowerCase(attacked);
}
x += deltaX;
y += deltaY;
}
}
public static void main(String[] args) {
String[] board = new String[] { //
"tc.drf.t", //
"ppp.pppp", //
"...p...c", //
".....f..", //
"..C..P..", //
"..P.D.P.", //
"PP.....P", //
"T.F.RFCT" };
// white queen left up
System.out.println("" + attackLine(board, 7, 4, -1, -1));
// white queen right up
System.out.println("" + attackLine(board, 7, 4, 1, -1));
// white queen left down
System.out.println("" + attackLine(board, 7, 4, -1, 1));
// white queen right down
System.out.println("" + attackLine(board, 7, 4, 1, 1));
// white tower up
System.out.println("" + attackLine(board, 7, 0, 0, -1));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论