void KBBLevelConfigurationPreview::preview(int balls, int columns, int rows) { m_blackbox->setSize(columns, rows); scene()->setSceneRect(0, 0, columns*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE, rows*KBBScalableGraphicWidget::RATIO + 2*KBBScalableGraphicWidget::BORDER_SIZE); // Show balls on the black box while (m_balls.count()>0) { delete m_balls.last(); m_balls.removeLast(); } QList<int> ballPos; KRandomSequence random; random.setSeed(0); int boxPos; KBBGraphicsItem* item; for (int i=0;i<balls;i++) { do { boxPos = random.getLong(columns*rows); } while (ballPos.contains(boxPos)); item = new KBBGraphicsItem(KBBScalableGraphicWidget::playerBall, scene(), m_themeManager); item->setPos(KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos % columns), KBBScalableGraphicWidget::BORDER_SIZE + KBBScalableGraphicWidget::RATIO*(boxPos / columns)); ballPos.append(boxPos); m_balls.append(item); } resizeEvent(0); }
Coord Solver::advise(BaseField &field, float &probability) { Coord point; probability = 1; delete d->facts; d->facts = new AdviseFast::FactSet(&field); delete d->rules; d->rules = new AdviseFast::RuleSet(d->facts); if( AdviseFast::adviseFast(&point, d->facts, d->rules) ) return point; CoordSet surePoints; AdviseFull::ProbabilityMap probabilities; AdviseFull::adviseFull(d->facts, &surePoints, &probabilities); // return one of the sure point (random choice to limit the tropism) [NH] if( !surePoints.empty() ) { KRandomSequence r; uint k = r.getLong(surePoints.size()); CoordSet::iterator it = surePoints.begin(); for (uint i=0; i<k; i++) ++it; return *it; } // Just a minimum probability logic here if( !probabilities.empty() ) { probability = probabilities.begin()->first; return probabilities.begin()->second; } // Otherwise the Field is already solved :) return Coord(-1,-1); }
void SoundFactory::setSoundSequence() { // Seed the random number generator KRandomSequence randomSequence; randomList.clear(); //get the number of sounds then shuffle it: each number will be taken once then the sequence will come back for (uint j = 0; j < sounds; j++) randomList.append(j); randomSequence.randomize(randomList); }
QStringList WQQuiz::multiOptions(int i) { QString *s; QStringList Result; WQListItem li = m_list[i]; typedef QPtrList<QString> LS; LS *ls; ls = new QPtrList<QString>(); int j; if (li.question() == 0) { j = 1; } else { j= 0; } s= new QString(m_table->text(li.oneOp(), j)); if (Prefs::enableBlanks()) { s->remove("["); s->remove("]"); } ls->append(s); s = new QString(m_table->text(li.twoOp(), j)); if (Prefs::enableBlanks()) { s->remove("["); s->remove("]"); } ls->append(s); s = new QString(m_table->text(li.threeOp(), j)); if (Prefs::enableBlanks()) { s->remove("["); s->remove("]"); } ls->append(s); KRandomSequence rs; rs.randomize(ls); while (ls->count()) { Result.append(*ls->first()); ls->removeFirst(); } return Result; }
void WQQuiz::addToList(int aCol, int bCol) { //build a list of row numbers containing text in both columns typedef QValueList<int> IntList; IntList tempList; for (int current = 0; current < m_table ->numRows(); ++current) { if (!m_table->text(current, 0).isEmpty() && !m_table->text(current, 1).isEmpty()) { tempList.append(current); } } KRandomSequence *rs = new KRandomSequence(0); int count = tempList.count(); IntList::ConstIterator it; for ( it = tempList.begin(); it != tempList.end(); ++it ) { WQListItem *li; li = new WQListItem(); li->setQuestion(aCol); li->setCorrect(1); li->setOneOp(*it); if (count > 2) { int a, b; do a = rs->getLong(count); //rand() % count; while(a==*it); li->setTwoOp(a); do b = rs->getLong(count); //rand() % count; while(b == *it || b == a /*|| b < 0*/); li->setThreeOp(b); } m_quizList.append(*li); } }
void KWQQuizModel::activateBaseList() { m_list.clear(); m_errorList.clear(); foreach(int l, m_quizList) m_list.append(l); if (m_quizMode > 2) { KRandomSequence rs; rs.randomize(m_list); }; m_questionCount = m_list.count(); m_currentQuestion = 0; }
void initswarm(Window win, KRandomSequence &rnd) { swarmstruct *sp = &swarms[screen]; int b; XWindowAttributes xwa; sp->beecount = batchcount; (void) XGetWindowAttributes(dsp, win, &xwa); sp->width = xwa.width; sp->height = xwa.height; sp->border = (sp->width + sp->height) / 50; /* Clear the background. */ XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen)); XFillRectangle(dsp, win, Scr[screen].gc, 0, 0, sp->width, sp->height); /* Now static data structures. epirker */ //if (!sp->segs) { //sp->segs = (XSegment *) malloc(sizeof (XSegment) * sp->beecount); //sp->old_segs = (XSegment *) malloc(sizeof (XSegment) * sp->beecount); //sp->x = (short *) malloc(sizeof (short) * sp->beecount * TIMES); //sp->y = (short *) malloc(sizeof (short) * sp->beecount * TIMES); //sp->xv = (short *) malloc(sizeof (short) * sp->beecount); //sp->yv = (short *) malloc(sizeof (short) * sp->beecount); //} /* Initialize point positions, velocities, etc. */ /* wasp */ sp->wx[0] = sp->border + rnd.getLong(sp->width - 2 * sp->border); sp->wy[0] = sp->border + rnd.getLong(sp->height - 2 * sp->border); sp->wx[1] = sp->wx[0]; sp->wy[1] = sp->wy[0]; sp->wxv = 0; sp->wyv = 0; /* bees */ for (b = 0; b < sp->beecount; b++) { X(0, b) = rnd.getLong(sp->width); X(1, b) = X(0, b); Y(0, b) = rnd.getLong(sp->height); Y(1, b) = Y(0, b); sp->xv[b] = balance_rand(7); sp->yv[b] = balance_rand(7); } }
void WQQuiz::listRandom() { QPtrList<void> list; for(int i = 0; i < m_quizList.count(); i++) list.append((void*) i); KRandomSequence seq; seq.randomize(&list); QuizList items; for(int i = 0; i < m_quizList.count(); i++) items.append(m_quizList[(long) list.take()]); m_quizList.clear(); for(int i = 0; i < items.count(); i++) m_quizList.append(items[i]); }
void KScienceSaver::initialize() { KRandomSequence rnd; initLens(); signed int ws = (signed int) (width() - diam); signed int hs = (signed int) (height() - diam); x = (ws > 0) ? (rnd.getDouble() * ws ) : 0.0; y = (hs > 0) ? (rnd.getDouble() * hs ) : 0.0; xcoord = (int) x; ycoord = (int) y; switch( bpp ) { case 1 : applyLens = &KScienceSaver::applyLens8bpp; break; case 2 : applyLens = &KScienceSaver::applyLens16bpp; break; case 3 : applyLens = &KScienceSaver::applyLens24bpp; break; case 4 : applyLens = &KScienceSaver::applyLens32bpp; break; default: myAssert( false, "unsupported colordepth "\ "(only 8, 16, 24, 32 bpp supported)" ); } }
QStringList KWQQuizModel::multiOptions() { QStringList ls; KRandomSequence rs; int a = -1; int b = -1; do a = rs.getLong(m_questionCount); while(a == m_currentQuestion); do b = rs.getLong(m_questionCount); while(b == m_currentQuestion || b == a); int row = m_list.at(m_currentQuestion); int col = column(row); if (col == 0) col = 0; else col = 1; ls.append(data(index(qAbs(row), col, QModelIndex()), Qt::DisplayRole).toString()); row = m_list.at(a); ls.append(data(index(qAbs(row), col, QModelIndex()), Qt::DisplayRole).toString()); row = m_list.at(b); ls.append(data(index(qAbs(row), col, QModelIndex()), Qt::DisplayRole).toString()); if (Prefs::enableBlanks()) { for (int i = 0; i < ls.count(); i++) { ls[i].remove('['); ls[i].remove(']'); } } rs.randomize(ls); return ls; }
int main(int argc, char *argv[]) { KApplication a(argc, argv, "krandomsequencetest"); long seed; KRandomSequence seq; seed = 2; seq.setSeed(seed);printf("Seed = %4ld :", seed); for(int i = 0; i < 20; i++) printf("%3ld ", seq.getLong(100)); printf("\n"); seed = 0; seq.setSeed(seed);printf("Seed = %4ld :", seed); for(int i = 0; i < 20; i++) printf("%3ld ", seq.getLong(100)); printf("\n"); seed = 0; seq.setSeed(seed);printf("Seed = %4ld :", seed); for(int i = 0; i < 20; i++) printf("%3ld ", seq.getLong(100)); printf("\n"); seed = 2; seq.setSeed(seed);printf("Seed = %4ld :", seed); for(int i = 0; i < 20; i++) printf("%3ld ", seq.getLong(100)); seq.setSeed(kapp->random()); QPtrList<QString> list; list.append(new QString("A")); list.append(new QString("B")); list.append(new QString("C")); list.append(new QString("D")); list.append(new QString("E")); list.append(new QString("F")); list.append(new QString("G")); for(QString *str = list.first(); str; str = list.next()) printf("%s", str->latin1()); printf("\n\n"); seq.randomize(&list); for(QString *str = list.first(); str; str = list.next()) printf("%s", str->latin1()); printf("\n\n"); seq.randomize(&list); for(QString *str = list.first(); str; str = list.next()) printf("%s", str->latin1()); printf("\n\n"); seq.randomize(&list); for(QString *str = list.first(); str; str = list.next()) printf("%s", str->latin1()); printf("\n\n"); printf("\n"); }
int main(int argc, char *argv[]) { KCmdLineArgs::init(argc, argv, appName, "kscreensaver", ki18n("Random screen saver"), KDE_VERSION_STRING, ki18n(description)); KCmdLineOptions options; options.add("setup", ki18n("Setup screen saver")); options.add("window-id wid", ki18n("Run in the specified XWindow")); options.add("root", ki18n("Run in the root XWindow")); KCmdLineArgs::addCmdLineOptions(options); KApplication app; WId windowId = 0; KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); if (args->isSet("setup")) { KRandomSetup setup; setup.exec(); exit(0); } if (args->isSet("window-id")) { windowId = args->getOption("window-id").toInt(); } #ifdef Q_WS_X11 if (args->isSet("root")) { QX11Info info; windowId = RootWindow(QX11Info::display(), info.screen()); } #endif args->clear(); const KService::List lst = KServiceTypeTrader::self()->query( "ScreenSaver"); KService::List availableSavers; KConfig type("krandom.kssrc", KConfig::NoGlobals); const KConfigGroup configGroup = type.group("Settings"); const bool opengl = configGroup.readEntry("OpenGL", false); const bool manipulatescreen = configGroup.readEntry("ManipulateScreen", false); // TODO replace this with TryExec=fortune in the desktop files const bool fortune = !KStandardDirs::findExe("fortune").isEmpty(); foreach( const KService::Ptr& service, lst ) { //QString file = KStandardDirs::locate("services", service->entryPath()); //kDebug() << "Looking at " << file; const QString saverType = service->property("X-KDE-Type").toString(); foreach (const QString &type, saverType.split(QLatin1Char(';'))) { //kDebug() << "saverTypes is "<< type; if (type == QLatin1String("ManipulateScreen")) { if (!manipulatescreen) goto fail; } else if (type == QLatin1String("OpenGL")) { if (!opengl) goto fail; } else if (type == QLatin1String("Fortune")) { if (!fortune) goto fail; } } availableSavers.append(service); fail: ; } KRandomSequence rnd; const int indx = rnd.getLong(availableSavers.count()); const KService::Ptr service = availableSavers.at(indx); const QList<KServiceAction> actions = service->actions(); QString cmd; if (windowId) cmd = exeFromActionGroup(actions, "InWindow"); if (cmd.isEmpty() && windowId == 0) cmd = exeFromActionGroup(actions, "Root"); if (cmd.isEmpty()) cmd = service->exec(); QHash<QChar, QString> keyMap; keyMap.insert('w', QString::number(windowId)); const QStringList words = KShell::splitArgs(KMacroExpander::expandMacrosShellQuote(cmd, keyMap)); if (!words.isEmpty()) { QString exeFile = KStandardDirs::findExe(words.first()); if (!exeFile.isEmpty()) { char **sargs = new char *[words.size() + 1]; int i = 0; for (; i < words.size(); i++) sargs[i] = qstrdup(words[i].toLocal8Bit().constData()); sargs[i] = 0; execv(exeFile.toLocal8Bit(), sargs); } } // If we end up here then we couldn't start a saver. // If we have been supplied a window id or root window then blank it. #ifdef Q_WS_X11 QX11Info info; Window win = windowId ? windowId : RootWindow(QX11Info::display(), info.screen()); XSetWindowBackground(QX11Info::display(), win, BlackPixel(QX11Info::display(), info.screen())); XClearWindow(QX11Info::display(), win); #endif }
void drawswarm(Window win, KRandomSequence &rnd) { swarmstruct *sp = &swarms[screen]; int b; /* <=- Wasp -=> */ /* Age the arrays. */ sp->wx[2] = sp->wx[1]; sp->wx[1] = sp->wx[0]; sp->wy[2] = sp->wy[1]; sp->wy[1] = sp->wy[0]; /* Accelerate */ sp->wxv += balance_rand(WASPACC); sp->wyv += balance_rand(WASPACC); /* Speed Limit Checks */ if (sp->wxv > WASPVEL) sp->wxv = WASPVEL; if (sp->wxv < -WASPVEL) sp->wxv = -WASPVEL; if (sp->wyv > WASPVEL) sp->wyv = WASPVEL; if (sp->wyv < -WASPVEL) sp->wyv = -WASPVEL; /* Move */ sp->wx[0] = sp->wx[1] + sp->wxv; sp->wy[0] = sp->wy[1] + sp->wyv; /* Bounce Checks */ if ((sp->wx[0] < sp->border) || (sp->wx[0] > sp->width - sp->border - 1)) { sp->wxv = -sp->wxv; sp->wx[0] += sp->wxv; } if ((sp->wy[0] < sp->border) || (sp->wy[0] > sp->height - sp->border - 1)) { sp->wyv = -sp->wyv; sp->wy[0] += sp->wyv; } /* Don't let things settle down. */ sp->xv[rnd.getLong(sp->beecount)] += balance_rand(3); sp->yv[rnd.getLong(sp->beecount)] += balance_rand(3); /* <=- Bees -=> */ for (b = 0; b < sp->beecount; b++) { int distance, dx, dy; /* Age the arrays. */ X(2, b) = X(1, b); X(1, b) = X(0, b); Y(2, b) = Y(1, b); Y(1, b) = Y(0, b); /* Accelerate */ dx = sp->wx[1] - X(1, b); dy = sp->wy[1] - Y(1, b); distance = abs(dx) + abs(dy); /* approximation */ if (distance == 0) distance = 1; sp->xv[b] += (dx * BEEACC) / distance; sp->yv[b] += (dy * BEEACC) / distance; /* Speed Limit Checks */ if (sp->xv[b] > BEEVEL) sp->xv[b] = BEEVEL; if (sp->xv[b] < -BEEVEL) sp->xv[b] = -BEEVEL; if (sp->yv[b] > BEEVEL) sp->yv[b] = BEEVEL; if (sp->yv[b] < -BEEVEL) sp->yv[b] = -BEEVEL; /* Move */ X(0, b) = X(1, b) + sp->xv[b]; Y(0, b) = Y(1, b) + sp->yv[b]; /* Fill the segment lists. */ sp->segs[b].x1 = X(0, b); sp->segs[b].y1 = Y(0, b); sp->segs[b].x2 = X(1, b); sp->segs[b].y2 = Y(1, b); sp->old_segs[b].x1 = X(1, b); sp->old_segs[b].y1 = Y(1, b); sp->old_segs[b].x2 = X(2, b); sp->old_segs[b].y2 = Y(2, b); } XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen)); XDrawLine(dsp, win, Scr[screen].gc, sp->wx[1], sp->wy[1], sp->wx[2], sp->wy[2]); XDrawSegments(dsp, win, Scr[screen].gc, sp->old_segs, sp->beecount); XSetForeground(dsp, Scr[screen].gc, WhitePixel(dsp, screen)); XDrawLine(dsp, win, Scr[screen].gc, sp->wx[0], sp->wy[0], sp->wx[1], sp->wy[1]); if (!mono && Scr[screen].npixels > 2) { XSetForeground(dsp, Scr[screen].gc, Scr[screen].pixels[sp->pix]); if (++sp->pix >= Scr[screen].npixels) sp->pix = 0; } XDrawSegments(dsp, win, Scr[screen].gc, sp->segs, sp->beecount); }
int main(int argc, char *argv[]) { KLocale::setMainCatalogue("kscreensaver"); KCmdLineArgs::init(argc, argv, appName, I18N_NOOP("Random screen saver"), description, version); KCmdLineArgs::addCmdLineOptions(options); KApplication app; Window windowId = 0; KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); if (args->isSet("setup")) { KRandomSetup setup; setup.exec(); exit(0); } if (args->isSet("window-id")) { windowId = atol(args->getOption("window-id")); } if (args->isSet("root")) { windowId = RootWindow(qt_xdisplay(), qt_xscreen()); } KGlobal::dirs()->addResourceType("scrsav", KGlobal::dirs()->kde_default("apps") + "apps/ScreenSavers/"); KGlobal::dirs()->addResourceType("scrsav", KGlobal::dirs()->kde_default("apps") + "System/ScreenSavers/"); QStringList tempSaverFileList = KGlobal::dirs()->findAllResources("scrsav", "*.desktop", false, true); QStringList saverFileList; KConfig type("krandom.kssrc"); type.setGroup("Settings"); bool opengl = type.readBoolEntry("OpenGL"); bool manipulatescreen = type.readBoolEntry("ManipulateScreen"); bool fortune = !KStandardDirs::findExe("fortune").isEmpty(); for (uint i = 0; i < tempSaverFileList.count(); i++) { kdDebug() << "Looking at " << tempSaverFileList[i] << endl; KDesktopFile saver(tempSaverFileList[i], true); if(!saver.tryExec()) continue; kdDebug() << "read X-KDE-Type" << endl; QString saverType = saver.readEntry("X-KDE-Type"); if (saverType.isEmpty()) // no X-KDE-Type defined so must be OK { saverFileList.append(tempSaverFileList[i]); } else { QStringList saverTypes = QStringList::split(";", saverType); for (QStringList::ConstIterator it = saverTypes.begin(); it != saverTypes.end(); ++it ) { kdDebug() << "saverTypes is "<< *it << endl; if (*it == "ManipulateScreen") { if (manipulatescreen) { saverFileList.append(tempSaverFileList[i]); } } else if (*it == "OpenGL") { if (opengl) { saverFileList.append(tempSaverFileList[i]); } } if (*it == "Fortune") { if (fortune) { saverFileList.append(tempSaverFileList[i]); } } } } } KRandomSequence rnd; int indx = rnd.getLong(saverFileList.count()); QString filename = *(saverFileList.at(indx)); KDesktopFile config(filename, true); QString cmd; if (windowId && config.hasActionGroup("InWindow")) { config.setActionGroup("InWindow"); } else if ((windowId == 0) && config.hasActionGroup("Root")) { config.setActionGroup("Root"); } cmd = config.readPathEntry("Exec"); QTextStream ts(&cmd, IO_ReadOnly); QString word; ts >> word; QString exeFile = KStandardDirs::findExe(word); if (!exeFile.isEmpty()) { char *sargs[MAX_ARGS]; sargs[0] = new char [strlen(word.ascii())+1]; strcpy(sargs[0], word.ascii()); int i = 1; while (!ts.atEnd() && i < MAX_ARGS-1) { ts >> word; if (word == "%w") { word = word.setNum(windowId); } sargs[i] = new char [strlen(word.ascii())+1]; strcpy(sargs[i], word.ascii()); kdDebug() << "word is " << word.ascii() << endl; i++; } sargs[i] = 0; execv(exeFile.ascii(), sargs); }