package com.hrf.lanou.java05;
import java.util.Scanner;
public class Method03 {
private static char[][] arr = new char[15][15];
private static char[] mark= {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static Scanner sc = new Scanner(System.in);
// 棋子黑子和白子
private static char blackMark = '●';
private static char whiteMark = '○';
public static void main(String[] args) {
// 初始化棋盘
createCheersBorder();
// 打印棋盘
printChessBoard();
// 调用输入下标的方法
inputMark();
}
// 输入下标所下的子
public static void inputMark() {
int i = 0;
for(i = 0 ; i < 15*15; i++) {
System.out.println("请下黑子,输入坐标为 4 , A:");
char[] loc = getLocation();
boolean canPutDown = canPutDown(loc);
while(canPutDown == false) {
System.out.println("此处已经有子,请重新输入其它子的坐标:");
loc = getLocation();
canPutDown = canPutDown(loc);
}
putDown(loc, blackMark);
// 打印棋盘
printChessBoard();
if(isWin(loc) == true) {
System.out.println("黑方获胜");
break;
}
System.out.println("请下白子,输入坐标为 4 , A:");
char[] locW = getLocation();
boolean canPutDownW = canPutDown(locW);
while(canPutDownW == false) {
System.out.println("此处已经有子,请重新输入其它子的坐标:");
locW = getLocation();
canPutDownW = canPutDown(locW);
}
putDown(locW, whiteMark);
// 打印棋盘
printChessBoard();
// 判断输赢
if(isWin(locW) == true) {
System.out.println("白方获胜");
break;
}
}
if(i == 15*15) {
System.out.println("和局,大家棋艺相等!");
}
}
// 初始化棋盘
public static void createCheersBorder() {
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j <arr[i].length; j++) {
if(i == 0) {
if(j == 0) {
arr[i][j]='┌';
}else if(j ==14) {
arr[i][j]='┐';
// System.out.print('┐');//●○
}
else {
arr[i][j]='┬';
}
}else if(i==14) {
if(j == 0) {
arr[i][j]='└';
}else if(j ==14) {
arr[i][j]='┘';
//┬├ ┼ ┤ ┴ ┘ ─
}else {
arr[i][j]='┴';
}
}else {
if(j == 0) {
arr[i][j]='├';
}else if(j == 14) {
arr[i][j]='┤';
}
else {
arr[i][j]='┼';
}
}
}
}
}
// 打印棋盘
public static void printChessBoard() {
System.out.print(" ");
for(int i = 0; i < mark.length; i++) {
System.out.print(mark[i] + " ");
}
System.out.println();
for(int i = 0; i< arr.length;i++) {
System.out.print(mark[i] + " ");
for(int j = 0;j < arr[i].length; j++) {
if(j != arr.length - 1) {
System.out.print(arr[i][j]+ "─");
}else {
System.out.print(arr[i][j]);
}
}
System.out.println();
}
}
//
// 判断字符是否正确
public static boolean isCharOk(char c) {
if(c >= 'A' && c <= 'F' || c >= '1' && c <= '9') {
return true;
}else {
return false;
}
}
// 把字符转化成数组下标
public static int charToInt(char c) {
if(c >= '1' && c <= '9') {
return c - '1';
}else {
return c - 'A' + 9;
}
}
// 获取落子的位置
private static char[] getLocation() {
String input = sc.nextLine(); // 输入字符
char c1 = 0;
char c2 = 0;
char c3 = 0;
while (true) {
if (input.length() == 3) {
c1 = input.charAt(0);// 第一个字符
c2 = input.charAt(1);
c3 = input.charAt(2);
if (isCharOk(c1) && c2 == ',' && isCharOk(c3)) {
break;
}
}
System.out.println("输入有误,请重新输入");
input = sc.nextLine();
}
char[] location = { c1, c3 };
return location;
}
// 是否可以落子
public static boolean canPutDown(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
if(arr[rowIndex][columnIndex] == blackMark || arr[rowIndex][columnIndex] == whiteMark) {
return false;
}else {
return true;
}
}
// 开始落子
public static void putDown(char[] location, char c) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
arr[rowIndex][columnIndex] = c;
}
// 判断输赢
public static boolean isWin(char[] location) {
if (upSameCount(location) + downSameCount(location) >= 4
|| leftSameCount(location) + rightSameCount(location) >= 4
|| leftUpSameCount(location) + rightDownSameCount(location) >= 4
|| leftDownSameCount(location) + rightUpSameCount(location) >= 4) {
return true;
} else {
return false;
}
}
// 看当前坐标上面有几个一样的棋子
public static int upSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex - 1;
int j = columnIndex;
while(i >= 0 && j <=14) {
if(arr[i][j] == c) {
count++;
i--;
}else {
break;
}
}
return count;
}
// 看当前坐标下面有几个一样的棋子
public static int downSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex + 1;
int j = columnIndex;
while(i >= 0 && i <=14) {
if(arr[i][j] == c) {
count++;
i++;
}else {
break;
}
}
return count;
}
// 看当前坐标左面有几个一样的棋子
public static int leftSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex;
int j = columnIndex - 1;
while(j >= 0 && j <=14) {
if(arr[i][j] == c) {
count++;
j--;
}else {
break;
}
}
return count;
}
// 看当前坐标右面有几个一样的棋子
public static int rightSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex;
int j = columnIndex + 1;
while(i >= 0 && j <=14) {
if(arr[i][j] == c) {
count++;
j++;
}else {
break;
}
}
return count;
}
// 看当前坐标右上有几个一样的棋子
public static int rightUpSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex - 1;
int j = columnIndex + 1;
while(i >= 0 && j <=14) {
if(arr[i][j] == c) {
count++;
i--;
j++;
}else {
break;
}
}
return count;
}
// 看当前坐标右下有几个一样的棋子
public static int rightDownSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex + 1;
int j = columnIndex + 1;
while(i <= 14 && j <=14) {
if(arr[i][j] == c) {
count++;
i++;
j++;
}else {
break;
}
}
return count;
}
// 看当前坐标左上有几个一样的棋子
public static int leftUpSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex - 1;
int j = columnIndex - 1;
while(i >= 0 && j >=0) {
if(arr[i][j] == c) {
count++;
i--;
j--;
}else {
break;
}
}
return count;
}
// 看当前坐标左下有几个一样的棋子
public static int leftDownSameCount(char[] location) {
char c1 = location[0];//拿到行号
char c2 = location[1];//拿到列号
int rowIndex = charToInt(c1); //转换成数组下标,整数类型的
int columnIndex = charToInt(c2);
char c = arr[rowIndex][columnIndex];
int count = 0;
int i = rowIndex + 1;
int j = columnIndex - 1;
while(i <= 14 && j >=0) {
if(arr[i][j] == c) {
count++;
i++;
j--;
}else {
break;
}
}
return count;
}
// 判断是否有空位
// 再来一局
public static void again() {
}
}
|