无法使用 ascii 显示带有数组的棋盘 table

Can't display a checkerboard with array using ascii table

我是 C++ 的新手。我有这个作业,我无法用 for 循环显示我的棋盘!电路板必须如下图所示 每个 case case 8 个字符,4 列 2 行:

#include "iostream"
#include "string"
#include <conio.h>
#include <time.h>

const int up = 80, down = 72, left = 77, right = 75, HD = 73, HG = 71, BG = 79, BD = 81;
const int NB_COL = 12, NB_LIG = 8;

using namespace std;

struct Mat_Pos_s 
{
    int lig;
    int Col;
};


// Matrice composant le damier du jeu :
// 0==Normal(white), 1==$$$$ hidden, 2==$$$$ visible, 3==grey, 4== black wall

int Mat[NB_LIG][NB_COL] =
{
    { 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 1 },
    { 0, 0, 4, 4, 0, 0, 0, 0, 0, 4, 0, 4 },
    { 0, 0, 4, 1, 4, 0, 0, 0, 0, 0, 4, 1 },
    { 0, 0, 4, 1, 4, 0, 0, 4, 4, 0, 4, 1 },
    { 1, 0, 4, 4, 4, 1, 4, 0, 4, 0, 4, 0 },
    { 1, 0, 1, 1, 0, 1, 4, 1, 4, 0, 4, 0 },
    { 1, 0, 1, 0, 0, 0, 4, 4, 4, 0, 4, 0 },
    { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

void main(void)
{
    Mat_Pos_s Depart; 
    Mat_Pos_s Arrive; 

    char piece = 178, q = 176;
    char a = 201, b = 200, c = 203, d = 202, e = 187, f = 188 ;

    gotoxy(10, 2);
    for (int y = 0; y < NB_LIG; y++)
    {
        cout << endl ;

        for (int x = 0; x < NB_COL; x++)
        {

            if (Mat[y][x] == 0)

            {
                cvm_SetColor(BLEU);

                cout << piece << piece << piece << piece << " " <<endl;

                cout << piece << piece << piece << piece << " ";
            }
            else if (Mat[y][x] == 1)
            {
                cvm_SetColor(BLEU); 
                cout << piece << piece << piece << piece << " " <<endl ;
                cout << piece << piece << piece << piece << " ";
            }
            else
            {
                cvm_SetColor(NOIR);

                cout << piece << piece << piece << piece << " " <<endl;
                cout << piece << piece << piece << piece << " ";
            }
        }

        cout << endl ;
    }
    _getch()
}

如果每个单元格的宽度为四个字符,高度为两行,那么您将不得不做一些比以下更棘手的事情:

cout << piece << piece << piece << piece << " " <<endl;
cout << piece << piece << piece << piece << " ";

对于每个单元格,因为您需要每个 并且一行只有半个单元格高。

因此你的循环需要类似于(伪代码):

for row = 1 to maxrow:
    for height = 1 to 2:
        for col = 1 to maxcol:
            set color
            output four copies of piece, no newline.
        output newline

这样,您可以分两次完成每一行,每行一次,就像这样(对于使用小写字母后跟大写字母进行排序的 3x2 矩阵):

+----+----+----+
|abcd|efgh|ijkl|
|mnop|qrst|uvwx|
+----+----+----+
|yzAB|CDEF|GHIJ|
|KLMN|OPQR|STUV|
+----+----+----+

下面的代码显示了一些更类似于您需要的内容,在每行两行高的行之间有一个空行以强调传播:

#include <iostream>
using namespace std;

#define NB_ROW 8
#define NB_COL 12
int Mat[NB_ROW][NB_COL] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 1 },
    { 0, 0, 4, 4, 0, 0, 0, 0, 0, 4, 0, 4 },
    { 0, 0, 4, 1, 4, 0, 0, 0, 0, 0, 4, 1 },
    { 0, 0, 4, 1, 4, 0, 0, 4, 4, 0, 4, 1 },
    { 1, 0, 4, 4, 4, 1, 4, 0, 4, 0, 4, 0 },
    { 1, 0, 1, 1, 0, 1, 4, 1, 4, 0, 4, 0 },
    { 1, 0, 1, 0, 0, 0, 4, 4, 4, 0, 4, 0 },
    { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

int main(){
    for (int row = 0; row < NB_ROW; row++) {
        for (int line = 0; line < 2; line++) {
            for (int col = 0; col < NB_COL; col++) {
                switch (Mat[row][col]) {
                    case 0: cout << "WHIT "; break;
                    case 1: cout << "HIDN "; break;
                    case 2: cout << "VSBL "; break;
                    case 3: cout << "GREY "; break;
                    case 4: cout << "BLAK "; break;
                }
            }
            cout << '\n';
        }
        cout << '\n'; // This one's for the spread.
    }
    return 0;
}

输出为:

WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT BLAK WHIT WHIT HIDN 
WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT BLAK WHIT WHIT HIDN 

WHIT WHIT BLAK BLAK WHIT WHIT WHIT WHIT WHIT BLAK WHIT BLAK 
WHIT WHIT BLAK BLAK WHIT WHIT WHIT WHIT WHIT BLAK WHIT BLAK 

WHIT WHIT BLAK HIDN BLAK WHIT WHIT WHIT WHIT WHIT BLAK HIDN 
WHIT WHIT BLAK HIDN BLAK WHIT WHIT WHIT WHIT WHIT BLAK HIDN 

WHIT WHIT BLAK HIDN BLAK WHIT WHIT BLAK BLAK WHIT BLAK HIDN 
WHIT WHIT BLAK HIDN BLAK WHIT WHIT BLAK BLAK WHIT BLAK HIDN 

HIDN WHIT BLAK BLAK BLAK HIDN BLAK WHIT BLAK WHIT BLAK WHIT 
HIDN WHIT BLAK BLAK BLAK HIDN BLAK WHIT BLAK WHIT BLAK WHIT 

HIDN WHIT HIDN HIDN WHIT HIDN BLAK HIDN BLAK WHIT BLAK WHIT 
HIDN WHIT HIDN HIDN WHIT HIDN BLAK HIDN BLAK WHIT BLAK WHIT 

HIDN WHIT HIDN WHIT WHIT WHIT BLAK BLAK BLAK WHIT BLAK WHIT 
HIDN WHIT HIDN WHIT WHIT WHIT BLAK BLAK BLAK WHIT BLAK WHIT 

HIDN HIDN WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT 
HIDN HIDN WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT WHIT