将整数或字符串放置在2D数组的特定坐标上

Placing Integer or String inside of 2D array at certain co-ordinates?

本文关键字:数组 坐标 2D 整数 字符串      更新时间:2023-10-16

试图将整数或字符串放入2D字符数组,但我没有运气。尝试将整数转换为字符串,然后将其放入数组中,但甚至不确定这是否有效。作为一个相对较新的程序员,我想不出任何其他方法来将整数插入到数组中,所以任何帮助将是伟大的!下面是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();

}

尝试将char()转换为整数

唯一可以放入字符数组的是一个字符。

char是整型,将值与字符关联起来。例如:

char n = q;

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

当您将67作为整数添加到char数组(同样假设是ASCII)时,它将显示为c。这是因为char类型将该数值转换为字符。

类型转换将适用于个位数整数,如:

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

但是如果在myInt中有多于一个数字就会很乱。

同样,你也不能将整型数的整个值存储在字符数组的一个位置上,因为整型数的每一个数字都需要一个'slot'。

是否可以这样做:

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

在您正在使用的任何输出流上?这将允许你把整型放在其他所有元素的旁边。

编辑:

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

我将发布一个新的答案,因为这是一个相当不同的方法。下面是使用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.

请记住,我还没有测试过,需要进行调整才能完全适应。