Skip to content

Commit ef383e3

Browse files
committed
Now uses stringstreams
1 parent 3ca6ae9 commit ef383e3

4 files changed

Lines changed: 74 additions & 66 deletions

File tree

src/NxNMoveGen.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ This code is here to deprecate the original Scrambles.hpp and stringCleanup.hpp
33
*/
44
#pragma once
55

6-
#include <string>
6+
#include <sstream>
77
#include <random>
88

99
inline std::random_device rng;
@@ -81,7 +81,7 @@ inline void createMove(puzzle_move &newMove, const char moveType)
8181
}
8282
}
8383

84-
inline std::string getRepresentation(const puzzle_move *pMove)
84+
static inline std::string getRepresentation(const puzzle_move *pMove)
8585
{
8686
std::string finalMove;
8787
finalMove += pMove->base;
@@ -107,3 +107,8 @@ inline std::string getRepresentation(const puzzle_move *pMove)
107107

108108
return finalMove;
109109
}
110+
111+
std::ostringstream& operator<< (std::ostringstream& oss, const puzzle_move& pMove){
112+
oss << getRepresentation(&pMove);
113+
return oss;
114+
}

src/Scrambles.hpp

Lines changed: 66 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ You need a minimum of 2 references to a move struct.
5656

5757
static std::string Two_By_Two()
5858
{
59-
std::string scramble;
59+
std::ostringstream scramble;
6060
puzzle_move Move{};
6161
puzzle_move PrevMove{};
6262
const int moveCount = getRandomNum(9, 13);
@@ -71,15 +71,15 @@ static std::string Two_By_Two()
7171
createMove(Move, '2');
7272
} while (!canUseMove(&PrevMove, &Move));
7373

74-
scramble += getRepresentation(&Move) + ' ';
74+
scramble << Move << ' ';
7575
PrevMove = Move; // Don't forget this!! :DDDDDDDDDD
7676
}
77-
return scramble;
77+
return scramble.str();
7878
}
7979

8080
static std::string Three_By_Three(const bool blind)
8181
{
82-
std::string scramble;
82+
std::ostringstream scramble;
8383
puzzle_move Move{};
8484
puzzle_move PrevMove{};
8585
puzzle_move TwoPrevMove{};
@@ -102,7 +102,7 @@ static std::string Three_By_Three(const bool blind)
102102
createMove(Move, '3');
103103
} while (!canUseMove(&TwoPrevMove, &PrevMove, &Move));
104104

105-
scramble += getRepresentation(&Move) + ' ';
105+
scramble << Move << ' ';
106106
TwoPrevMove = PrevMove;
107107
PrevMove = Move; //
108108
}
@@ -117,12 +117,12 @@ static std::string Three_By_Three(const bool blind)
117117
} while (!canUseMove(&TwoPrevMove, &PrevMove, &Move));
118118

119119
Move.wsize = '2';
120-
scramble += getRepresentation(&Move) + ' ';
120+
scramble << Move << ' ';
121121
TwoPrevMove = PrevMove;
122122
PrevMove = Move;
123123
}
124124
}
125-
return scramble;
125+
return scramble.str();
126126
}
127127

128128
static std::string FMC()
@@ -135,6 +135,8 @@ static std::string FMC()
135135
const puzzle_move U_Prime{'U', '\'', ' '};
136136
const puzzle_move F{'F', ' ', ' '};
137137

138+
// This is unchanged because for some reason c++ doesn't like it when I do scramble << R_Prime << ' '.
139+
// No it's not because I'm using a std::string instead of a std::ostringstream.
138140
scramble += getRepresentation(&R_Prime) + ' ' + getRepresentation(&U_Prime) + ' ' + getRepresentation(&F) + ' ';
139141

140142
puzzle_move Move = F;
@@ -171,7 +173,7 @@ static std::string FMC()
171173

172174
static std::string Four_By_Four(bool blind)
173175
{
174-
std::string scramble;
176+
std::ostringstream scramble;
175177
puzzle_move Move{};
176178
puzzle_move PrevMove{};
177179
puzzle_move TwoPrevMove{};
@@ -195,7 +197,7 @@ static std::string Four_By_Four(bool blind)
195197
createMove(Move, '4');
196198
} while (!canUseMove(&TwoPrevMove, &PrevMove, &Move));
197199

198-
scramble += getRepresentation(&Move) + " ";
200+
scramble << Move << " ";
199201
TwoPrevMove = PrevMove;
200202
PrevMove = Move; //
201203
}
@@ -204,25 +206,25 @@ static std::string Four_By_Four(bool blind)
204206
{
205207
for (int k = 0; k < cubeRotateCount; k++)
206208
{
207-
scramble += ('x' + k);
209+
scramble << (char)('x' + k);
208210
int dir = getRandomNum(1, 3);
209211
if (dir == 2)
210212
{
211-
scramble += '2';
213+
scramble << '2';
212214
}
213215
else if (dir == 3)
214216
{
215-
scramble += '\'';
217+
scramble << '\'';
216218
}
217-
scramble += ' ';
219+
scramble << ' ';
218220
}
219221
}
220-
return scramble;
222+
return scramble.str();
221223
}
222224

223225
static std::string Five_By_Five(bool blind)
224226
{
225-
std::string scramble;
227+
std::ostringstream scramble;
226228
puzzle_move Move{};
227229
puzzle_move PrevMove{};
228230
puzzle_move TwoPrevMove{};
@@ -245,7 +247,7 @@ static std::string Five_By_Five(bool blind)
245247
createMove(Move, '5');
246248
} while (!canUseMove(&TwoPrevMove, &PrevMove, &Move));
247249

248-
scramble += getRepresentation(&Move) + " ";
250+
scramble << Move << " ";
249251
TwoPrevMove = PrevMove;
250252
PrevMove = Move; //
251253
}
@@ -260,17 +262,17 @@ static std::string Five_By_Five(bool blind)
260262
} while (!canUseMove(&TwoPrevMove, &PrevMove, &Move));
261263

262264
Move.wsize = '3';
263-
scramble += getRepresentation(&Move) + ' ';
265+
scramble << Move << ' ';
264266
TwoPrevMove = PrevMove;
265267
PrevMove = Move;
266268
}
267269
}
268-
return scramble;
270+
return scramble.str();
269271
}
270272

271273
static std::string Six_By_Six()
272274
{
273-
std::string scramble;
275+
std::ostringstream scramble;
274276
puzzle_move Move{};
275277
puzzle_move PrevMove{};
276278
puzzle_move TwoPrevMove{};
@@ -293,16 +295,16 @@ static std::string Six_By_Six()
293295
createMove(Move, '6');
294296
} while (!canUseMove(&TwoPrevMove, &PrevMove, &Move));
295297

296-
scramble += getRepresentation(&Move) + " ";
298+
scramble << Move << " ";
297299
TwoPrevMove = PrevMove;
298300
PrevMove = Move; //
299301
}
300-
return scramble;
302+
return scramble.str();
301303
}
302304

303305
static std::string Seven_By_Seven()
304306
{
305-
std::string scramble;
307+
std::ostringstream scramble;
306308
puzzle_move Move{};
307309
puzzle_move PrevMove{};
308310
puzzle_move TwoPrevMove{};
@@ -325,16 +327,16 @@ static std::string Seven_By_Seven()
325327
createMove(Move, '7');
326328
} while (!canUseMove(&TwoPrevMove, &PrevMove, &Move));
327329

328-
scramble += getRepresentation(&Move) + " ";
330+
scramble << Move << " ";
329331
TwoPrevMove = PrevMove;
330332
PrevMove = Move; //
331333
}
332-
return scramble;
334+
return scramble.str();
333335
}
334336

335337
static std::string Skewb()
336338
{
337-
std::string scramble;
339+
std::ostringstream scramble;
338340
puzzle_move Move{};
339341
puzzle_move PrevMove{};
340342
const int moveCount = getRandomNum(9, 13);
@@ -349,60 +351,60 @@ static std::string Skewb()
349351
createMove(Move, 'S');
350352
} while (!canUseMove(&PrevMove, &Move));
351353

352-
scramble += getRepresentation(&Move) + ' ';
354+
scramble << Move << ' ';
353355
PrevMove = Move; // Don't forget this!! :DDDDDDDDDD
354356
}
355-
return scramble;
357+
return scramble.str();
356358
}
357359

358360
static std::string Megaminx()
359361
{
360362
bool Dpp;
361-
std::string scramble;
363+
std::ostringstream scramble;
362364

363365
for (int i = 0; i < 5; i++)
364366
{
365367
for (int n = 0; n < 7; n++)
366368
{
367-
scramble += "R";
369+
scramble << "R";
368370
if (getRandomNum(0, 1) == 0)
369371
{
370-
scramble += "++ ";
372+
scramble << "++ ";
371373
}
372374
else
373375
{
374-
scramble += "-- ";
376+
scramble << "-- ";
375377
}
376378

377-
scramble += "D";
379+
scramble << "D";
378380
if (getRandomNum(0, 1) == 0)
379381
{
380-
scramble += "++ ";
382+
scramble << "++ ";
381383
Dpp = true;
382384
}
383385
else
384386
{
385-
scramble += "-- ";
387+
scramble << "-- ";
386388
Dpp = false;
387389
}
388390
}
389391
if (Dpp)
390392
{
391-
scramble += "U \n";
393+
scramble << "U \n";
392394
}
393395
else
394396
{
395-
scramble += "U' \n";
397+
scramble << "U' \n";
396398
}
397399
}
398-
return scramble;
400+
return scramble.str();
399401
}
400402

401403
static std::string Pyraminx()
402404
{
403405
constexpr char tips[] = {'u', 'l', 'r', 'b'};
404-
std::string scramble;
405-
scramble += Skewb();
406+
std::ostringstream scramble;
407+
scramble << Skewb();
406408

407409
for (int i = 0; i < 3; i++)
408410
{
@@ -412,54 +414,55 @@ static std::string Pyraminx()
412414
}
413415
else if (direction == 2)
414416
{
415-
scramble += tips[i];
416-
scramble += " ";
417+
scramble << tips[i];
418+
scramble << " ";
417419
}
418420
else if (direction == 3)
419421
{
420-
scramble += tips[i];
421-
scramble += "' ";
422+
scramble << tips[i];
423+
scramble << "' ";
422424
}
423425
}
424-
return scramble;
426+
return scramble.str();
425427
}
426428

427429
static std::string Clock()
428430
{
429431
const std::string hours[] = {"5-", "4-", "3-", "2-", "1-", "0+", "1+", "2+", "3+", "4+", "5+", "6+"};
430-
std::string scramble = "UR" + hours[getRandomNum(0, 11)] + " ";
431-
scramble += "DR" + hours[getRandomNum(0, 11)] + " ";
432-
scramble += "DL" + hours[getRandomNum(0, 11)] + " ";
433-
scramble += "UL" + hours[getRandomNum(0, 11)] + " ";
434-
scramble += "U" + hours[getRandomNum(0, 11)] + " ";
435-
scramble += "R" + hours[getRandomNum(0, 11)] + " ";
436-
scramble += "D" + hours[getRandomNum(0, 11)] + " ";
437-
scramble += "L" + hours[getRandomNum(0, 11)] + " ";
438-
scramble += "ALL" + hours[getRandomNum(0, 11)] + " " + "y2" + " ";
439-
scramble += "U" + hours[getRandomNum(0, 11)] + " ";
440-
scramble += "R" + hours[getRandomNum(0, 11)] + " ";
441-
scramble += "D" + hours[getRandomNum(0, 11)] + " ";
442-
scramble += "L" + hours[getRandomNum(0, 11)] + " ";
443-
scramble += "ALL" + hours[getRandomNum(0, 11)] + " ";
432+
std::ostringstream scramble;
433+
scramble << "UR" << hours[getRandomNum(0, 11)] << " "
434+
<< "DR" << hours[getRandomNum(0, 11)] << " "
435+
<< "DL" << hours[getRandomNum(0, 11)] << " "
436+
<< "UL" << hours[getRandomNum(0, 11)] << " "
437+
<< "U" << hours[getRandomNum(0, 11)] << " "
438+
<< "R" << hours[getRandomNum(0, 11)] << " "
439+
<< "D" << hours[getRandomNum(0, 11)] << " "
440+
<< "L" << hours[getRandomNum(0, 11)] << " "
441+
<< "ALL" << hours[getRandomNum(0, 11)] << " " << "y2" << " "
442+
<< "U" << hours[getRandomNum(0, 11)] << " "
443+
<< "R" << hours[getRandomNum(0, 11)] << " "
444+
<< "D" << hours[getRandomNum(0, 11)] << " "
445+
<< "L" << hours[getRandomNum(0, 11)] << " "
446+
<< "ALL" << hours[getRandomNum(0, 11)] << " ";
444447

445448
if (getRandomNum(0, 1) == 1)
446449
{
447-
scramble += "UR ";
450+
scramble << "UR ";
448451
}
449452
if (getRandomNum(0, 1) == 1)
450453
{
451-
scramble += "DR ";
454+
scramble << "DR ";
452455
}
453456
if (getRandomNum(0, 1) == 1)
454457
{
455-
scramble += "DL ";
458+
scramble << "DL ";
456459
}
457460
if (getRandomNum(0, 1) == 1)
458461
{
459-
scramble += "UL ";
462+
scramble << "UL ";
460463
}
461464

462-
return scramble;
465+
return scramble.str();
463466
}
464467

465468
// Use this one

src/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "cmdLineParser.hpp"
22
#include "Scrambles.hpp"
33
#include "fileIO.hpp"
4-
#include "outputting.hpp"
54

65
int main(int argc, char const *argv[])
76
{

src/outputting.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,5 @@ void outputVersion()
180180
std::cout << "Scramble outputs ⅓ of the screen + avg to right" << std::endl;
181181
std::cout << "Moved the outputting code to another file." << std::endl;
182182
std::cout << "Fixed a bug if something was of length 0." << std::endl;
183+
std::cout << "Used std::stringstream instead of std::string" << std::endl;
183184
}

0 commit comments

Comments
 (0)