簡體   English   中英

井字游戲板-GetWinner

[英]Tic Tac Toe Board - GetWinner

這是給井字游戲委員會獲獎者名稱(X或Y)的方法。 如果沒有贏家,則返回空。

玩家X首先進入:

但在一種特殊情況下,如果不起作用:x播放:2,0 o播放:1,0 x播放:2,1 o播放:1,1 x播放2,2(此時,“ X”應獲勝但什么也沒發生

o上場:1,2(此時'O'應該獲勝,但什么也沒有發生)

x發揮任何作用

O被宣布為贏家。

我不知道該錯誤。

公共靜態char checkWinner(char [] []數組){

// check for row winners
for (int row = 0; row < array.length; row++) {
  // if all three columns are the same then that player wins!
  if (array[row][0] == array[row][1] && array[row][1] == array[row][2]) {
    // return the winner!
    return array[row][0];
  }
}

// check for column winners
for (int col = 0; col < array.length; col++) {
  // if all three rows are the same then that player wins!
  if (array[0][col] == array[1][col] && array[1][col] == array[2][col]) {
    // return our winner!
    return array[0][col];
  }
}

// check for diag winners
if (array[0][0] == array[1][1] && array[1][1] == array[2][2]) {
  return array[0][0];
}

if (array[0][2] == array[1][1] && array[1][1] == array[2][0]) {
  return array[0][2];
}

// otherwise just return an empty character
return ' ';

}

如果有幫助,請看下面的完整代碼:

import java.util.Scanner;

public class TicTacToe {

  public static void main(String[] args) {
    // create an empty board
    char[][] board = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };

    Scanner input = new Scanner(System.in);

    // start off with player 'X'
    char player = 'X';

    while (true) {
      printBoard(board);

      // ask the player for a location
      System.out.println("Welcome, player " + player);
      System.out.print("Enter a row: ");
      int row = input.nextInt();
      System.out.print("Enter a col: ");
      int col = input.nextInt();

      // check to see if this row is already taken
      if (board[row][col] != ' ') {
        System.out.println("\nThat spot is already taken!");
      } else {
        // otherwise assign the player's token to this spot
        board[row][col] = player;

        // check to see if someone won
        char testWinner = checkWinner(board);

        // do we have a winner?
        if (testWinner != ' ') {
          printBoard(board);
          System.out.println("We have a winner! " + testWinner);
          break;
        } else {
          // switch to the next player
          if (player == 'X') {
            player = 'O';
          } else {
            player = 'X';
          }
        }
      }
    }
  }

  public static char checkWinner(char[][] array) {
    // check for row winners
    for (int row = 0; row < array.length; row++) {
      // if all three columns are the same then that player wins!
      if (array[row][0] == array[row][1] && array[row][1] == array[row][2]) {
        // return the winner!
        return array[row][0];
      }
    }

    // check for column winners
    for (int col = 0; col < array.length; col++) {
      // if all three rows are the same then that player wins!
      if (array[0][col] == array[1][col] && array[1][col] == array[2][col]) {
        // return our winner!
        return array[0][col];
      }
    }

    // check for diag winners
    if (array[0][0] == array[1][1] && array[1][1] == array[2][2]) {
      return array[0][0];
    }

    if (array[0][2] == array[1][1] && array[1][1] == array[2][0]) {
      return array[0][2];
    }

    // otherwise just return an empty character
    return ' ';
  }

  public static void printBoard(char[][] array) {
    System.out.println("\n----------");
    for (int row = 0; row < array.length; row++) {
      System.out.print("|");

      for (int col = 0; col < array.length; col++) {
        System.out.print(array[row][col] + " |");
      }

      System.out.println("\n----------");
    }

    System.out.println();
  }

}

問題是,當您看到當前組合的所有三個字符都相同時,無需檢查它是否為三個空字符。 在您的示例中,由於x = 0的列用' '填充,因此現在將給出' ' 這種檢查所有組合的方法也有點長,我會這樣做:

public static char checkWinner(char[][] array) {
    int[][] combinations = {
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        {0, 4, 8},
        {2, 4, 6}
    };
    for (int[] co : combinations) {
        char[] cs = new char[] {
            array[co[0] % 3][co[0] / 3],
            array[co[1] % 3][co[1] / 3],
            array[co[2] % 3][co[2] / 3]
        };
        if (cs[0] != ' ' && cs[0] == cs[1] && cs[1] == cs[2]) {
            return cs[0];
        }
    }
    return ' ';
}

如果條件忽略空格字符,則為checkWinner。 由於第一行是所有空格,因此該行塊將在第一次嘗試時返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM