2021-11-01

2021-11-01,第1张

2021-11-01 java程序——五子棋实现代码

import java.util.Scanner;

public class WuZiQi {

public static void main(String[] args) {

    WuZiQi.init();
    WuZiQi.print();
    WuZiQi.xiaqi();


}

static char[][] qp = new char[15][15];//×atic才可以调用
static char[] num = {'⒈', '⒉', '⒊', '⒋', '⒌', '⒍', '⒎', '⒏', '⒐', '⒑', '⒒', '⒓', '⒔', '⒕', '⒖'};
static boolean flo = true;//flo为true黑棋下,flo为false白棋下
static Scanner sc = new Scanner(System.in);

public static void init() {//初始化
    // 遍历
    for (int i = 0; i < qp.length; i++) {//行
        for (int j = 0; j < qp[i].length; j++) {//列
            qp[i][j] = '十';
            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[i].length; j++) {//列

            System.out.print(qp[i][j]);
        }
        System.out.println();
    }


}

public static void xiaqi() {//下棋
    //一直下,谁先下,交替下
    while (true) {//死循环,一直下
        int count=1;

        if (flo == true) {//下黑棋

            System.out.println("黑棋下子");
            int x = sc.nextInt();
            int y = sc.nextInt();
            if (WuZiQi.weizhi(x, y)) {
                WuZiQi.H(qp,x,y);

                qp[x - 1][y - 1] = '●';
                WuZiQi.shuyinghei(qp);

                WuZiQi.print();
                flo = false;


            } else {
                    System.out.println("您输入的位置有误,请重新输入");
                    continue;
                }



            } else {//下白棋
                System.out.println("白棋下子");
                int x = sc.nextInt();
                int y = sc.nextInt();
                if (WuZiQi.weizhi(x, y)) {

WuZiQi.B(qp,x,y);
qp[x - 1][y - 1] = ‘○’;

                    WuZiQi.shuyingbai(qp);
                    WuZiQi.print();
                    flo = true;

                } else {
                    System.out.println("您输入的位置有误,请重新输入");
                    continue;
                }


            }




    }


    }

    public static boolean weizhi ( int x, int y){
        if (x < 0 || x > 14 || y < 0 || y > 14) {
            return false;//输入不合法
        }
        if (qp[x - 1][y - 1] != '十') {
            return false;
        }
        return true; //输入合法
    }
    public static void shuyinghei(char[][] qp) {
    for(int i=0;i 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4970995.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存