博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Design Tic-Tac-Toe
阅读量:6905 次
发布时间:2019-06-27

本文共 1551 字,大约阅读时间需要 5 分钟。

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...

转载于:https://www.cnblogs.com/shuashuashua/p/5631734.html

你可能感兴趣的文章
20050425:公测啊,晚点再说
查看>>
Windows Azure媒体服务使得伦敦奥运会的云端传输成为可能
查看>>
错误:媒体集有 2 个媒体簇,但只提供了 1 个 sql2005 备份错误。
查看>>
(实用篇)PHP ftp上传文件操作类
查看>>
java常用类--与用户互动
查看>>
马化腾IT领袖峰会力推,微信小程序即将迎来爆发拐点
查看>>
javascript js 判断页面是否加载完成
查看>>
【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
查看>>
架构思维案例:速学正则
查看>>
使用 IntraWeb (11) - 基本控件之 TIWButton
查看>>
Python数据结构——散列表
查看>>
责任链模式
查看>>
Android 仿携程活动列表边框布局
查看>>
Android 实现ActionBar定制
查看>>
Git 子模块 - submodule(转)
查看>>
iPhone开发笔记[12/50]:内存泄漏是新手必然要经历的痛,NSMutableArray的正确使用...
查看>>
MySQL常用命令大全
查看>>
查看Android应用签名信息
查看>>
MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录...
查看>>
WCF学习之旅—WCF服务的批量寄宿(十三)
查看>>