From 9204e48fbcdf4e771fff6c61dd718a14845c57ba Mon Sep 17 00:00:00 2001 From: Trygve Date: Wed, 13 Mar 2024 00:06:47 +0100 Subject: [PATCH] 21 --- lab_3/21/Makefile | 21 +++++++++++++++ lab_3/21/queens.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++ lab_3/21/queens.hh | 48 +++++++++++++++++++++++++++++++++ lab_3/21/test.cpp | 21 +++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 lab_3/21/Makefile create mode 100644 lab_3/21/queens.cpp create mode 100644 lab_3/21/queens.hh create mode 100644 lab_3/21/test.cpp diff --git a/lab_3/21/Makefile b/lab_3/21/Makefile new file mode 100644 index 0000000..2c0c131 --- /dev/null +++ b/lab_3/21/Makefile @@ -0,0 +1,21 @@ +binary = queens +folder = queens +objects = $(patsubst %.cpp,%.o,$(wildcard *.cpp)) + +run: $(binary) + ./$(binary) + +$(binary): $(objects) + g++ -g3 -o $@ $^ + +%.o: %.cpp + g++ -g3 -c -o $@ $< + +clean: + rm -f *.o + +clear: clean + rm -f *.zip $(binary) + +zip: clean + zip $(folder) Makefile *.cpp *.h diff --git a/lab_3/21/queens.cpp b/lab_3/21/queens.cpp new file mode 100644 index 0000000..f577398 --- /dev/null +++ b/lab_3/21/queens.cpp @@ -0,0 +1,65 @@ +#include "queens.hh" +#include +#include +#include +#include + +using namespace queens; + +Configuration::Configuration(int m, int n, int number_of_pieces) { + this->number_of_pieces = number_of_pieces; + this->board = new Board; + + this->board->m = m; //x direction + this->board->n = n; //y direction + + //get a random number generator: + //the random device that will seed the generator + std::random_device seeder; + //then make a mersenne twister engine + std::mt19937 engine(seeder()); + //then the easy part... the distribution + std::uniform_int_distribution m_rand(1, m); + std::uniform_int_distribution n_rand(1, n); + + for (int i=0; ipieces.push_back(Queen(m_rand(engine), n_rand(engine))); + } +} + +std::vector Configuration::get_conflicts() +{ + std::vector conflicts; + for (int i = 0; inumber_of_pieces; i++) + { + + // The piece we will check against the others: + Piece* piece = &this->pieces[i]; + // For every new i we need to check one fewer i + for (int j = i+1; jpieces[j]; + if (piece->x == enemy_piece->x or + piece->y == enemy_piece->y or + piece->x - enemy_piece->x == piece->y - enemy_piece->x) + { + Conflict conflict; + conflict.piece_1 = i; + conflict.piece_1_x = piece->x; + conflict.piece_1_y = piece->y; + conflict.piece_2 = j; + conflict.piece_2_x = enemy_piece->x; + conflict.piece_2_y = enemy_piece->y; + conflicts.push_back(conflict); + } + } + } + return conflicts; +} + +Piece::Piece(int x, int y) +{ + this->x = x; + this->y = y; +} \ No newline at end of file diff --git a/lab_3/21/queens.hh b/lab_3/21/queens.hh new file mode 100644 index 0000000..8af8c96 --- /dev/null +++ b/lab_3/21/queens.hh @@ -0,0 +1,48 @@ +#include +#include + +namespace queens +{ + // Easy way represent a conflict: + struct Conflict + { + int piece_1; + int piece_1_x; + int piece_1_y; + int piece_2; + int piece_2_x; + int piece_2_y; + }; + class Piece + { + public: + int x; + int y; + + Piece(int x, int y); + }; + class Queen: public Piece + { + using Piece::Piece; + }; + + class Board + { + public: + int m; + int n; + }; + + class Configuration + { + public: + int number_of_pieces; + std::vector pieces; + Board* board; + + Configuration(int m, int n, int number_of_pieces); + std::vector get_conflicts(); + }; + + +} \ No newline at end of file diff --git a/lab_3/21/test.cpp b/lab_3/21/test.cpp new file mode 100644 index 0000000..54c082c --- /dev/null +++ b/lab_3/21/test.cpp @@ -0,0 +1,21 @@ +#include "queens.hh" +#include + +void print_conflict(queens::Conflict c) +{ + std::cout << "🚨 Conflict! "<< "Piece 1: x="<< c.piece_1_x << ", y=" << c.piece_1_y + << " Piece 2: x="<< c.piece_2_x << ", y=" << c.piece_2_y << "\n"; +} + +int main() +{ + queens::Configuration config = queens::Configuration(8, 8, 10); + std::vector conflicts = config.get_conflicts(); + for (int i=0; i