package com.test;
import java.util.Scanner;
public class TestMain{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
char[] name = { '空', '黑', '白' };
int count = 0;// 计数器,用来计算该谁下棋了
char[][] qi = new char[16][16];// 把棋盘情况放进二维数组中
while (true) {
int row = 0;// 初始化从控制台接收到的row和col 的index为0
int col = 0;
if (count == 0) {
print(qi, name[count], row, col);
count++;
}
if (count == 1) {
System.out.println("请黑(@)下棋(i j):");
row = console.nextInt(16)%16;// 从控制台接收到的row的index
col = console.nextInt(16)%16;// 从控制台接收到的col的index
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 下棋正确
count++;
}
if (condition.length() == 2) {// 下棋出现错误,要下的位置已经下过棋子
System.out.println("此位置已经有棋子,黑棋重新再下");
}
if (condition.length() == 3) {// 黑棋子赢
System.out.println("黑棋很给力,白棋是浮云,黑棋赢了");
break;
}
if (condition.length() == 4) {
System.out.println("白棋子赢");
break;
}
}
if (count == 2) {
System.out.println("请白(*)下棋(i j):");
row = console.nextInt(16);
col = console.nextInt(16);
String condition = print(qi, name[count], row, col);
if (condition.length() == 1) {// 打印棋盘
count--;
}
if (condition.length() == 2) {// 下棋出现错误,要下的位置已经下过棋子
System.out.println("此位置已经有棋子,白棋重新再下");
}
if (condition.length() == 3) {
System.out.println("黑棋子赢");
break;
}
if (condition.length() == 4) {
System.out.println("白棋很给力,黑棋是浮云,白棋赢了");
break;
}
}
}
}
/**
* @param qi是装棋盘情况
* @param name轮到谁下棋了
* row 行号 col 列号 return true-下棋正确,false-下棋错误,重新下
*/
public static String print(char[][] qi, char name, int row, int col) {
String b = "无";// 初始化下棋是正确的
String str = "0123456789111111";
// 打印下第一行
System.out.print(" ");
for (int i = 0; i < str.length(); i++) {
System.out.print(str.charAt(i));
}
System.out.println();
for (int i = 0; i < qi.length; i++) {
System.out.print(str.charAt(i));
for (int j = 0; j < qi[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 (row < 0) {
break;
}
}
row++;// row此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
north = row;
while (qi[row][col] == ch) {
row++;
if (row > 15) {
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 (col < 0) {
break;
}
}
col++;// col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
west = col;
while (qi[row][col] == ch) {
col++;
if (col > 15) {
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 (row < 0 || col < 0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
wn = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row > 15 || col > 15) {
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 (row < 0 || col < 0) {
break;
}
}
row++;// row和col此时要++,不然等到下面的while(..)中的qi[row][col]永不会==ch
col++;
en = row;
while (qi[row][col] == ch) {
row++;
col++;
if (row > 15 || col > 15) {
break;
}
}
ws = row;
if ((ws - en) >= 5) {
win = true;
}
return win;
}
}