-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
courses.tolstenko.net |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
subdirlist(activity_dir ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
foreach(subdir ${activity_dir}) | ||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${subdir}/CMakeLists.txt") | ||
add_subdirectory(${subdir}) | ||
endif() | ||
endforeach() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
file(GLOB adv_intro_src CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) | ||
|
||
enable_testing() | ||
|
||
add_executable(adv-01-intro ${adv_intro_src}) | ||
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake) | ||
target_include_directories(adv-01-intro PUBLIC ${DOCTEST_INCLUDE_DIR}) | ||
target_link_libraries(adv-01-intro doctest::doctest) | ||
doctest_discover_tests(adv-01-intro) | ||
|
||
if(ENABLE_TEST_COVERAGE) | ||
target_compile_options(adv-01-intro PUBLIC -O0 -g -fprofile-arcs -ftest-coverage) | ||
target_link_options(adv-01-intro PUBLIC -fprofile-arcs -ftest-coverage) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN | ||
#include <doctest/doctest.h> | ||
using namespace std; | ||
|
||
// create a function that allocates memory on the heap and returns a raw pointer to it | ||
char* allocateMemoryAndClear(int numBytes, char value) { | ||
// implement this function | ||
} | ||
|
||
// create a function that deallocates memory on the heap | ||
void deallocateMemory(char*& ptr) { | ||
// implement this function | ||
} | ||
|
||
|
||
// DO NOT CHANGE THE CODE BELOW THIS LINE | ||
TEST_CASE("allocateMemory") { | ||
char* ptr = allocateMemoryAndClear(3, 'u'); | ||
CHECK(ptr != nullptr); | ||
CHECK(ptr[0] == 'u'); | ||
CHECK(ptr[1] == 'u'); | ||
CHECK(ptr[2] == 'u'); | ||
deallocateMemory(ptr); | ||
CHECK(ptr == nullptr); | ||
} |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
subdirlist(advcpp_chapters ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
foreach(subdir ${advcpp_chapters}) | ||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${subdir}/CMakeLists.txt") | ||
add_subdirectory(${subdir}) | ||
endif() | ||
endforeach() |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
function(add_custom_test TEST_NAME TEST_EXECUTABLE TEST_INPUT_LIST TEST_EXPECTED_OUTPUT_LIST) | ||
list(LENGTH TEST_INPUT_LIST num_tests) | ||
|
||
MATH(EXPR num_tests "${num_tests} - 1") | ||
message(STATUS "Adding ${num_tests} tests for ${TEST_NAME}.") | ||
|
||
set(TEST_COMMANDS "") | ||
foreach(index RANGE 0 ${num_tests}) | ||
list(GET TEST_INPUT_LIST ${index} TEST_INPUT) | ||
list(GET TEST_EXPECTED_OUTPUT_LIST ${index} TEST_EXPECTED_OUTPUT) | ||
|
||
list(APPEND TEST_COMMANDS | ||
COMMAND ${CMAKE_COMMAND} -E echo "Running test: ${TEST_NAME}_${index}. Using input file: ${TEST_INPUT}" | ||
COMMAND ${CMAKE_COMMAND} -E cat ${TEST_INPUT} | ||
COMMAND ${CMAKE_COMMAND} -E echo "==================================" | ||
COMMAND ${CMAKE_COMMAND} -E echo "Expected Output from ${TEST_NAME}_${index}:" | ||
COMMAND ${CMAKE_COMMAND} -E cat ${TEST_EXPECTED_OUTPUT} | ||
COMMAND ${CMAKE_COMMAND} -E echo "==================================" | ||
COMMAND ${TEST_EXECUTABLE} < ${TEST_INPUT} > test_output_${index}.txt | ||
COMMAND ${CMAKE_COMMAND} -E echo "Actual Output from ${TEST_NAME}_${index}:" | ||
COMMAND ${CMAKE_COMMAND} -E cat test_output_${index}.txt | ||
COMMAND ${CMAKE_COMMAND} -E echo "==================================" | ||
COMMAND ${CMAKE_COMMAND} -E compare_files ${TEST_EXPECTED_OUTPUT} test_output_${index}.txt | ||
COMMAND ${CMAKE_COMMAND} -E echo "Test ${TEST_NAME}_${index} passed." | ||
) | ||
endforeach() | ||
|
||
add_custom_target(${TEST_NAME} | ||
${TEST_COMMANDS} | ||
DEPENDS ${TEST_EXECUTABLE} | ||
) | ||
endfunction() | ||
|
||
add_subdirectory(assignments/flocking) | ||
add_subdirectory(assignments/maze) | ||
add_subdirectory(assignments/life) | ||
add_subdirectory(assignments/rng) | ||
#add_subdirectory(assignments/catchthecat) |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef CAT_h | ||
#define CAT_h | ||
#include "IAgent.h" | ||
|
||
struct Cat : public IAgent { | ||
std::pair<int,int> move(const std::vector<bool>& world, std::pair<int,int> catPos, int sideSize ) override{ | ||
return {0,0}; // todo: change this | ||
} | ||
}; | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef CATCHER_H | ||
#define CATCHER_H | ||
#include "IAgent.h" | ||
|
||
struct Catcher : public IAgent { | ||
std::pair<int,int> move(const std::vector<bool>& world, std::pair<int,int> catPos, int sideSize ) override{ | ||
return {0,0}; // todo: change this | ||
} | ||
}; | ||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
add_executable(ai-flocking flocking.cpp) | ||
|
||
file(GLOB TEST_INPUT_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.in) | ||
file(GLOB TEST_OUTPUT_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.out) | ||
|
||
add_custom_test(ai-flocking-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/flocking "${TEST_INPUT_FILES}" "${TEST_OUTPUT_FILES}") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <vector> | ||
#include <utility> | ||
#include <cmath> | ||
|
||
using namespace std; | ||
|
||
struct Vector2 { | ||
double x=0, y=0; | ||
Vector2() : x(0), y(0){}; | ||
Vector2(double x, double y) : x(x), y(y){}; | ||
Vector2(const Vector2& v) = default; | ||
|
||
// unary operations | ||
Vector2 operator-() const { return {-x, -y}; } | ||
Vector2 operator+() const { return {x, y}; } | ||
|
||
// binary operations | ||
Vector2 operator-(const Vector2& rhs) const { return {x - rhs.x, y - rhs.y}; } | ||
Vector2 operator+(const Vector2& rhs) const { return {x + rhs.x, y + rhs.y}; } | ||
Vector2 operator*(const double& rhs) const { return {x * rhs, y * rhs}; } | ||
friend Vector2 operator*(const double& lhs, const Vector2& rhs) { return {lhs * rhs.x, lhs * rhs.y}; } | ||
Vector2 operator/(const double& rhs) const { return {x / rhs, y / rhs}; } | ||
Vector2 operator/(const Vector2& rhs) const { return {x / rhs.x, y / rhs.y}; } | ||
bool operator!=(const Vector2& rhs) const { return (*this - rhs).sqrMagnitude() >= 1.0e-6; }; | ||
bool operator==(const Vector2& rhs) const { return (*this - rhs).sqrMagnitude() < 1.0e-6; }; | ||
|
||
// assignment operation | ||
Vector2& operator=(Vector2 const& rhs) = default; | ||
Vector2& operator=(Vector2&& rhs) = default; | ||
|
||
// compound assignment operations | ||
Vector2& operator+=(const Vector2& rhs) { | ||
x += rhs.x; | ||
y += rhs.y; | ||
return *this; | ||
} | ||
Vector2& operator-=(const Vector2& rhs) { | ||
x -= rhs.x; | ||
y -= rhs.y; | ||
return *this; | ||
} | ||
Vector2& operator*=(const double& rhs) { | ||
x *= rhs; | ||
y *= rhs; | ||
return *this; | ||
} | ||
Vector2& operator/=(const double& rhs) { | ||
x /= rhs; | ||
y /= rhs; | ||
return *this; | ||
} | ||
Vector2& operator*=(const Vector2& rhs) { | ||
x *= rhs.x; | ||
y *= rhs.y; | ||
return *this; | ||
} | ||
Vector2& operator/=(const Vector2& rhs) { | ||
x /= rhs.x; | ||
y /= rhs.y; | ||
return *this; | ||
} | ||
|
||
double sqrMagnitude() const { return x * x + y * y; } | ||
double getMagnitude() const { return sqrt(sqrMagnitude()); } | ||
static double getMagnitude(const Vector2& vector) { return vector.getMagnitude(); } | ||
|
||
static double Distance(const Vector2& a, const Vector2& b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }; | ||
double Distance(const Vector2& b) const { return sqrt((x - b.x) * (x - b.x) + (y - b.y) * (y - b.y)); }; | ||
static double DistanceSquared(const Vector2& a, const Vector2& b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); }; | ||
double DistanceSquared(const Vector2& b) const { return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y); }; | ||
|
||
static Vector2 normalized(const Vector2& v) { return v.normalized(); }; | ||
Vector2 normalized() const { | ||
auto magnitude = getMagnitude(); | ||
|
||
// If the magnitude is not null | ||
if (magnitude > 0.) | ||
return Vector2(x, y) / magnitude; | ||
else | ||
return {x, y}; | ||
}; | ||
|
||
static const Vector2 zero; | ||
}; | ||
|
||
const Vector2 Vector2::zero = {0, 0}; | ||
|
||
struct Boid { | ||
Boid(const Vector2& pos, const Vector2& vel): position(pos), velocity(vel){}; | ||
Boid():position({0,0}), velocity({0,0}){}; | ||
Vector2 position; | ||
Vector2 velocity; | ||
}; | ||
|
||
struct Cohesion { | ||
double radius; | ||
double k; | ||
|
||
Cohesion() = default; | ||
|
||
Vector2 ComputeForce(const vector<Boid>& boids, int boidAgentIndex) { | ||
return {}; | ||
} | ||
}; | ||
|
||
struct Alignment { | ||
double radius; | ||
double k; | ||
|
||
Alignment() = default; | ||
|
||
Vector2 ComputeForce(const vector<Boid>& boids, int boidAgentIndex) { | ||
return {}; | ||
} | ||
}; | ||
|
||
struct Separation { | ||
double radius; | ||
double k; | ||
double maxForce; | ||
|
||
Separation() = default; | ||
|
||
Vector2 ComputeForce(const vector<Boid>& boids, int boidAgentIndex) { | ||
return {}; | ||
} | ||
}; | ||
|
||
// feel free to edit this main function to meet your needs | ||
int main() { | ||
// Variable declaration | ||
Separation separation{}; | ||
Alignment alignment{}; | ||
Cohesion cohesion{}; | ||
int numberOfBoids; | ||
string line; // for reading until EOF | ||
vector<Boid> currentState, newState; | ||
// Input Reading | ||
cin >> cohesion.radius >> separation.radius >> separation.maxForce >> alignment.radius >> cohesion.k >> separation.k >> alignment.k >> numberOfBoids; | ||
for (int i = 0; i < numberOfBoids; i++) { | ||
Boid b; | ||
cin >> b.position.x >> b.position.y >> b.velocity.x >> b.velocity.y; | ||
//cout << "b.y: " << b.y << endl; | ||
currentState.push_back(b); | ||
newState.push_back(b); | ||
} | ||
cin.ignore(256, '\n'); | ||
// Final input reading and processing | ||
// todo: edit this. probably my code will be different than yours. | ||
while (getline(cin, line)) { // game loop | ||
// Use double buffer! you should read from the current and store changes in the new state. | ||
currentState = newState; | ||
double deltaT = stod(line); | ||
// a vector of the sum of forces for each boid. | ||
vector<Vector2> allForces = vector<Vector2>(numberOfBoids, {0, 0}); | ||
// Compute Forces | ||
for (int i = 0; i < numberOfBoids; i++) // for every boid | ||
{ | ||
for (int j = 0; j < numberOfBoids; j++) // for every boid combination. Pre-processing loop. | ||
{ | ||
// Process Cohesion Forces | ||
auto dist = (currentState[i].position-currentState[j].position).getMagnitude(); | ||
if (i != j && dist <= cohesion.radius) { | ||
allForces[i] += cohesion.ComputeForce(currentState, i); | ||
} | ||
// Process Separation Forces | ||
if (i != j && dist <= separation.radius) { | ||
allForces[i] += separation.ComputeForce(currentState, i); | ||
} | ||
// Process Alignment Forces | ||
if (i != j && dist <= alignment.radius) { | ||
allForces[i] += alignment.ComputeForce(currentState, i); | ||
} | ||
} | ||
} | ||
// Tick Time and Output | ||
// todo: edit this. probably my code will be different than yours. | ||
cout << fixed << setprecision(3); // set 3 decimal places precision for output | ||
for (int i = 0; i < numberOfBoids; i++) // for every boid | ||
{ | ||
newState[i].velocity += allForces[i] * deltaT; | ||
newState[i].position += currentState[i].velocity * deltaT; | ||
cout << newState[i].position.x << " " << newState[i].position.y << " " | ||
<< newState[i].velocity.x << " " << newState[i].velocity.y << endl; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1.000 0.000 0.000 0.000 1.000 0.000 0.000 2 | ||
0.000 0.500 0.000 0.000 | ||
0.000 -0.500 0.000 0.000 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0.000 0.484 0.000 -0.125 | ||
0.000 -0.484 0.000 0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.000 1.000 1.000 0.000 0.000 2.000 0.000 2 | ||
0.000 0.500 0.000 0.000 | ||
0.000 -0.500 0.000 0.000 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0.000 0.531 0.000 0.250 | ||
0.000 -0.531 0.000 -0.250 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.000 0.000 0.000 2.000 0.000 0.000 2.000 2 | ||
0.000 0.500 3.000 0.000 | ||
0.000 -0.500 -2.000 0.000 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0.391 0.500 3.125 0.000 | ||
-0.234 -0.500 -1.875 0.000 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1.000 1.000 1.000 0.000 3.000 2.000 0.000 2 | ||
0.000 0.500 0.000 0.000 | ||
0.000 -0.500 0.000 0.000 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0.000 0.484 0.000 -0.125 | ||
0.000 -0.484 0.000 0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
0.000 2.000 1.000 1.000 0.000 2.000 3.000 2 | ||
0.000 0.500 -2.000 -1.000 | ||
0.000 -0.500 2.000 3.000 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-0.250 0.453 -2.000 -0.375 | ||
0.250 -0.109 2.000 3.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1.000 0.000 0.000 1.000 1.000 0.000 2.000 2 | ||
0.000 0.500 -1.000 2.000 | ||
0.000 -0.500 3.000 1.000 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-0.094 0.781 -0.750 2.250 | ||
0.406 -0.312 3.250 1.500 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
1.000 2.000 1.000 1.000 1.000 2.000 3.000 2 | ||
0.000 0.500 -2.000 -1.000 | ||
0.000 -0.500 2.000 3.000 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-0.250 0.438 -2.000 -0.500 | ||
0.250 -0.094 2.000 3.250 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1.000 2.000 1.000 0.000 1.000 2.000 0.000 2 | ||
0.000 0.500 -2.000 -1.000 | ||
0.000 -0.500 2.000 3.000 | ||
0.125 | ||
0.125 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-0.250 0.391 -2.000 -0.875 | ||
0.250 -0.141 2.000 2.875 | ||
-0.514 0.295 -2.114 -0.765 | ||
0.514 0.205 2.114 2.765 |