五子棋
思路:
1.初始化棋盘
2.打印棋盘
3.黑白棋交替落子
判断落子位置是否合法(是否越界,是否已有棋子落入)
合法:判断输赢(赢-游戏结束,输-继续交替落子)
不合法:重新输入,继续判断是否合法
import java.util.Scanner;
public class WZQ {
static String[][] qp = new String[15][15]; //{{null,null},{null,null}}
static String white = "☆";
static String black = "★";
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
//启动游戏
public static void runWzq() {
//初始化棋盘
WZQ.init();
//打印棋盘
WZQ.print();
WZQ.play();
}
//初始化棋盘
public static void init() {
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp[i].length; j++) {
if (i == qp.length - 1) {
qp[i][j] = num[j];
} else if (j == qp[i].length - 1) {
qp[i][j] = num[i];
} else {
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() {
boolean flag = true;
Scanner in = new Scanner(System.in);
while (true) {
if (flag) {
System.out.println("请黑棋落子:");
int y = in.nextInt() - 1;
int x = in.nextInt() - 1;
//判断棋子位置是否合法
boolean b = WZQ.check(y, x);
if (b) {
qp[y][x] = black;
WZQ.print();
//判断输赢
boolean win = WZQ.iswin(y, x, black);
if (win) {
// WZQ.print();
System.out.println("黑棋胜利");
break;
}
flag = false;
}
} else {
System.out.println("请白棋落子:");
int y = in.nextInt() - 1;
int x = in.nextInt() - 1;
boolean b = WZQ.check(y, x);
if (b) {
qp[y][x] = white;
WZQ.print();
boolean win = WZQ.iswin(y, x, white);
if (win) {
//WZQ.print();
System.out.println("白棋胜利");
break;
}
flag = true;
}
}
}
}
public static boolean check(int y, int x) {
if (y < 0 || y >= qp.length - 1 || x < 0 || x >= qp[0].length - 1) {
System.out.println("输入越界,请重新输入!");
return false;
} else if (!qp[y][x].equals(line)) {
System.out.println("该位置已有棋子存在,请重新输入!");
return false;
} else {
return true;
}
}
public static boolean iswin(int y, int x, String qz) {
//水平方向
int spsum = 1;
//左边
for (int i = x - 1; i >= 0; i--) {
if (qp[y][i] == qz) {
spsum++;
} else {
break;
}
}
//右边
for (int i = x + 1; i < qp[0].length - 1; i++) {
if (qp[y][i] == qz) {
spsum++;
} else {
break;
}
}
if (spsum >= 5) {
return true;
}
//竖直方向
int szsum = 1;
//向上
for (int i = y - 1; i >= 0; i--) {
if (qp[i][x] == qz) {
szsum++;
} else {
break;
}
}
//向下
for (int i = y + 1; i < qp[0].length - 1; i++) {
if (qp[i][x] == qz) {
szsum++;
} else {
break;
}
}
if (szsum >= 5) {
return true;
}
//左上,右下
int zssum = 1;
for (int i = y - 1, j = x - 1; i >= 0 && j >= 0; i--, j--) {
if (qp[i][j] == qz) {
zssum++;
} else {
break;
}
if (zssum == 5) {
return true;
}
}
for (int i = y + 1, j = x + 1; i < qp.length && j < qp[0].length - 1; i++, j++) {
if (qp[i][j] == qz) {
zssum++;
} else {
break;
}
if (zssum == 5) {
return true;
}
}
//右上,左下
int yssum = 1;
for (int i = y - 1, j = x + 1; i >= 0 && j < qp[0].length - 1; i--, j++) {
if (qp[i][j] == qz) {
yssum++;
} else {
break;
}
if (yssum == 5) {
return true;
}
}
for (int i = y + 1, j = x - 1; i < qp.length && j >= 0; i++, j--) {
if (qp[i][j] == qz) {
yssum++;
} else {
break;
}
if (yssum == 5) {
return true;
}
}
return false;
}
}
public class RunWZQ {
public static void main(String[] args) {
System.out.println("--------------------------");
System.out.println(" 五子棋 ");
System.out.println("游戏规则:黑棋先下,落子先行后列");
System.out.println("--------------------------");
WZQ.runWzq();
}
}