标签:who system 地址 行数据 nbsp win 判断输赢 col lse
public void whoWin(int row, int col, char ch) { // row是传入的棋子行地址,col为列地址,ch代表棋子,"黑"或“白”
int i;
int j;
int k;
char[] line = new char[getColOfBoard()]; //存储斜行数据
int chNum = 0; //若chNum >= 5,则赢
//从行判断输赢
for(i = 0; i < getColOfBoard(); i++) {
if(cheboard[row][i] == ch) {
chNum++;
if(chNum >= 5) {
System.out.println(ch + "获胜!游戏结束!");
return;
}
} else {
chNum = 0;
}
}
//从列判断输赢
chNum = 0;
for(i = 0; i < getRowOfBoard(); i++) {
if(cheboard[i][col] == ch) {
chNum++;
if(chNum >= 5) {
System.out.println(ch + "获胜!游戏结束!");
return;
}
} else {
chNum = 0;
}
}
//从右下斜行判断
chNum = 0;
k = 0;
for(i = 0; i < getColOfBoard(); i++) { //将line数组清零
line[i] = ‘0‘;
}
i = row + 1;
j = col + 1;
while(i > 0 && j > 0) {
i--;
j--;
}
while(i < getRowOfBoard() && j < getColOfBoard()) {
line[k++] = cheboard[i][j];
i++;
j++;
}
for(i = 0; i < line.length; i++) {
if(line[i] == ch) {
chNum++;
if(chNum >= 5) {
System.out.println(ch + "获胜!游戏结束!");
return;
}
} else {
chNum = 0;
}
}
//从右上斜行判断
chNum = 0;
k = 0;
for(i = 0; i < getColOfBoard(); i++) { //将line数组清零
line[i] = ‘0‘;
}
i = row;
j = col;
while(i < getRowOfBoard() - 1 && j > 0) {
i++;
j--;
}
while(i >= 0 && j < getColOfBoard()) {
line[k++] = cheboard[i][j];
i--;
j++;
}
for(i = 0; i < line.length; i++) {
if(line[i] == ch) {
chNum++;
if(chNum >= 5) {
System.out.println(ch + "获胜!游戏结束!");
return;
}
} else {
chNum = 0;
}
}
}
标签:who system 地址 行数据 nbsp win 判断输赢 col lse
原文地址:https://www.cnblogs.com/soldierback/p/9281328.html