-
Notifications
You must be signed in to change notification settings - Fork 2
/
move.cc
92 lines (80 loc) · 1.55 KB
/
move.cc
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
#include "move.h"
#include "board.h"
#include <vector>
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
namespace checkers
{
Move::Move() : begun(false),time(0) {}
Move::Move(const std::vector<unsigned int>& m) : begun(false),time(0),move(m){}
Move::Move(const std::string& str) : begun(false), time(0)
{
std::string::const_iterator It = str.begin();
int i=0;
std::string tmpstr;
while( It != str.end()) {
if(*It == '-') {
add(static_cast<unsigned int>(pow(2.0, atof(tmpstr.c_str())-1)));
tmpstr = "";
i++;
It++;
continue;
}
tmpstr += *It;
It++;
}
add(static_cast<unsigned int>(pow(2.0, atof(tmpstr.c_str())-1)));
}
Move::~Move(){}
int Move::validate(Board& board)
{
full = move;
return board.validateMove(full);
}
void Move::add(unsigned int to)
{
move.push_back(to);
}
void Move::clear()
{
move.clear();
}
unsigned int Move::first() const
{
return move.front();
}
bool Move::makeNext(Board& board)
{
unsigned int tmp;
if(!begun) {
iter = full.begin();
begun = true;
}
if(iter == full.end())
return false;
tmp = *iter;
iter++;
if(iter == full.end())
return false;
board.move(tmp, *iter);
return iter != full.end();
}
const Move& Move::operator=(const std::vector<unsigned int>& m)
{
move = m;
full.clear();
begun = false;
return *this;
}
std::ostream& operator<<(std::ostream& os, const Move& move)
{
for(size_t i = 0; i<move.move.size(); i++)
{
if(i > 0) os << "-";
os << log2(move.move[i]) + 1;
}
return os;
}
}