Java 比较数组中的元素

发布于 2024-12-15 05:10:28 字数 374 浏览 1 评论 0原文

我正在用 java 创建一个基于文本的圆圈和十字游戏,并希望确定获胜情况。我已经为板使用了一个数组,例如 String [] board :-

[0][1][2]

[3][4][5]

[6][7][8]

我有一个完整的可能性列表检查 if 语句中连续 3 个值是否相同。

这是我所拥有的一个例子:

if ((board [0] != "" && board [0] == board [1] && board [1] == board [2])) {
return true
}

有没有一种方法可以用更少的代码完成同样的事情?

谢谢

I'm creating a textbased noughts and crosses game in java and want to determine winning situations. I have used an array for the board such as String [] board :-

[0][1][2]

[3][4][5]

[6][7][8]

I have a whole list of possibilities that checks if 3 values in a row are the same in if statements.

This is an example of what i have:

if ((board [0] != "" && board [0] == board [1] && board [1] == board [2])) {
return true
}

Is there a way that does the same thing with a less amount of code?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

︶葆Ⅱㄣ 2024-12-22 05:10:28

来表达您的想法

  • 如果您确实要存储字符串,则可以通过使用 .equals() 而不是 ==
  • ,或者存储字符而不是字符串,在这种情况下 == 是安全的。

现在,如果你真的在制作一个圈和十字游戏,你有 8 个不同的获胜条件,如果你发展你当前的编码风格,这些条件将具有以下形式(这里我假设字符,而不是字符串):

winner = 
    (b[0] != ' ' && b[0] == b[1] && b[1] == b[2]) || 
    (b[3] != ' ' && b[3] == b[4] && b[4] == b[5]) ||
    ...
    (b[0] != ' ' && b[0] == b[4] && b[4] == b[8]);

< /em> 其他方法来做到这一点;我确信在谷歌上搜索井字棋或零和十字实现会告诉你很多。

如果您想尝试一下,有一种众所周知的技术,可以用 2 的幂“标记”每个单元格。然后,通过将分数相加(即查看玩家的位向量)并对八个获胜条件集执行二进制 AND,您可以一次性确定获胜位置。

这是一个井字棋游戏的评论块,说明了该技术。 (您没有要求提供实际的代码,因此我在答案中保留了该代码):

/*
 * To determine a win condition, each square is "tagged"
 * from left to right, top to bottom, with successive
 * powers of 2.  Each cell thus represents an individual
 * bit in a 9-bit string, and a player's squares at any
 * given time can be represented as a unique 9-bit value.
 * A winner can thus be easily determined by checking
 * whether the player's current 9 bits have covered any
 * of the eight "three-in-a-row" combinations.
 *
 *     273                 84
 *        \               /
 *          1 |   2 |   4  = 7
 *       -----+-----+-----
 *          8 |  16 |  32  = 56
 *       -----+-----+-----
 *         64 | 128 | 256  = 448
 *       =================
 *         73   146   292
 *
 */
var wins = [7, 56, 448, 73, 146, 292, 273, 84];

这是 JavaScript。对于 Java,

private static final int[] WINS = new int[]{7, 56, 448, 73, 146, 292, 273, 84};

如果这里的二进制逻辑方法不是您想要的,请使用道歉;我认为这将是一个展示它的好地方,以防其他人登陆此页面。

Your idea can be made sound either by

  • Using .equals() instead of == if you are really storing strings, or
  • Store characters instead of strings, in which case == is safe.

Now if you are really making a noughts and crosses game, you have 8 different winning conditions, which, if you grow your current style of coding will have the form (here I am assuming characters, not strings):

winner = 
    (b[0] != ' ' && b[0] == b[1] && b[1] == b[2]) || 
    (b[3] != ' ' && b[3] == b[4] && b[4] == b[5]) ||
    ...
    (b[0] != ' ' && b[0] == b[4] && b[4] == b[8]);

There are other ways to do this; I'm sure a google search for tic-tac-toe or naughts and crosses implementations will show you quite a few.

If you would like to get fancy, there is a well-known technique of "labeling" each cell with a power of two. Then by adding up the scores (i.e., looking at the player's bit vector) and doing a binary AND on the set of eight winning conditions you can determine a winning position in one shot.

Here is a comment block for a tic-tac-toe game that illustrates the technique. (You didn't ask for actual code, so I'm withholding that from the answer):

/*
 * To determine a win condition, each square is "tagged"
 * from left to right, top to bottom, with successive
 * powers of 2.  Each cell thus represents an individual
 * bit in a 9-bit string, and a player's squares at any
 * given time can be represented as a unique 9-bit value.
 * A winner can thus be easily determined by checking
 * whether the player's current 9 bits have covered any
 * of the eight "three-in-a-row" combinations.
 *
 *     273                 84
 *        \               /
 *          1 |   2 |   4  = 7
 *       -----+-----+-----
 *          8 |  16 |  32  = 56
 *       -----+-----+-----
 *         64 | 128 | 256  = 448
 *       =================
 *         73   146   292
 *
 */
var wins = [7, 56, 448, 73, 146, 292, 273, 84];

This was JavaScript. For Java, use

private static final int[] WINS = new int[]{7, 56, 448, 73, 146, 292, 273, 84};

Apologies if the binary logic approach here is not what you want; I thought it would be a good place to show it off though, in case others land on this page.

深府石板幽径 2024-12-22 05:10:28

您如何存储数据?作为字符串? “X”和“O”?或者作为数字? 0、1?

就像其他人所说,如果您使用字符串(一个对象),== 运算符将不起作用!。您希望将 board[0].equals(....) 用于 ==!board.equals(...)代码> 为 !=

话虽如此,使用 Strings 对于测试场景来说确实不灵活。我在高中时玩过井字棋,所以我知道你的痛苦。使用复制和复制粘贴它确实不需要那么长的时间来编写一个大的 if 语句来检查所有行/列/对角线。五到十分钟最多。

如果您对紧凑的代码更感兴趣,您可能需要考虑使用数字,因为您可以使用数学来检查条件...

if(行不为空)和
((行中索引的总和 == 3) 玩家 1 获胜) 或
(行中索引的总和 == 0)玩家 2 获胜
)

明白了吗?

另外,我建议的一个技巧是使用二维数组(String[][]),这样您就可以更轻松地检查行/列!

How are you storing your data? As Strings? "X" and "O"? or as numbers? 0, 1?

Like others said if you use Strings (an object) the == operator will not work!. You'd want to use board[0].equals(....) for == and !board.equals(...) for !=.

Having said that, using Strings is really not flexable for testing scenarios. I did tic-tac-toe in highschool so I know your pain. Using copy & paste it really doesn't take that long to write a large if-statement to check all rows/columns/diagnals. Five or ten minutes tops.

If you're more interested in compact code you might want to consider using numbers since you can use math to check conditions...

if (row is not empty) and
((sum of indexes in row == 3) player 1 wins) or
(sum of indexes in row == 0) player 2 wins
)

Get the idea?

Also, one tip I would suggest is using a two dimensional array (String[][]) so you can check on rows/columns easier!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文