首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++棋盘BGI图形

C++棋盘BGI图形
EN

Stack Overflow用户
提问于 2016-01-16 12:19:27
回答 1查看 1.4K关注 0票数 0

我知道这很业余,但我有一项任务是画一张8x8国际象棋桌,旁边是通常的"A“.”12,3“文本。我必须使用2作为循环,但是我被困住了,我只能显示一行8条,下面是我的代码:

代码语言:javascript
复制
#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。任何帮助都是受欢迎的,只要简单就行。*)干杯

EN

回答 1

Stack Overflow用户

发布于 2016-01-16 12:44:12

考虑下面的董事会

代码语言:javascript
复制
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

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

代码语言:javascript
复制
  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

你现在知道怎么做吗?

如果没有,请阅读下面的内容。

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

  • 所有偶数行都先启动“White”单元格,而所有奇数行都先启动“Black”单元格。(就像行号-1是偶数一样,我从0开始。习惯:P)

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

  • 第0行在它旁边有1,但是第0列与它相关联。(我认为这是董事会的本意,但你可以很容易地修复它,如果不是)所以,你现在看到,你需要有某种额外的绘图过程,或者你可以聪明,抵消你的循环2。(为什么2?(想想板的对称性)现在,如果你稍微想想,你可以想出如何处理偏移量为2的循环。如果你不能算出它,你总是可以在循环之外使用额外的绘图函数,毕竟你使用的是图形,你不局限于'\n‘,它是打印顺序:p

样本代码:

代码语言:javascript
复制
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';
}

这是我做的游戏,它有一个和你类似的棋盘。我相信你能从这里算出剩下的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34826856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档