我知道这很业余,但我有一项任务是画一张8x8国际象棋桌,旁边是通常的"A“.”12,3“文本。我必须使用2作为循环,但是我被困住了,我只能显示一行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。任何帮助都是受欢迎的,只要简单就行。*)干杯
发布于 2016-01-16 12:44:12
考虑下面的董事会
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你现在知道怎么做吗?
如果没有,请阅读下面的内容。
为此您需要两个循环,一个用于行,另一个用于列。你说得对。接下来,您需要进行以下观察:
如果你能做到这一点,很容易看出你的循环应该如何设置它们的条件。(第2行和第2行应该给你一个想法)下一个很简单,但我还是要说明一下:
样本代码:
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';
}这是我做的游戏,它有一个和你类似的棋盘。我相信你能从这里算出剩下的。
https://stackoverflow.com/questions/34826856
复制相似问题