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

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
CC = g++
CFLAGS = -std=c++17 -Wall -Wdeprecated -Wextra -pedantic -g
all: program clean
program: element.o matrix.o processor.o program.o
$(CC) -o $@ $^
element.o: element.cxx element.hxx utils.hxx
$(CC) -c $(CFLAGS) $<
matrix.o: matrix.cxx matrix.hxx utils.hxx
$(CC) -c $(CFLAGS) $<
processor.o: processor.cxx processor.hxx utils.hxx
$(CC) -c $(CFLAGS) $<
program.o: program.cxx
$(CC) -c $(CFLAGS) $<
.PHONY: clean cleanest
clean:
rm *.o
cleanest: clean
rm octhecdec
#include <algorithm>
#include <cassert>
#include "element.hxx"
Element::Element(std::vector<Symbol> symbols)
:symbols_(symbols)
{
assert(symbols.size() > 0);
}
/**
* Do not copy alignment and orientation
*/
Element::Element(const Element& other)
: Element{other.symbols_} {}
Symbol Element::get(std::size_t pos) const
{
return symbols_.at(pos);
}
Symbol Element::getReversed(std::size_t pos) const
{
return symbols_.at(symbols_.size() - pos - 1);
}
bool Element::nextState()
{
if(align == Alignment::Horizontal)
{
align = Alignment::Vertical;
return true;
}
if(dir == Direction::Ordered)
{
dir = Direction::Reversed;
return true;
}
// set back initial state
align = Alignment::Horizontal;
dir = Direction::Ordered;
return false;
}
#ifndef _ELEMENT_HXX_
#define _ELEMENT_HXX_
#include <vector>
#include "utils.hxx"
using Symbol = int;
class Element
{
private:
Alignment align = Alignment::Horizontal;
Direction dir = Direction::Ordered;
std::vector<Symbol> symbols_;
bool used_ = false;
public:
Element(std::vector<Symbol> symbols);
Element(const Element&);
Symbol get(std::size_t pos) const;
Symbol getReversed(std::size_t pos) const;
std::size_t size() const { return symbols_.size();}
Alignment getAlign() const { return align; }
Direction getDir() const { return dir; }
/**
* Sets the Element into a new state. State is determined by the alignment
* and direction of the Element.
*
* Returns true, if there is a next state. If there are no more states, the
* funciton will set the initial state, and return false.
*
* The possible states are the following:
* 1. horizontal, ordered
* 2. vertical, ordered
* 3. vertical, reversed
* 4. vertical, ordered
*/
bool nextState();
};
#endif
#include "matrix.hxx"
#include "utils.hxx"
#include "element.hxx"
#include <cassert>
Matrix::Matrix(std::size_t dim)
: board_{dim, std::vector<int>(dim, 0)}
{ }
bool Matrix::put(const Element& element, const Position& pos)
{
Direction dir = element.getDir();
Alignment align = element.getAlign();
// check if free
for(std::size_t i=0; i< element.size(); ++i)
{
if(align == Alignment::Horizontal)
{
if(pos.y+i >= size() )
return false;
if(board_.at(pos.x).at(pos.y+i) > 0)
return false;
}
else // Alignment::Vertical
{
if(pos.x+i >= size() )
return false;
if(board_.at(pos.x+i).at(pos.y) > 0)
return false;
}
}
// fill in
for(std::size_t i=0; i< element.size(); ++i)
{
if(align == Alignment::Horizontal)
{
if( dir == Direction::Ordered )
board_[pos.x][pos.y+i] = element.get(i);
else
board_[pos.x][pos.y+i] = element.getReversed(i);
}
else // Alignment::Vertical
{
if( dir == Direction::Ordered )
board_[pos.x + i][pos.y] = element.get(i);
else
board_[pos.x + i][pos.y] = element.getReversed(i);
}
}
return true;
}
bool Matrix::putFirst(const Element& elem)
{
for( std::size_t i=0; i< board_.size(); ++i)
for( std::size_t j=0; j<board_.size(); ++j)
{
Position pos{i,j};
if( isFree(pos) )
return put(elem, pos);
}
assert(false); // matrix is full (solved)
return false;
}
Symbol Matrix::at(const Position& pos) const
{
return board_.at(pos.x).at(pos.y);
}
bool Matrix::isFree(const Position& pos)
{
return at(pos) <= 0;
}
#ifndef _MATRIX_HXX_
#define _MATRIX_HXX_
#include <vector>
#include "element.hxx"
#include "utils.hxx"
class Matrix
{
private:
std::vector<std::vector<Symbol>> board_;
bool solved = false;
protected:
Symbol at(const Position& pos) const;
public:
Matrix(std::size_t dim);
bool put(const Element& elem, const Position& pos);
bool putFirst(const Element& elem);
bool isFree(const Position& pos);
std::size_t size() const { return board_.size(); }
bool isSolved() const { return solved; }
};
#endif
#include "processor.hxx"
#include <iostream>
#include <algorithm>
Processor::Processor(Matrix& matrix)
: rMatrix_{matrix} {}
// Elemeket egy láncolt listába rakjuk.
// - Ha az elemet nem lehet behelyezni, akkor megváltoztatjuk az állapotát.
// + ha nincs több állapota, akkor kivesszük az elemet, és ÚJ elemet próbálok
// oda.
// * Ha nincs több elem, akkor kivesszük az azelőtti elemet, és új elemet
// próbálunk oda. (rekurzív gyanús, méghozzá ugyanazzal a listával, és
// egy indexxel, hogy épp hol tart az elempróba)
void Processor::process(std::list<Element> list)
{
if(list.size() == 0)
{
// TODO do something with the matrix
std::cout << "kesz" << std::endl;
return;
}
bool success = false;
bool hasMoreState = true;
auto it = list.begin();
while( hasMoreState || it!= list.end() )
{
success = rMatrix_.putFirst(*it);
if(success)
{
// copy everything except current
std::list<Element> copy;
std::copy(list.begin(), it, std::back_inserter(copy));
copy.insert(copy.end(), std::next(it), list.end());
process(std::move(copy));
}
else
{
hasMoreState = it->nextState();
if(!hasMoreState)
it++;
}
}
}
#ifndef _PROCESSOR_HXX_
#define _PROCESSOR_HXX_
#include <vector>
#include <list>
#include "element.hxx"
#include "matrix.hxx"
class Processor
{
private:
Matrix& rMatrix_;
public:
Processor(Matrix& matrix);
void process(std::list<Element> vec);
};
#endif
#include <list>
#include "processor.hxx"
#include "matrix.hxx"
int main()
{
Matrix matrix{5};
std::list<Element> els;
els.push_back(Element{ {1, 2} });
els.push_back(Element{ {3, 4, 5} });
els.push_back(Element{ {2, 5, 4} });
els.push_back(Element{ {5, 3} });
els.push_back(Element{ {3, 1, 2} });
els.push_back(Element{ {4, 1, 2} });
els.push_back(Element{ {1, 3, 5} });
els.push_back(Element{ {1, 4, 5} });
els.push_back(Element{ {3, 2, 4} });
Processor proci{matrix};
proci.process(els);
return 0;
}
#ifndef _UTILS_HXX_
#define _UTILS_HXX_
#include <array>
enum class Alignment
{
Horizontal,
Vertical
};
enum class Direction
{
Ordered,
Reversed,
};
struct Position
{
std::size_t x, y;
Position(std::size_t x, std::size_t y)
:x(x), y(y) {}
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment