栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-11-01

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390308.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号