目录
内容
题目
解题
代码
实现
内容
题目
五子棋
使用二维数组,实现五子棋功能.
1.使用二维数组存储五子棋棋盘
如下图

2.在控制台通过Scanner输入黑白棋坐标(例如:1,2 2,1格式 表示二维数组坐标),使用实心五角星和空心五角星表示黑白棋子.
如下图:

输入后重新输出棋盘如下图:

白棋输入后如下图

黑白棋依次重复输入下棋
3.判断棋子是否越界,棋子是否重复,判断输赢
java语言实现思路有
解题
图解:

文字描述:
   五子棋
   1、创建一个棋盘
   提供一个启动五子棋游戏的方法
        初始化棋盘方法
        打印棋盘方法
  2、开始下棋,黑白棋交替
      请黑子下棋
      请白子下棋
3、判断坐标是否合法,是否重复
4、判断输赢
代码
代码实现使用的是思路二,思路一只提供思想,不进行代码实现。
提供部分
    String white = "☆";
    String black = "★";
    String[][] qp = new String[15][15];
    String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
    String line = "十";package day1;
import java.util.Scanner;
 
public class wuziqi {
    static String white = "☆";
    static String black = "★";
    static String[][] qp = new String[15][15];
    static String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
    static String line = "十";
    static boolean flag = true;//true--表示黑子下棋;false--表示白子下棋。
    static Scanner scanner = new Scanner(System.in);
    //启动五子棋游戏
    public static void startGame(){
        wuziqi.init();
        wuziqi.print();
        wuziqi.play();
    }
    //初始化棋盘
    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==14){
                    qp[i][j]=num[i];
                }
                if(i==14){
                    qp[i][j]=num[j];
                }
            }
        }
    }
    //打印棋盘
    public static void print(){
        for(int i=0;i< qp.length;i++){//行打印
            for(int j=0;j< qp.length;j++){//列打印
                System.out.print(qp[i][j]+"\t");
            }
            System.out.println();//换行
        }
    }
    //开始下棋
    public static void play(){
        System.out.println("快来一把紧张刺激的五子棋");
        while(true){
            if(flag) {
                System.out.println("请黑子下棋");
                System.out.println("输入行:");
                int row = scanner.nextInt() - 1;//行 二维数组索引
                System.out.println("输入列:");
                int column = scanner.nextInt() - 1;//列 一维数组索引
                //判断是否越界(合法),判断是否重复
                boolean res = wuziqi.judgment(row,column);
                if(res) {
                    qp[row][column]=black;//真正的落子
                    wuziqi.print();//打印
                    //判断输赢 赢了终止循环
                    boolean winer = wuziqi.winer(row,column,black);
                    if(winer){
                        System.out.println("黑棋胜利");
                        break;
                    }
                    flag = false;
                }else{
                    System.out.println("坐标越界或重复,请重新输入:");
                }
            }else{
                System.out.println("请白子下棋");
                System.out.println("行:");
                int row = scanner.nextInt() - 1;//行 二维数组索引
                System.out.println("列:");
                int column = scanner.nextInt() - 1;//列 一维数组索引
                //判断是否越界(合法),判断是否重复
                boolean res = wuziqi.judgment(row,column);
                if(res) {
                    qp[row][column]=white;//真正的落子
                    wuziqi.print();//打印
                    //判断输赢 赢了终止循环
                    boolean winer = wuziqi.winer(row,column,white);
                    if(winer){
                        System.out.println("白棋胜利");
                        break;
                    }
                    flag = true;
                }else{
                    System.out.println("坐标越界或重复,请重新输入:");
                }
            }
        }
    }
    //判断是否重复,是否越界(合法)
    public static boolean judgment(int row,int column){
        //判断是否越界
        if(row<0 || row>15 || column<0 || column>15){//满足条件,返回false,提醒用户输入错误,请重新输入
            return false;
        }
        //判断是否重复
        if(qp[row][column]!=line){//满足条件,返回false,提醒用户输入错误,请重新输入
            return false;
        }
        return true;//不满足判断重复和越界的条件,返回true,请下一位选手下棋
    }
    //判断输赢
    public static boolean winer(int row,int column,String qz){
        //思路一:每下一个棋子,就从二维数组的第一个位置开始向向下,协向开始查找,直到有满足5个相连的终止
        //思路二:以落下棋子为中心进行检索   水平,垂直,斜向(2种)
        //水平判断输赢
        int spsum = 1;
        //水平向左找
        for(int leftColumn = column-1;leftColumn>=0;leftColumn--){
            if(qp[row][leftColumn]==qz){
                spsum++;
            }else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        //水平向右找
        for(int rightColumn = column+1;rightColumn< qp.length;rightColumn++){
            if(qp[row][rightColumn]==qz){
                spsum++;
            }else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        //垂直判断输赢
        //垂直向上找
        for(int upRow = row-1;upRow>=0;upRow--){
            if(qp[upRow][column]==qz){
                spsum++;
            }else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        //垂直向下找
        for(int downRow = row+1;downRow<qp.length;downRow++){
            if(qp[downRow][column]==qz){
                spsum++;
            }else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        //斜向判断输赢
        //左上至右下斜向
        //斜向左上找
        for(int leftupRow = row-1,leftupColumn = column-1;leftupRow>=0 && leftupColumn>=0;leftupRow--,leftupColumn--){
            if (qp[leftupRow][leftupColumn]==qz) {
                spsum++;
            }
            else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        //斜向右下找
        for(int rightdownRow = row+1,rightdownColumn = column+1;rightdownRow< qp.length && rightdownColumn< qp[0].length;rightdownRow++,rightdownColumn++){
            if (qp[rightdownRow][rightdownColumn]==qz) {
                spsum++;
            }
            else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        //右上至左下斜向
        //斜向右上找
        for(int rightupRow = row-1,rightupColumn = column+1;rightupRow>=0 && rightupColumn< qp[0].length;rightupRow--,rightupColumn++){
            if (qp[rightupRow][rightupColumn]==qz) {
                spsum++;
            }
            else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        //斜向左下找
        for(int leftdownRow = row+1,leftdowmColumn = column-1;leftdownRow< qp.length && leftdowmColumn>=0;leftdownRow++,leftdowmColumn--){
            if (qp[leftdownRow][leftdowmColumn]==qz) {
                spsum++;
            }
            else{
                break;
            }
        }
        if(spsum==5){
            return true;
        }
        return false;//true-胜利  false-没有胜利
    }
}
package day1;
public class startWuziqi {
    public static void main(String[] args) {
        wuziqi.startGame();
    }
}
实现
















![[LWC] Components Communication](https://img-blog.csdnimg.cn/direct/3975290d87c448639f50f870db02817a.png)












