void initgrid(Grid g) { int x,y,s; for (x = 0; x < xmax; x++) { for (y = 0; y < ymax; y++) { for (s = 0; s < nspecies; s++) { g[xy2i(x,y)]->val[s] = initbase[s]; } } } for (s = 0; s < nspecies; s++) { g[xy2i(xmax / 2, ymax / 2)]->val[s] = initseed[s]; } }
double nextgridstate(double t, Grid curstate, Grid nextstate) { int i; double tcopy = t; for (int x = 0; x < xmax; x++) { for (int y = 0; y < ymax; y++) { for (int s = 0; s < nspecies; s++) { i = xy2i(x,y); yt[i * nspecies + s] = curstate[i]->val[s]; } } } int status = gsl_odeiv2_driver_apply(driver, &tcopy, t + timestep, yt); //int status = gsl_odeiv2_step_apply(stepper, tcopy, timestep, yt, yerr, NULL, NULL, &sys); if (status != GSL_SUCCESS) { fprintf(stderr, "transitionode.nextgridstate: Error when applying step function.\n"); exit(EXIT_FAILURE); } gsl_odeiv2_driver_reset(driver); // double sumerr[3] = {0,0,0}; // for (int x = 0; x < xmax; x++) { // for (int y = 0; y < ymax; y++) { // for (int s = 0; s < nspecies; s++) { // i = xy2i(x,y); // sumerr[s] += yerr[i * nspecies + s]; // } // } // } // for (int s = 0; s < nspecies; s++) { // printf("%.20f\t", sumerr[s]); // } // printf("\n"); for (int x = 0; x < xmax; x++) { for (int y = 0; y < ymax; y++) { for (int s = 0; s < nspecies; s++) { i = xy2i(x,y); if (yt[i*nspecies + s] > 1) yt[i*nspecies + s] = 1; if (yt[i*nspecies + s] < 0) yt[i*nspecies + s] = 0; nextstate[i]->val[s] = yt[i * nspecies + s]; } } } return t + timestep; }
void output(Grid g, double t) { //unsigned int savsize = xmax * ymax; /* Population count */ double count[nspecies]; double errsum[nspecies]; for(int s = 0; s < nspecies; s++) { count[s] = 0; errsum[s] = 0; } for(int x = 0; x < xmax; x++) { for(int y = 0; y < ymax; y++) { for(int s = 0; s < nspecies; s++) { count[s] += speciesoncell(g[xy2i(x,y)],s); errsum[s] += getcurrenterror(x,y,s); } } } printf("%f,", t); for(int i = 0; i < nspecies - 1; i++) { printf("%.5e,%.5e,", count[i] / ((double) xmax * ymax), errsum[i]/ ((double) xmax * ymax)); } printf("%.5e,%.5e", count[nspecies - 1] / ((double) xmax * ymax), errsum[nspecies - 1] / ((double) xmax * ymax)); printf("\n"); }
void GameOfLife::GoLstep(){ // Run 1 simulation step of the GoL algorithm // Loop through all cells and determine their next state for(uint8_t y=0; y!=_rows; ++y){ for(uint8_t x=0; x!=_cols; ++x){ // First reset the next array next[xy2i(x,y)] = false; // Then calculate number of neighbours (n) uint8_t n = 0; // loop through all neighbouring cells for(int8_t dy=-1; dy!=2; ++dy){ for(int8_t dx=-1; dx!=2; ++dx){ if( dx==0 && dy==0 ) continue; //do not count itself n += getNeighbour((int8_t)y+dy,(int8_t)x+dx); } } // Make a decision based on number of alive neighbours if(n==3){ // keeps living or is born next round next[xy2i(x,y)] = true; } else if(alive[xy2i(x,y)] && n==2){ //stays alive with 2 neighbours next[xy2i(x,y)] = true; } // All other cases: die or stay dead } } // Try to find a stall, or period-1 oscillation boolean same_prev = true; boolean same_alive = true; for(uint8_t i=0; i!=_num_leds; ++i){ if(next[i] != prev[i]) same_prev = false; if(next[i] != alive[i]) same_alive = false; } if((same_prev || same_alive) && !seedFlag){ seedFlag = true; seedTime = millis(); } // Now copy current to prev and next to current (alive) memcpy(prev,alive,_num_leds * sizeof(bool)); memcpy(alive,next,_num_leds * sizeof(bool)); }
bool checkEnd(TicTacToe* tictactoe) { for (int y = 1; y <= getBoard(tictactoe)->height; ++y) { for (int x = 1; x <= getBoard(tictactoe)->width; ++x) { int i = xy2i(x, y, getBoard(tictactoe)->width, getBoard(tictactoe)->height); if (getBoard(tictactoe)->table[i] == ' ') { return false; } } } return true; }
void computeneighbourhood(Grid g) { int x,y,i,s; for (s = 0; s < nspecies; s++) { ngbhood[s] = 0; } for (x = 0; x < xmax; x++) { for (y = 0; y < ymax; y++) { i = xy2i(x,y); for (s = 0; s < nspecies; s++) { ngbhood[s] += speciesoncell(g[i], s); } } } }
double getcurrenterror(int x,int y,int s) { return yerr[xy2i(x,y) * nspecies + s]; }
bool checkWin(TicTacToe* tictactoe) { // horizontal for (int y = 1; y <= getBoard(tictactoe)->height; ++y) { char firstCase = getBoard(tictactoe)->table[xy2i(1, y, getBoard(tictactoe)->width, getBoard(tictactoe)->height)]; if (firstCase == ' ') continue; bool win = true; for (int x = 2; x <= getBoard(tictactoe)->width; ++x) { if (getBoard(tictactoe)->table[xy2i(x, y, getBoard(tictactoe)->width, getBoard(tictactoe)->height)] != firstCase) { win = false; break; } } if (win) return true; } // vertical for (int x = 1; x <= getBoard(tictactoe)->width; ++x) { char firstCase = getBoard(tictactoe)->table[xy2i(x, 1, getBoard(tictactoe)->width, getBoard(tictactoe)->height)]; if (firstCase == ' ') continue; bool win = true; for (int y = 2; y <= getBoard(tictactoe)->width; ++y) { if (getBoard(tictactoe)->table[xy2i(x, y, getBoard(tictactoe)->width, getBoard(tictactoe)->height)] != firstCase) { win = false; break; } } if (win) return true; } //diagonals { char firstCase_ul2br = getBoard(tictactoe)->table[xy2i(1, 1, getBoard(tictactoe)->width, getBoard(tictactoe)->height)]; bool win_ul2br = true; char firstCase_ur2bl = getBoard(tictactoe)->table[xy2i(getBoard(tictactoe)->width, 1, getBoard(tictactoe)->width, getBoard(tictactoe)->height)]; bool win_ur2bl = true; for (int y = 2; y <= getBoard(tictactoe)->width; ++y) { if (firstCase_ul2br == ' ' || getBoard(tictactoe)->table[xy2i(y, y, getBoard(tictactoe)->width, getBoard(tictactoe)->height)] != firstCase_ul2br) win_ul2br = false; if (firstCase_ur2bl == ' ' || getBoard(tictactoe)->table[xy2i(getBoard(tictactoe)->width + 1 - y, y, getBoard(tictactoe)->width, getBoard(tictactoe)->height)] != firstCase_ur2bl) win_ur2bl = false; } if (win_ul2br || win_ur2bl) return true; } return false; }
void tick(TicTacToe* tictactoe, Player* player, int x, int y) { int i = xy2i(x, y, getBoard(tictactoe)->width, getBoard(tictactoe)->height); getBoard(tictactoe)->table[i] = player->mark; }
uint8_t GameOfLife::getNeighbour(int8_t y, int8_t x){ if( y<0 || y>=_rows || x<0 || x>=_cols) return 0; if(alive[xy2i(x,y)]) return 1; return 0; }