21
This commit is contained in:
		
							parent
							
								
									61e230c0a5
								
							
						
					
					
						commit
						9204e48fbc
					
				
							
								
								
									
										21
									
								
								lab_3/21/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								lab_3/21/Makefile
									
									
									
									
									
										Normal file
									
								
							| @ -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 | ||||
							
								
								
									
										65
									
								
								lab_3/21/queens.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								lab_3/21/queens.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,65 @@ | ||||
| #include "queens.hh" | ||||
| #include <cstdlib> | ||||
| #include <iostream> | ||||
| #include <vector> | ||||
| #include <random> | ||||
| 
 | ||||
| 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<int> m_rand(1, m); | ||||
|     std::uniform_int_distribution<int> n_rand(1, n); | ||||
| 
 | ||||
|     for (int i=0; i<number_of_pieces; i++) | ||||
|     { | ||||
|         this->pieces.push_back(Queen(m_rand(engine), n_rand(engine))); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| std::vector<Conflict> Configuration::get_conflicts() | ||||
| { | ||||
|     std::vector<Conflict> conflicts; | ||||
|     for (int i = 0; i<this->number_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; j<number_of_pieces; j++) | ||||
|         { | ||||
|             Piece* enemy_piece = &this->pieces[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; | ||||
| } | ||||
							
								
								
									
										48
									
								
								lab_3/21/queens.hh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								lab_3/21/queens.hh
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,48 @@ | ||||
| #include <array> | ||||
| #include <vector> | ||||
| 
 | ||||
| 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<Piece> pieces; | ||||
|             Board* board; | ||||
| 
 | ||||
|             Configuration(int m, int n, int number_of_pieces); | ||||
|             std::vector<Conflict> get_conflicts(); | ||||
|     }; | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										21
									
								
								lab_3/21/test.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								lab_3/21/test.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,21 @@ | ||||
| #include "queens.hh" | ||||
| #include <iostream> | ||||
| 
 | ||||
| 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<queens::Conflict> conflicts = config.get_conflicts(); | ||||
|     for (int i=0; i<conflicts.size(); i++) | ||||
|     { | ||||
|         print_conflict(conflicts[i]); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user