public class TicTacToe {
int[][] board;
int[] hScore;//horizontal
int[] vScore;//vertical
int[] dScore;//diagnal
public TicTacToe(int n){
board=new int[n][n];
hScore=new int[n];
vScore=new int[n];
dScore=new int[2];
}
public void addMove(char c,int i,int j){
board[i][j]=c;
int n=board.length;
if(c=='X'){
hScore[i]-=1;
vScore[j]-=1;
if(i==j) dScore[0]-=1;
if(i+j==n) dScore[1]+=1;
if(hScore[i]==-n||vScore[j]==-n||dScore[0]==-n||dScore[1]==-n){
System.out.println("Player X is win!");
return;
}
}else{
hScore[i]+=1;
vScore[j]+=1;
if(i==j) dScore[0]+=1;
if(i+j==board.length) dScore[1]+=1;
if(hScore[i]==n||vScore[j]==n||dScore[0]==n||dScore[1]==n){
System.out.println("Player O is win!");
return;
}
}
}
}
写了一个不知道对不对,欢迎批评指正