方法可以可读并且更短吗?
我写了这个方法:
private int maxSequence (char player , Cell c)
{
int row = c.getRow();
int col = c.getCol();
int maxVert = 0;
int maxHor = 0;
int maxDiag = 0;
if (player == 'O')
{
for (int j = 0; j < _board[0].length; j++)
{
if ( (_board[col][row+j] == 'O') || (_board[col][row-j] == 'O') )
{
maxVert++;
}
if ( (_board[col+j][row] == 'O') || (_board[col-j][row] == 'O') )
{
maxHor++;
}
if ( (_board[col+j][row+j] == 'O') || (_board[col-j][row-j] == 'O') )
{
maxDiag++;
}
}
}
if (player == 'X')
{
for (int j = 0; j < _board[0].length; j++)
{
if ( (_board[col][row+j] == 'O') || (_board[col][row-j] == 'X') )
{
maxVert++;
}
if ( (_board[col+j][row] == 'O') || (_board[col-j][row] == 'X') )
{
maxHor++;
}
if ( (_board[col+j][row+j] == 'O') || (_board[col-j][row-j] == 'X') )
{
maxDiag++;
}
}
}
if ( (maxDiag >= maxVert) && (maxDiag >= maxHor) )
{
return maxDiag;
}
else if ( (maxVert >= maxDiag) && (maxVert >= maxHor) )
{
return maxVert;
}
else
{
return maxHor;
}
}
我想知道是否有一种方法可以改进该方法以使其可读并且 \ 或更短?
I wrote this method:
private int maxSequence (char player , Cell c)
{
int row = c.getRow();
int col = c.getCol();
int maxVert = 0;
int maxHor = 0;
int maxDiag = 0;
if (player == 'O')
{
for (int j = 0; j < _board[0].length; j++)
{
if ( (_board[col][row+j] == 'O') || (_board[col][row-j] == 'O') )
{
maxVert++;
}
if ( (_board[col+j][row] == 'O') || (_board[col-j][row] == 'O') )
{
maxHor++;
}
if ( (_board[col+j][row+j] == 'O') || (_board[col-j][row-j] == 'O') )
{
maxDiag++;
}
}
}
if (player == 'X')
{
for (int j = 0; j < _board[0].length; j++)
{
if ( (_board[col][row+j] == 'O') || (_board[col][row-j] == 'X') )
{
maxVert++;
}
if ( (_board[col+j][row] == 'O') || (_board[col-j][row] == 'X') )
{
maxHor++;
}
if ( (_board[col+j][row+j] == 'O') || (_board[col-j][row-j] == 'X') )
{
maxDiag++;
}
}
}
if ( (maxDiag >= maxVert) && (maxDiag >= maxHor) )
{
return maxDiag;
}
else if ( (maxVert >= maxDiag) && (maxVert >= maxHor) )
{
return maxVert;
}
else
{
return maxHor;
}
}
I wonder if there is a way to improve the method to be readable and \ or shorter ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
O
作为变量名是一个坏主意。O
很容易被误读或误输入为0
,反之亦然,正如您在几个地方所做的那样。在这种情况下,出于上述原因,我认为您应该使用诸如
O_PLAYER
和O_MARKER
之类的内容来代替O
,并且还可以区分两者代码中常量的不同含义。 (事实上,根据更大的上下文,我可能为这两种情况创建了几个enum
类型。)将左花括号放在前一行可以节省空间,并且(IMO)就像可读;例如,
使用
_ 作为实例变量前缀违反了 Java 命名标准。
最后,最后 12 行可以重写为:
Using
O
as a variable name is a bad idea.O
can easily misread or mistyped as0
or vice versa, as you appear to have done in a couple of places.In this case, I think you should use something like
O_PLAYER
andO_MARKER
instead ofO
, for the reason above, and also to distinguish the two different meanings of the constant in your code. (In fact, depending on the larger context, I might have created a couple ofenum
types for these two cases.)Putting the opening curly braces on the previous line saves space and is (IMO) just as readable; e.g.
versus
Prefixing instance variables with _ violates the Java naming standard.
Finally, the last 12 lines could be rewritten as:
首先,您可能需要遵循 java 编码约定,尤其是命名方面(有意义的名称,没有下划线...)并且您还可以删除布尔值评估周围的额外括号,如下所示。
您还可以将 for 循环合并为一个,因为它们正在做真正相似的工作
First of all, you may want to follow java coding convention especially for namings (meaningful names and no underscores...) and also you can remove the extra parenthesis around the boolean evaluations like below.
Also you can merge the for loops into one as they are doing real similar job
除了这无法编译这一事实(除非
X
和O
是实例变量)...玩家“O”的代码与玩家“X”有何不同'?答案是每个
if
语句中的第二个表达式...将其替换为变量,然后您就将代码减半了。然而,更好的方法是根本不使用这些条件。但这需要思考如何对棋盘上的任意位置进行 Tic-Tac-Toe AI 操作。
Aside from the fact that this won't compile (unless
X
andO
are instance variables) ...How does the code for player 'O' differ from player 'X'? The answer is the second expression in each
if
statement ... replace it by a variable, and you've cut the code in half.However, a better approach is to not have those conditionals at all. But that requires some thought as to how you might do a Tic-Tac-Toe AI for any arbitrary place on the board.
在大多数情况下,构建复杂的单行代码并不是一个好主意。然而,在某些情况下,比如这里,你可以让眼睛很容易看到多行上的共同点和不同点:
重新整理行后,我观察到在第一个块中,你测试了 OO,OO, OO,在 OX,OX,OX 的第二个块中,我怀疑,您想测试 XX,XX,XX。
如果没有编译,在第二个视图中我看到有一个 OO0,我期望有一个 OOO(不是 XXX,就像上面提到的那样)。 :) - 啊,但是这种模式也用在纯 O-Block 中。
我想补充一点,我喜欢 Stehpen C 表达 max(a, b, c) 的想法,前两个块应该替换为参数化块,并且整个程序的更大图片可能会导致完全不同的结果设计,更加面向对象。
结合这些建议,我们暂时停在这里:
中间,因为我们可能会做一个完全不同的设计 - 我至少闻到了棋盘级和玩家级的味道。
To build complex one-liners is in most cases a bad idea. However - in some cases like here, you make it easy for the eyes to see, what is common over multiple rows, and what is different:
After resorting the lines, I observed that in the first block, you test for OO,OO,OO, and in the second block for OX,OX,OX, and I doubt, you would like to test for XX,XX,XX.
Without compilation, in a second view I see, that there is a OO0, where I would expect an OOO (wasn't it XXX, like mentioned above). :) - Ah, but that pattern is used in the pure O-Block too.
I like to append, that I like Stehpen C's idea to express max(a, b, c), that the first two blocks should be replaced by an parametrized block, and that an bigger picture of the whole program could lead to a complete different design, more object oriented.
Combining the suggestions, we intermediately stop here:
Intermediately, because we might do a completely different design - I'm smelling a Board-class and a Player-class at least.
这里不是完整的答案,抱歉,今天早上喝咖啡有点懒(这看起来也像学校的问题)。因此,我将保持高水平。
Not a full answer here, sorry, drinking coffee and sort of lazy this morning (also this kind of looks like an school problem). I'll therefore keep it high level.