/** * Function that returns valuation of the formula based on valuation of its variables. * * @param[in] valuation map of variable valuations in the form (name, value) * @param[in] formula formula to resolve * * @return true iff valuation of the formula is true */ static bool resolve (const Vals & valuation, string formula) { // If there is a ! symbol, negate the formula and remove it bool negate = false; bool result; boost::trim(formula); if (formula[0] == '!') { negate = true; formula = formula.substr(1); } boost::trim(formula); // Search for any operator, if not found, assume formula to be an atom and return its valuation if (formula.find("|") == string::npos && formula.find("&") == string::npos) { if (formula.compare("tt") == 0) { result = true; } else if (formula.compare("ff") == 0) { result = false; } else { auto variable = valuation.find(formula); if (variable == valuation.end()) throwException(formula, err_notfound); result = variable->second; } } else { // Find position of the operator and its kind (or/and) bool is_or = false; size_t division_pos = 0; readFormula(formula, is_or, division_pos); // Divide formula by the operator and remove its outer parenthesis, then descend recursivelly string first = formula.substr(1, division_pos - 1); string second = formula.substr(division_pos + 1, formula.size() - division_pos - 2); result = is_or ? resolve(valuation, first) | resolve(valuation, second) : resolve(valuation, first) & resolve(valuation, second); } // Return the value based on valuations of its parts return negate ? !result : result; }
void Demo::Draw() { Tree::clear_window( Tree::Color::black ); str.SetText( boost::lexical_cast<std::string>( t.GetTime() ) ); str.SetPosition( 100, 5 ); Tree::draw( str ); str.SetText( boost::lexical_cast<std::string>( st.GetTime() ) ); str.SetPosition( 100, 15 ); Tree::draw( str ); str.SetText( boost::lexical_cast<std::string>( cd.GetTime() ) ); str.SetPosition( 200, 5 ); Tree::draw( str ); if( cd.IsDone() ) { str.SetText( "done" ); } else { str.SetText( "not done" ); } str.SetPosition( 200, 15 ); Tree::draw( str ); // Draw shuffle bag's contents int n = 1; const float h = 10; for( Ints::iterator it = bagged.begin(); it != bagged.end(); ++it, ++n ) { str.SetPosition( 10, 30 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } n = 1; for( Ints::iterator it = rest.begin(); it != rest.end(); ++it, ++n ) { str.SetPosition( 30, 30 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } Vec2i mpos = Tree::get_mouse_pos(); Vec2i l1 = point - mpos; Tree::clip( l1, 0, 50 ); Tree::draw( sf::Shape::Line( point, point - l1, 1.0, Tree::Color( 0xff557733))); Vec2i p2( point.x, point.y - 50 ); Vec2i l2 = point - p2; Tree::clip( l2, 0, 50 ); Tree::draw( sf::Shape::Line( point, point - l2, 1.0, Tree::Color( 0xff775533))); float rad = angle( l1, l2 ); str.SetPosition( point.x + 20, point.y - 30 ); str.SetText( boost::lexical_cast<std::string>( rad ) ); Tree::draw( str ); float degree = Tree::rad2deg( rad ); str.SetPosition( point.x + 20, point.y - 20 ); str.SetText( boost::lexical_cast<std::string>( degree ) ); Tree::draw( str ); // Check neighbours str.SetPosition( 250, 350 ); str.SetText( "neighbours" ); Tree::draw( str ); typedef std::vector<Vec2i> Points; Points ps = Tree::generate_neighbours( point ); n = 1; for( Points::iterator it = ps.begin(); it < ps.end(); ++it, ++n ) { str.SetPosition( 250, 350 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } str.SetPosition( 320, 350 ); str.SetText( "corners" ); Tree::draw( str ); ps = Tree::generate_corners( point ); n = 1; for( Points::iterator it = ps.begin(); it < ps.end(); ++it, ++n ) { str.SetPosition( 320, 350 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } str.SetPosition( 400, 350 ); str.SetText( "both" ); Tree::draw( str ); ps = Tree::generate_surroundings( point ); n = 1; for( Points::iterator it = ps.begin(); it < ps.end(); ++it, ++n ) { str.SetPosition( 400, 350 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } // Draw weight bag's selections typedef std::vector<std::string> Vals; typedef std::vector<float> Weights; Vals vals = weight_bag.GetVals(); Weights weights = weight_bag.GetWeights(); std::stringstream ss; ss.precision( 2 ); n = 1; for( Weights::iterator it = weights.begin(); it < weights.end(); ++it, ++n ) { str.SetPosition( 60, 30 + h * n ); ss.str(""); ss << *it; //str.SetText( boost::lexical_cast<std::string>( *it ) ); str.SetText( ss.str() ); Tree::draw( str ); } n = 1; for( Vals::iterator it = vals.begin(); it < vals.end(); ++it, ++n ) { str.SetPosition( 85, 30 + h * n ); str.SetText( boost::lexical_cast<std::string>( *it ) ); Tree::draw( str ); } str.SetPosition( 170, 40 ); ss.str(""); ss << "0.2: " << apples << " " << (float)apples / (float)total_weight << " Apples"; str.SetText( ss.str() ); Tree::draw( str ); str.SetPosition( 170, 50 ); ss.str(""); ss << "0.2: " << oranges << " " << (float)oranges / (float)total_weight << " Oranges"; str.SetText( ss.str() ); Tree::draw( str ); str.SetPosition( 170, 60 ); ss.str(""); ss << "0.6: " << strawberries << " " << (float)strawberries / (float)total_weight << " Strawberries"; str.SetText( ss.str() ); Tree::draw( str ); str.SetPosition( 170, 70 ); str.SetText( "last: " + curr_weight ); Tree::draw( str ); // Draw shapes Tree::draw_line( 500, 200, 520, 220, Tree::Color::white ); Tree::draw_line( 520, 220, 540, 180, Tree::Color::yellow, 2.0 ); Tree::draw_line( Vec2i( 540, 180 ), Vec2i( 560, 200 ), Tree::Color::magenta, 2.0, Tree::Color::cyan, 1.0 ); Tree::draw_rect( 600, 100, 640, 140, Tree::Color::blue ); Tree::draw_rect( 600, 200, 640, 240, Tree::Color::green, Tree::Color::red, 1.0 ); Tree::draw_circle( 700, 250, 40, Tree::Color::white ); Tree::draw_triangle( 610, 300, 600, 320, 620, 320, Tree::Color::white ); Tree::draw_triangle( 610, 360, 600, 340, 620, 340, Tree::Color::white, Tree::Color::red, 2.0 ); // Draw statusbar float perc = st.GetTime() - (int)(st.GetTime() / 2.0) * 2.0; Tree::draw_bar( 700, 580, 790, 590, perc, Tree::Color( 0xff333333 ), Tree::Color( 0xffaaaaaa ), Tree::Color::white, 1.0 ); // Draw indexed sprites for( size_t i = 0; i < sprites.size(); ++i ) { sf::Sprite &spr = sprites[i]; spr.SetPosition( 40 + i * 32, 530 ); Tree::draw( spr ); } // Draw regular sprite aspr.SetPosition( 50, 200 ); if( perc > 1.0 ) perc = 1.0; Tree::set_alpha( aspr, Tree::enbyten( perc ) ); Tree::draw( aspr ); }