Funktion über Zeitspanne gleichverteilt aufrufen



  • hi, bin im programmiern noch blutiger anfänger un hab da jetzt n ähnliches problem, nur nicht ganz so kompliziert denk ich^^

    hier mein programm: einfaches würfelspiel was fragt ob gewürfelt werden soll, nach eingabe "j" wird gewürfelt mit 2 würfeln (random ausgabe). nebenbei soll ne zeitschleife laufen die alle 5minuten nachdem man wieder "j" zum würfeln eingegeben hat nen 3er pasch ausgibt.
    dachte da an sowas wie if(time-gepseichertertimestamp>300) mach paschausgabe else random nur mangelts mir an der umsetzung^^

    do
    {
    cout << "Wuerfeln? (j/n)";
    cin >> würfeln;
    if (würfeln == 'j')

    srand( time(NULL) );
    int w1 = rand() % 6 + 1;
    int w2 = rand() % 6 + 1;
    printf("Wuerfel 1: %d\nWuerfel 2: %d\n%s", w1, w2, (w1==w2) ? "Pasch!\n" : ""); } while (würfeln == 'j');

    so sieht das zur zeit aus, hoffe mir kann da jemand weiterhelfen 🙂



  • Warum verwendest du printf lieber std::cout

    srand( time(NULL) );
    do 
    {
    cout << "Wuerfeln? (j/n)"; 
    cin >> würfeln; 
    if (würfeln == 'j') 
    {
    int w1 = rand() % 6 + 1; 
    int w2 = rand() % 6 + 1;
    cout << "w1=" << w1 << " und w2=" << w2 << endl;
    if (w1 == w2)
    cout << "Pasch!" << endl;
    }
    }while (würfeln == 'j');
    


  • danke dafür schonmal^^ hatte mir den würfelcode aus nem forum geholt, deswegen hab ichs einfach so stehn lassen 🙂



  • wie kann ich denn die zeit überhaupt speichern. ich brauch ja erstmal die startzeit nachdem das erste mal gewürfelt wurde.



  • #include <iostream>
    #include <time.h>               // contains clock()
    
    using namespace std;
    
    int main()
    {
    	double time1=0.0, tstart;      // time measurment variables
    
    	//      read data 
    
    	tstart = clock();              // start 
    
    	//      some code
    
    	time1 += clock() - tstart;     // end
    	time1 = time1/CLOCKS_PER_SEC;  // rescale to seconds or "/ 1000"
    	cout << "  time = " << time1 << " sec." << endl;
    
    	return 0;
    }
    


  • hmm danke aber damit funktionierts irgendwie nich so wirklich 😞



  • willst du boost verwenden?

    #include <boost/thread.hpp>
    class delayer {
      boost::system_time last_delay;
      boost::time_duration delay_time;
    public:
      delayer(boost::time_duration delay) : last_delay(boost::get_system_time()), delay_time(delay) {}
      void delay() {
        last_delay = std::max(last_delay + delay_time, boost::get_system_time());
        boost::this_thread::sleep(last_delay);
      }
    };
    
    delayer d(boost::posix_time::microseconds(50));
    for (;;) {
      foo();
      d.delay(); // Wartet bis die 50 Millisekunden verstrichen sind
    }
    


  • Ups, ich habe auf die ursprüngliche Frage geantwortet.



  • Aber boost::thread::get_system_time() sollte schon einmal einen Tipp geben, wie man Zeit speichert.



  • zween schrieb:

    hmm danke aber damit funktionierts irgendwie nich so wirklich 😞

    Du musst schon Code zeigen was du machst, sonst kann dir nicht geholfen werden.



  • do{
    			cout << "Wuerfeln? (j/n)";
    			cin >> würfel;
    			if (würfel == 'j'){
    
    				srand( time(NULL) );
    gettime //zeitspeichern ka wie, if(time>5min) ausgabe w1=3 und w2=3, danach zeit resetten und neustart nach erneutem würfeln.
    				int w1 = rand() % 6 + 1;
    				int w2 = rand() % 6 + 1;
    				cout << "Wuerfel 1=" << w1 << endl <<"Wuerfel 2=" << w2 << endl;
    				if (w1 == w2)
    				cout << "Pasch!" << endl;   
    			}
    		}while(würfel == 'j');
    

    ums nochmal zu erklärn: nach dem ersten mal würfeln soll der timer gestartet werden der nach ablauf der 5min beim erneuten würfeln ein 3er pasch anstatt der random variante ausgibt. danach soll der timer resettet un neu gestartet werden.



  • #include <iostream> 
    #include <cstdio>
    #include <ctime>               // contains clock() 
    
    using namespace std; 
    
    int main() 
    { 
        double tstart;      // time measurment variables 
    	char würfeln;
    
    	tstart = clock();
    	srand( time(NULL) ); 
    
    	do 
    	{ 
    		cout << "Wuerfeln? (j/n)"; 
    		cin >> würfeln; 
    
    		if (würfeln == 'j') 
    		{ 
    			if (clock() - tstart < 5000)
    			{
    				int w1 = rand() % 6 + 1; 
    				int w2 = rand() % 6 + 1; 
    				cout << "w1=" << w1 << " und w2=" << w2 << endl; 
    				if (w1 == w2) 
    				cout << "Pasch!" << endl; 
    			}
    			else
    			{
    				cout << "w1=3 und w2=3" <<  endl;
    				cout << "Pasch dank schummeln!" << endl; 
    				tstart = clock();
    			}
    		}
    	}while (würfeln == 'j');
    
        return 0; 
    }
    

    Ist doch nicht so schwer das eine und das andere zusammenzubasteln?



  • aaaah super dank dir! ich hatte einfach n fehler drin. wie gesagt bin noch blutiger anfänger 😃


Anmelden zum Antworten