码迷,mamicode.com
首页 > 其他好文 > 详细

348. Design Tic-Tac-Toe

时间:2016-07-13 09:16:28      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

        /*
         * 348. Design Tic-Tac-Toe 
         * 2016-7-12 by Mingyang
         */
        class TicTacToe {
            private int[] rows;
            private int[] cols;
            private int diagonal;
            private int antiDiagonal;
        
            /** Initialize your data structure here. */
            public TicTacToe(int 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) {
                    diagonal += toAdd;
                }
                if (col == (cols.length - row - 1)) {
                    antiDiagonal += toAdd;
                }
                int size = rows.length;
                if (Math.abs(rows[row]) == size || Math.abs(cols[col]) == size || Math.abs(diagonal) == size
                        || Math.abs(antiDiagonal) == size) {
                    return player;
                }
                return 0;
            }
        }

 

348. Design Tic-Tac-Toe

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5665595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!