Leetcode - Design Tic-Tac-Toe

My code:

public class TicTacToe {
    int[] rows;
    int[] cols;
    int diag;
    int anti_diag;
    int n;
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        rows = new int[n];
        cols = new int[n];
        diag = 0;
        anti_diag = 0;
        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) {
            rows[row] += -1;
            cols[col] += -1;
            if (row == col) {
                diag += -1;
            }
            if (row == n - col - 1) {
                anti_diag += -1;
            }
            if (rows[row] == -n || cols[col] == -n || diag == -n || anti_diag == -n) {
                return 1;
            }
            else {
                return 0;
            }
        }
        else {
            rows[row] += 1;
            cols[col] += 1;
            if (row == col) {
                diag += 1;
            }
            if (row == n - col - 1) {
                anti_diag += 1;
            }
            if (rows[row] == n || cols[col] == n || diag == n || anti_diag == n) {
                return 2;
            }
            else {
                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);
 */

一开始到是没想出什么思路。后来看了提示才发现这么简单。

今天有些懈怠。可能因为前段时间有点累。不能这样。
真正要做的事,还没做完,路还很长。坚持下去!

Anyway, Good luck, Richardo! – 09/14/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/cadf54f94dcb#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞