java 348.设计Tic-Tac-Toe(#1).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 348.设计Tic-Tac-Toe(#1).java相关的知识,希望对你有一定的参考价值。

class TicTacToe(object):

    def __init__(self, n):
        """
        Initialize your data structure here.
        :type n: int
        """
        self.rows = [0] * n
        self.cols = [0] * n
        self.diags = [0] * n
        self.n = n
        self.mp = {1 : 1, 2 : -1};
        
        

    def move(self, row, col, player):
        """
        Player {player} makes a move at ({row}, {col}).
        @param row The row of the board.
        @param col The column of the board.
        @param player The player, can be either 1 or 2.
        @return The current winning condition, can be either:
                0: No one wins.
                1: Player 1 wins.
                2: Player 2 wins.
        :type row: int
        :type col: int
        :type player: int
        :rtype: int
        """
        
        self.rows[row] += self.mp[player]
        self.cols[col] += self.mp[player]
        target = [self.rows[row], self.cols[col]]
        if row == col :
            self.diags[0] += self.mp[player]
            target.append(self.diags[0])
        if row + col + 1 == self.n :
            self.diags[1] += self.mp[player]
            target.append(self.diags[1])
        
        for i in target :
            if abs(i) == self.n : 
                return player
        return 0

# Your TicTacToe object will be instantiated and called as such:
# obj = TicTacToe(n)
# param_1 = obj.move(row,col,player)
public class TicTacToe {
    
    private int[] rows;
    private int[] cols;
    private int n;
    private int diags;
    private int antiDiags;
    
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        this.n = n;
        rows = new int[n];
        cols = new int[n];
    }
    
    /** Player {player} makes a move at ({row}, {col}).
        @param row The row of the board.
        @param col The column of the board.
        @param player The player, can be either 1 or 2.
        @return The current winning condition, can be either:
                0: No one wins.
                1: Player 1 wins.
                2: Player 2 wins. */
    public int move(int row, int col, int player) {
        int toAdd = (player == 1)? +1: -1;
        
        rows[row] += toAdd;
        cols[col] += toAdd;
        if (row == col) {
            diags += toAdd;
        }
        if (row+col == n-1) {
            antiDiags += toAdd;
        }
        
        if (Math.abs(rows[row]) == n || Math.abs(cols[col]) == n || Math.abs(diags) == n || Math.abs(antiDiags) == n) {
            return player;
        }
        
        return 0;
    }

}

/**
 * Your TicTacToe object will be instantiated and called as such:
 * TicTacToe obj = new TicTacToe(n);
 * int param_1 = obj.move(row,col,player);
 */
class TicTacToe {

    /** Initialize your data structure here. */
    int[] rows;
    int[] cols;
    int[] diags;
    int left, n;
    public TicTacToe(int n) {
        rows = new int[n];
        cols = new int[n];
        diags = new int[2];
        left = n * n;
        this.n = n;
    }
    
    /** Player {player} makes a move at ({row}, {col}).
        @param row The row of the board.
        @param col The column of the board.
        @param player The player, can be either 1 or 2.
        @return The current winning condition, can be either:
                0: No one wins.
                1: Player 1 wins.
                2: Player 2 wins. */
    public int move(int row, int col, int player) {
        if (player == 1) {
            if (++rows[row] == n) return 1;
            if (++cols[col] == n) return 1;
            if (col == row && ++diags[0] == n) return 1;
            if (col + row + 1 == n && ++diags[1] == n) return 1;
        } else if (player == 2) {
            if (--rows[row] == -n) return 2;
            if (--cols[col] == -n) return 2;
            if (col == row && --diags[0] == -n) return 2;
            if (col + row + 1 == n && --diags[1] == -n) return 2;
        }
        return 0;
    }
}

/**
 * Your TicTacToe object will be instantiated and called as such:
 * TicTacToe obj = new TicTacToe(n);
 * int param_1 = obj.move(row,col,player);
 */

以上是关于java 348.设计Tic-Tac-Toe(#1).java的主要内容,如果未能解决你的问题,请参考以下文章

java 348.设计Tic-Tac-Toe(#1).java

java 348.设计Tic-Tac-Toe(#1).java

java 348.设计Tic-Tac-Toe(#1).java

java 348.设计Tic-Tac-Toe(#1).java

2021-11-09:设计井字棋。谁先同行或者同列都是自己的棋子,就算获得胜利 。力扣348。

348JSP047电子商务在线购物商城系统