二维数组设计简单五子棋

目录

1、初始化和输出

2、黑白子下棋

3、判断棋子是否越界

4、判断输赢

5、完整代码

6、示例(一部分)仅供参考


1、初始化和输出

 //棋盘的初始化
    public void ErWei() {
        int k = 0;
        for (int i = 0; i < chess.length; i++) {
            for (int j = 0; j < chess[i].length; j++) {
                if (j == chess[i].length - 1) {
                    chess[i][j] = num[k];
                } else {
                    chess[i][j] = line;
                }
            }
            if (i == chess.length - 1) {
                chess[i] = num;
            }
            k++;
        }
    }
   //打印输出
    public void display() {
        for (int i = 0; i < chess.length; i++) {
            for (int j = 0; j < chess[i].length; j++) {
                System.out.print(chess[i][j]);
            }
            System.out.println();
        }
    }

2、黑白子下棋

 //白子下棋
    public void White_Chessman(int x, int y) {
        WuZiQi wu = new WuZiQi();
        chess[x][y] = white;
        wu.display();
    }
    //黑子下棋
    public void Black_Chessman(int x, int y) {
        WuZiQi wu = new WuZiQi();
        chess[x][y] = black;
        wu.display();
    }

3、判断棋子是否越界

//判断棋子是否越界
   public boolean checkPoint(int x,int y){
                 if(x<0||x>13||y<0||y>13){
                     return false;
                 }
                 if(chess[x-1][y-1]!=line){
                     return false;
                 }
                 return true;
    }

4、判断输赢

  //判断输赢
    public static int Judge(char[][] chess) {
        int result = 0;    //返回结果
        //逐行扫描
        for (int i = 0; i < chess.length; i++) {
            int count = 1;
            char value = chess[i][0];
            for (int j = 1; j < 10; j++) {
                if (chess[i][j] == value) {
                    count++;
                }
                if (chess[i][j] != value) {
                    count = 1;
                    value = chess[i][j];
                }
                if (count == 5 && value == black) {    //5个1相连
                    result = 1;
                }
                if (count == 5 && value == white) {    //5个2相连
                    result = 2;
                }
            }
        }
        //逐列扫描
        for(int j=0;j<chess.length;j++) {
            int count = 1;
            char value = chess[j][0];
            for(int i=1;i<10;i++) {
                if(chess[i][j]== value) {
                    count++;
                }
                if(chess[i][j] != value) {
                    count = 1;
                    value = chess[i][j];
                }
                if(count==5 && value== black) {	//5个1相连
                    result = 1;
                }
                if(count==5 && value== white) {	//5个2相连
                    result = 2;
                }
            }
        }
        //斜线扫描
        for(int i=0;i<chess.length-4;i++) {
            int count = 1;
            char value = chess[0][i];
            int x = 0;
            int y = i;
            for(int j=1;j<chess.length-i;j++,x++,y++) {
                if(chess[x+1][y+1]== value) {
                    count++;
                }
                if(chess[x+1][y+1]!= value) {
                    count = 1;
                    value = chess[x+1][y+1];
                }
                if(count==5 && value== black) {	//5个1相连
                    result = 1;
                }
                if(count==5 && value== white) {	//5个2相连
                    result = 2;
                }
            }
        }
        for(int i=1;i<chess.length-4;i++) {
            int count = 1;
            char value = chess[i][0];
            int x = i;
            int y = 0;
            for(int j=1;j<chess.length-i;j++,x++,y++) {
                if(chess[x+1][y+1]==value) {
                    count++;
                }
                if(chess[x+1][y+1]!=value) {
                    count = 1;
                    value = chess[x+1][y+1];
                }
                if(count==5 && value== black) {	//5个1相连
                    result = 1;
                }
                if(count==5 && value== white) {	//5个2相连
                    result = 2;
                }
            }
        }
        return result;
    }

5、完整代码

import java.util.Scanner;

public class WuZiQi {
    static char[] num = {'⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖'};
    static char black = '★';
    static char white = '☆';
    static char line = '十';
    static char[][] chess = new char[15][15];

    public static void main(String[] args) {
        System.out.println("五子棋");
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        WuZiQi wu = new WuZiQi();
        wu.ErWei();
        wu.display();
        int win;
        while (true) {
            if (flag) {
                System.out.println("请黑子下棋:(请输入横纵坐标)");
                int blackh = sc.nextInt();
                int blackz = sc.nextInt();
                if(wu.checkPoint(blackh,blackz)){
                    wu.Black_Chessman(blackh - 1,blackz - 1);
                    if(wu.Judge(chess) == 1){
                        System.out.println("黑棋胜");
                        break;
                    }
                }else{
                    System.out.println("请重新坐标或此处已有棋子!!!");
                    continue;
                }
                flag = false;
            } else {
                System.out.println("请白子下棋:(请输入横纵坐标)");
                int whiteh = sc.nextInt();
                int whitez = sc.nextInt();
               if(wu.checkPoint(whiteh,whitez) ){
                   wu.White_Chessman(whiteh - 1,whitez - 1);
                   if(wu.Judge(chess) == 2){
                       System.out.println("白棋胜");
                       break;
                   }
               }else{
                   System.out.println("请重新坐标或此处已有棋子!!!");
                   continue;
               }
                flag = true;
            }
        }
    }
    //棋盘的初始化
    public void ErWei() {
        int k = 0;
        for (int i = 0; i < chess.length; i++) {
            for (int j = 0; j < chess[i].length; j++) {
                if (j == chess[i].length - 1) {
                    chess[i][j] = num[k];
                } else {
                    chess[i][j] = line;
                }
            }
            if (i == chess.length - 1) {
                chess[i] = num;
            }
            k++;
        }
    }
   //打印输出
    public void display() {
        for (int i = 0; i < chess.length; i++) {
            for (int j = 0; j < chess[i].length; j++) {
                System.out.print(chess[i][j]);
            }
            System.out.println();
        }
    }
    //白子下棋
    public void White_Chessman(int x, int y) {
        WuZiQi wu = new WuZiQi();
        chess[x][y] = white;
        wu.display();
    }
    //黑子下棋
    public void Black_Chessman(int x, int y) {
        WuZiQi wu = new WuZiQi();
        chess[x][y] = black;
        wu.display();
    }
  //判断棋子是否越界
   public boolean checkPoint(int x,int y){
                 if(x<0||x>13||y<0||y>13){
                     return false;
                 }
                 if(chess[x-1][y-1]!=line){
                     return false;
                 }
                 return true;
    }
    //判断输赢
    public static int Judge(char[][] chess) {
        int result = 0;    //返回结果
        //逐行扫描
        for (int i = 0; i < chess.length; i++) {
            int count = 1;
            char value = chess[i][0];
            for (int j = 1; j < 10; j++) {
                if (chess[i][j] == value) {
                    count++;
                }
                if (chess[i][j] != value) {
                    count = 1;
                    value = chess[i][j];
                }
                if (count == 5 && value == black) {    //5个1相连
                    result = 1;
                }
                if (count == 5 && value == white) {    //5个2相连
                    result = 2;
                }
            }
        }
        //逐列扫描
        for(int j=0;j<chess.length;j++) {
            int count = 1;
            char value = chess[j][0];
            for(int i=1;i<10;i++) {
                if(chess[i][j]== value) {
                    count++;
                }
                if(chess[i][j] != value) {
                    count = 1;
                    value = chess[i][j];
                }
                if(count==5 && value== black) {	//5个1相连
                    result = 1;
                }
                if(count==5 && value== white) {	//5个2相连
                    result = 2;
                }
            }
        }
        //斜线扫描
        for(int i=0;i<chess.length-4;i++) {
            int count = 1;
            char value = chess[0][i];
            int x = 0;
            int y = i;
            for(int j=1;j<chess.length-i;j++,x++,y++) {
                if(chess[x+1][y+1]== value) {
                    count++;
                }
                if(chess[x+1][y+1]!= value) {
                    count = 1;
                    value = chess[x+1][y+1];
                }
                if(count==5 && value== black) {	//5个1相连
                    result = 1;
                }
                if(count==5 && value== white) {	//5个2相连
                    result = 2;
                }
            }
        }
        for(int i=1;i<chess.length-4;i++) {
            int count = 1;
            char value = chess[i][0];
            int x = i;
            int y = 0;
            for(int j=1;j<chess.length-i;j++,x++,y++) {
                if(chess[x+1][y+1]==value) {
                    count++;
                }
                if(chess[x+1][y+1]!=value) {
                    count = 1;
                    value = chess[x+1][y+1];
                }
                if(count==5 && value== black) {	//5个1相连
                    result = 1;
                }
                if(count==5 && value== white) {	//5个2相连
                    result = 2;
                }
            }
        }
        return result;
    }
}


6、示例(一部分)仅供参考

 

 

 循环往复

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值