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