-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.cc
115 lines (101 loc) · 2.46 KB
/
helper.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* helper.cc
*
* Created on: 24-10-2012
* Author: Krzysztof Pobiarżyn
*/
#include "includes/helper.h"
#include "includes/types.h"
#include "includes/game_state.h"
#include <sstream>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <stdio.h>
namespace helper{
BITBOARD shift(int i) {
return (1 << i);
}
/*
* WYDRUK PLANSZY - DO DEBUGOWANIA
*/
std::string printBoard(GameState *state) {
std::stringstream stream;
stream << "board:";
for (int i = 63; i >= 0; i--) {
if (i % 8 == 7 && i != 63) {
stream << "\n";
}
if (i % 2 == 0
&& ((i >= 0 && i < 8) || (i >= 16 && i < 24)
|| (i >= 32 && i < 40) || (i >= 48 && i < 56))) {
if ((helper::shift(i / 2) & state->whites()) != 0) {
stream << i / 2 << "WH ";
if (i / 2 < 10)
stream << " ";
} else if ((helper::shift(i / 2) & state->blacks()) != 0) {
stream << i / 2 << "BL ";
if (i / 2 < 10)
stream << " ";
} else {
stream << "---- ";
}
} else if (i % 2 == 1
&& ((i >= 8 && i < 16) || (i >= 24 && i < 32)
|| (i >= 40 && i < 48) || (i >= 56 && i < 64))) {
if ((helper::shift(i / 2) & state->whites()) != 0) {
stream << i / 2 << "WH ";
if (i / 2 < 10)
stream << " ";
} else if ((helper::shift(i / 2) & state->blacks()) != 0) {
stream << i / 2 << "BL ";
if (i / 2 < 10)
stream << " ";
} else {
stream << "---- ";
}
} else {
stream << "---- ";
}
}
stream << "\n";
return (stream.str());
}
BITBOARD reverse(BITBOARD bitboard) {
BITBOARD result = 0;
for(int i = 0; i<32; i++)
if((bitboard & (1 << (31-i))))
result += (1 << i);
return (result);
}
std::vector<std::string> args2vector(std::string player){
using namespace std;
string arg;
vector<string> tokens;
size_t sep_pos = player.find_first_of(",");
while(sep_pos != string::npos){
arg = player.substr(0, sep_pos);
tokens.push_back(arg);
player = player.substr(sep_pos+1, string::npos);
sep_pos = player.find_first_of(",");
}
tokens.push_back(player);
return tokens;
}
void bitboard2stream(std::stringstream &stream, const uint32_t bitboard){
for(int i=0; i<32; i++)
if(bitboard & (1<<i))
stream << i << ",";
}
std::string message2stringArgs(const std::string message){
size_t sepPos = message.find_first_of(":");
return (message.substr(sepPos + 1));
}
void logmsg(const char* pMsg){
fprintf(stdout,"logmsg: %s\n",pMsg);
}
void errormsg(const char* pMsg){
fprintf(stderr,"logerr: %s\n",pMsg);
}
}