五子棋代码java随机 五子棋代码js(16)


String b = "无";// 初始化下棋是正确的
String str = "0123456789111111";
// 打印下第一行
System.out.print(" ");
for (int i = 0; istr.length(); i++) {
System.out.print(str.charAt(i));
}
System.out.println();
for (int i = 0; iqi.length; i++) {
System.out.print(str.charAt(i));
for (int j = 0; jqi[i].length; j++) {
if (name == '空') { // 还没开始下棋时,棋盘默认显示是'.'
qi[i][j] = '.';
}
if ((name == '黑')(i == row)(j == col)) {
if (qi[i][j] != '.') {// 下的位置已经有棋子了,返回false
b = "有棋";
} else {
qi[i][j] = '@';// 黑方下棋后的位置显示是'@'
if (win(qi, '@', row, col)) {
b = "黑棋子";
}
}
}
if ((name == '白')(i == row)(j == col)) {
if (qi[i][j] != '.') {
b = "有棋";
} else {
qi[i][j] = '*';// 黑方下棋后的位置显示是'*'
if (win(qi, '*', row, col)) {
b = "白棋子赢";
}
}
}
System.out.print(qi[i][j]);
}
System.out.println();
}
return b;
}
public static boolean win(char[][] qi, char ch, int row, int col) {
boolean b = false;
boolean ns = n2s(qi, ch, row, col);
boolean we = w2e(qi, ch, row, col);
boolean wnes = wn2es(qi, ch, row, col);
boolean enws = en2ws(qi, ch, row, col);
b = ns || we || wnes || enws;
return b;
}
/* 从北到南方向 */
public static boolean n2s(char[][] qi, char ch, int row, int col) {
boolean win = false;
int north = 0;// begin
int south = 0;// end
while (qi[row][col] == ch) {
row--;
if (row0) {
break;
}
}
row++;// row此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
north = row;
while (qi[row][col] == ch) {
row++;
if (row15) {
break;
}
}
south = row;
if ((south - north) = 5) {
win = true;
}
return win;
}
/* 从西到东方向 */
public static boolean w2e(char[][] qi, char ch, int row, int col) {
boolean win = false;
int west = 0;// begin
int east = 0;// end
while (qi[row][col] == ch) {
col--;
if (col0) {
break;
}
}
col++;// col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
west = col;
while (qi[row][col] == ch) {
col++;
if (col15) {
break;
}
}
east = col;
if ((east - west) = 5) {
win = true;
}
return win;
}
/* 从西北到东南方向 */
public static boolean wn2es(char[][] qi, char ch, int row, int col) {
boolean win = false;
int wn = 0;// begin
int es = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row0 || col0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
wn = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row15 || col15) {
break;
}
}
es = row;
if ((es - wn) = 5) {
win = true;
}
return win;
}
/* 从东北到西南方向 */
public static boolean en2ws(char[][] qi, char ch, int row, int col) {
boolean win = false;
int en = 0;// begin
int ws = 0;// end
while (qi[row][col] == ch) {
row--;
col--;
if (row0 || col0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
en = row;
while (qi[row][col] == ch) {

推荐阅读