Skip to content

Commit

Permalink
Created solution working with c++17
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu authored and Ubuntu committed Oct 19, 2022
1 parent 9e8a902 commit 7552fd9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
project(Robots)

set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wconversion -Werror -O2 -std=gnu++20")
set(CMAKE_CXX_FLAGS "-O2 -std=c++17")

# in case of compiling with g++11.2 on students
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath -Wl,/opt/gcc-11.2/lib64")
Expand Down
4 changes: 2 additions & 2 deletions client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void initiate_connections(const ClientParameters &parameters) {
}

// Function executed by thread that reads from GUI and sends to server.
[[noreturn]] void *from_gui_to_server([[maybe_unused]] void *thread_data) {
[[noreturn]] void *from_gui_to_server(void *thread_data) {
while (true) {
uint8_t message_type = read_message_from_gui(data);
send_message_to_server(data, message_type);
Expand Down Expand Up @@ -61,4 +61,4 @@ int main(int argc, char *argv[]) {
CHECK_ERRNO(pthread_detach(from_gui_to_server_thread));

from_server_to_gui();
}
}
9 changes: 5 additions & 4 deletions client/messages/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static void findExplosions(ClientData &data, const Position &bomb_position) {
Position position = convertPosition(bomb_position);
for (uint16_t i = 0; i <= host_explosion_radius; i++) {
data.explosions.insert(convertPosition(position));
if (data.blocks.contains(convertPosition(position)) || position.y == ntohs(data.size_y) - 1) {
if (data.blocks.find(convertPosition(position)) != data.blocks.end() || position.y == ntohs(data.size_y) - 1) {
break;
}
position.y++;
Expand All @@ -185,7 +185,7 @@ static void findExplosions(ClientData &data, const Position &bomb_position) {
position = convertPosition(bomb_position);
for (uint16_t i = 0; i <= host_explosion_radius; i++) {
data.explosions.insert(convertPosition(position));
if (data.blocks.contains(convertPosition(position)) || position.x == ntohs(data.size_x) - 1) {
if (data.blocks.find(convertPosition(position)) != data.blocks.end()|| position.x == ntohs(data.size_x) - 1) {
break;
}
position.x++;
Expand All @@ -195,7 +195,7 @@ static void findExplosions(ClientData &data, const Position &bomb_position) {
position = convertPosition(bomb_position);
for (uint16_t i = 0; i <= host_explosion_radius; i++) {
data.explosions.insert(convertPosition(position));
if (data.blocks.contains(convertPosition(position)) || position.y == 0) {
if (data.blocks.find(convertPosition(position)) != data.blocks.end() || position.y == 0) {
break;
}
position.y--;
Expand All @@ -205,7 +205,7 @@ static void findExplosions(ClientData &data, const Position &bomb_position) {
position = convertPosition(bomb_position);
for (uint16_t i = 0; i <= host_explosion_radius; i++) {
data.explosions.insert(convertPosition(position));
if (data.blocks.contains(convertPosition(position)) || position.x == 0) {
if (data.blocks.find(convertPosition(position)) != data.blocks.end() || position.x == 0) {
break;
}
position.x--;
Expand Down Expand Up @@ -473,3 +473,4 @@ void send_message_to_gui(ClientData &data) {
send_game(data);
}
}

27 changes: 15 additions & 12 deletions server/messages/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static bool try_moving_player(const ServerParameters &parameters, ServerData &da
new_position.x--;
}

if (data.blocks.contains(new_position)) {
if (data.blocks.find(new_position) != data.blocks.end()) {
return false;
}

Expand Down Expand Up @@ -154,7 +154,7 @@ static void find_destroyed_up(const ServerParameters &parameters, ServerData &da
Position explosion_position = data.bombs[id].position;
for (uint16_t i = 0; i <= parameters.explosion_radius; i++) {
find_destroyed_robots(data, explosion_position);
if (data.blocks.contains(explosion_position)) {
if (data.blocks.find(explosion_position) != data.blocks.end()) {
data.blocks_destroyed.emplace(explosion_position);
break;
}
Expand All @@ -171,7 +171,7 @@ static void find_destroyed_right(const ServerParameters &parameters, ServerData
Position explosion_position = data.bombs[id].position;
for (uint16_t i = 0; i <= parameters.explosion_radius; i++) {
find_destroyed_robots(data, explosion_position);
if (data.blocks.contains(explosion_position)) {
if (data.blocks.find(explosion_position) != data.blocks.end()) {
data.blocks_destroyed.emplace(explosion_position);
break;
}
Expand All @@ -188,7 +188,7 @@ static void find_destroyed_down(const ServerParameters &parameters, ServerData &
Position explosion_position = data.bombs[id].position;
for (uint16_t i = 0; i <= parameters.explosion_radius; i++) {
find_destroyed_robots(data, explosion_position);
if (data.blocks.contains(explosion_position)) {
if (data.blocks.find(explosion_position) != data.blocks.end()) {
data.blocks_destroyed.emplace(explosion_position);
break;
}
Expand All @@ -205,7 +205,7 @@ static void find_destroyed_left(const ServerParameters &parameters, ServerData &
Position explosion_position = data.bombs[id].position;
for (uint16_t i = 0; i <= parameters.explosion_radius; i++) {
find_destroyed_robots(data, explosion_position);
if (data.blocks.contains(explosion_position)) {
if (data.blocks.find(explosion_position) != data.blocks.end()) {
data.blocks_destroyed.emplace(explosion_position);
break;
}
Expand Down Expand Up @@ -265,9 +265,11 @@ static void clear_exploded_bombs(ServerData &data) {
// Removes destroyed blocks from [data].
static void clear_destroyed_blocks(ServerData &data) {
Set<Position> survived_blocks;
std::set_difference(data.blocks.begin(), data.blocks.end(),
data.all_blocks_destroyed.begin(), data.all_blocks_destroyed.end(),
std::inserter(survived_blocks, survived_blocks.end()));
for (const auto block : data.blocks) {
if (data.all_blocks_destroyed.find(block) == data.all_blocks_destroyed.end()) {
survived_blocks.insert(block);
}
}
data.blocks = survived_blocks;
}

Expand Down Expand Up @@ -317,7 +319,7 @@ List<uint8_t> build_turn_0(const ServerParameters &parameters, ServerData &data)
// Place blocks.
for (uint16_t i = 0; i < parameters.initial_blocks; i++) {
Position position = get_random_position(parameters, data);
if (!data.blocks.contains(position)) {
if (data.blocks.find(position) == data.blocks.end()) {
spawn_block(data, position, events_message);
events++;
}
Expand All @@ -340,19 +342,19 @@ List<uint8_t> build_turn(const ServerParameters &parameters, ServerData &data) {

for (PlayerId id = 0; id < parameters.players_count; id++) {
uint8_t last_message = data.clients_last_messages[data.poll_ids[id]];
if (data.all_robots_destroyed.contains(id)) {
if (data.all_robots_destroyed.find(id) != data.all_robots_destroyed.end()) {
spawn_player(data, id, get_random_position(parameters, data), events_message);
data.scores[id]++;
events++;
}
else if (data.disconnected_players.contains(id)) {
else if (data.disconnected_players.find(id) != data.disconnected_players.end()) {
continue;
}
else if (last_message == PLACE_BOMB) {
spawn_bomb(data, data.player_positions[id], parameters.bomb_timer, events_message);
events++;
}
else if (last_message == PLACE_BLOCK && !data.blocks.contains(data.player_positions[id])) {
else if (last_message == PLACE_BLOCK && data.blocks.find(data.player_positions[id]) == data.blocks.end()) {
spawn_block(data, data.player_positions[id], events_message);
events++;
}
Expand Down Expand Up @@ -431,3 +433,4 @@ uint8_t read_move(Deque<uint8_t> &buffer) {
buffer.pop_front();
return (uint8_t) result;
}

0 comments on commit 7552fd9

Please sign in to comment.