一、构思实现方法:
1、初始化棋盘(抽取到一个方法中)
2、打印棋盘 (抽取到一个方法中)
3、实现黑白双方交替下棋,每次下完后打印棋盘
4、判断棋子是否正确落下(是否越界,是否重复)
5、判断输赢
二、分部实现:
1、设置初始变量
public class WZQ {
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];//棋盘
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
}
2、初始化棋盘:
public static void init() {
for (int i = 0; i < qp.length; i++)
for (int j = 0; j < qp[i].length; j++) {
if (j == qp[i].length - 1) {
qp[i][j] = num[i];
continue;
}
if (i == qp.length - 1) {
qp[i][j] = num[j];
continue;
}
qp[i][j] = line;
}
}
3、编写输出程序:
public static void print() {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
4、开始下棋:
import java.util.Scanner;
public static void startGame() {
Scanner scanner = new Scanner(System.in);
boolean flage = true;//true==黑棋 false==白棋
int flage1 = 1;//判断是否越界 0越界 1不越界
do {
if (flage) {
//下黑棋
System.out.print("请输入黑棋横坐标:");
int x = scanner.nextInt() - 1;
System.out.print("请输入黑棋纵坐标:");
int y = scanner.nextInt() - 1;
qp[x][y] = black;
print();
flage = false;
}
if (flage == false) {
//下白棋
System.out.print("请输入白棋横坐标:");
int x = scanner.nextInt() - 1;
System.out.print("请输入白棋纵坐标:");
int y = scanner.nextInt() - 1;
qp[x][y] = white;
print();
flage = true;
}
} while (true);
}
5、判断棋子是否正确落下(是否越界,是否重复)
startGame内部:
flage1 = which(x, y);
if (flage1 == 0) {
System.out.println("黑棋越界或重复,请重新输入");
continue;
}
public static int which(int a, int b) {
//是否越界
if ((a < 0 || a > 13) || (b < 0 || b > 13)) {
return 0;
}
//是否重复
if (qp[a][b] == white || qp[a][b] == black) {
return 0;
}
return 1;
}
6、判断输赢
startGame内部:
boolean winer = win(x, y, black);
if (winer) {
break;
}
flage = false;
public static boolean win(int a, int b, String what) {
int n = 1;
//判断行
for (int y = b + 1; y < qp[a].length; y++) {
if (qp[a][y] == what) {
n += 1;
} else {
break;
}
}
for (int y = b - 1; y >= 0; y--) {
if (qp[a][y] == what) {
n += 1;
} else {
break;
}
}
//判断列
n = 1;
for (int x = a + 1; x < qp.length; x++) {
if (qp[x][b] == what) {
n += 1;
} else {
break;
}
}
for (int x = a - 1; x >= 0; x--) {
if (qp[x][b] == what) {
n += 1;
} else {
break;
}
}
//判断右下到左上
n = 1;
for (int x = a + 1, y = b + 1; x < qp.length && y < qp[x].length; x++, y++) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
for (int x = a - 1, y = b - 1; x >= 0 && y >= 0; x--, y--) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
//判断右上到左下
for (int x = a - 1, y = b + 1; x >= 0 && y < qp[x].length; x--, y++) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
for (int x = a + 1, y = b - 1; x < qp.length && y >= 0; x++, y--) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
if (n >= 5) {
return true;
}
return false;
}
三、总代码:
import java.util.Scanner;
public class WZQ {
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];//棋盘
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
public static void main(String[] args) {
init();
print();
startGame();
}
/*public static void start() {
init();
print();
startGame();
}*/
/**
* 开始下棋
*/
public static void startGame() {
Scanner scanner = new Scanner(System.in);
boolean flage = true;//true==黑棋 false==白棋
int flage1 = 1;//判断是否越界 0越界 1不越界
do {
if (flage) {
//下黑棋
System.out.print("请输入黑棋横坐标:");
int x = scanner.nextInt() - 1;
System.out.print("请输入黑棋纵坐标:");
int y = scanner.nextInt() - 1;
//判断下棋是否规范
flage1 = which(x, y);
if (flage1 == 0) {
System.out.println("黑棋越界或重复,请重新输入");
continue;
}
qp[x][y] = black;
print();
//判断输赢
boolean winer = win(x, y, black);
if (winer) {
break;
}
flage = false;
}
if (flage == false) {
//下白棋
System.out.print("请输入白棋横坐标:");
int x = scanner.nextInt() - 1;
System.out.print("请输入白棋纵坐标:");
int y = scanner.nextInt() - 1;
//判断输入是否正确
flage1 = which(x, y);
if (flage1 == 0) {
System.out.println("白棋越界或重复,请重新输入");
continue;
}
qp[x][y] = white;
print();
//判断输赢
boolean winer = win(x, y, white);
if (winer) {
break;
}
flage = true;
}
} while (true);
if (flage) {
System.out.println("黑棋胜");
} else {
System.out.println("白棋胜");
}
}
/**
* 判断越界重复
*/
public static int which(int a, int b) {
//是否越界
if ((a < 0 || a > 13) || (b < 0 || b > 13)) {
return 0;
}
//是否重复
if (qp[a][b] == white || qp[a][b] == black) {
return 0;
}
return 1;
}
/**
* 判断输赢
*/
public static boolean win(int a, int b, String what) {
int n = 1;
//判断行
for (int y = b + 1; y < qp[a].length; y++) {
if (qp[a][y] == what) {
n += 1;
} else {
break;
}
}
for (int y = b - 1; y >= 0; y--) {
if (qp[a][y] == what) {
n += 1;
} else {
break;
}
}
//判断列
n = 1;
for (int x = a + 1; x < qp.length; x++) {
if (qp[x][b] == what) {
n += 1;
} else {
break;
}
}
for (int x = a - 1; x >= 0; x--) {
if (qp[x][b] == what) {
n += 1;
} else {
break;
}
}
//判断右下到左上
n = 1;
for (int x = a + 1, y = b + 1; x < qp.length && y < qp[x].length; x++, y++) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
for (int x = a - 1, y = b - 1; x >= 0 && y >= 0; x--, y--) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
//判断右上到左下
for (int x = a - 1, y = b + 1; x >= 0 && y < qp[x].length; x--, y++) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
for (int x = a + 1, y = b - 1; x < qp.length && y >= 0; x++, y--) {
if (qp[x][y] == what) {
n += 1;
} else {
break;
}
}
if (n >= 5) {
return true;
}
return false;
}
/**
* 初始化棋盘
*/
public static void init() {
for (int i = 0; i < qp.length; i++)
for (int j = 0; j < qp[i].length; j++) {
if (j == qp[i].length - 1) {
qp[i][j] = num[i];
continue;
}
if (i == qp.length - 1) {
qp[i][j] = num[j];
continue;
}
qp[i][j] = line;
}
}
/**
* 打印棋盘
*/
public static void print() {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
}