-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
178 lines (150 loc) · 3.54 KB
/
player.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "player.h"
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
std::vector<std::string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
vector<move<point,point> > generate_neighbour(int id )
{
int direction_x[] = {0,0,1,-1,1,-1};
int direction_y[] = {1,-1,0,0,1,-1};
vector<move<point,point> > neighbours ;
//starting point should be ring
for (int i = 0 ; i < 5 ; i++)
{
//take a ring point and
pair<int , int > starting ;
if (id == 1)
{
starting = ring_p1.at(i);
}
else
{
starting = ring_p2.at(i);
}
move<point,point> new_move ;
new_move.start = starting ;
//check it is a valid ring ?
if (board::validRing(current)==true) //validring function tellls validity of rings
{
//then generate termination points
//traverse in 6 directions
for(int j = 0 ; j < 6 ; j++)
{
int marker_crossed = 0; // signifying marker has crossed or not
int new_x = starting.x + direction_x[j];
int new_y = starting.y + direction_y[j];
while(board::isValid(new_x,new_y)==true)
{
pair<int,int> current ;
current.x = new_x ;
current.y = new_y ;
if (isEmpty(new_x , new_y) == true )
{
new_move.end = current ;
neighbours.push_back(new_move);
if (marker_crossed == 1)
break ;
}
if (board::value(new_x,new_y) == 3 | board::value(new_x,new_y) == 4 )
break;
else if (board::value(new_x,new_y) == 0 | board::value(new_x,new_y) == 1 ) //gives value stored at that point
{
}
new_x += direction_x[j];
new_y += direction_y[j];
}
}
}
}
return neighbours ;
}
string generate_ring_place(int id , board b)
{
//iterate to all the positions
vector<pair<int,int> > positions ;
pair middle ;
middle.x = 0;
middle.y = 0;
int position
if (board::isEmpty(0,0))
{
positions.push_back(middle);
}
for(int i = 1 ; i <= 5 ; i++ )
{
int hexagon = i;
for (int j = 0 ; j < 6*i < j++)
{
int position = j ;
pair<int, int> current = board::convert(hexagon,position);
if (isEmpty(current.x,current.y)== true)
{
positions.push_back(current);
}
}
}
pair<int , int> best = board::best_ring_place(positions);
pair<int , int> hexa = board::to_hexagon(best.x,best.y);
//to_hexagon converts coordinates in hexagoal format ;
//generating string format of move ;
string final = "P " ;
final.append(to_string(hexa.x));
final.append(" ");
final.append(to_string(hexa.y));
return final;
}
void player::execute_move(int id ,string str)
{
//check the board
board::check_board();
vector<string> token = split(str,' ');
string move_type = token.at(0);
string hexagon = token.at(1);
string position = token.at(2);
pair start = board::convert(hexagon,position);
if (move_type == "P" )
{
if (board::isEmpty(start.x,start.y) )
{
board::place_ring(id , start.x , start.y);
}
else
{
cout << "non empty position" << endl ;
return ;
}
}
else if (move_type == "S" )
{
string next_step = token.at(3);
hexagon = token.at(4);
position = token.at(5);
pair end = board::convert(hexagon,position);
if (next_step == "M")
{
board::move_ring(id , start.x , start.y , end.x , end.y);
}
else
{
cout << "invalid move" << endl ;
}
}
else
{
cout << "invalid move " << endl ;
}
//check the board
board::check_board();
}