artificial intelligence - Langtons Ant in C++ (console) - core dumped -
i wrote simple langtons ant in c++ (console). (dont know why) i'm getting core dumped every time run 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 = 0; <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 = 0; < 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 rid of error?
it's use of modulo this:
x = ((x-1) % m); keep in mind negative % positive = negative, meaning can out of bounds.
Comments
Post a Comment