Skip to content

Commit

Permalink
Merge pull request #3 from sat2707/master
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
Flya authored Oct 1, 2017
2 parents eca809e + e4bc96e commit f0e9f8d
Show file tree
Hide file tree
Showing 23 changed files with 18,329 additions and 27 deletions.
45 changes: 45 additions & 0 deletions baseline/purecpp14_client/strategy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "strategy.h"
#include <iostream>

std::ostream & operator << (std::ostream &s, const Elevator &e)
{
s << "Elevator(id=" << e.id << ")";
return s;
}

std::ostream & operator << (std::ostream &s, const Passenger &p)
{
s << "Passenger(id=" << p.id << ")";
return s;
}

void Strategy::on_tick(const std::vector<Elevator>& myElevators,
const std::vector<Passenger>& myPassengers,
const std::vector<Elevator>& enemyElevators,
const std::vector<Passenger>& enemyPassengers)
{
for (auto& elevator : myElevators)
{
for (auto& passenger : myPassengers)
{
if (passenger.state < 5)
{
if (elevator.state != 1)
{
elevator.go_to_floor(passenger.from_floor);
}
if (elevator.floor == passenger.from_floor)
{
passenger.set_elevator(elevator);
}
}
}
if(elevator.passengers.size() > 0 && elevator.state != 1)
{
elevator.go_to_floor(elevator.passengers[0].dest_floor);
}
}

log() << "Hello " << myElevators[0];
}

17 changes: 17 additions & 0 deletions baseline/purecpp14_client/strategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef STRATEGY_H
#define STRATEGY_H

#include <vector>
#include "base_strategy.h"
#include "api.h"

class Strategy : public BaseStrategy
{
public:
void on_tick(const std::vector<Elevator>& myElevators,
const std::vector<Passenger>& myPassengers,
const std::vector<Elevator>& enemyElevators,
const std::vector<Passenger>& enemyPassengers);
};

#endif
10 changes: 10 additions & 0 deletions clients/cpp14_client/client/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.6)
project(cppclient)

SET(CMAKE_CXX_FLAGS "-D_LINUX -g -std=c++14 -g -Wall -Wno-unknown-pragmas")

FILE(GLOB sources core/*.cpp csimplesocket/*.cpp csimplesocket/*.h)
add_executable(cppclient main.cpp ${sources})

install(TARGETS cppclient RUNTIME DESTINATION bin)

25 changes: 25 additions & 0 deletions clients/cpp14_client/client/compile-g++14.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
set COMPILER_PATH=

if "%GCC6_HOME%" neq "" (
if exist "%GCC6_HOME%\bin\g++.exe" (
set COMPILER_PATH="%GCC6_HOME%\bin\"
)
)

SetLocal EnableDelayedExpansion EnableExtensions

set FILES=

for %%i in (*.cpp) do (
set FILES=!FILES! %%i
)

for %%i in (core\*.cpp) do (
set FILES=!FILES! %%i
)

for %%i in (csimplesocket\*.cpp) do (
set FILES=!FILES! %%i
)

"%COMPILER_PATH:"=%g++.exe" -std=c++14 -static -fno-optimize-sibling-calls -fno-strict-aliasing -DWIN32 -lm -s -x c++ -Wl,--stack=268435456 -O2 -Wall -Wno-unknown-pragmas -o MyStrategy.exe!FILES! -lws2_32 -lwsock32 2>compilation.log
18 changes: 18 additions & 0 deletions clients/cpp14_client/client/compile-g++14.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
files=""

for i in core/*.cpp
do
files="$files $i"
done

for i in ./*.cpp
do
files="$files $i"
done

for i in csimplesocket/*.cpp
do
files="$files $i"
done

g++ -std=c++14 -static -fno-optimize-sibling-calls -fno-strict-aliasing -D_LINUX -lm -s -x c++ -O2 -Wall -Wno-unknown-pragmas -o MyStrategy.out $files 2>compilation.log
39 changes: 39 additions & 0 deletions clients/cpp14_client/client/compile-vscpp.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@echo off

set VS_VERSION=

if "%VS_VERSION%" == "" (
if "%VS140COMNTOOLS%" neq "" (
call "%VS140COMNTOOLS%\vsvars32.bat"
call "%VS140COMNTOOLS%\..\..\VC\bin\vcvars32.bat"
set VS_VERSION="140"
echo Compiling with Visual Studio 2015
)
)

if "%VS_VERSION%" == "" (
echo No Visual Studio installation has been found
exit 1
)

echo ***

del /F /Q MyStrategy.exe

SetLocal EnableDelayedExpansion EnableExtensions

set FILES=

for %%i in (*.cpp) do (
set FILES=!FILES! %%i
)

for %%i in (core\*.cpp) do (
set FILES=!FILES! %%i
)

for %%i in (csimplesocket\*.cpp) do (
set FILES=!FILES! %%i
)

cl /FeMyStrategy.exe /W3 /F268435456 /EHsc /O2 /DWIN32 /D_WINSOCK_DEPRECATED_NO_WARNINGS ws2_32.lib!FILES! 1>compilation.log
76 changes: 76 additions & 0 deletions clients/cpp14_client/client/core/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef API_H
#define API_H

#include <string>
#include <vector>

class Elevator;

class Passenger {
public:
int id; // идентификатор
double x, y; // координаты
double weight;
int from_floor, dest_floor; // этаж появления и цели
int time_to_away;
std::string type;
int floor;
int elevator; // идентификатор назначенного лифта
int state;

bool has_elevator() const
{
return elevator != -1;
}

int get_last_set_elevator_id() const
{
if (!set_elevator_ids.empty())
return *set_elevator_ids.rbegin();
return -1;
}

void set_elevator(const Elevator& elevator) const;
private:
mutable std::vector<int> set_elevator_ids;
friend class Client;
};

class Elevator {
public:
int id;
double y;
std::vector<Passenger> passengers;
double speed;
int time_on_floor;
int next_floor;
std::string type;
int floor;
int state;

int get_last_go_to_floor() const
{
if (!go_to_floor_floors.empty())
return *go_to_floor_floors.rbegin();
return -1;
}

public:
void go_to_floor(int floor) const;

private:
mutable std::vector<int> go_to_floor_floors;
friend class Client;
};

inline void Passenger::set_elevator(const Elevator &elevator) const
{
set_elevator_ids.push_back(elevator.id);
}

inline void Elevator::go_to_floor(int floor) const
{
go_to_floor_floors.push_back(floor);
}

#endif // API_H
29 changes: 29 additions & 0 deletions clients/cpp14_client/client/core/base_strategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef BASE_STRATEGY_H
#define BASE_STRATEGY_H

#include <vector>
#include <string>
#include <sstream>
#include <utility>

class BaseStrategy
{
private:
std::vector<std::pair<bool/*is exception*/, std::stringstream>> logs;
friend class Client;
public:

std::stringstream &log()
{
logs.push_back(std::make_pair(false, std::stringstream()));
return logs.rbegin()->second;
}

std::stringstream & exception()
{
logs.push_back(std::make_pair(true, std::stringstream()));
return logs.rbegin()->second;
}
};

#endif // BASE_STRATEGY_H
Loading

0 comments on commit f0e9f8d

Please sign in to comment.