void Cross() { int i, j, l, loc, temp; int NCross = 0; for (i=0; i<MaxPop; i++) /* for each individual in the population */ { if (FRandom(1.0) > ProbCross) continue; /* skip unless within prob'y */ NCross++; do j = IRandom(MaxPop); while (j == i); /* select random mate (not with itself) */ loc = IRandom(CodeSize - 2) + 1; /* cross-over location 1 to size-1 */ for (l=loc; l<CodeSize; l++) /* for loci at loc and beyond, exchange */ { temp = Pop[i].Code[l]; Pop[i].Code[l] = Pop[j].Code[l]; Pop[j].Code[l] = temp; } } if (NTop) /* print unless NTop is 0 */ printf( "\n There were %d crossovers. \n",NCross); }
/* perform mutations for this generation */ void Mutate() { int i, l; int NMut = 0; for (i=0; i<MaxPop; i++) /* for each individual in the population */ { if (FRandom(1.0) > ProbMut) continue; /* skip unless within prob'y */ NMut++; l = IRandom(CodeSize); /* select locus to mutate */ Pop[i].Code[l] = (Pop[i].Code[l] +IRandom(CodeSchema[l]-2)+1)%CodeSchema[l]; /* different value */ } if (NTop) /* print unless NTop is 0 */ printf( "\n There were %d mutations. \n", NMut); }
void CRaceSelect::Mouse (int button, int state, int x, int y) { TWidget *focused = ClickGUI(state, x, y); if (state == 0) { if (textbuttons[0]->focussed()) SetRaceConditions (); else if (textbuttons[1]->focussed()) State::manager.RequestEnterState (GameTypeSelect); if (random_btn->focussed()) { mirror->SetValue(IRandom (0, 1)); light->SetValue(IRandom (0, 3)); snow->SetValue(IRandom (0, 3)); wind->SetValue(IRandom (0, 3)); } //UpdateInfo (); if (focused) focused->focus = false; } }
/* create an initial population */ void InitPop() { int i, l; for (i=0; i<MaxPop; i++) /* for each individual in the population */ { for (l=0; l<CodeSize; l++) /* select random digit for each locus */ Pop[i].Code[l] = IRandom(CodeSchema[l]); } Test(); /* get the goodness data */ Pop[MaxPop].Good = -999.0; /* --dummy value to support search for best */ }
TrollJumper::TrollJumper(TrollScreen *scr, IShort sx, IShort sy, IUShort secrt, IUShort shft) : TrollStandardMonster(scr, secrt, 1) { if ((sx == TROLL_XYRANDOM) && (sy == TROLL_XYRANDOM)) { findOpen(); } else { x = sx; y = sy; } sprite = TROLL_SPRITE_TOAD; shift = shft; facing = 0; direction = IRandom(4); frame = 0; hp = 1; time = IRandom(8) * 2; state = TROLL_JUMPER_SICKNESS; }
/*********************************************************************** Shuffle function ***********************************************************************/ void StochasticLib1::Shuffle(int * list, int min, int n) { /* This function makes a list of the n numbers from min to min+n-1 in random order. The parameter 'list' must be an array with at least n elements. The array index goes from 0 to n-1. If you want to shuffle something else than integers then use the integers in list as an index into a table of the items you want to shuffle. */ int i, j, swap; // put numbers from min to min+n-1 into list for (i=0, j=min; i<n; i++, j++) list[i] = j; // shuffle list for (i=0; i<n-1; i++) { // item number i has n-i numbers to choose between j = IRandom(i,n-1); // swap items i and j swap = list[j]; list[j] = list[i]; list[i] = swap;}}
void TrollJumper::react() { int i; bool pass = true; IUShort xCur, yCur; if (invincible) { invincible--; } time++; if (state == TROLL_JUMPER_SICKNESS) { if (time == 32) { time = 0; state = 0; // state = (IRandom(5) % 3) * 4; } if (time % TROLL_JUMPER_SPEED == 0) { frame++; frame %= 2; } } else if (state == TROLL_JUMPER_WAIT) { if (facing > 1) facing %= 2; if (time == TROLL_JUMPER_SPEED * 4) { time = 0; state = 0; // state = (IRandom(5) % 3) * 4; } if (time % TROLL_JUMPER_SPEED == 0) { frame++; frame %= 2; } } else if (flyBack) { pass = true; if ((x % TROLL_SQUARE_X == 0) && ((y - TROLL_BUFFER_Y) % TROLL_SQUARE_Y == 0)) { xCur = x / TROLL_SQUARE_X; yCur = (y - TROLL_BUFFER_Y) / TROLL_SQUARE_Y; pass = checkPassabilityFrom(xCur, yCur); } if (pass) { move(); frame++; frame %= 2; } flyBack--; if (flyBack == 0) { state = TROLL_JUMPER_ONELEAP; } time = 0; } else if (time == TROLL_JUMPER_SPEED) { const Sprite *pic = TrollSpriteHandler.getSprite(sprite); switch (state % 4) { case 0: if (state == 0) direction = IRandom(4); switch (direction) { case TROLL_UP: // up and right x += TROLL_SQUARE_X / 4; y -= TROLL_SQUARE_Y / 4; facing = 2; break; case TROLL_DOWN: // up and left x -= TROLL_SQUARE_X / 4; y -= TROLL_SQUARE_Y / 4; facing = 3; break; case TROLL_RIGHT: // up and right x += TROLL_SQUARE_X / 4; y -= TROLL_SQUARE_Y / 4; facing = 2; break; case TROLL_LEFT: // up and left x -= TROLL_SQUARE_X / 4; y -= TROLL_SQUARE_Y / 4; facing = 3; break; } break; case 1: case 2: switch (direction) { case TROLL_UP: // up and right x += TROLL_SQUARE_X / 4; y -= TROLL_SQUARE_Y / 4; break; case TROLL_DOWN: // down and left x -= TROLL_SQUARE_X / 4; y += TROLL_SQUARE_Y / 4; break; case TROLL_RIGHT: // down and right x += TROLL_SQUARE_X / 4; y += TROLL_SQUARE_Y / 4; break; case TROLL_LEFT: // up and left x -= TROLL_SQUARE_X / 4; y -= TROLL_SQUARE_Y / 4; break; } break; case 3: switch (direction) { case TROLL_UP: // down and right x += TROLL_SQUARE_X / 4; y += TROLL_SQUARE_Y / 4; break; case TROLL_DOWN: // down and left x -= TROLL_SQUARE_X / 4; y += TROLL_SQUARE_Y / 4; break; case TROLL_RIGHT: // down and right x += TROLL_SQUARE_X / 4; y += TROLL_SQUARE_Y / 4; break; case TROLL_LEFT: // down and left x -= TROLL_SQUARE_X / 4; y += TROLL_SQUARE_Y / 4; break; } break; } if (x < 0) { x = 0; } else if (x + pic->getXSize() > 320) { x = 320 - pic->getXSize(); } if (y < TROLL_BUFFER_Y) { y = TROLL_BUFFER_Y; } else if (y + pic->getYSize() > 200) { y = 200 - pic->getYSize(); } state++; time = 0; frame++; frame %= 2; } }