0

我知道这很业余,但我的任务是绘制一个 8x8 国际象棋桌,旁边有通常的“AB C...”“1 2 3”文字。我必须使用 2 个 for 循环,但我很卡住,我只能显示一行 8 条,这是我的代码:

#include<iostream>
#include<cstdio>
using namespace std;
#include<graphics.h>

int main()
{
    int i,j=0;
    int upperline=50;
    int widthline=50;
    double godown=500/8;
    double goright=700/8;

    initwindow(800,600,"Chessboard");
    setbkcolor(LIGHTGRAY);
    cleardevice();
    for(i=0;i<8;i++)
        {
            for(j=0;j<8;j++)
            {
                if(i % 2==0) setfillstyle(SOLID_FILL,BLACK);
                else setfillstyle(SOLID_FILL,WHITE);
                bar(widthline,upperline,widthline+goright,upperline+godown);
                outtextxy(widthline+goright/2-5,upperline/2,"A");
                outtextxy(widthline+goright/2-5,600-upperline/2,"B");
            }
            widthline=widthline+goright;
        }
    getch();
    closegraph();
}

顺便说一句,我正在使用 CodeBlocks。欢迎任何形式的帮助,请保持简单。:) 干杯

4

1 回答 1

1

考虑以下板

W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W

这就是你想要的,但现在考虑这个:

  H G F E D C B A
 -----------------
1|W B W B W B W B|1
2|B W B W B W B W|2
3|W B W B W B W B|3
4|B W B W B W B W|4
5|W B W B W B W B|5
6|B W B W B W B W|6
7|W B W B W B W B|7
8|B W B W B W B W|8
 -----------------
  H G F E D C B A

您现在知道该怎么做吗?

如果没有,请阅读下文。

为此,您需要两个循环,一个用于行,一个用于列。你说对了那部分。接下来,您需要进行以下观察:

  • 所有偶数行都有“白色”单元格首先开始,而所有奇数行都有“黑色”单元格首先开始。(即使在行号 - 1 是偶数,我从 0 开始。习惯:P)

如果你能做到这一点,就很容易看出你的循环应该如何设置它们的条件。(row % 2 和 col % 2 应该给你一个想法)下一个很容易看到,但我还是会说明它:

  • 第 0 行旁边有 1,但第 0 列有 H 与之关联。(我认为这就是电路板的本意,但如果没有,您可以轻松修复它)所以,您现在看到您需要进行某种额外的绘图过程,或者您可以很聪明并将循环偏移 2 . (为什么是 2?想想板的对称性)现在,如果你想一想,你可以弄清楚如何处理偏移量为 2 的循环。如果你无法弄清楚,你总是可以使用额外的在循环之外绘制函数,毕竟你使用图形,你不仅限于 '\n' 它是打印顺序:p

示例代码:

void Grid::display_top() const {
    uint widthmax = width << 1;
    cout << "  ";
    for (uint i = 0; i < 2; ++i) {
        for (uint j = 0; j < widthmax; ++j) {
            if (!i) {
                if (!(j % 2))
                    cout << ' ';
                else
                    cout << (j >> 1);
            }
            else {
                if (!j)
                    cout << " *-";
                else if (j == widthmax - 1)
                    cout << "-*";
                else
                    cout << "-";
            }
        }
        cout << '\n';
    }
}

void Grid::display_bottom() const {
    uint widthmax = width << 1;
    for (uint i = 0; i < 2; ++i) {
        if (i)
            cout << "  ";
        for (uint j = 0; j < widthmax; ++j) {
            if (i) {
                if (!(j % 2))
                    cout << ' ';
                else
                    cout << (j >> 1);
            }
            else {
                if (!j)
                    cout << " *-";
                else if (j == widthmax - 1)
                    cout << "-*";
                else
                    cout << "-";
            }
        }
        cout << '\n';
    }
}

void Grid::display(const Player& P1, const Player& P2) const {
    cout << '\n';
    display_top();
    uint scorepos = (height >> 1) - 2;
    for (uint i = 0; i < height; ++i) {
        for (uint j = 0; j < width + 4; ++j) {
            if (!j || j == width + 3)
                cout << i;
            else if (j == 1 || j == width + 2)
                cout << '|';
            else {
                cout << " ";
                spots[i][j - 2].display();
            }
        }
        cout << '\n';
    }
    display_bottom();
    cout << '\n';
}

这是针对我制作的一款与你的棋盘相似的游戏。我相信你可以从这里找出其余的。

于 2016-01-16T12:44:12.877 回答