好吧,正如标题所说,我正在画一个游戏板。好吧,董事会本身,我做了,它看起来像他的:
4 |
3 |
2 |
1 |
0 |
- - - - -
0 1 2 3 4
但这不是问题所在。我觉得很难的是我想在黑板上画星形的图标,坐标是这样的数组
posX {2,4,2,1}
posY {2,3,4,4}
在这种情况下,我应该在坐标(2,2),(4,3),(2,4),(1,4)中画*等等。有什么想法吗?
最佳答案
这说明了我在评论中的意思,也许会有所帮助。它在打印之前准备一个表示游戏板的字符串数组。
#include <stdio.h>
#include <stdlib.h>
#define BOARDX 5
#define BOARDY 5
#define BOARDW (BOARDX*2) // length of text line
char board [BOARDY][BOARDW+1]; // allow for string terminator
void print_board(void)
{
int y, x;
for(y=BOARDY-1; y>=0; y--) {
printf("%-2d|%s\n", y, board[y]);
}
printf(" ");
for(x=0; x<BOARDX; x++)
printf(" -");
printf("\n");
printf(" ");
for(x=0; x<BOARDX; x++)
printf("%2d", x);
printf("\n");
}
void empty_board(void)
{
int y, x;
for(y=0; y<BOARDY; y++) {
for(x=0; x<BOARDW; x++) {
board[y][x] = ' ';
}
board[y][x] = '\0';
}
}
void poke_board(int x, int y, char c)
{
if (y >= 0 && y < BOARDY && x >= 0 && x < BOARDX)
board[y][x*2+1] = c; // correctly spaced
}
int main(void)
{
int posX[]= {2,4,2,1};
int posY[]= {2,3,4,4};
int len = sizeof(posX) / sizeof(posX[0]);
int n;
empty_board();
for(n=0; n<len; n++) {
poke_board(posX[n], posY[n], '*');
}
print_board();
return 0;
}
程序输出:
4 | * *
3 | *
2 | *
1 |
0 |
- - - - -
0 1 2 3 4