-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.cpp
106 lines (91 loc) · 2.28 KB
/
move.cpp
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
/*************************************************************
*
* Copyright (C) 2010 John O'Laughlin
*
* All rights reserved.
*
*************************************************************
*/
#include <assert.h>
#include <string.h>
#include <algorithm>
#include "move.h"
using namespace std;
Move::Move() {
_action = Pass;
_len = 0;
_score = 0;
_equity = -1000.0f;
}
Move::Move(uint len, uchar *tiles, float equity) {
_action = Exchange;
assert(len > 0 && len <= MAXIMUM_RACK_SIZE);
memcpy(_tiles, tiles, len);
_len = len;
_score = 0;
_equity = equity;
}
Move::Move(uint len, uchar *tiles, uchar *letters, uint row, uint col,
bool horiz, int score, float equity) {
_action = Place;
assert(len > 0 && len <= MAXIMUM_RACK_SIZE);
memcpy(_tiles, tiles, len);
memcpy(_letters, letters, len);
_row = row;
_col = col;
_horiz = horiz;
_len = len;
_score = score;
_equity = equity;
}
void Move::sortByEquity(vector<Move> *list) {
stable_sort(list->begin(), list->end(), equityComparator);
}
bool Move::equityComparator(const Move &move1, const Move &move2) {
return move1.equity() > move2.equity();
}
void Move::writePosition(ostream &o) {
if (_horiz) {
writeRow(o);
writeCol(o);
} else {
writeCol(o);
writeRow(o);
}
}
void Move::writeRow(ostream &o) {
uint displayRow = _row + 1;
o << displayRow;
}
void Move::writeCol(ostream &o) {
uchar displayCol = (uchar)(_col + 'a');
o << displayCol;
}
void Move::writeLetters(ostream &o) {
for (int i = 0; i < _len; ++i) writeLetter(o, i);
}
void Move::writeLetter(ostream &o, uint idx) {
char tile = _tiles[idx];
char letter = _letters[idx];
if (tile == BLANK) Util::writeBlankLetter(o, letter);
else Util::writeLetter(o, letter);
}
void Move::writeTiles(ostream &o) {
for (int i = 0; i < _len; ++i) writeTile(o, i);
}
void Move::writeTile(ostream &o, uint idx) {
Util::writeTile(o, _tiles[idx]);
}
ostream &operator<<(ostream &o, Move &move) {
if (move.isPass()) {
o << "Pass";
} else if (move.isExchange()) {
o << "Exch ";
move.writeTiles(o);
} else {
move.writePosition(o);
o << " ";
move.writeLetters(o);
}
return o;
}