Skip to content
Snippets Groups Projects
Commit 692df17a authored by Tamas Bunth's avatar Tamas Bunth
Browse files

Add symbol constraint

parent d6dd84be
No related branches found
No related tags found
No related merge requests found
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <iostream>
#include "element.hxx" #include "element.hxx"
...@@ -42,3 +43,17 @@ bool Element::nextState() ...@@ -42,3 +43,17 @@ bool Element::nextState()
dir = Direction::Ordered; dir = Direction::Ordered;
return false; return false;
} }
bool Element::contains(Symbol smybol) const
{
return std::find(symbols_.begin(), symbols_.end(), smybol) != symbols_.end();
}
void Element::print() const
{
for(auto symbol : symbols_)
{
std::cout << symbol << ", ";
}
std::cout << std::endl;
}
...@@ -12,7 +12,6 @@ private: ...@@ -12,7 +12,6 @@ private:
Alignment align = Alignment::Horizontal; Alignment align = Alignment::Horizontal;
Direction dir = Direction::Ordered; Direction dir = Direction::Ordered;
std::vector<Symbol> symbols_; std::vector<Symbol> symbols_;
bool used_ = false;
public: public:
Element(std::vector<Symbol> symbols); Element(std::vector<Symbol> symbols);
Element(const Element&); Element(const Element&);
...@@ -24,6 +23,10 @@ public: ...@@ -24,6 +23,10 @@ public:
Alignment getAlign() const { return align; } Alignment getAlign() const { return align; }
Direction getDir() const { return dir; } Direction getDir() const { return dir; }
bool contains(Symbol symbol) const;
void print() const;
/** /**
* Sets the Element into a new state. State is determined by the alignment * Sets the Element into a new state. State is determined by the alignment
* and direction of the Element. * and direction of the Element.
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "utils.hxx" #include "utils.hxx"
#include "element.hxx" #include "element.hxx"
#include <cassert> #include <cassert>
#include <iostream>
Matrix::Matrix(std::size_t dim) Matrix::Matrix(std::size_t dim)
: board_{dim, std::vector<int>(dim, 0)} : board_{dim, std::vector<int>(dim, 0)}
...@@ -12,25 +13,71 @@ bool Matrix::put(const Element& element, const Position& pos) ...@@ -12,25 +13,71 @@ bool Matrix::put(const Element& element, const Position& pos)
Direction dir = element.getDir(); Direction dir = element.getDir();
Alignment align = element.getAlign(); Alignment align = element.getAlign();
// check if free // check out of bound
if((align == Alignment::Horizontal && pos.y + element.size() > size())
|| ( align == Alignment::Vertical && pos.x + element.size() > size()))
return false;
// check if position not free
for(std::size_t i=0; i< element.size(); ++i) for(std::size_t i=0; i< element.size(); ++i)
{ {
if(align == Alignment::Horizontal) if(align == Alignment::Horizontal)
{ {
if(pos.y+i >= size() )
return false;
if(board_.at(pos.x).at(pos.y+i) > 0) if(board_.at(pos.x).at(pos.y+i) > 0)
return false; return false;
} }
else // Alignment::Vertical else // Alignment::Vertical
{ {
if(pos.x+i >= size() )
return false;
if(board_.at(pos.x+i).at(pos.y) > 0) if(board_.at(pos.x+i).at(pos.y) > 0)
return false; return false;
} }
} }
// check symbol collision
if(align == Alignment::Horizontal)
{
std::size_t x = pos.x;
for(std::size_t i=0; i< size(); ++i)
if(element.contains(at(Position{x, i})))
return false;
if(dir == Direction::Ordered)
{
for(std::size_t i=0; i<size(); ++i)
for(std::size_t j=0; j<element.size(); j++)
if(element.get(j) == at(Position{i,j+pos.y}))
return false;
}
else
{
for(std::size_t i=0; i<size(); ++i)
for(std::size_t j=0; j<element.size(); j++)
if(element.getReversed(j) == at(Position{i,j+pos.y}))
return false;
}
}
else if(align == Alignment::Vertical)
{
std::size_t y = pos.y;
for(std::size_t i=0; i< size(); ++i)
if(element.contains(at(Position{i, y})))
return false;
if(dir == Direction::Ordered)
{
for(std::size_t i=0; i<size(); ++i)
for(std::size_t j=0; j<element.size(); j++)
if(element.get(j) == at(Position{j+pos.x,i}))
return false;
}
else
{
for(std::size_t i=0; i<size(); ++i)
for(std::size_t j=0; j<element.size(); j++)
if(element.getReversed(j) == at(Position{j+pos.x,i}))
return false;
}
}
// fill in // fill in
for(std::size_t i=0; i< element.size(); ++i) for(std::size_t i=0; i< element.size(); ++i)
{ {
...@@ -53,19 +100,44 @@ bool Matrix::put(const Element& element, const Position& pos) ...@@ -53,19 +100,44 @@ bool Matrix::put(const Element& element, const Position& pos)
return true; return true;
} }
bool Matrix::putFirst(const Element& elem) bool Matrix::putFirst(const Element& elem, Position& posIn)
{ {
for( std::size_t i=0; i< board_.size(); ++i) for( std::size_t i=0; i< board_.size(); ++i)
for( std::size_t j=0; j<board_.size(); ++j) for( std::size_t j=0; j<board_.size(); ++j)
{ {
Position pos{i,j}; Position pos{i,j};
if( isFree(pos) ) if( isFree(pos) )
{
posIn = pos;
return put(elem, pos); return put(elem, pos);
} }
}
assert(false); // matrix is full (solved) assert(false); // matrix is full (solved)
return false; return false;
} }
void Matrix::free(const Element& elem, const Position& pos)
{
Alignment align = elem.getAlign();
if(align == Alignment::Horizontal)
assert(pos.y + elem.size() <= size());
else
assert(pos.x + elem.size() <= size());
for(std::size_t i=0; i< elem.size(); ++i)
{
if(align == Alignment::Horizontal)
{
board_[pos.x][pos.y+i] = 0;
}
else // Alignment::Vertical
{
board_[pos.x + i][pos.y] = 0;
}
}
}
Symbol Matrix::at(const Position& pos) const Symbol Matrix::at(const Position& pos) const
{ {
return board_.at(pos.x).at(pos.y); return board_.at(pos.x).at(pos.y);
...@@ -75,3 +147,14 @@ bool Matrix::isFree(const Position& pos) ...@@ -75,3 +147,14 @@ bool Matrix::isFree(const Position& pos)
{ {
return at(pos) <= 0; return at(pos) <= 0;
} }
void Matrix::print() const
{
for(auto& row : board_)
{
for(auto symbol : row )
std::cout << symbol << " ";
std::cout << std::endl;
}
std::cout << "--------------------" << std::endl;
}
...@@ -16,11 +16,14 @@ protected: ...@@ -16,11 +16,14 @@ protected:
public: public:
Matrix(std::size_t dim); Matrix(std::size_t dim);
bool put(const Element& elem, const Position& pos); bool put(const Element& elem, const Position& pos);
bool putFirst(const Element& elem); bool putFirst(const Element& elem, Position& posIn);
void free(const Element& elem, const Position& pos);
bool isFree(const Position& pos); bool isFree(const Position& pos);
std::size_t size() const { return board_.size(); } std::size_t size() const { return board_.size(); }
bool isSolved() const { return solved; } bool isSolved() const { return solved; }
void print() const;
}; };
......
...@@ -18,8 +18,8 @@ void Processor::process(std::list<Element> list) ...@@ -18,8 +18,8 @@ void Processor::process(std::list<Element> list)
if(list.size() == 0) if(list.size() == 0)
{ {
// TODO do something with the matrix rMatrix_.print();
std::cout << "kesz" << std::endl; ready_ = true;
return; return;
} }
...@@ -29,20 +29,24 @@ void Processor::process(std::list<Element> list) ...@@ -29,20 +29,24 @@ void Processor::process(std::list<Element> list)
auto it = list.begin(); auto it = list.begin();
while( hasMoreState || it!= list.end() ) while( hasMoreState || it!= list.end() )
{ {
success = rMatrix_.putFirst(*it); Position pos{0,0};
success = rMatrix_.putFirst(*it, pos);
if(success) if(success)
{ {
// rMatrix_.print();
// copy everything except current // copy everything except current
std::list<Element> copy; std::list<Element> copy;
std::copy(list.begin(), it, std::back_inserter(copy)); std::copy(list.begin(), it, std::back_inserter(copy));
copy.insert(copy.end(), std::next(it), list.end()); copy.insert(copy.end(), std::next(it), list.end());
process(std::move(copy)); process(std::move(copy));
if(ready_)
it->print();
rMatrix_.free(*it, pos);
} }
else
{
hasMoreState = it->nextState(); hasMoreState = it->nextState();
if(!hasMoreState) if(!hasMoreState)
it++; it++;
} }
} }
}
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
class Processor class Processor
{ {
private: private:
bool ready_ = false;
Matrix& rMatrix_; Matrix& rMatrix_;
public: public:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment