Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : Penney-Ante Simulation v1.01
- // Author : cosypanther
- // Version : 1.01 final
- // Copyright : written by cosypanther on 29.06.2012
- // Description : small test tool for penney-ante. for more information visit my blog:
- // http://cosypanther.blogspot.de/2012/06/penney-ante-ein-kleines.html
- //============================================================================
- #include <iostream>
- #include <stdlib.h>
- #include <time.h>
- #include <vector>
- #include <algorithm>
- using namespace std;
- //Funktionsdeklarationen
- void percentage(int games_counter, int NUMBER_OF_GAMES);
- void initiate_new_game(vector<bool> &results, bool player1[], bool player2[], int NUMBER_OF_COINS, bool penney_ante_mode);
- bool compliance(bool player1[], bool player2[], int NUMBER_OF_COINS);
- bool compare(vector<bool> results, bool player[], int NUMBER_OF_COINS);
- int main()
- {
- //rand() initialisieren
- srand( time(NULL) );
- //Queue für jeweils aktuelle Münzenkombination erstellen
- vector<bool> results;
- //für Schließen-/Erneutabfrage
- char retry_char = 'y';
- do
- {
- //Variablen
- int NUMBER_OF_COINS = 0;
- int NUMBER_OF_GAMES = 0;
- int games_counter = 0;
- int p1_wins = 0;
- int p2_wins = 0;
- bool penney_ante_mode = false;
- //Nutzereingabe
- cout << "----------------------------------" << endl
- << "Penney-Ante Simulation v1.01" << endl
- << "----------------------------------"<< endl
- << endl << endl;
- cout << "Enter the NUMBER OF COINS: ";
- cin >> NUMBER_OF_COINS;
- cout << "Enter the NUMBER OF GAMES: ";
- cin >> NUMBER_OF_GAMES;
- cout << endl << endl;
- //Arrays für Spielerkombinationen erstellen
- bool player1[NUMBER_OF_COINS];
- bool player2[NUMBER_OF_COINS];
- //Ausgabe
- cout << "----------------------------------" << endl
- << endl
- << "Results" << endl
- << endl
- << "----------------------------------"
- << endl << endl;
- /*
- * 1.Durchlauf: Spieler2 wählt Kombination rein zufällig (!= Spieler1);
- * 2.Durchlauf: Spieler2 wählt Kombination taktisch aus (Penney-Ante)
- * -->Anmerkung: für NUMBER_OF_COINS == 3 kein signifikanter Unterschied zwischen 1. und 2.,
- * da bei Dreierkombination die Penney-Ante Kombination häufig durch Zufall auftritt
- */
- do
- {
- //Ausgabe des aktuellen Spielmodus
- if (penney_ante_mode==false)
- {
- cout << ">> Penney-Ante disabled (1/2) <<";
- }
- else
- {
- cout << ">> Penney-Ante enabled (2/2) <<";
- }
- cout << endl << endl << endl << "Processing... " << "0%";
- //ein Schleifendurchlauf <=> ein Spiel
- for(games_counter = 0; games_counter < NUMBER_OF_GAMES; games_counter++)
- {
- //Prozentanzeige aktualisieren
- percentage(games_counter, NUMBER_OF_GAMES);
- //neues Spiel initialisieren
- initiate_new_game(results, player1, player2, NUMBER_OF_COINS, penney_ante_mode);
- //in jedem Schleifendurchlauf wird eine Münze geworfen, solange bis die Kombination einer der beiden Spieler geworfen wurde
- while(1)
- {
- //Kombination Spieler 1 überprüfen
- if ( compare(results, player1, NUMBER_OF_COINS) )
- {
- p1_wins++;
- break;
- }
- //Kombination Spieler 2 überprüfen
- if ( compare(results, player2, NUMBER_OF_COINS) )
- {
- p2_wins++;
- break;
- }
- //neuer Münzwurf
- results.erase(results.begin());
- results.push_back( rand()%2 );
- }
- }
- //Ausgabe Ergebnis
- cout << "\r \r";
- cout << "Player1 won " << 100.0 * p1_wins/(p1_wins+p2_wins) << "% of the games." << endl
- << "Player2 won " << 100.0 * p2_wins/(p1_wins+p2_wins) << "% of the games." << endl
- << endl
- << "Ratio Player1 : Player2 = " << (double) p1_wins/p2_wins << endl
- << endl
- << endl;
- cout << "Processing... 100% (successfully completed)" << endl
- << endl
- << "----------------------------------"
- << endl<< endl;
- //switch mode
- penney_ante_mode = !penney_ante_mode;
- //reset counter
- p1_wins = 0;
- p2_wins = 0;
- games_counter = 0;
- }
- while (penney_ante_mode == true); //(ACHTUNG: evtl. verwirrend, aber erst 2.Durchlauf ist im Penney-Ante Modus)
- cout << endl << "Retry? (y/n)" << endl;
- fflush(stdin);
- retry_char = cin.get();
- system("cls");
- }
- while (retry_char == 'y');
- return 0;
- }
- void percentage(int games_counter, int NUMBER_OF_GAMES)
- {
- //Bemerkung: Korrektur des Zählers um 1, da games_counter mit dem Wert 0 beginnt und sonst um 1 nach unten verschoben zu NUMBER_OF_GAMES zählen würde
- if ( (games_counter+1) % (NUMBER_OF_GAMES/100!=0 ? NUMBER_OF_GAMES/100:NUMBER_OF_GAMES) == 0)
- {
- //Zeile löschen und Prozentstand aktualisieren
- cout << "\r \r";
- cout << "Processing... " << 100*(games_counter+1)/NUMBER_OF_GAMES << "%";
- }
- return;
- }
- //Variablen für ein neues Spiel intialisieren
- void initiate_new_game(vector<bool> &results, bool player1[], bool player2[], int NUMBER_OF_COINS, bool penney_ante_mode)
- {
- //Player1 wählt Kombination und gleichzeitig werden die ersten N_O_C Münzwürfe durchgeführt und abgespeichert
- int i = 0;
- results.clear();
- for (i=0; i<NUMBER_OF_COINS; i++)
- {
- player1[i] = rand()%2;
- results.push_back( rand()%2 );
- }
- if (penney_ante_mode)
- {
- //Player 2 die Kombination in Abhängigkeit von player1 (Penney-Ante Methode)
- player2[0] = !(player1[1]);
- for (i=1; i<NUMBER_OF_COINS; i++)
- player2[i]=player1[i-1];
- }
- else
- {
- //zufällige Kombination für player2 generieren (!= player1)
- do
- {
- for (i=0; i<NUMBER_OF_COINS; i++)
- player2[i]=rand()%2;
- }
- while ( compliance(player1, player2, NUMBER_OF_COINS));
- }
- return;
- }
- //Gibt true zurück, wenn Kombinationen der Spieler übereinstimmen
- bool compliance(bool player1[], bool player2[], int NUMBER_OF_COINS)
- {
- int i = 0;
- for(i=0; i<NUMBER_OF_COINS; i++)
- {
- if (player1[i] != player2[i])
- return false;
- }
- return true;
- }
- //Vergleicht die Kombination eines Spielers mit der aktuellen zufälligen Kombination
- bool compare(vector<bool> results, bool player[], int NUMBER_OF_COINS)
- {
- typedef vector<bool>::iterator iterTyp;
- iterTyp iterator;
- int i = 0;
- int matches = 0;
- for(iterator=results.begin(); iterator<=results.end(); iterator++)
- {
- if (player[i++] == *iterator)
- matches++;
- }
- if (matches == NUMBER_OF_COINS)
- return true;
- else
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement