bool Creature::hit(int dmg) { if (!checkAlive()) return !checkAlive(); healthPoints -= dmg; if (healthPoints <= 0) healthPoints = 0; return !checkAlive(); }
/* perform the operations for a frame: * - check to see if the connections are still alive (checkAlive) * - this will emit connectionsChanged if there is a any change in the connection status * - grab a text stream of the current model data ( stream << *subjectList ) * - put the text stream on the wire s->write(...) */ void MyServer::process() { stopProfile("Other"); working = true; startProfile("checkAlive"); int alive = checkAlive(); stopProfile("checkAlive"); if(alive > 0) { startProfile("Serve"); count++; QString buffer; QTextStream stream(&buffer); // The following operation is threadsafe. startProfile("Fetch"); subjectList->read(stream, true); stopProfile("Fetch"); startProfile("Wait"); listMutex.lock(); stopProfile("Wait"); // for each connection for(QList<ServerConnection *>::iterator i = connections.begin(); i != connections.end(); i++) { QTcpSocket *s = (*i)->socket; if(s->state() != QAbstractSocket::ConnectedState) continue; QString d = QString("%1\nEND\r\n").arg(buffer); startProfile("Write"); int written = s->write(d.toUtf8()); stopProfile("Write"); if(written == -1) { emit outMessage(QString(" Error writing to %1").arg(s->peerAddress().toString())); } else { s->flush(); } } listMutex.unlock(); stopProfile("Serve"); } working = false; startProfile("Other"); }
int GameOfLife::countNeighbours(int row, int col) { int alive = 0; if (checkAlive(row - 1, col - 1)) ++alive; if (checkAlive(row - 1, col)) ++alive; if (checkAlive(row - 1, col + 1)) ++alive; if (checkAlive(row, col - 1)) ++alive; if (checkAlive(row, col + 1)) ++alive; if (checkAlive(row + 1, col - 1)) ++alive; if (checkAlive(row + 1, col)) ++alive; if (checkAlive(row + 1, col + 1)) ++alive; return alive; }
bool GameEngine::update() { if (_input.getKey(SDLK_ESCAPE) || _input.getInput(SDL_QUIT)) return (false); _context.updateClock(_clock); _context.updateInputs(_input); for (size_t i = 0; i < _objects.size(); ++i) _objects[i]->update(_clock, _input); if (!checkAlive()) return gameOver(); _bonuses.update(); _camera.update(_input); return (true); }
void GroupManager::setLocalUser(ServerUser *u) { localUser = u; userName = u->qsName; QString temp = prefix + "/" + userName; pNdnMediaPro->addLocalUser(temp); enumTimer = new QTimer(this); connect(enumTimer, SIGNAL(timeout()), this, SLOT(enumerate())); enumTimer->start(4000); aliveTimer = new QTimer(this); connect(aliveTimer, SIGNAL(timeout()), this, SLOT(checkAlive())); aliveTimer->start(25000); StartThread(); //need lock enumerate(); pNdnMediaPro->startThread(); }
int main(){ int n, current[20], next[20]; do{ scanf("%d", &n); }while(n < 0 || n > 20 ); do { for(int i=0; i < n; i++) do scanf("%d", ¤t[i]); while(current[i] < 0 || current[i] > 1 ); }while(current[0]!=0 || current[n-1]!=0 ); for(int i=0; i < 1000; i++){ for(int j = 0; j < n; j++) printf("%c", (current[j] == 0) ? '.' : '*'); printf("\n"); next_generation( ¤t[0], &next[0], n); if(checkAlive(¤t[0], n)){ for(int j = 0; j < n; j++) printf("%c", (current[j] == 0) ? '.' : '*'); printf("\n"); break; } } return 0; }