簡體   English   中英

將整數或字符串放在2D數組中的特定坐標處?

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

試圖將整數或字符串放入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();

}

嘗試將char()轉換為您擁有的int

可以放入char數組的唯一一件事是char。

char是整數類型,並將值與字符相關。 例如,在:

char n = q;

n實際上具有113的數值(假設您使用ASCII)。

當您添加字符時,將67作為整數(再次假定為ASCII)表示為char數組,它將顯示為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,然后將這些各個值按順序放入數組中。 我在atm有點忙,但是如果可以的話,稍后會發布一些代碼。

我將其發布為新答案,因為這是一種完全不同的方法。 這是使用字符串流的想法:

#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.

請記住,我尚未對此進行測試,因此需要進行調整以使其完美匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM