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

Design Tic-Tac-Toe

时间:2016-07-01 06:42:27      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

 1 public class TicTacToe {
 2     private int[] rows;
 3     private int[] cols;
 4     private int diag;
 5     private int antidiag;
 6     private int n;
 7     /** Initialize your data structure here. */
 8     public TicTacToe(int n) {
 9         this.n = n;
10         rows = new int[n];
11         cols = new int[n];
12         diag = 0;
13         antidiag = 0;
14     }
15     
16     /** Player {player} makes a move at ({row}, {col}).
17         @param row The row of the board.
18         @param col The column of the board.
19         @param player The player, can be either 1 or 2.
20         @return The current winning condition, can be either:
21                 0: No one wins.
22                 1: Player 1 wins.
23                 2: Player 2 wins. */
24     public int move(int row, int col, int player) {
25         int toAdd = player == 1 ? 1 : -1;
26         rows[row] += toAdd;
27         cols[col] += toAdd;
28         diag += row == col ? toAdd : 0;
29         antidiag += row == (n - col - 1) ? toAdd : 0;
30         
31         if (Math.abs(rows[row]) == n ||
32             Math.abs(cols[col]) == n ||
33             Math.abs(diag) == n ||
34             Math.abs(antidiag) == n) {
35                 return player;
36             }
37         return 0;
38     }
39 }
40 
41 /**
42  * Your TicTacToe object will be instantiated and called as such:
43  * TicTacToe obj = new TicTacToe(n);
44  * int param_1 = obj.move(row,col,player);
45  */

1. One thing need to be notice that if player put to wrong place...

Design Tic-Tac-Toe

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/5631734.html

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