SHOW:
|
|
- or go back to the newest paste.
| 1 | //============================================================================ | |
| 2 | // Name : Penney-Ante Simulator v1.0 | |
| 3 | // Author : cosypanther | |
| 4 | // Version : 1.0 final | |
| 5 | // Copyright : written by cosypanther on 29.06.2012 | |
| 6 | - | // Description : small test tool for penny-ante |
| 6 | + | // Description : small test tool for penny-ante. for more information visit my blog: |
| 7 | // http://cosypanther.blogspot.de/2012/06/penney-ante-ein-kleines.html | |
| 8 | //============================================================================ | |
| 9 | ||
| 10 | #include <iostream> | |
| 11 | #include <stdlib.h> | |
| 12 | #include <time.h> | |
| 13 | #include <vector> | |
| 14 | #include <algorithm> | |
| 15 | ||
| 16 | using namespace std; | |
| 17 | ||
| 18 | //Funktionsdeklarationen | |
| 19 | void percentage(int games_counter, int NUMBER_OF_GAMES); | |
| 20 | void initiate_new_game(vector<bool> &results, bool player1[], bool player2[], int NUMBER_OF_COINS, bool penney_ante_mode); | |
| 21 | bool compliance(bool player1[], bool player2[], int NUMBER_OF_COINS); | |
| 22 | bool compare(vector<bool> results, bool player[], int NUMBER_OF_COINS); | |
| 23 | ||
| 24 | int main() | |
| 25 | {
| |
| 26 | //rand() initialisieren | |
| 27 | srand( time(NULL) ); | |
| 28 | ||
| 29 | //Queue für jeweils aktuelle Münzenkombination erstellen | |
| 30 | vector<bool> results; | |
| 31 | ||
| 32 | //für Schließen-/Erneutabfrage | |
| 33 | char retry_char = 'y'; | |
| 34 | ||
| 35 | do | |
| 36 | {
| |
| 37 | //Variablen | |
| 38 | int NUMBER_OF_COINS = 0; | |
| 39 | int NUMBER_OF_GAMES = 0; | |
| 40 | int games_counter = 0; | |
| 41 | int p1_wins = 0; | |
| 42 | int p2_wins = 0; | |
| 43 | bool penney_ante_mode = false; | |
| 44 | ||
| 45 | //Nutzereingabe | |
| 46 | cout << "----------------------------------" << endl | |
| 47 | << "Penney-Ante Simulation v1.0" << endl | |
| 48 | << "----------------------------------"<< endl | |
| 49 | << endl << endl; | |
| 50 | cout << "Enter the NUMBER OF COINS: "; | |
| 51 | cin >> NUMBER_OF_COINS; | |
| 52 | cout << "Enter the NUMBER OF GAMES: "; | |
| 53 | cin >> NUMBER_OF_GAMES; | |
| 54 | cout << endl << endl; | |
| 55 | ||
| 56 | //Arrays für Spielerkombinationen erstellen | |
| 57 | bool player1[NUMBER_OF_COINS]; | |
| 58 | bool player2[NUMBER_OF_COINS]; | |
| 59 | ||
| 60 | //Ausgabe | |
| 61 | cout << "----------------------------------" << endl | |
| 62 | << endl | |
| 63 | << "Results" << endl | |
| 64 | << endl | |
| 65 | << "----------------------------------" | |
| 66 | << endl << endl; | |
| 67 | ||
| 68 | /* | |
| 69 | * 1.Durchlauf: Spieler2 wählt Kombination rein zufällig (!= Spieler1); | |
| 70 | * 2.Durchlauf: Spieler2 wählt Kombination taktisch aus (Penney-Ante) | |
| 71 | * -->Anmerkung: für NUMBER_OF_COINS == 3 kein signifikanter Unterschied zwischen 1. und 2., | |
| 72 | * da bei Dreierkombination die Penney-Ante Kombination häufig durch Zufall auftritt | |
| 73 | */ | |
| 74 | do | |
| 75 | {
| |
| 76 | //Ausgabe des aktuellen Spielmodus | |
| 77 | if (penney_ante_mode==false) | |
| 78 | {
| |
| 79 | cout << ">> Penney-Ante disabled (1/2) <<"; | |
| 80 | ||
| 81 | } | |
| 82 | else | |
| 83 | {
| |
| 84 | cout << ">> Penney-Ante enabled (2/2) <<"; | |
| 85 | } | |
| 86 | cout << endl << endl << endl << "Processing... " << "0%"; | |
| 87 | ||
| 88 | //ein Schleifendurchlauf <=> ein Spiel | |
| 89 | for(games_counter = 0; games_counter < NUMBER_OF_GAMES; games_counter++) | |
| 90 | {
| |
| 91 | //Prozentanzeige aktualisieren | |
| 92 | percentage(games_counter, NUMBER_OF_GAMES); | |
| 93 | ||
| 94 | //neues Spiel initialisieren | |
| 95 | initiate_new_game(results, player1, player2, NUMBER_OF_COINS, penney_ante_mode); | |
| 96 | ||
| 97 | //in jedem Schleifendurchlauf wird eine Münze geworfen, solange bis die Kombination einer der beiden Spieler geworfen wurde | |
| 98 | while(1) | |
| 99 | {
| |
| 100 | //Kombination Spieler 1 überprüfen | |
| 101 | if ( compare(results, player1, NUMBER_OF_COINS) ) | |
| 102 | {
| |
| 103 | p1_wins++; | |
| 104 | break; | |
| 105 | } | |
| 106 | ||
| 107 | //Kombination Spieler 2 überprüfen | |
| 108 | if ( compare(results, player2, NUMBER_OF_COINS) ) | |
| 109 | {
| |
| 110 | p2_wins++; | |
| 111 | break; | |
| 112 | } | |
| 113 | ||
| 114 | //neuer Münzwurf | |
| 115 | results.erase(results.begin()); | |
| 116 | results.push_back( rand()%2 ); | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | //Ausgabe Ergebnis | |
| 121 | cout << "\r \r"; | |
| 122 | cout << "Player1 won " << 100.0 * p1_wins/(p1_wins+p2_wins) << "% of the games." << endl | |
| 123 | << "Player2 won " << 100.0 * p2_wins/(p1_wins+p2_wins) << "% of the games." << endl | |
| 124 | << endl | |
| 125 | << "Ratio Player1 : Player2 = " << (double) p1_wins/p2_wins << endl | |
| 126 | << endl | |
| 127 | << endl; | |
| 128 | cout << "Processing... 100% (successfully completed)" << endl | |
| 129 | << endl | |
| 130 | << "----------------------------------" | |
| 131 | << endl<< endl; | |
| 132 | ||
| 133 | //switch mode | |
| 134 | penney_ante_mode = !penney_ante_mode; | |
| 135 | ||
| 136 | //reset counter | |
| 137 | p1_wins = 0; | |
| 138 | p2_wins = 0; | |
| 139 | games_counter = 0; | |
| 140 | } | |
| 141 | while (penney_ante_mode == true); //(ACHTUNG: evtl. verwirrend, aber erst 2.Durchlauf ist im Penney-Ante Modus) | |
| 142 | ||
| 143 | cout << endl << "Retry? (y/n)" << endl; | |
| 144 | fflush(stdin); | |
| 145 | retry_char = cin.get(); | |
| 146 | system("cls");
| |
| 147 | } | |
| 148 | while (retry_char == 'y'); | |
| 149 | ||
| 150 | return 0; | |
| 151 | } | |
| 152 | ||
| 153 | void percentage(int games_counter, int NUMBER_OF_GAMES) | |
| 154 | {
| |
| 155 | //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 | |
| 156 | if ( ((games_counter+1) % (NUMBER_OF_GAMES/100)) == 0) | |
| 157 | {
| |
| 158 | //Zeile löschen und Prozentstand aktualisieren | |
| 159 | cout << "\r \r"; | |
| 160 | cout << "Processing... " << 100*(games_counter+1)/NUMBER_OF_GAMES << "%"; | |
| 161 | } | |
| 162 | return; | |
| 163 | } | |
| 164 | ||
| 165 | //Variablen für ein neues Spiel intialisieren | |
| 166 | void initiate_new_game(vector<bool> &results, bool player1[], bool player2[], int NUMBER_OF_COINS, bool penney_ante_mode) | |
| 167 | {
| |
| 168 | //Player1 wählt Kombination und gleichzeitig werden die ersten N_O_C Münzwürfe durchgeführt und abgespeichert | |
| 169 | int i = 0; | |
| 170 | ||
| 171 | results.clear(); | |
| 172 | for (i=0; i<NUMBER_OF_COINS; i++) | |
| 173 | {
| |
| 174 | player1[i] = rand()%2; | |
| 175 | results.push_back( rand()%2 ); | |
| 176 | } | |
| 177 | ||
| 178 | if (penney_ante_mode) | |
| 179 | {
| |
| 180 | //Player 2 die Kombination in Abhängigkeit von player1 (Penney-Ante Methode) | |
| 181 | player2[0] = !(player1[1]); | |
| 182 | for (i=1; i<NUMBER_OF_COINS; i++) | |
| 183 | player2[i]=player1[i-1]; | |
| 184 | } | |
| 185 | else | |
| 186 | {
| |
| 187 | //zufällige Kombination für player2 generieren (!= player1) | |
| 188 | do | |
| 189 | {
| |
| 190 | for (i=0; i<NUMBER_OF_COINS; i++) | |
| 191 | player2[i]=rand()%2; | |
| 192 | } | |
| 193 | while ( compliance(player1, player2, NUMBER_OF_COINS)); | |
| 194 | } | |
| 195 | return; | |
| 196 | } | |
| 197 | ||
| 198 | ||
| 199 | //Gibt true zurück, wenn Kombinationen der Spieler übereinstimmen | |
| 200 | bool compliance(bool player1[], bool player2[], int NUMBER_OF_COINS) | |
| 201 | {
| |
| 202 | int i = 0; | |
| 203 | ||
| 204 | for(i=0; i<NUMBER_OF_COINS; i++) | |
| 205 | {
| |
| 206 | if (player1[i] != player2[i]) | |
| 207 | return false; | |
| 208 | } | |
| 209 | return true; | |
| 210 | } | |
| 211 | ||
| 212 | ||
| 213 | //Vergleicht die Kombination eines Spielers mit der aktuellen zufälligen Kombination | |
| 214 | bool compare(vector<bool> results, bool player[], int NUMBER_OF_COINS) | |
| 215 | {
| |
| 216 | typedef vector<bool>::iterator iterTyp; | |
| 217 | iterTyp iterator; | |
| 218 | ||
| 219 | int i = 0; | |
| 220 | int matches = 0; | |
| 221 | ||
| 222 | for(iterator=results.begin(); iterator<=results.end(); iterator++) | |
| 223 | {
| |
| 224 | if (player[i++] == *iterator) | |
| 225 | matches++; | |
| 226 | } | |
| 227 | ||
| 228 | if (matches == NUMBER_OF_COINS) | |
| 229 | return true; | |
| 230 | else | |
| 231 | return false; | |
| 232 | } |