横向判断、纵向判断、主对角线判断、副对角线判断
import java.util.*;
public class Board {
public boolean checkWon(int[][] board) {
int len = board.length;//棋盘的横向或纵向长度
int sum = 0;
//判断横向
for(int i = 0; i < len; ++i){
sum = 0;
for(int j = 0; j < len; ++j){
sum += board[i][j];
}
if(sum == len){
return true;
}
}
//判断纵向
for(int i = 0; i < len; ++i){
sum = 0;
for(int j = 0; j < len; ++j){
sum += board[i][j];
}
if(sum == len){
return true;
}
}
//主对角线
sum = 0;
for(int i = 0; i < len; ++i){
sum += board[i][i];
}
if(sum == len){
return true;
}
//副对角线
sum = 0;
for(int i = 0; i < len; ++i){
sum += board[i][len - i - 1];
}
if(sum == len){
return true;
}
//都不满足执行false
return false;
}
}