import java.util.Scanner;
public class Qipan {
static char[][] chessBoard = new char[16][16];
//当isBlack为true时,黑方下子
//O表示黑方,#表示白方
static boolean isBlack =true;
public static void main(String[] args){
start();
print();
Scanner c = new Scanner(System.in);
while(true){
if(isBlack){
System.out.println("请黑方下子");
}else{
System.out.println("请白方下子");
}
//获取输入的横纵坐标
int i = c.nextInt();
int j = c.nextInt();
if(chessBoard[i][j] !='*'){
System.out.println("该位置已经有棋子,请重新输入");
}else{
if(isBlack){
chessBoard[i][j] = 'O';
//判断输赢的方法
if(isWin(i,j)){
System.out.println("黑方胜利!");
break;
}
}else{
chessBoard[i][j] = '#';
if(isWin(i,j)){
System.out.println("白方胜利!");
break;
}
}
isBlack = !isBlack;
print();
}
}
}
public static void start(){
for(int i=0;i<chessBoard.length;i++){
for(int j=0;j<chessBoard[i].length;j++){
chessBoard[i][j]='*';
}
}
}
public static void print(){
char[] c = {'0','1','2','3','4','5','6','7','8','9'};
System.out.print(" ");
for(int i=0;i<c.length;i++){
System.out.print(c[i]);
}
System.out.println();
for(int i =0;i<10;i++){
System.out.print(c[i]);
for(int j =0;j<10;j++){
System.out.print(chessBoard[i][j]);
}
//换行
System.out.println();
}
}
//判断输赢的总方法
public static boolean isWin(int i,int j){
return isLeftAndRught(i,j) ||
isUpAndDown(i,j) ||
isLeftUpAndRightDown(i,j)||
isRightUpAndLeftDown(i,j);
}
//判断左右方向的输赢
public static boolean isLeftAndRught(int i ,int j){
int j1 = j-1;
while(true){
if(j1<0||chessBoard[i][j1] != chessBoard[i][j]){
break;
}else{
j1--;
}
}
j1++;
int count = 0;
while(true){
if(j1>=16||chessBoard[i][j1] != chessBoard[i][j]){
break;
}else{
count++;
j1++;
}
}
return count >= 5;
}
//判断上下方向
public static boolean isUpAndDown(int i,int j){
int i1 = i-1;
while(true){
if(i1<0||chessBoard[i1][j] != chessBoard[i][j]){
break;
}else{
i1--;
}
}
i1++;
int count = 0;
while(true){
if(i1>=16||chessBoard[i1][j] != chessBoard[i][j]){
break;
}else{
count++;
i1++;
}
}
return count >= 5;
}
//判断左上右下
public static boolean isLeftUpAndRightDown(int i,int j){
int j1 = j-1;
int i1 = i-1;
while(true){
if(j1<0||i1<0||chessBoard[i1][j1] != chessBoard[i][j]){
break;
}else{
j1--;
i1--;
}
}
j1++;
i1++;
int count = 0;
while(true){
if(j1>=16||i1>=16||chessBoard[i1][j1] != chessBoard[i][j]){
break;
}else{
count++;
j1++;
i1++;
}
}
return count >= 5;
}
//判断右上左下
public static boolean isRightUpAndLeftDown(int i,int j){
int j1 = j-1;
int i1 = i+1;
while(true){
if(j1<0||i1>=16||chessBoard[i1][j1] != chessBoard[i][j]){
break;
}else{
j1--;
i1++;
}
}
j1++;
i1--;
int count = 0;
while(true){
if(j1>=16||i1<0||chessBoard[i1][j1] != chessBoard[i][j]){
break;
}else{
count++;
j1++;
i1--;
}
}
return count >= 5;
}
}
打印结果:
0123456789
0**********
1**********
2**********
3**********
4**********
5**********
6**********
7**********
8**********
9**********
请黑方下子