将Integer或String放在某个坐标的2D数组中?

时间:2013-05-10 19:24:10

标签: c++ arrays char int 2d

尝试将整数或字符串放入2D char数组中,但我没有运气。尝试将整数转换为字符串,然后将其放在数组中,但甚至不确定是否有效。作为一个相对较新的程序员,我想不出任何其他方法将整数插入到数组中,所以任何帮助都会很棒!这是下面的2D数组的一部分,我想在' ScoreInt '中放置一个整数,如果有任何帮助,我可以在类中设置变量。

Level::Level(Snake *s, Item *i)

/// TITLE SECTION

for(int y=1; y<8 ; y++)
{
    map[y][0]=' ';

    for(int x=1; x<70 ; x++)
    {
        ///Letter 'S'

        map[y][x]=' ';
        map[2][19]= 177;
        map[2][20]= 176;
        map[2][21]= 176;
        map[2][22]= 176;
        map[2][23]= 177;
        map[3][19]= 176;
        map[4][19]= 178;
        map[4][20]= 176;
        map[4][21]= 176;
        map[4][22]= 178;
        map[4][23]= 177;
        map[5][23]= 176;
        map[6][23]= 176;
        map[6][22]= 177;
        map[6][21]= 177;
        map[6][20]= 176;
        map[6][19]= 178;

        ///Letter 'N'

        map[2][26]= 178;
        map[3][26]= 178;
        map[4][26]= 176;
        map[5][26]= 177;
        map[6][26]= 176;
        map[2][27]= 176;
        map[3][27]= 177;
        map[4][28]= 176;
        map[5][29]= 176;
        map[6][29]= 177;
        map[2][30]= 176;
        map[3][30]= 176;
        map[4][30]= 178;
        map[5][30]= 176;
        map[6][30]= 177;

        ///Letter 'A'

        map[2][33]= 178;
        map[2][34]= 176;
        map[2][35]= 178;
        map[2][36]= 176;
        map[2][37]= 177;
        map[3][37]= 176;
        map[4][37]= 176;
        map[5][37]= 176;
        map[6][37]= 178;
        map[3][33]= 176;
        map[4][33]= 177;
        map[5][33]= 176;
        map[6][33]= 177;
        map[4][34]= 176;
        map[4][35]= 177;
        map[4][36]= 177;

        ///Letter 'K'

        map[2][40]= 176;
        map[3][40]= 177;
        map[4][40]= 176;
        map[5][40]= 178;
        map[6][40]= 176;
        map[4][41]= 176;
        map[3][42]= 178;
        map[2][43]= 177;
        map[5][42]= 178;
        map[6][43]= 177;

        ///Letter 'E'

        map[2][46]= 177;
        map[2][47]= 176;
        map[2][48]= 176;
        map[2][49]= 176;
        map[2][50]= 177;
        map[3][46]= 177;
        map[4][46]= 176;
        map[4][47]= 176;
        map[4][48]= 177;
        map[5][46]= 176;
        map[6][46]= 176;
        map[6][47]= 178;
        map[6][48]= 178;
        map[6][49]= 176;
        map[6][50]= 178;
    }
}

for(int x=0; x<71; x++)
{
    map[8][0]= 218;
    map[8][x]= 196;
    map[8][70]= 191;
}

/// INFORMATION SECTION (SCORE, LIVES)

for(int y=9; y<12 ; y++)
{
    map[y][0]= 179;

    for(int x=1; x<70 ; x++)
    {
        map[y][x]=' ';

        map[10][20] = 'S';
        map[10][21] = 'C';
        map[10][22] = 'O';
        map[10][23] = 'R';
        map[10][24] = 'E';
        map[10][25] = ':';

        map[10][27] = ScoreInt;

        map[10][40] = 'L';
        map[10][41] = 'I';
        map[10][42] = 'V';
        map[10][43] = 'E';
        map[10][44] = 'S';
        map[10][45] = ':';
    }

    map[y][70]= 179;
}

for(int x=0; x<71; x++)
{
    map[12][0]= 195;
    map[12][x]= 196;
    map[12][70]= 180;
}

/// GAME SECTION

for(int y=13; y<42 ; y++)
{
    map[y][0]= 179;

    for(int x=1; x<70 ; x++)
    {
        map[y][x]=' ';
    }

    map[y][70]= 179;
}

for(int x=0; x<71; x++)
{
    map[42][0]= 192;
    map[42][x]= 196;
    map[42][70]= 217;
}

///FOOTER

/*for(int y=43; y<44 ; y++)
{
    map[y][0]= "FOOTER";
}*/

refresh_display = false;

lSnake = s;
map[lSnake->getLocation().y][lSnake->getLocation().x]=lSnake->getSnakeAppearanceRight();

lItem = i;
map[lItem->getLocation().y][lItem->getLocation().x]=lItem->getItemAppearance();

}

3 个答案:

答案 0 :(得分:0)

尝试在您拥有的整体上投射char()

答案 1 :(得分:0)

你可以放在char数组中的唯一东西就是char。

char是一个整数类型,它将一个值与一个字符相关联。例如,在:

char n = q;

n实际上有一个数值113(假设您使用ASCII)。

当您添加时,将67作为整数添加到您的char数组(再次假设为ASCII)时,它将显示为C.这是因为char类型将该数值传递给字符。

类型转换适用于单位数整数,例如:

chars[x][y] = (char)myInt;

如果myInt中有多个数字,那将会很混乱。

此外,您将无法将整个int的值存储在char数组中的单个位置,因为int的每个数字都需要1个“slot”。

是否可以执行以下操作:

cout << (everything_up_to_map[10]25) << ScoreInt << (map[10][40]_onwards);

在您使用的输出流中?这将允许您将int与其他所有内容保持一致。

修改

我有个主意。如何使用stringstream,将int放入,从流中提取字符串,然后对于字符串中的每个项目,将其强制转换为char,然后将每个单独的值按顺序放入数组中。我有点忙,但如果可以,我会稍后发布一些代码。

答案 2 :(得分:0)

我会发布一个新的答案,因为这是一个完全不同的方法。以下是使用stringstreams的想法:

#include <sstream>
//Your includes
//& other code
int scoreInt;

//This bit takes your int, and places in a string as a chars, rather than a value
stringstream ss;
ss << scoreInt;
std::string tmpStr = ss.str(); //A temporary string for the next bit,
                               //Now with added int!

char digits[maxDigits] = {' ', ' ', ' ', ' ', ' '}; //Use the max number of 
                                                    //digits you have room for

//Take each digit from the string, put it in digits
//So you don't overfill your array accidentally
//This should automatically truncate the number
for(int i = 0; i < maxDigits; i++)
{
    digits[i] = tmpStr[i];
}

map[1stSlot][ForScore] = digits[0];
map[2ndSlot][ForScore] = digits[1];
//So on, so on.

请记住,我没有对此进行测试,并且需要适应完美的适应性。