我可以为调用方法返回一个方法吗?

时间:2016-10-06 21:16:22

标签: java return

我想干掉这段代码:

YourConsultant.GameState gameState() {

    for (int i = 0; i < 3; i++) {
        // an 'xxx' column returns 'x', an 'ooo' returns 'o', a mixed row returns '0'
        char c = areTheSame(board[i][0], board[i][1], board[i][2]);
        if (c == 'x') {return YourConsultant.GameState.WON_BY_X;}
        else if (c == 'o') {return YourConsultant.GameState.WON_BY_O;}
    }

    for (int i = 0; i < 3; i++) {
        char c = areTheSame(board[0][i], board[1][i], board[2][i]);
        if (c == 'x') {return YourConsultant.GameState.WON_BY_X;}
        else if (c == 'o') {return YourConsultant.GameState.WON_BY_O;}
    }

    {
        char c = areTheSame(board[0][0], board[1][1], board[2][2]);
        if (c == 'x') {return YourConsultant.GameState.WON_BY_X;}
        else if (c == 'o') {return YourConsultant.GameState.WON_BY_O;}
    }

    {
        char c = areTheSame(board[0][2], board[1][1], board[2][0]);
        if (c == 'x') {return YourConsultant.GameState.WON_BY_X;}
        else if (c == 'o') {return YourConsultant.GameState.WON_BY_O;}
    }

    ...
}

为此,我想写一个简短的方法来做到这一点:

        if (c == 'x') {return YourConsultant.GameState.WON_BY_X;}
        else if (c == 'o') {return YourConsultant.GameState.WON_BY_O;}

但这会使新方法返回。我想我不能做super.return之类的事情?我可以再次检查返回值,但它不会使我的代码干掉。你有什么建议? (对不起,如果以前被问到,我发现这很难找到)

更新:我不能简单地传递值,因为如果areTheSame == 0那么我不应该返回(还)。

更新2:我修改了代码,用这个代替每两行:

        if (c == 'x' || c == 'o')  return declareWinner(c);

它工作正常,并做同样的事情。仍然有一些重复,但更好的IMO。

3 个答案:

答案 0 :(得分:2)

不,方法不能为其调用者执行返回,但调用者可以直接返回被调用方法返回的值。但是,这似乎并不符合您的目的,因为您只想有条件地返回。

我会通过更深层次的改变来解决问题。请注意您的四个节有多相似:它不仅仅是条件性的回报有点潮湿。你想要执行的测试很少,可以枚举,所以你可以考虑以下几点:

private final static int[][][] TRIPLES = new int[][][] {
    { {0, 0}, {0, 1}, {0, 2} },
    { {1, 0}, {1, 1}, {1, 2} },
    { {2, 0}, {2, 1}, {2, 2} },
    { {0, 0}, {1, 0}, {2, 0} },
    { {0, 1}, {1, 1}, {2, 1} },
    { {0, 2}, {1, 2}, {2, 2} },
    { {0, 0}, {1, 1}, {2, 2} },
    { {0, 2}, {1, 1}, {2, 0} },
};

YourConsultant.GameState gameState() {

    for (int i = 0; i < TRIPLES.length; i++) {
        char c = areTheSame(
            board[TRIPLES[i][0][0]][TRIPLES[i][0][1]],
            board[TRIPLES[i][1][0]][TRIPLES[i][1][1]],
            board[TRIPLES[i][2][0]][TRIPLES[i][2][1]]
        );
        if (c == 'x') {
            return YourConsultant.GameState.WON_BY_X;
        } else if (c == 'o') {
            return YourConsultant.GameState.WON_BY_O;
        }
    }

    return YourConsultant.GameState.NO_WINNER;
}

答案 1 :(得分:1)

你只需要一个findWinner方法

private YourConsultant.GameState findWinner(YourConsultant.GameState previousWinner, char boardResult) {
  if (previousWinner!=null) {
      return previousWinner;
  }
  YourConsultant.GameState winner=null;
  switch(boardResult) {
     case 'x': 
         winner = YourConsultant.GameState.WON_BY_X; 
         break;
     case 'o':
         winner = YourConsultant.GameState.WON_BY_O;
         break;
     default:
         winner = null;
  }       
  return winner;
}

那么你的董事会方法......

YourConsultant.GameState currentWinner=null;

for (int i = 0; i < 3; i++) {
    currentWinner = findWinner(currentWinner,areTheSame(board[i][0], board[i][1], board[i][2]));      
}
for (int i = 0; i < 3; i++) {
   currentWinner = findWinner(currentWinner,areTheSame(board[0][i], board[1][i], board[2][i]));
}
...

当然这不是最有效的方式......

答案 2 :(得分:0)

你不能超级回归,但你可以先收集所有的字符,然后返回第一个字符,否则返回null。并且没有任何新方法:

private YourConsultant.GameState gameState() {
    List<Character> chars = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        chars.add(areTheSame(board[i][0], board[i][1], board[i][2]));
    }

    for (int i = 0; i < 3; i++) {
        chars.add(areTheSame(board[0][i], board[1][i], board[2][i]));
    }

    chars.add(areTheSame(board[0][0], board[1][1], board[2][2]));
    chars.add(areTheSame(board[0][2], board[1][1], board[2][0]));

    return chars.stream()
            .filter(c -> c == 'x' || c == 'o')
            .map(c -> c == 'x' ? YourConsultant.GameState.WON_BY_X : YourConsultant.GameState.WON_BY_O)
            .findFirst()
            .orElse(null); // or whatever "non winning" value you want
}

&#34;表现&#34;检查所有抽动式脚趾板的影响,而不是在第一个可恢复状态下停止,将以微秒为单位进行测量。