-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWord.h
85 lines (61 loc) · 1.77 KB
/
Word.h
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
#ifndef WORD_H
#define WORD_H
#include <stdint.h>
#include <cassert>
#include "IAnimator.h"
class Word {
private:
uint8_t x;
uint8_t y;
uint8_t length;
public:
Word() : x(0), y(0), length(0) { }
Word(uint8_t x, uint8_t y, uint8_t length) : x(x), y(y), length(length) {
assert(length <= 11);
}
void show(IAnimator *animator) const;
void hide(IAnimator *animator) const;
bool operator == (const Word& w) const {
return x == w.x && y == w.y && length == w.length;
}
bool isOverlapping (const Word& w) const {
/* overlapping words must be on same row */
if (y != w.y)
return false;
/* THIS can start before W and run into it */
if (x <= w.x && x + length > w.x)
return true;
/* W can start before THIS and run into it */
if (w.x <= x && w.x + w.length > x)
return true;
return false;
}
bool isEmpty () const {
return length == 0;
}
const Word diff(const Word& w) const {
if (!isOverlapping(w))
return *this;
/* THIS starts *before* W (and runs into it); the *diff* are the letters before W
THIS
WWWW
^^
*/
if (x < w.x)
return Word(x, y, w.x - x);
/* THIS starts *after* W (and W runs into THIS); the *diff* is the part of THIS beyond W
WWWW
THIS
^^
If W is longer than THIS (considering the different starting points), then the result
is just empty.
WWWWWWWWWW
THIS
--> THIS completely re-used; i.e. yield empty
*/
if (w.x + w.length >= x + length)
return Word();
return Word(w.x + w.length, y, x + length - w.x - w.length);
}
};
#endif /* !WORD_H */