Exemple #1
0
void update(int mili) {
	Byte c = 0;
	if (!queueEmpty(&keys)) {
		c = queueGet(&keys);
		if (c == ESC_KEY)
			exit(0);
	}
	Bool mouseEvent = parse_mouse_event(mouseQueue, &mouse);

	updateHammer(hammer, mili, c, (mouseEvent ? &mouse : NULL));

	static Bool shift_flag = false;
	char k;
	int i, len;
	switch (state) {
		case PLAYING:
			for (i = 0; i < NUM_PCS; i++)
				updateCScreen(cscreens[i], hammer, &numPCs, &score->score, mili);	

			if (numPCs == 0) {
				state = END;
				hammer->state = GET_HAMMER;
			}
			
			updateButton(helpButton, hammer);
			if (helpButton->state == CLICKED)
				state = HELP;
				
			updateButton(exitButton, hammer);
			if (exitButton->state == CLICKED)
				exit(0);
			
			break;
		case END:
			if (hammer->state == HIT) {
				state = SCORE;
				highScore = isHighScore(highScores, score);
			}
			
			updateButton(helpButton, hammer);
			if (helpButton->state == CLICKED)
				state = HELP;
				
			updateButton(exitButton, hammer);
			if (exitButton->state == CLICKED)
				exit(0);
				
			break;
		case SCORE:
			if (highScore) {
				if (c != 0) {
					if (c == SHIFT_KEY) {
						shift_flag = true;
					}
					if (c == (SHIFT_KEY | RELEASED)) {
						shift_flag = false;
						break;
					}
						
					if (c == BACKSPACE_KEY) {
						len = strlen(score->name);
						if (len > 0) {
							score->name[len - 1] = NULL;
						}
						break;
					}
					
					k = scancodeToAscii(c);
					if (k != 0) {
						len = strlen(score->name);
						if (len < SCORE_NAME_LEN) {
							score->name[len] = (shift_flag ? toupper(k) : k);
							score->name[len + 1] = NULL;
						}
					}
				}
			}

			if (hammer->state == HIT && (!highScore || (c != SPACE_KEY && score->name[0] != NULL))) {
				if (highScore) {
					putScore(highScores, score);
					saveHighScores(highScores);
				}
				reset_game();
				state = PLAYING;
			}
			break;
		case HELP:
			if (hammer->state == HIT)
				state = PLAYING;
			break;
		default:
			break;
	}
}
Exemple #2
0
void scoring::ScoreCache::read(std::string filename) {
    network = new datastructures::BayesianNetwork();
    
    std::ifstream in(filename.c_str());
    std::vector<std::string> tokens;
    std::string line;

    // read meta information until we hit the first variable
    while (!in.eof()) {
        std::getline(in, line);

        // skip empty lines and comments
        if (line.size() == 0 || line.compare(0, 1, "#") == 0) {
            continue;
        }

        // check if we reached the first variable
        if (contains(line, "variable ")) break;

        // make sure this is a meta line
        if (!contains(line, "meta")) {
            throw std::runtime_error("Error while parsing META information of network.  Expected META line or Variable.  Line: '" + line + "'");
        }

        tokens = parseMetaInformation(line);

        if (tokens.size() != 2) {
            throw std::runtime_error("Error while parsing META information of network.  Too many tokens.  Line: '" + line + "'");
        }

        boost::trim(tokens[0]);
        boost::trim(tokens[1]);
        updateMetaInformation(tokens[0], tokens[1]);
    }

    // line currently points to a variable name
    tokens = parse(line, 0, " ");
    datastructures::Variable *v = network->addVariable(tokens[1]);

    // read in the variable names
    while (!in.eof()) {
        std::getline(in, line);

        // skip empty lines and comments
        if (line.size() == 0 || line.compare(0, 1, "#") == 0) {
            continue;
        }

        if (contains(line, "meta")) {
            tokens = parseMetaInformation(line);

            if (contains(tokens[0], "arity")) {
                v->setArity(atoi(tokens[1].c_str()));
            } else if (contains(tokens[0], "values")) {
                std::vector<std::string> values = parseVariableValues(tokens[1]);
                v->setValues(values);
            } else {

                boost::trim(tokens[0]);
                boost::trim(tokens[1]);
                v->updateMetaInformation(tokens[0], tokens[1]);
            }
        }

        if (contains(line, "variable ")) {
            tokens = parse(line, 0, " ");
            v = network->addVariable(tokens[1]);
        }
    }

    in.close();
    setVariableCount(network->size());

    // now that we have the variable names, read in the parent sets
    in.open(filename.c_str());
    while (!in.eof()) {
        std::getline(in, line);
        
        if (line.size() == 0 || line.compare(0, 1, "#") == 0 || contains(line, "meta")) {
            continue;
        }

        tokens = parse(line, 0, " ");
        if (contains(line, "variable ")) {
            v = network->get(tokens[1]);
            continue;
        }

        // then parse the score for the current variable
        VARSET_NEW(parents, network->size());
        float score = -1 * atof(tokens[0].c_str()); // multiply by -1 to minimize

        for (int i = 1; i < tokens.size(); i++) {
            int index = network->getVariableIndex(tokens[i]);
            VARSET_SET(parents, index);
        }

        putScore(v->getIndex(), parents, score);
    }

    in.close();
}