-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.h
42 lines (37 loc) · 998 Bytes
/
action.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
#pragma once
#include <string>
#include "board.h"
class action {
public:
action(const action& act) : opcode(act) {}
action(const int& op = -1) : opcode(op) {}
operator int() const { return opcode; }
int apply(board& b) const {
if ((0b11 & opcode) == (opcode)) {
// player action (slide up, right, down, left)
return b.move(opcode);
} else if (b(opcode & 0x0f) == 0) {
// environment action (place a new tile)
b(opcode & 0x0f) = (opcode >> 4);
return 0;
}
return -1;
}
std::string name() const {
if ((0b11 & opcode) == (opcode)) {
std::string opname[] = { "up", "right", "down", "left" };
return "slide " + opname[opcode];
} else {
return "place " + std::to_string(opcode >> 4) + "-index at position " + std::to_string(opcode & 0x0f);
}
return "null";
}
static action move(const int& oper) {
return action(oper);
}
static action place(const int& tile, const int& pos) {
return action((tile << 4) | (pos));
}
private:
const int opcode;
};