交叉匹配二维数组

发布于 2025-01-16 20:28:54 字数 400 浏览 2 评论 0原文

尝试实现更大规模的井字游戏,

该游戏就可以有超过 3 行和列,

只要找到 4 个连续模式(水平、垂直或交叉),

玩家就是赢家

我已经找到了水平和垂直数学实现,

但是无法找到一种方法来识别二维数组中某个字符的交叉模式

考虑以下二维数组

`

        char[][] char2d={
            {'*','o','o','*'},
            {'o','*','o','o'},
            {'o','o','*','o'},
            {'o','o','o','*'}
    }

`

我如何检查'*'字符是否有四个连续的交叉模式这个二维数组

trying to implement a bigger scale of tic tac toe game

this game can have more than 3 rows and columns

whenever a 4 consecutive pattern is found(horizantal,vertical or cross)

the player is the winner

i already found the horizantal and vertical mathcing implemantation

but can't find a way to identify the cross pattern of a certain charcter inside a 2d array

consider the following 2d array

`

        char[][] char2d={
            {'*','o','o','*'},
            {'o','*','o','o'},
            {'o','o','*','o'},
            {'o','o','o','*'}
    }

`

how can i check if the '*' character has four consecutive cross pattern in this 2d array

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

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

发布评论

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

评论(3

雾里花 2025-01-23 20:28:54

如果给定的起始位置是长度为 s 且充满字符 的向下对角线(如图所示,从左到右前进),则此函数返回 true c.

public static void isFullDownDiagonal(char[][] board, 
         int startCol, int startRow, int s, char c) {

   for (int i=0; i<s; i++) {
      if (board[startRow+i][startCol+i] != c) return false; 
                     //  \_ change this sign to test up diagonal         
   }
   return true;
}

通过微小的改变,您可以测试向上的对角线。在 NxN 板上,只能在顶部的 (N-s+1)x(N-s+1) 区域中存在对角线起点 -左角和下角。

This function returns true if the given start position is a downwards diagonal (as drawn in your picture, advancing left-to-right) of length s full of character c.

public static void isFullDownDiagonal(char[][] board, 
         int startCol, int startRow, int s, char c) {

   for (int i=0; i<s; i++) {
      if (board[startRow+i][startCol+i] != c) return false; 
                     //  \_ change this sign to test up diagonal         
   }
   return true;
}

With a small change you can test for the upwards diagonal. And in an NxNboard there can only be starts-of-diagonal in a (N-s+1)x(N-s+1) area at the top-left and bottom-corners.

悸初 2025-01-23 20:28:54

另一个...

  public static void main(String[] args) {       
    char[][] char2d={
      {'*','o','o','*'},
      {'o','*','o','o'},
      {'o','o','*','o'},
      {'o','o','o','*'}
    };
    System.out.println(hasCross(char2d, '*'));
  }

  public static boolean hasCross(char[][] board, char c) {
    // Assumes a SQUARE board!
    boolean crossA = true;
    boolean crossB = true;
    for(int col=0, rowA=0, rowB=(board.length-1); col<board.length; col++, rowA++, rowB--) {
      if (board[rowA][col]!=c) {
        crossA = false;
      }
      if (board[rowB][col]!=c) {
        crossB = false;
      }
    }
    return crossA || crossB;
  }

Another one...

  public static void main(String[] args) {       
    char[][] char2d={
      {'*','o','o','*'},
      {'o','*','o','o'},
      {'o','o','*','o'},
      {'o','o','o','*'}
    };
    System.out.println(hasCross(char2d, '*'));
  }

  public static boolean hasCross(char[][] board, char c) {
    // Assumes a SQUARE board!
    boolean crossA = true;
    boolean crossB = true;
    for(int col=0, rowA=0, rowB=(board.length-1); col<board.length; col++, rowA++, rowB--) {
      if (board[rowA][col]!=c) {
        crossA = false;
      }
      if (board[rowB][col]!=c) {
        crossB = false;
      }
    }
    return crossA || crossB;
  }
心房的律动 2025-01-23 20:28:54

您可以使用 IntStream 检查对角线是否只有 '*'

public static boolean checkDiagonals(char[][] arr) {
    int dimension = arr.length;
    return IntStream.range(0, dimension)
            .allMatch(i -> arr[i][i] == '*') 
    || IntStream.range(0, dimension)
            .allMatch(i -> arr[i][dimension - i - 1] == '*');
}

You can use IntStream to check if the diagonals have only '*':

public static boolean checkDiagonals(char[][] arr) {
    int dimension = arr.length;
    return IntStream.range(0, dimension)
            .allMatch(i -> arr[i][i] == '*') 
    || IntStream.range(0, dimension)
            .allMatch(i -> arr[i][dimension - i - 1] == '*');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文