gpt4 book ai didi

C++象棋 table 华大图形

转载 作者:行者123 更新时间:2023-11-28 05:52:58 30 4
gpt4 key购买 nike

我知道这很业余,但我有一个任务是绘制一个 8x8 的国际象棋 table ,旁边有通常的“A B 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。欢迎任何形式的帮助,保持简单。 :)干杯

最佳答案

考虑下面的板

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)

如果你能做到这一点,就很容易看出你的循环应该如何设置它们的条件。 (行 % 2 和列 % 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';
}

这是我制作的一款游戏,其棋盘与您的相似。我相信您可以从这里找出其余部分。

关于C++象棋 table 华大图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34826856/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com