簡體   English   中英

輸出中的 C++ 奇怪數字

[英]C++ weird numbers in Output

所以我嘗試重新創建 Conways Game of Life 並且我得到了很多工作,但是我的輸出中有數字,我不知道它們來自哪里。 我對 c++ 和編程還很陌生,我以前學過 python 和 java,但我從來沒有見過這個,谷歌似乎也沒有理解我的問題,所以我希望你們人類也能理解。

我的輸出如下:1664447571170186994045474010000255652800 /其對這個號碼0000000000 00.11億01.1億0001000000 0000000000 0000000000 0000000000 0000000000 0000000000

任何人都知道他們來自哪里,因為我當然不知道。 有時這些數字甚至會像這樣與棋盤“碰撞”:

0000-1-1-1-116644475711701869940 4547401010000000 791621423110000000 0101000000 0000000000 0000000000000000000

我的 sphagetthi 代碼:

#include <iostream>

#include <chrono>
#include <thread> 


using namespace std;
using namespace std::this_thread;
using namespace std::chrono;

int main(){
    
    int x = 10, y = 10, i, j, c, b;
    int field[y][x] =
    
    {{ 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,1,1,0,0,0,0},
     { 0,0,0,0,1,0,1,0,0,0},
     { 0,0,0,0,1,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0}};
     
    int updateField[y][x];
    
    
    
    
    cout << "start";
    for(b = 0;b<=10; b++){
        cout << "main loop: "<<b<<"\n";
        for(y = 0; y < 10; y++)
        {
            
            for(x = 0; x < 10; x++)
            {
            
            c = 0;
            
            if(y == 0 || y == 9 || x == 0 || x == 9){
                
                
                
            }
            else{
            
                /*v v v lebende zelle v v v*/
                if(field[y][x] == 1){
                    
                    if(field[y-1][x-1] == 1){
                        c++;
                    }
                    if(field[y-1][x] == 1){
                        c++;
                    }
                    if(field[y-1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x] == 1){
                        c++;
                    }
                    if(field[y+1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x+1] == 1){
                        c++;
                    }
                    /*regeln v v v*/
                    if(c < 2 or c > 3){
                        updateField[y][x] = 0;
                    }
                    else
                    {
                        updateField[y][x] = 1;
                    }
                    
                    
                    
                    
                }
                /*^ ^ ^ lebende zelle ^ ^ ^*/
                /*v v v tote zelle v v v*/
                
                else if(field[y][x] == 0){
                    
                    if(field[y-1][x-1] == 1){
                        c++;
                    }
                    if(field[y-1][x] == 1){
                        c++;
                    }
                    if(field[y-1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x-1] == 1){
                        c++;
                    }
                    if(field[y+1][x] == 1){
                        c++;
                    }
                    if(field[y+1][x+1] == 1){
                        c++;
                    }
                    if(field[y][x+1] == 1){
                        c++;
                    }
                    /*regeln v v v*/
                    if(c == 3)
                    {
                        updateField[y][x] = 1; 
                    }
                    else
                    {
                        updateField[y][x] = 0;
                    }
                }
                
            }
            
        }
    }
    cout << "\n";
    for(i = 0; i < 10; i++){
        for(j = 0; j < 10; j++){
            field[i][j] = updateField[i][j];
        }
    }
        
        for(i = 0;i < x; i++){
            for(j = 0; j < y; j++ )
            {
                cout << field[i][j];
                
            }
        cout << "\n";
        }
        sleep_for(nanoseconds(500000000));
    }
    
    
    
}

我很感激任何答案。 提前致謝。

問題是您在構建updateField (組件中的 0 或 9)時忽略了邊框,然后將包含這些邊框的updateField復制到field updateField中的邊界從未設置過,因此它們只包含之前內存中不受程序控制的任何數字。

您應該將updateField的邊界設置為零,或者不要將邊界復制到從 1 到排除 9 而不是從 0 到 10 迭代的field

雖然我希望這能解決您的問題,但我也想分享一些如何重構代碼的想法。 例如,您可以使用布爾值數組而不是整數,因為根據游戲邏輯,這些值顯然只能是 0 或 1。 掃描一個單元格的鄰域也增加了許多可以縮短的冗余代碼。 只需將以下內容視為有關可以完成的一些改進的一般想法:

#include <iostream>

#include <chrono>
#include <thread> 


using namespace std;
using namespace std::this_thread;
using namespace std::chrono;

int main(){
    
    int x = 10, y = 10, i, j, c, b;
    bool field[y][x] =
    
    {{ 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,1,1,0,0,0,0},
     { 0,0,0,0,1,0,1,0,0,0},
     { 0,0,0,0,1,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0},
     { 0,0,0,0,0,0,0,0,0,0}};//too lazy to rewrite to true/false, should also work this way
     
    bool updateField[y][x];
    
    
    
    
    cout << "start";
    for(b = 0;b<=10; b++){
        cout << "main loop: "<<b<<"\n";
        for(y = 0; y < 10; y++)
        {
            
            for(x = 0; x < 10; x++)
            {
            
            c = 0;
            
            if(y == 0 || y == 9 || x == 0 || x == 9){
                
                updateField[y][x] = false;
                
            }
            else{   
                for(i=-1;i<2;i++){
                    for(j=-1;j<2;j++){
                        if((i != 0 || j != 0) && field[y+i][x+j] == 1){// exclude i == j == 0
                            c++;
                        }
                    }
                }
                    
                if(field[y][x]){
                    if(c < 2 or c > 3){
                        updateField[y][x] = false;
                    }
                    else
                    {
                        updateField[y][x] = true;
                    }
                }else{
                    if(c == 3)
                    {
                        updateField[y][x] = true; 
                    }
                    else
                    {
                        updateField[y][x] = false;
                    }
                }
                
            }
            
        }
    }
    cout << "\n";
    for(i = 0; i < 10; i++){
        for(j = 0; j < 10; j++){
            field[i][j] = updateField[i][j];
        }
    }
        
        for(i = 0;i < x; i++){
            for(j = 0; j < y; j++ )
            {
                cout << field[i][j];
                
            }
        cout << "\n";
        }
        sleep_for(nanoseconds(500000000));
    }
    
    
    
}

如果您也初始化 updateField,那么奇怪的數字就會消失。 由於這個 if 語句,updatefield 的邊緣不會更新:

                if (y == 0 || y == 9 || x == 0 || x == 9) ;
                    else {

///long statements
                        if (c < 2 || c > 3) {
                            updateField[y][x] = 0;
                        }
                        else
                        {
                            updateField[y][x] = 1;
                        }
                  }

此外,您應該更一致地使用變量,例如您將地圖定義為 x*y 大小,但稍后在 for 循環中您將 x 和 y 的最大值指定為 10。

你看到的是未初始化內存的內容。 當你聲明int updateField[y][x]; 編譯器為您保留了適當的內存空間,但它不會為您初始化內存,因此它包含任何已經存在的隨機垃圾。

要解決此問題,請在運行循環之前將updateField初始化為與 field 相同:

for (int i = 0; i < y; ++i) {
    for (int j = 0; j < x; ++j) {
        updateField[i][j] = field[i][j];
    }
}

暫無
暫無

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

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