五子棋开始
public class WZQRun {
public static void main(String[] args) {
WZQ.start();
}
}
五子棋小程序具体实现代码:
import java.util.Scanner;
public class WZQ {
static String white = "○";//白字图案
static String black = "●";//黑子图案
static Scanner scan = new Scanner(System.in);
static String[][] qp = new String[15][15];
static String line = "十";//棋盘格子
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static boolean mark = true;//黑方或白方
static boolean sign = false;
static int count = 0;
public static void start() {
chessboard();//棋盘初始化
while (true) {
if (sign) {
if (mark == false) {
print();
System.out.println("黑方胜利");
} else {
print();
System.out.println("白方胜利");
}
return;
}
print();
play();
}
}
public static void chessboard() {//棋盘初始化
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();
}
}
public static void play() {//黑白交替下棋
System.out.println("请" + (mark ? "黑" : "白") + "子下子");
System.out.print("x(行):");
int x = scan.nextInt();
System.out.print("y(列):");
int y = scan.nextInt();
if (x < 1 || x > qp.length - 1 || y < 1 || y > qp.length - 1) {//判断是否越界
System.out.println("请重新输入:");
return;
}
if (qp[x - 1][y - 1] != line) {//判断是否重复
System.out.println("请重新输入:");
return;
}
qp[x - 1][y - 1] = (mark ? black : white);
if (mark) {
mark = false;
} else {
mark = true;
}
sign = judge(x, y);
}
public static boolean judge(int x, int y) {
for (int i = 0; i < qp[x - 1].length - 5; i++) {//遍历横坐标那一行
if (qp[x - 1][i] == black) {//遇到黑子,进入判断
for (int j = 0; j < 5; j++) {//判断从当前位置向后四个是否都为黑子
if (qp[x - 1][i + j] != black) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else if (qp[x - 1][i] == white) {
for (int j = 0; j < 5; j++) {//判断从当前位置向后四个是否都为白子
if (qp[x - 1][i + j] != white) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else {
continue;
}
}
for (int i = 0; i < qp[y - 1].length - 5; i++) {//遍历纵坐标那一列
if (qp[i][y - 1] == black) {//遇到黑子,进入判断
for (int j = 0; j < 5; j++) {//判断从当前位置向下四个是否都为黑子
if (qp[i + j][y - 1] != black) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else if (qp[i][y - 1] == white) {
for (int j = 0; j < 5; j++) {//判断从当前位置向下四个是否都为白子
if (qp[i + j][y - 1] != white) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else {
continue;
}
}
for (int i = 0; i < qp.length - 5; i++) {//先判断"/"
for (int j = 4; j < qp.length; j++) {
if (qp[i][j] == black) {//遇到黑子,进入判断
for (int k = 0; k < 5; k++) {//判断从当前位置向"/"四个是否都为黑子
if (qp[i + k][j - k] != black) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else if (qp[i][j] == white) {//遇到白子,进入判断
for (int k = 0; k < 5; k++) {//判断从当前位置向"/"四个是否都为白子
if (qp[i + k][j - k] != white) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else {
continue;
}
}
}
for (int i = 0; i < qp.length - 5; i++) {//先判断"\"
for (int j = qp.length - 1 - 5; j >= 0; j--) {
if (qp[i][j] == black) {//遇到黑子,进入判断
for (int k = 0; k < 5; k++) {//判断从当前位置向"\"四个是否都为黑子
if (qp[i + k][j + k] != black) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else if (qp[i][j] == white) {//遇到黑子,进入判断
for (int k = 0; k < 5; k++) {//判断从当前位置向"\"四个是否都为黑子
if (qp[i + k][j + k] != white) {
break;
} else {
count++;
}
}
if (count == 5) {//计数为5则返回真
count = 0;
return true;
} else {
count = 0;
}
} else {
continue;
}
}
}
return false;
}
}