Repository navigation
Expand file tree
/
Copy pathoptimised.cpp
More file actions
271 lines (200 loc) · 5.01 KB
/
Copy pathoptimised.cpp
File metadata and controls
271 lines (200 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <ctime>
using namespace std;
// http://stackoverflow.com/a/20861692
namespace patch
{
template<typename T> std::string to_string( const T& n )
{
std::ostringstream stm;
stm << n;
return stm.str();
}
}
unsigned int PlayOneGame(string deck)
{
string players[2];
players[0] = deck.substr(0, 26);
players[1] = deck.substr(26, 26);
string pile = "";
unsigned int placeCount = 0;
signed short turn = 0;
const unsigned int infiniteFlag = 50000;
bool checkedInfiniteGame = false;
while(players[0] != "" && players[1] != "")
{
bool battleInProgress = false;
unsigned short debt = 1;
while(debt > 0)
{
if(players[turn] == "")
{
break;
}
char nextCard = players[turn].at(0);
players[turn] = players[turn].substr(1, players[turn].length());
pile += nextCard;
placeCount++;
if(!checkedInfiniteGame && placeCount >= infiniteFlag)
{
ofstream file;
file.open("POSSIBLE_INFINITE_GAME.txt");
file << deck;
file.close();
cout <<"POSSIBLE INFINITE GAME DETECTED!!! - SAVED DECK TO FILE"<< endl;
checkedInfiniteGame = true;
}
if(nextCard == '-')
{
if(battleInProgress)
{
debt--;
}
else
{
turn = ((turn == 1) ? 0 : 1);
}
}
else
{
battleInProgress = true;
switch(nextCard)
{
case 'A':
debt = 4;
break;
case 'K':
debt = 3;
break;
case 'Q':
debt = 2;
break;
case 'J':
debt = 1;
break;
default:
debt = 0;
break;
}
turn = ((turn == 1) ? 0 : 1);
}
}
turn = ((turn == 1) ? 0 : 1);
players[turn] += pile;
pile = "";
}
return placeCount;
}
string ShuffledDeck()
{
string cards = "------------------------------------AAAAJJJJKKKKQQQQ";
random_shuffle(cards.begin(), cards.end());
return cards;
}
void RandomGames()
{
// configurations:
const unsigned short safetyFileLimit = 10000;
const unsigned long log = 1000000;
const string directory = "random/";
unsigned long long cGamesPlayed = 0;
unsigned short cFilesCreated = 0;
unsigned int bestSoFar = 4000;
cout <<"Playing lots of shuffled decks to find the LONGEST game between 2 players."<< endl;
cout <<"Must do better than "<< bestSoFar <<" before solutions will be logged/saved."<< endl;
cout <<"A maximum of "<< safetyFileLimit <<" files can be created, at which point the search will halt."<< endl;
while(cFilesCreated < safetyFileLimit)
{
string deck = ShuffledDeck();
unsigned int gameLength = PlayOneGame(deck);
if((++cGamesPlayed % log) == 0)
{
cout <<"[LOG] "<< cGamesPlayed <<" games played. "<< cFilesCreated <<" files created."<< endl;
}
if(gameLength > bestSoFar)
{
string fileName = directory + patch::to_string(gameLength) +".txt";
ofstream file;
file.open(fileName.c_str());
file << deck;
file.close();
cout <<"[!!!] New solution discovered: "<< gameLength <<". "<< ++cFilesCreated <<" files created."<< endl;
bestSoFar = gameLength;
}
}
}
void Permutations()
{
// configurations:
const unsigned short safetyFileLimit = 10000;
const unsigned long log = 10000000;
const unsigned long save = 10000000;
const string directory = "permutations/";
unsigned long long cGamesPlayed = 0;
unsigned short cFilesCreated = 0;
unsigned int bestSoFar = 2000;
ifstream startFile("permutations/start.txt");
string deck = "";
getline(startFile, deck);
startFile.close();
cout <<"Permutations starting at:"<< endl << deck << endl;
bool playing = false;
long long i = 0;
while(next_permutation(deck.begin(), deck.end()) && cFilesCreated < safetyFileLimit)
{
if(playing)
{
unsigned int gameLength = PlayOneGame(deck);
cGamesPlayed++;
if(cGamesPlayed % log == 0)
{
cout <<"[LOG] "<< cGamesPlayed <<" games played. "<< cFilesCreated <<" files created."<< endl;
}
if(cGamesPlayed % save == 0)
{
cout <<"[LOG] Saving deck to file: "<< deck << endl;
ofstream file;
file.open("permutations/start.txt");
file << deck;
file.close();
}
if(gameLength > bestSoFar)
{
string fileName = directory + patch::to_string(gameLength) +".txt";
ofstream file;
file.open(fileName.c_str());
file << deck;
file.close();
cout <<"[!!!] New solution discovered: "<< gameLength <<". "<< ++cFilesCreated <<" files created."<< endl;
bestSoFar = gameLength;
}
}
else
{
i++;
if(i % 100000000 == 0)
{
cout <<"Saved ["<< i <<"] "<< deck << endl;
ofstream file;
file.open("permutations/start.txt");
file << deck;
file.close();
}
if(deck.at(25) == 'A' || deck.at(25) == 'K' || deck.at(25) == 'Q' || deck.at(25) == 'J')
{
playing = true;
cout <<"Simulations begin."<< endl;
}
}
}
}
int main()
{
srand(time(0));
Permutations();
return 0;
}