目录
1.游戏准则:
黑子白子依次下棋,不能重复,也不能越界,若违规,则重新下棋。
判断输赢:
判断各方向是否五个棋子连在一起,若有则胜,若无,则败。
2.游戏细节:
☆ —— 白棋
★ —— 黑棋
代码如下:
package Day3;
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 = "十";
static boolean flag = true;
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎玩家来到五子棋小游戏");
WZQ.intiQP();
WZQ.outQP();
WZQ.start();
}
public static void start() { //开始下棋
while (true) {
if (flag) {
System.out.println("请黑子下棋");
int a = s.nextInt() - 1;
int b = s.nextInt() - 1;
boolean res = WZQ.check(a, b);
if (res) {
qp[a][b] = black; //落子
flag = false;
WZQ.outQP(); //打印棋盘
boolean win = WZQ.isWin(a, b, black);
if (win) {
System.out.println("黑子获胜!");
break;
}
}else
{
System.out.println("坐标有误,请重新输入");
continue;
}
}
else{
System.out.println("请白子下棋");
int a = s.nextInt() - 1;
int b = s.nextInt() - 1;
boolean res = WZQ.check(a, b);
if(res){
qp[a][b] = white; //落子
flag = true;
WZQ.outQP(); //打印棋盘
boolean win = WZQ.isWin(a, b, white);
if (win) {
System.out.println("白子获胜!");
break;
}
}
else{
System.out.println("坐标有误,请重新输入");
continue;
}
}
}
}
private static boolean isWin(int a, int b, String qz) {
int sum = 1;
//水平计数器
for (int lb = b - 1; lb >= 0; lb--) { //向当前棋子的左边查找
if (qp[a][lb].equals(qz)) {
sum++;
} else break;
if (sum >= 5) {
return true;
}
}
for (int rb = b + 1; rb < qp.length - 1; rb++) { //向当前棋子的右边查找
if (qp[a][rb].equals(qz)) {
sum++;
} else {
sum = 1;
break;
}
if (sum >= 5) {
return true;
}
}
//竖直计数器
for (int sa = a - 1; sa >= 0; sa--) { //向当前棋子的上边查找
if (qp[sa][b].equals(qz)) {
sum++;
} else break;
if (sum >= 5) {
return true;
}
}
for (int xa = a + 1; xa <= qp.length - 1; xa++) { //向当前棋子的下边查找
if (qp[xa][b].equals(qz)) {
sum++;
} else {
sum = 1;
break;
}
if (sum >= 5) {
return true;
}
}
//斜向计数器
for (int ma = a - 1, mb = b + 1; ma >= 0 && mb <= qp.length - 1; ma--, mb++) { //右上查找
if (qp[ma][mb].equals(qz)) {
sum++;
} else {
break;
}
if (sum >= 5) {
return true;
}
}
for (int ma = a + 1, mb = b - 1; ma <= qp.length - 1 && mb >= 0; ma++, mb--) { //左下查找
if (qp[ma][mb].equals(qz)) {
sum++;
} else {
sum = 1;
break;
}
if (sum >= 5) {
return true;
}
}
for (int na = a + 1, nb = b + 1; na <= qp.length - 1 && nb <= qp.length - 1; na--, nb--) { //右下查找
if (qp[na][nb].equals(qz)) {
sum++;
} else break;
if (sum >= 5) {
return true;
}
}
for (int na = a - 1, nb = b - 1; na >= 0 && nb >= 0; na--, nb--) { //左上查找
if (qp[na][nb].equals(qz)) {
sum++;
} else {
sum=1;
break;
}
if (sum >= 5) {
return true;
}
}
return false;
}
public static boolean check(int a, int b) { //判断越界、 重复
if (!qp[a][b].equals(line)) {
return false;
} else if (a < 0 || a > qp.length - 1 || b < 0 || b > qp.length - 1) {
return false;
} else
return true;
}
public static void intiQP() { //输入棋盘
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
qp[i][j] = line;
/*qp[i][qp.length - 1]= num[i];
qp[qp.length - 1][j] = num[j];*/
if (i == qp.length - 1) {
qp[i][j] = num[j];
}
if (j == qp.length - 1) {
qp[i][j] = num[i];
}
}
}
}
public static void outQP() { //输出(打印)棋盘
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();
}
}
}
运行结果:
直至最后某一方获胜
bug测试:
棋子重复: