代码实现
public class Demo4 {
public static class WZQ {
static Scanner K=new Scanner(System.in);
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];
static String[] num = new String[]{"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
static int z = 5;//可以改变五子棋获胜的连珠数
// static int sum=0;//判断是否为5子连珠的依据,judge方法里会用到
public static void main(String[] args) {
System.out.println("欢迎来到五子棋");
//打印棋盘
System.out.println("请选择:>");
System.out.println("********************");
System.out.println("******1.开始游戏******");
System.out.println("******2.结束游戏******");
System.out.println("********************");
int s=K.nextInt();
//检测是否进入游戏
while(true){
if(s==1){
init();
start();
}
else if(s==2){
break;
}
else{
System.out.println("请重新输入:>");
}
}
}
public static void check() {//打印棋盘
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 init() {//初始化棋盘
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j < qp.length; j++) {
qp[i][j] = line;//初始化棋盘主题
if (j == qp.length - 1) {
qp[i][j] = num[i];//初始化棋盘内的行列数
}
}
if (i == qp.length - 1) {
qp[i] = num;
}
}
check();//打印棋盘
}//初始化棋盘
public static void start() {
boolean flag = true;
while (true) {
//黑子下棋阶段
Scanner s = new Scanner(System.in);
System.out.println("请输入位置\n[a,b](1~15)");
int m = s.nextInt() - 1;
int n = s.nextInt() - 1;
//判断是否越界
boolean rule=qp[m][n] != black && qp[m][n] != white&&m>=0&&m< qp.length&&n>=0&&n< qp.length;
if (rule) {//判断该点是否已有落子
if (flag) {
qp[m][n] = black;
flag = !flag;//黑子落子,flag改变,下一次循环进入白子下棋阶段
check();
if(win(n,m,black)){
System.out.println("游戏结束,黑子胜利");
break;
}
} else {
qp[m][n] = white;
flag = !flag;//白子落子,flag改变,下一次循环进入黑子下棋阶段
check();
if(win(n,m,white)){
System.out.println("游戏结束,白子胜利");
break;
}
}
}
else {//若该点已有落子,则重新输入(flag仍为true,while循环依旧进入黑子下棋阶段)
System.out.println("非法坐标,请重新输入:");
}
}
}//游戏主体
public static boolean win(int x,int y,String chess_pieces){
int spsum=1;//水平
int czsum=1;//垂直
int zxsum=1;//左下
int yxsum=1;//右下
//判断横向
for(int xx=x-1;xx>=0;xx--) {//判断横左
if(qp[y][xx].equals(chess_pieces)) {
spsum++;
}
else{
break;
}
}//判断横左
if(spsum>=z){//判断是否已经胜利
return true;
}
else{//判断横右
for(int xx=x+1;xx< qp.length-1;xx++){
if(qp[y][xx].equals(chess_pieces)) {
spsum++;
}
else{
break;
}
if(spsum>=z){
return true;
}
}
}//判断横右
//判断纵向
for(int yy=y-1;yy>=0;yy--) {//判断纵上
if(qp[yy][x].equals(chess_pieces)) {
czsum++;
}
else{
break;
}
}//判断纵上
if(czsum>=z){
return true;
}
else{//判断纵下
for(int yy=y+1;yy< qp.length-1;yy++){
if(qp[yy][x].equals(chess_pieces)) {
czsum++;
}
else{
break;
}
if(czsum>=z){
return true;
}
}
}//判断纵下
//判断左下
for(int xy1=x-1,xy2=y+1;xy1>=0&&xy2<qp.length-1;xy1--,xy2++) {//判断左下
if(qp[xy2][xy1].equals(chess_pieces)) {
zxsum++;
}
else{
break;
}
}//判断左下
if(zxsum>=z){
return true;
}
else{//判断右上
for(int xy1=x+1,xy2=y-1;xy1< qp.length-1&&xy2>=0;xy1++,xy2--){
if(qp[xy2][xy1].equals(chess_pieces)) {
zxsum++;
}
else{
break;
}
if(zxsum>=z){
return true;
}
}
}//判断右上
//判断右下
for(int yx1=x+1,yx2=y+1;yx1< qp.length-1&&yx2<qp.length-1;yx1++,yx2++) {//判断右下
if(qp[yx2][yx1].equals(chess_pieces)) {
yxsum++;
}
else{
break;
}
}//判断右下
if(yxsum>=z){
return true;
}
else{//判断左上
for(int yx1=x-1,yx2=y-1;yx1>=0&&yx2>=0;yx1--,yx2--){
if(qp[yx2][yx1].equals(chess_pieces)) {
yxsum++;
}
else{
break;
}
if(yxsum>=z){
return true;
}
}
}//判断左上
return false;
}//判断胜利
}
}
效果展示
![](https://img-blog.csdnimg.cn/53bb203627d94a23987f2fc190ec106b.png)