TIC TAC TOE WIN条件检查具有可变网格大小的检查

发布于 2025-02-03 00:39:24 字数 474 浏览 4 评论 0原文

对于TIC-TAC-toe游戏,我有一个2D的可变长度。数组中的每个条目都对应于网格中的正方形,并且可以具有0(空)1(x)或2(o)的值。

我正在写一种方法来检查任何一个播放器是否有一个并返回true,如果是的,但无法弄清楚该怎么做。当前的实现将所有内容与“锚点”进行了比较,该“锚点”对于行,列和对角线略有不同。

这是我当前的方法(仅检查行):

public static boolean hasWon(int[][] gridStatus){
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            if (gridStatus[row][col]!=gridStatus[row][0]){

            }
        }
    }

任何帮助将不胜感激。

I have a 2d array of variable length for a tic-tac-toe game. Each entry in the array corresponds to a square in the grid and can have a value of 0 (empty) 1(X) or 2(O).

I'm writing a method to check if EITHER player has one and return true if so but can't figure out how to do so. The current implementation compares everything to an "anchor point" that is slightly different for rows, columns, and diagonals.

here is my current method (only checks rows):

public static boolean hasWon(int[][] gridStatus){
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            if (gridStatus[row][col]!=gridStatus[row][0]){

            }
        }
    }

any help would be greatly appreciated.

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

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

发布评论

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

评论(3

神妖 2025-02-10 00:39:24

我最近做了一个3x3 TIC TAC TOE,但是我的Haswon()方法是通用的,在您的情况下应该有效。

我的演示的codesandbox链接:

// board is NxN 2D array where board[i][j] can be "O" or "X"
// ch is the player we want to check for winner, can be "O" or "X"
const isWinner = (board, ch) => {
  // Check rows and columns
  for (let i = 0; i < board.length; i++) {
    let row_count = 0;
    let col_count = 0;
    for (let j = 0; j < board.length; j++) {
      if (board[i][j] === ch) row_count++;
      if (board[j][i] === ch) col_count++;
    }
    if (row_count === board.length || col_count === board.length) return true;
    row_count = 0;
    col_count = 0;
  }

  // Check diagonals
  let count = 0;
  let anti_count = 0;
  for (let i = 0; i < board.length; i++) {
    if (board[i][i] === ch) count++;
    if (board[i][board.length - i - 1] === ch) anti_count++;
    if (count === board.length || anti_count === board.length) return true;
  }

  return false;
};

I recently made a 3x3 Tic Tac Toe but my hasWon() method is generic and should work in your case.

Codesandbox Link for my Demo : Tic Tac Toe Demo

// board is NxN 2D array where board[i][j] can be "O" or "X"
// ch is the player we want to check for winner, can be "O" or "X"
const isWinner = (board, ch) => {
  // Check rows and columns
  for (let i = 0; i < board.length; i++) {
    let row_count = 0;
    let col_count = 0;
    for (let j = 0; j < board.length; j++) {
      if (board[i][j] === ch) row_count++;
      if (board[j][i] === ch) col_count++;
    }
    if (row_count === board.length || col_count === board.length) return true;
    row_count = 0;
    col_count = 0;
  }

  // Check diagonals
  let count = 0;
  let anti_count = 0;
  for (let i = 0; i < board.length; i++) {
    if (board[i][i] === ch) count++;
    if (board[i][board.length - i - 1] === ch) anti_count++;
    if (count === board.length || anti_count === board.length) return true;
  }

  return false;
};

秋日私语 2025-02-10 00:39:24

我假设在您的网格中,1表示X,2表示O和0表示没有人。

我想到的是tic tac脚趾,所以让我们把这个想法纳入程序。这是一条行,让您可以将播放器处于任何位置:

interface Line {
    int get(int n);
}

现在我们可以编写返回行,列和对角线的实现行实现的方法。

Line row(int[][] grid, int rowNumber) {
    return columnNumber -> grid[rowNumber][columnNumber];
}

Line column(int[][] grid, int columnNumber) {
    return rowNumber -> grid[rowNumber][columnNumber];
}

Line rightDiagonal(int[][] grid) {
    return i -> grid[i][i];
}

Line leftDiagonal(int[][] grid, int length) {
    return i -> grid[i][length - 1 - i];
}

这是一种将所有线路列入网格中的方法:

private List<Line> makeLines(int[][] grid) {
    List<Line> result = new ArrayList<>();
    for (int i = 0; i < grid.length; i++) {
        result.add(row(grid, i));
        result.add(column(grid, i));
    }
    result.add(rightDiagonal(grid));
    result.add(leftDiagonal(grid, grid.length));
    return result;
}

一种看谁赢得一线赢的方法:

int getWinnerFromLine(Line line, int length) {
    int first = line.get(0);
    if (first == 0) return 0;
    for (int i = 0; i < length; i++) {
        if (line.get(i) != first) return 0;
    }
    return first;
}

最后,很容易找出谁在抽奖的情况下赢了,或者如果没有人赢,则零。

int getWinningPlayer(int[][] grid) {
    List<Line> lines = makeLines(grid); // This could be created once at the start of the game.
    
    for (Line line : lines) {
        int winner = getWinnerFromLine(line, grid.length);
        if (winner != 0) return winner;
    }
    return 0;
}

I am assuming that in your grid, 1 means X, 2 means O and 0 means nobody.

I think of tic tac toe in terms of lines, so let's put that idea into the program. Here is a line that let's you get the player at any position:

interface Line {
    int get(int n);
}

Now we can write methods that return Line implementations for rows, columns and diagonals.

Line row(int[][] grid, int rowNumber) {
    return columnNumber -> grid[rowNumber][columnNumber];
}

Line column(int[][] grid, int columnNumber) {
    return rowNumber -> grid[rowNumber][columnNumber];
}

Line rightDiagonal(int[][] grid) {
    return i -> grid[i][i];
}

Line leftDiagonal(int[][] grid, int length) {
    return i -> grid[i][length - 1 - i];
}

and a method to get all the lines in the grid:

private List<Line> makeLines(int[][] grid) {
    List<Line> result = new ArrayList<>();
    for (int i = 0; i < grid.length; i++) {
        result.add(row(grid, i));
        result.add(column(grid, i));
    }
    result.add(rightDiagonal(grid));
    result.add(leftDiagonal(grid, grid.length));
    return result;
}

And a method to see who won in a line:

int getWinnerFromLine(Line line, int length) {
    int first = line.get(0);
    if (first == 0) return 0;
    for (int i = 0; i < length; i++) {
        if (line.get(i) != first) return 0;
    }
    return first;
}

Then finally it is easy to find out who won in the case of a draw, or zero if nobody won.

int getWinningPlayer(int[][] grid) {
    List<Line> lines = makeLines(grid); // This could be created once at the start of the game.
    
    for (Line line : lines) {
        int winner = getWinnerFromLine(line, grid.length);
        if (winner != 0) return winner;
    }
    return 0;
}
老娘不死你永远是小三 2025-02-10 00:39:24

我认为您可以通过控制每种可能的获胜方式和他们的结果来检查玩家是否赢了。另外,我将传递您的检查方法,以确定玩家是否赢了。

通过使用流,您可以大大减少代码长度并使其更具可读性。

public static boolean hasPlayerWon(int[][] grid, int player) {
    return isTopLeftDiagonalWin(grid, player) || isTopRightDiagonalWin(grid, player) || isHorizontalWin(grid, player) || isVerticalWin(grid, player);
}

public static boolean isTopLeftDiagonalWin(int[][] grid, int player) {
    //Checks if every element on the diagonal starting from top left is equal to the given player's value
    return IntStream.range(0, grid.length).allMatch(i -> grid[i][i] == player);
}

public static boolean isTopRightDiagonalWin(int[][] grid, int player) {
    //Checks if every element on the diagonal starting from top right is equal to the given player's value
    return IntStream.range(0, grid.length).allMatch(i -> grid[i][(grid.length - 1) - i] == player);
}

public static boolean isHorizontalWin(int[][] grid, int player) {
    //Checks if any row has all columns with the same player's value
    return IntStream.range(0, grid.length).anyMatch(
            i -> IntStream.range(0, grid[i].length).allMatch(j -> grid[i][j] == player));
}

public static boolean isVerticalWin(int[][] grid, int player) {
    //Assuming this is an N x N matrix
    //Checks if any column has all rows with the same player's value
    return IntStream.range(0, grid.length).anyMatch(
            i -> IntStream.range(0, grid.length).allMatch(j -> grid[j][i] == player));
}

这是一个示例项目,用于测试上述代码:

https://www.jdoodle.com/iembed /v0/rv3

I think you could check if a player has won or not by controlling each possible way of winning and the OR-ing their result. Also, I would pass to your checking method the player's value to establish if the player has won or not.

By using streams, you can drastically reduce the code length and make it much more readable.

public static boolean hasPlayerWon(int[][] grid, int player) {
    return isTopLeftDiagonalWin(grid, player) || isTopRightDiagonalWin(grid, player) || isHorizontalWin(grid, player) || isVerticalWin(grid, player);
}

public static boolean isTopLeftDiagonalWin(int[][] grid, int player) {
    //Checks if every element on the diagonal starting from top left is equal to the given player's value
    return IntStream.range(0, grid.length).allMatch(i -> grid[i][i] == player);
}

public static boolean isTopRightDiagonalWin(int[][] grid, int player) {
    //Checks if every element on the diagonal starting from top right is equal to the given player's value
    return IntStream.range(0, grid.length).allMatch(i -> grid[i][(grid.length - 1) - i] == player);
}

public static boolean isHorizontalWin(int[][] grid, int player) {
    //Checks if any row has all columns with the same player's value
    return IntStream.range(0, grid.length).anyMatch(
            i -> IntStream.range(0, grid[i].length).allMatch(j -> grid[i][j] == player));
}

public static boolean isVerticalWin(int[][] grid, int player) {
    //Assuming this is an N x N matrix
    //Checks if any column has all rows with the same player's value
    return IntStream.range(0, grid.length).anyMatch(
            i -> IntStream.range(0, grid.length).allMatch(j -> grid[j][i] == player));
}

Here is a sample project to test the code above:

https://www.jdoodle.com/iembed/v0/rv3

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