C++의 Langtons Ant(콘솔) - 코어 덤프됨 (Langtons Ant in C++ (console) - core dumped)


문제 설명

C++의 Langtons Ant(콘솔) ‑ 코어 덤프됨 (Langtons Ant in C++ (console) ‑ core dumped)

I wrote a simple Langtons Ant in C++ (console). But (dont really know why) I'm getting a core dumped every time I run my program:

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    srand(time(NULL));

    bool endGame = false;
    enum compass {north, east, south, west};
    compass dir = north;
    int x = 0, y = 0;
    int n = 30, m = 30;

    int **board = new int*[n];
    for(int i = 0; i <n; i++)
        board[i] = new int[m];

    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            board[i][j] = rand()%2;

    long count = 0;
    while(!endGame)
    {
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                //Print board
                if(board[i][j] == 0)
                    cout << '+';
                else
                    cout << '#';
            }
            cout << endl;
        }

        //Ant
        if (board[x][y] == 0)
        {
            board[x][y] = 1;
            switch (dir){
            case north:
                dir = east;
                x = ((x+1)%m);
                break;
            case east:
                dir = south;
                y = ((y‑1) % n);
                break;
            case south:
                dir = west;
                x = ((x‑1) % m);
                break;
            case west:
                dir = north;
                y = ((y+1) %n);
                break;
            }
        }else
        {
            board[x][y] = 0;
            switch(dir){
            case north:
                dir = west;
                x = ((x‑1) % m);
                break;
            case west:
                dir = south;
                y = ((y‑1) % n);
                break;
            case south:
                dir = east;
                x = ((x+1)%m);
                break;
            case east:
                dir = north;
                y = ((y+1) %n);
                break;
            }
        }
        cout << endl << count << endl;
        count++;
        cin.sync();
        cin.get();
    }
    cin.sync();
    cin.get();
    return 0;
}

How can I get rid of this error?


참조 솔루션

방법 1:

It's probably use of modulo like this:

x = ((x‑1) % m);

Keep in mind negative % positive = negative, meaning you can get out of bounds.

(by yakPubby)

참조 문서

  1. Langtons Ant in C++ (console) ‑ core dumped (CC BY‑SA 3.0/4.0)

#C++ #Artificial-Intelligence






관련 질문

파일의 암호화/복호화? (Encryption/ Decryption of a file?)

이상한 범위 확인 연산자가 있는 C++ typedef (C++ typedef with strange scope resolution operator)

개체 배열 매개변수--오류: '문자' 필드에 불완전한 유형이 있습니다. (Object array parameter--error: field ‘letters’ has incomplete type)

C++에서 메모리 삭제 (Deleting memory in c++)

C++ 프로그램을 실행할 수 없습니다. No se ejecuta el programa (C++ i can't run a program. No se ejecuta el programa)

컴파일 시 변수의 이름과 수명 (Name and lifetime of variables at compile time)

control-c 후 Visual Studio 콘솔 프로그램 충돌 (visual studio console program crashes after control-c)

멤버 변수에 std::enable_if 또는 유사한 메서드 사용 (Using std::enable_if or similar method on member variable)

ifstream input_file(filename); (I am receiving an error "no matching function to call" in the line ifstream input_file(filename);)

ESP8266에서 잠시 실행하면 JsonData 크기가 0이 됩니다. (JsonData size becomes zero after awhile of running in ESP8266)

합에 대한 속도 문제(제수의 합) (Speed problem for summation (sum of divisors))

벡터 벡터에서 하위 벡터의 첫 번째 값을 찾기 위해 begin() 및 end() 반복기의 범위를 지정하는 방법은 무엇입니까? (How to specify a range for begin() and end() iterators to find first value of sub vector in a vector of vectors?)







코멘트