高分悬赏:Java语言实现象棋棋盘的输出,要求在控制台的环境下完成

高分悬赏:Java语言实现象棋棋盘的输出,要求在控制台的环境下完成

控制台呈现不够形象,实现功能也不太方便,这里有一个五子棋棋盘的绘制方法,可以参考:

public class FiveChessGame extends JFrame implements MouseListener, Runnable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String args[]) {
        FiveChessGame ff = new FiveChessGame();
        ff.setVisible(true);
    }

    int height = Toolkit.getDefaultToolkit().getScreenSize().height;
    int width = Toolkit.getDefaultToolkit().getScreenSize().width;

    BufferedImage bgImage = null;// 缓存图片
    int x = 0;
    int y = 0;

    int allChess[][] = new int[19][19];// 设置棋盘大小
    boolean isBlack = true;
    boolean canPlay = true;

    String message = "黑方先行";
    int maxTime = 0;
    Thread t = new Thread(this);// 创建一个线程
    int blackTime = 0;
    int whiteTime = 0;
    String blackMessage = "无限制";
    String whiteMessage = "无限制";

    public FiveChessGame() {
        this.setTitle("五子棋");
        this.setSize(600, 600);// 设置窗体大小
        this.setResizable(true);// 窗体是否可改变大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 单击窗口的关闭按钮
        this.setLocation((width - 500) / 2, (height - 500) / 2);// 离显示屏上下,左右像素
        this.addMouseListener(this);// 处理鼠标事件
        this.setVisible(true); // 窗体可视

        t.start();// 开始线程
        this.repaint();

        try {
            bgImage = ImageIO.read(new File("src/game/bgImage.jpg"));
        } catch (IOException e) {
            e.printStackTrace();// 运行时自动将io流异常初始化,并打印出程序的异常信息
        }
    }

    public void paint(Graphics g) {
        BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
        Graphics g2 = bi.createGraphics();

        g2.drawImage(bgImage, 0, 0, this);// 信息参数
        g2.setColor(Color.black);
        g2.setFont(new Font("黑体", Font.BOLD, 25));
        g2.drawString("游戏信息: " + message, 20, 60);
        g2.setFont(new Font("宋体", Font.BOLD, 20));
        g2.setColor(Color.white);
        g2.fillRect(15, 460, 185, 25);
        g2.fillRect(255, 460, 185, 25);
        g2.setColor(Color.black);
        g2.drawString("黑方时间:" + blackMessage, 20, 480);
        g2.drawString("白方时间:" + whiteMessage, 260, 480);

        g2.setColor(Color.yellow);
        g2.fill3DRect(390, 90, 90, 30, true);// x坐标,y坐标,长,宽,false凹 true凸
        g2.fill3DRect(390, 140, 90, 30, true);
        g2.fill3DRect(390, 190, 90, 30, true);
        g2.fill3DRect(390, 240, 90, 30, true);
        g2.fill3DRect(390, 290, 90, 30, true);
        g2.fill3DRect(390, 340, 90, 30, true);
        g2.fill3DRect(390, 395, 90, 30, true);

        g2.setColor(Color.red);
        g2.drawString("开始游戏", 394, 113);// 框体中的内容,x坐标。y坐标
        g2.drawString("游戏设置", 394, 163);
        g2.drawString("游戏说明", 394, 213);
        g2.drawString("暂停", 412, 263);
        g2.drawString("继续", 412, 313);
        g2.drawString("认输", 412, 363);
        g2.drawString("退出", 412, 418);

        g2.setColor(Color.black);

        //绘制棋盘,19 条横线和 19 条纵线
        for (int i = 0; i < 19; i++) {
            g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
            g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
        }

        //绘制棋盘上 8 个黑点
        g2.fillOval(66, 126, 8, 8);
        g2.fillOval(306, 126, 8, 8);
        g2.fillOval(306, 366, 8, 8);
        g2.fillOval(66, 366, 8, 8);
        g2.fillOval(306, 246, 8, 8);
        g2.fillOval(186, 126, 8, 8);
        g2.fillOval(66, 246, 8, 8);
        g2.fillOval(186, 366, 8, 8);
        g2.fillOval(186, 246, 8, 8);// 设置棋盘上的九个星

        for (int i = 0; i < 19; i++) {
            for (int j = 0; j < 19; j++) {
                if (allChess[i][j] == 1) {
                    int tempX = i * 20 + 10;
                    int tempY = j * 20 + 70;
                    g2.fillOval(tempX - 8, tempY - 8, 16, 16);
                }
                if (allChess[i][j] == 2) {
                    int tempX = i * 20 + 10;
                    int tempY = j * 20 + 70;
                    g2.setColor(Color.white);
                    g2.fillOval(tempX - 8, tempY - 8, 16, 16);
                    g2.setColor(Color.black);
                    g2.drawOval(tempX - 8, tempY - 8, 16, 16);
                }
            }
        }
        g.drawImage(bi, 0, 0, this);
    }

在控制台输出就是用二维数组模拟点阵,在屏幕相应的位置上打印点就可以了
比如打印一个方格

char[][] grid = {
    ". . . . . .".toCharArray(),
    ".         .".toCharArray(),
    ".         .".toCharArray(),
    ".         .".toCharArray(),
    ". . . . . .".toCharArray()
};
for (int i=0; i<grid.length; i++) {
    for (int j=0; j<grid[i].length; j++) {
        System.out.printf("%c", grid[i][j]);
    }
    System.out.println();
}

按照这个思路把整个棋盘画出来就可以了,这里就不画了。