Skip to content

Commit b9825b5

Browse files
committed
small change in communicating ok
1 parent bb25f56 commit b9825b5

9 files changed

+73
-52
lines changed

client/go.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
#~bin
1+
#!bin/sh
2+
3+
INC="0"
4+
while [ $INC -lt $3 ]
5+
do
6+
# ./execlient 10.114.12.13 8888 &
7+
./execlient $1 $2 &
8+
INC=$[$INC + 1]
9+
done

client/includes/Ai.class.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
class Ai
99
{
1010
private:
11-
Map& _heatmap;
12-
Graph _graph;
11+
const Map& _heatmap;
12+
Graph _graph;
1313
Ai();
1414

1515
public:

client/includes/CommunicationSocket.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CommunicationSocket
5050
CommunicationSocket();
5151

5252
std::string read(void) const;
53-
void get_datagram(void);
53+
bool get_datagram(void);
5454
void disconnect();
5555
void get_peer(std::string, Map &) const;
5656
void get_peers(Map &map) const;
@@ -66,6 +66,7 @@ class CommunicationSocket
6666
void wait_for_game(void) const;
6767
void wait_for_move(void);
6868
bool get_datagram(Datagram & datagram);
69+
6970
};
7071

7172
#endif

client/lol.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ int main(__attribute__((unused)) int argc, __attribute__((unused)) char *argv[])
6565

6666
int main(int argc, char *argv[])
6767
{
68-
6968
if (argc != 3)
7069
return 1;
7170
Car car(argv[1], std::atoi(argv[2]));

client/srcs/Ai.class.cpp

+11-13
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ Position Ai::where_to(Position pos, Position dest, double &speed)
2424
static std::vector<int> path;
2525
static int i = 0;
2626

27-
std::cout << dest.x << " " << dest.y << std::endl;
28-
std::cout << "current : " << current << std::endl;
29-
3027
/* calculating best path */
3128
if (i == path.size())
3229
i = 0;
33-
if (i == 0)
30+
if (i == 0 && path.size() == 0)
3431
this->bfs(dest, pos, path);
35-
std::cout << "after bfs" << std::endl;
3632
// for (auto& n : _graph.getNode(current).adjacency_list)
3733
// {
3834
// if (color < 0 || _graph.nodes[n].color < color)
@@ -66,13 +62,18 @@ Position Ai::where_to(Position pos, Position dest, double &speed)
6662
// }
6763
// }
6864
// }
69-
std::cout << "path size : " << path.size() << std::endl;
65+
// std::cout << "path size : " << path.size() << std::endl;
7066
Position printcurrent;
7167
index_to_coor(current, printcurrent.x, printcurrent.y, _graph.getMapWidth());
72-
std::cout << "i => " << i << "; current (" << printcurrent.x << "," << printcurrent.y << ") ;";
68+
// std::cout << "i => " << i << "; current (" << printcurrent.x << "," << printcurrent.y << ") ;";
7369
index_to_coor(path[i], pos.x, pos.y, _graph.getMapWidth());
74-
std::cout << " next (" << pos.x << "," << pos.y << ") ;";
75-
speed = 1;
70+
// std::cout << " next (" << pos.x << "," << pos.y << ") ;";
71+
srand (time(NULL));
72+
// double test = 0.5 + (rand() % 5) / 10;
73+
speed = 0.5 + abs(_heatmap[pos.x][pos.y].total_cars) * 0.5;
74+
// speed = 1;
75+
std::cout << "_heatmap[pos.x][pos.y].total_cars : " << _heatmap[pos.x][pos.y].total_cars << std::endl;
76+
std::cout << " speed : " << speed << std::endl;
7677
i++;
7778
return (pos);
7879
}
@@ -96,14 +97,12 @@ void Ai::bfs(Position &dest, Position &start, std::vector<int> &path)
9697
/* launching the bfs from the end to get the next node */
9798
s = coord_to_index(dest.x, dest.y, width, height);
9899

99-
std::cout << "starting node : ( " << dest.x << "," << dest.y << ")" << s << std::endl;
100100
visited[s] = true;
101101
queue.push(s);
102102
(this->_graph.getNode(s)).color = level;
103103
while (!queue.empty())
104104
{
105105
s = queue.front();
106-
std::cout << "icibfs0" << std::endl;
107106
queue.pop();
108107
level = this->_graph.getNode(s).color;
109108
for (auto& n : (_graph.getNode(s)).adjacency_list)
@@ -140,9 +139,8 @@ void Ai::bfs(Position &dest, Position &start, std::vector<int> &path)
140139
}
141140
path.push_back(index);
142141
current = index;
143-
std::cout << "[" << index << "] => ";
142+
// std::cout << "[" << index << "] => ";
144143
}
145-
std::cout << "ending bfs : " << path.size() << std::endl;;
146144
// sleep(3);
147145
return ;
148146
}

client/srcs/Car.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Car::Car(const char* addr, int port) : _communicator(addr, port)
99
ss >> from.x >> from.y >> to.x >> to.y;
1010
_map[from.x][from.y].total_cars -= 1;
1111
_map[to.x][to.y].total_cars += 1;
12+
// std::cout << <<
1213
};
1314
_communicator.get_first_info(_map, _current_pos, _destination);
1415
_ai = new Ai(_map);

client/srcs/CommunicationSocket.cpp

+26-24
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,50 @@ std::string CommunicationSocket::read(void) const
5757
if ((ret = recv(this->_socket, buffer, BUFF_SIZE - 1, 0)) < 0)
5858
throw("CommunicationSocket:get_datagram(): recv error\n");
5959
buffer[ret] = 0;
60-
std::cout << "I have read " << ret << " bytes [" << buffer << "]\n";
60+
// std::cout << "I have read " << ret << " bytes [" << buffer << "]\n";
6161
return (std::string(buffer));
6262
}
6363

6464
/* the private get_datagram has to be rewritten depending on the format of a datagram */
6565

66-
void CommunicationSocket::get_datagram(void)
66+
67+
bool CommunicationSocket::get_datagram(void)
6768
{
6869
Datagram datagram;
6970
std::size_t spliter;
7071
std::string raw;
72+
bool hasok = false;
7173

7274
raw = this->read();
7375
std::stringstream ss(raw);
74-
7576
for (std::string line; std::getline(ss, line);)
7677
{
77-
std::cout << "raw_message : [" << line << "]" << std::endl;
7878
if ((spliter = line.find(" ")) == std::string::npos)
79-
throw(std::runtime_error("CommunicationSocket(): get_datagram line.find error"));
80-
std::cout << " spliter : " << spliter << std::endl;
79+
{
80+
std::cout << "CommunicationSocket(): get_datagram line.find error" << std::endl;
81+
return false;
82+
}
8183
datagram.setHeader(line.substr(0, spliter));
8284
datagram.setMessage(line.substr(spliter));
83-
std::cout << datagram.getHeader() << "| === |" << datagram.getMessage() << std::endl;
84-
try
85+
// std::cout << datagram.getHeader() << "| === |" << datagram.getMessage() << std::endl;
86+
if (datagram.getHeader() == "ok")
8587
{
86-
this->_datagram_stack.push(datagram);
88+
std::cout << KYEL << " OK " << KNRM << std::endl;
89+
hasok = true;
8790
}
88-
catch (std::exception &e)
91+
else
8992
{
90-
std::cout << "CommunicationSocket::getDatagram(): " << e.what() << std::endl;
93+
try
94+
{
95+
this->_datagram_stack.push(datagram);
96+
}
97+
catch (std::exception &e)
98+
{
99+
std::cout << "CommunicationSocket::getDatagram(): " << e.what() << std::endl;
100+
}
91101
}
92102
}
103+
return hasok;
93104
}
94105

95106
void CommunicationSocket::get_peer(std::string data, Map &map) const
@@ -150,7 +161,7 @@ void CommunicationSocket::send_datagram(std::string header, std::string message
150161
void CommunicationSocket::send_datagram(Datagram const & datagram) const
151162
{
152163
std::string data(datagram.getHeader() + datagram.getMessage());
153-
164+
// std::cout << "sending datagram : [" << data << "]" << std::endl;;
154165
if (send(this->_socket, data.c_str(), data.length(), 0) < 0)
155166
throw("CommunicationSocket:send_datagram(): send error\n");
156167
}
@@ -237,8 +248,6 @@ Map CommunicationSocket::get_first_info(Map &map, Position& start, Position &en
237248
ss >> header >> id >> x >> y;
238249
map[x][y].total_cars++;
239250
};
240-
241-
242251
while (!done)
243252
{
244253
raw = this->read();
@@ -292,7 +301,7 @@ bool CommunicationSocket::get_datagram(Datagram & datagram)
292301
{
293302
if (!this->_datagram_stack.empty())
294303
{
295-
this->_datagram_stack.top();
304+
datagram = this->_datagram_stack.top();
296305
this->_datagram_stack.pop();
297306
return (true);
298307
}
@@ -326,18 +335,11 @@ void CommunicationSocket::wait_for_move(void)
326335
FD_COPY(&_rfds, &copy);
327336

328337
/* Need to think about the structure, multithreaded or not */
329-
std::cout << "Entering move" << std::endl;
330-
while (!moved && select(_socket + 1, &_rfds, NULL, NULL, &_tv) >= 0)
338+
while (!moved && select(_socket + 1, &_rfds, NULL, NULL, NULL) >= 0)
331339
{
332340
if (FD_ISSET(_socket, &copy))
333341
{
334-
this->get_datagram();
335-
data = this->_datagram_stack.top();
336-
if (data.getHeader() == "ok")
337-
{
338-
moved = true;
339-
this->_datagram_stack.pop();
340-
}
342+
moved = this->get_datagram();
341343
FD_ZERO(&_rfds);
342344
FD_COPY(&copy, &_rfds);
343345
}

client/srcs/Graph.class.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ Graph::Graph(Map & map)
1111
_mapHeight = map[0].size();
1212
_vertexNumber = _mapHeight * _mapWidth;
1313

14-
std::cout << _mapWidth << " -- " << _mapHeight << std::endl;
15-
std::cout << "vertex number : " << _vertexNumber;
16-
1714
this->_nodes.resize(_vertexNumber);
1815
// this->_graph.resize(_vertexNumber);
1916

servertest.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import socket
22
import sys
33
import time
4+
import random
5+
6+
def choose():
7+
n = random.randint(0, 10)
8+
if n < 2:
9+
return 0
10+
return 1
411

512
def send_map(conn):
6-
conn.sendall("msz 10 5\n")
7-
conn.sendall("bct 0 0 1\n")
8-
conn.sendall("bct 0 1 0\n")
9-
conn.sendall("bct 1 1 0\n")
10-
conn.sendall("bct 1 0 0\n")
11-
conn.sendall("ppo 1 0\n")
12-
conn.sendall("des 0 1\n")
13+
conn.sendall("WELCOME\n")
14+
time.sleep(1)
15+
conn.sendall("msz 4 4\n")
16+
for i in range(4):
17+
for j in range(4):
18+
msg = "bct "
19+
msg += str(i) + " " + str(j) + " " + str(choose()) + "\n"
20+
conn.sendall(msg)
21+
22+
conn.sendall("ppo 0 0\n")
23+
conn.sendall("des 3 3\n")
24+
conn.sendall("done\n")
1325
time.sleep(1)
26+
conn.sendall("pnw 1 1\n")
27+
conn.sendall("pnw 0 0\n")
1428
conn.sendall("done\n")
29+
time.sleep(1)
1530
conn.sendall("start\n")
1631

1732
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

0 commit comments

Comments
 (0)