Design a Tic-tac-toe game that is played between two players on a nn grid.

You may assume the following rules:

  1. A move is guaranteed to be valid and is placed on an empty block.
  2. Once a winning condition is reached, no more moves is allowed.
  3. A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
class TicTacToe {

    /** Initialize your data structure here. */

    int[] rows;
int[] cols;
int diagnal;
int antidiagnal;
int size;
public TicTacToe(int n) {
this.rows = new int[n];
this.cols = new int[n];
this.size = 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) {
diagnal += toAdd;
}
if (row + col + 1 == size) {
antidiagnal += toAdd;
}
// need to use Math.abs to consider player2
if (Math.abs(rows[row]) == size || Math.abs(cols[col]) == size || Math.abs(diagnal) == size || Math.abs(antidiagnal) == size) {
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);
*/

[LC] 348. Design Tic-Tac-Toe的更多相关文章

  1. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  2. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  3. 【leetcode】1275. Find Winner on a Tic Tac Toe Game

    题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...

  4. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  5. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  6. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  7. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  8. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

  9. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

随机推荐

  1. 201771010123汪慧和《面向对象程序设计Java》第十四周实验总结

    一.理论部分 1.Swing和MVC设计模式 (1)设计模式初识 (2)模式—试图—控制器模式 (3)Swing组件的模型—试图—控制器分析 2.Java组件有内容.外观.行为三个主要元素:这三个主要 ...

  2. SAP_MM常用代码

    1.采购申请创建/修改/查看:ME51N/ME52N/ME53N 2.采购申请审批:ME54N 3.采购订单创建/修改/查看:ME21N/ME22N/ME23N 4.单个采购订单审批:ME29N 5. ...

  3. [转]Linux命令行上传文件到 百度网盘 bypy

    安装软件工具: apt-get install python-pip pip install requests pip install bypy 授权登陆: 执行 bypy info,显示下边信息,根 ...

  4. Python中的抽象基类

    1.说在前头 "抽象基类"这个词可能听着比较"深奥",其实"基类"就是"父类","抽象"就是&quo ...

  5. Linux学习-课后练习(第二章命令)20200216

  6. Json字符串转Java对象和List集合

    对象POJO和JSON互转 public class JsonUtil { /** * JSON 转 POJO */ public static <T> T getObject(Strin ...

  7. html_位置偏移属性position

    定位属性 位置属性position:static.relative.absolute.fixed 偏移属性:top.bottom.left.right 浮动定位属性:float/clear 1.浮动定 ...

  8. Centos7 死循环登录问题

    问题:用户名和密码输入正确,登录后屏幕闪一下又回到初始的登录界面.不知道具体什么原因引起的,先记录下不知道是否正确的解决方案,网上找了些相关的方案有的也实现不了,可能这个问题跟装的虚拟机的版本也有关系 ...

  9. 吴裕雄--天生自然MySQL学习笔记:MySQL ALTER命令

    需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令. root@host# mysql -u root -p password; Enter password:******* ...

  10. 吴裕雄--天生自然 PHP开发学习:数组

    <?php $cars=array("Volvo","BMW","Toyota"); echo "I like " ...