-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrambleBox.cpp
More file actions
65 lines (53 loc) · 1.86 KB
/
Copy pathScrambleBox.cpp
File metadata and controls
65 lines (53 loc) · 1.86 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
#include "ScrambleBox.h"
ScrambleBox::ScrambleBox() {
int termWidth = getmaxx(stdscr);
boxPtr = newwin(3, termWidth, 0, 0);
boxWidth = termWidth - 2;
box(boxPtr, 0, 0);
newScramble();
}
ScrambleBox::~ScrambleBox() {
delwin(boxPtr);
boxPtr = nullptr;
}
std::string ScrambleBox::makeScramble() {
std::string scramble = "", chosenMove;
char moveLetter, lastMoveLetter = 'X', lastLastMoveLetter = 'X';
unsigned counter = 0;
bool first = true;
while (counter < SCRAMBLE_LENGTH) {
// choose random move
chosenMove = ALL_MOVES[rand() % NUM_MOVES];
// face of the chosen move
moveLetter = chosenMove.at(0);
// avoid sequences like L L2 or U D2 U'
if (moveLetter != lastMoveLetter &&
!(OPPOSITE_MOVES.at(moveLetter) == lastMoveLetter &&
moveLetter == lastLastMoveLetter)) {
// don't add space before first move
if (first) {
first = false;
} else {
scramble += ' ';
}
scramble += chosenMove;
counter++;
lastLastMoveLetter = lastMoveLetter;
lastMoveLetter = moveLetter;
}
}
return scramble;
}
void ScrambleBox::newScramble() {
// set currentScramble attribute
currentScramble = makeScramble();
// pad scramble with spaces to cover previous scramble
unsigned spacesBefore = (boxWidth - currentScramble.length()) / 2;
unsigned spacesAfter = boxWidth - currentScramble.length() - spacesBefore;
std::string paddedScramble = std::string(spacesBefore, ' ') +
currentScramble +
std::string(spacesAfter, ' ');
mvwprintw(boxPtr, 1, 1, paddedScramble.c_str());
// TODO: should refresh here? Or leave that to the calling scope
wrefresh(boxPtr);
}