方法可以可读并且更短吗?

发布于 2024-11-05 19:37:44 字数 1539 浏览 3 评论 0原文

我写了这个方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心如狂蝶 2024-11-12 19:37:44

使用 O 作为变量名是一个坏主意。 O 很容易被误读或误输入为 0,反之亦然,正如您在几个地方所做的那样

在这种情况下,出于上述原因,我认为您应该使用诸如 O_PLAYERO_MARKER 之类的内容来代替 O,并且还可以区分两者代码中常量的不同含义。 (事实上​​,根据更大的上下文,我可能为这两种情况创建了几个 enum 类型。)

将左花括号放在前一行可以节省空间,并且(IMO)就像可读;例如,

if (cond) {
   // blah
} else {
   // blah
}

使用

if (cond) 
{
   // blah
} 
else 
{
   // blah
}

_ 作为实例变量前缀违反了 Java 命名标准。

最后,最后 12 行可以重写为:

return Math.max(maxHor, Math.max(maxVert, maxDiag));

Using O as a variable name is a bad idea. O can easily misread or mistyped as 0 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 and O_MARKER instead of O, 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 of enum types for these two cases.)

Putting the opening curly braces on the previous line saves space and is (IMO) just as readable; e.g.

if (cond) {
   // blah
} else {
   // blah
}

versus

if (cond) 
{
   // blah
} 
else 
{
   // blah
}

Prefixing instance variables with _ violates the Java naming standard.

Finally, the last 12 lines could be rewritten as:

return Math.max(maxHor, Math.max(maxVert, maxDiag));
苦笑流年记忆 2024-11-12 19:37:44

首先,您可能需要遵循 java 编码约定,尤其是命名方面(有意义的名称,没有下划线...)并且您还可以删除布尔值评估周围的额外括号,如下所示。

您还可以将 for 循环合并为一个,因为它们正在做真正相似的工作

if (board[col][row + j] == O || board[col][row - j] == O) 


        if (maxDiag >= maxVert && maxDiag >= maxHor) {
            return maxDiag;
        }else if (maxVert >= maxDiag && maxVert >= maxHor) {
            return maxVert;
        }else {
            return maxHor;
        }

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

if (board[col][row + j] == O || board[col][row - j] == O) 


        if (maxDiag >= maxVert && maxDiag >= maxHor) {
            return maxDiag;
        }else if (maxVert >= maxDiag && maxVert >= maxHor) {
            return maxVert;
        }else {
            return maxHor;
        }
甩你一脸翔 2024-11-12 19:37:44

除了这无法编译这一事实(除非 XO 是实例变量)...

玩家“O”的代码与玩家“X”有何不同'?答案是每个 if 语句中的第二个表达式...将其替换为变量,然后您就将代码减半了。

然而,更好的方法是根本不使用这些条件。但这需要思考如何对棋盘上的任意位置进行 Tic-Tac-Toe AI 操作。

Aside from the fact that this won't compile (unless X and O 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.

温馨耳语 2024-11-12 19:37:44
private int maxSequence (char player , Cell c)
{
    int row = c.getRow();
    int col = c.getCol();
    int maxVert = 0;
    int maxHor = 0;
    int maxDiag = 0;

    for (int j = 0; j < _board[0].length; j++)
    {
        if ( (_board[col][row+j] == O) || (_board[col][row-j] == player) )
        {
            maxVert++;
        }

        if ( (_board[col+j][row] == O) || (_board[col-j][row] == player) )
        {
            maxHor++;
        }

        if ( (_board[col+j][row+j] == 0) || (_board[col-j][row-j] == player) )
        {
            maxDiag++;
        }
    }

    return Math.max(maxDiag, Math.max(maxVert, maxHor));
}
private int maxSequence (char player , Cell c)
{
    int row = c.getRow();
    int col = c.getCol();
    int maxVert = 0;
    int maxHor = 0;
    int maxDiag = 0;

    for (int j = 0; j < _board[0].length; j++)
    {
        if ( (_board[col][row+j] == O) || (_board[col][row-j] == player) )
        {
            maxVert++;
        }

        if ( (_board[col+j][row] == O) || (_board[col-j][row] == player) )
        {
            maxHor++;
        }

        if ( (_board[col+j][row+j] == 0) || (_board[col-j][row-j] == player) )
        {
            maxDiag++;
        }
    }

    return Math.max(maxDiag, Math.max(maxVert, maxHor));
}
肤浅与狂妄 2024-11-12 19:37:44

在大多数情况下,构建复杂的单行代码并不是一个好主意。然而,在某些情况下,比如这里,你可以让眼睛很容易看到多行上的共同点和不同点:

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] == 0) || (_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] == 0) || (_board [col-j][row-j] == X))   maxDiag++;
            }
        }
        if ((maxDiag >= maxVert) && (maxDiag >= maxHor))    return maxDiag;
        if ((maxVert >= maxDiag) && (maxVert >= maxHor))    return maxVert;
        return maxHor;
    }

重新整理行后,我观察到在第一个块中,你测试了 OO,OO, OO,在 OX,OX,OX 的第二个块中,我怀疑,您想测试 XX,XX,XX。

如果没有编译,在第二个视图中我看到有一个 OO0,我期望有一个 OOO(不是 XXX,就像上面提到的那样)。 :) - 啊,但是这种模式也用在纯 O-Block 中。

我想补充一点,我喜欢 Stehpen C 表达 max(a, b, c) 的想法,前两个块应该替换为参数化块,并且整个程序的更大图片可能会导致完全不同的结果设计,更加面向对象。

结合这些建议,我们暂时停在这里:

private int maxSequence (char player, Cell c)
{
    int row = c.getRow();
    int col = c.getCol();
    int maxVert = 0;
    int maxHor = 0;
    int maxDiag = 0;

    for (int j = 0; j < _board[0].length; j++)
    {
        if ((board [col]  [row+j] == player) || (board [col]  [row-j] == player))  maxVert++;
        if ((board [col+j][row]   == player) || (board [col-j][row]   == player))  maxHor++;
        if ((board [col+j][row+j] == player) || (board [col-j][row-j] == player))  maxDiag++;
    }
    return Math.max (maxHor, Math.max (maxVert, maxDiag));
}

中间,因为我们可能会做一个完全不同的设计 - 我至少闻到了棋盘级和玩家级的味道。

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:

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] == 0) || (_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] == 0) || (_board [col-j][row-j] == X))   maxDiag++;
            }
        }
        if ((maxDiag >= maxVert) && (maxDiag >= maxHor))    return maxDiag;
        if ((maxVert >= maxDiag) && (maxVert >= maxHor))    return maxVert;
        return maxHor;
    }

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:

private int maxSequence (char player, Cell c)
{
    int row = c.getRow();
    int col = c.getCol();
    int maxVert = 0;
    int maxHor = 0;
    int maxDiag = 0;

    for (int j = 0; j < _board[0].length; j++)
    {
        if ((board [col]  [row+j] == player) || (board [col]  [row-j] == player))  maxVert++;
        if ((board [col+j][row]   == player) || (board [col-j][row]   == player))  maxHor++;
        if ((board [col+j][row+j] == player) || (board [col-j][row-j] == player))  maxDiag++;
    }
    return Math.max (maxHor, Math.max (maxVert, maxDiag));
}

Intermediately, because we might do a completely different design - I'm smelling a Board-class and a Player-class at least.

╰沐子 2024-11-12 19:37:44

这里不是完整的答案,抱歉,今天早上喝咖啡有点懒(这看起来也像学校的问题)。因此,我将保持高水平。

  1. 不要害怕方法,它们可以大大提高清晰度。您的 for 循环可以封装在一个方法中,其中的名称解释了发生了什么。
  2. 避免使用魔法值,用常量替换“X”字符。
  3. 更好的是用玩家类包装角色。 Player.for('x') 返回一个玩家类。将循环中的行为委托给 XPlayer、YPlayer 类。
  4. 就像其他海报指出的那样,尽可能使用 Math.max 和其他内置函数

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.

  1. Don't be afraid of methods, they can improve clarity a lot. Your for loops can be encapsulated in a method where the name explains what's going on.
  2. Avoid using magic values, replace the 'X' chars with a constant.
  3. Even better wrap the char with a player class. Player.for('x') returns a player class. Delegate the behavior in your loops to an XPlayer, YPlayer classes.
  4. Like other posters have pointed out use Math.max and other built in functions where possible
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文