Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ endif ()
# Source Files
include_directories(src)
add_library(puzzles_lib OBJECT
src/common/twobitstorage.cpp
src/common/file_reader.cpp
src/cpic/data/easy.cpp
src/cpic/data/trivial.cpp
src/cpic/model/board.cpp
Expand All @@ -46,6 +46,11 @@ add_library(puzzles_lib OBJECT
src/cpic/solver/brute_force_board_solver.cpp
src/cpic/solver/heuristic_board_solver.cpp
src/cpic/view/board_logger.cpp
src/hangman/game.cpp
src/hangman/player/alpha_order_player.cpp
src/hangman/player/frequency_aware_player.cpp
src/hangman/player/random_player.cpp
src/hangman/word_repository.cpp
src/shurikens/data/easy.cpp
src/shurikens/data/real.cpp
src/shurikens/data/trivial.cpp
Expand All @@ -61,6 +66,9 @@ add_library(puzzles_lib OBJECT
src/sudoku/view/board_logger.cpp
)

# Assets
file(COPY assets DESTINATION .)

# Executable
add_executable(puzzles src/main.cpp $<TARGET_OBJECTS:puzzles_lib>)

Expand All @@ -76,8 +84,8 @@ add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})
add_subdirectory(libs/googletest)

set(test_sources
tests/common/arbitrary_container_test.cpp
tests/common/numbers_test.cpp
tests/common/twobitstorage_test.cpp
tests/cpic/model/cpic_board_builder_test.cpp
tests/cpic/model/cpic_board_solver_test.cpp
tests/cpic/model/cpic_board_state_test.cpp
Expand Down
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ clean:
check: build/debug/Makefile
cmake --build build/debug --target check -- --no-print-directory

# Debug targets
debug: build/debug/Makefile
cmake --build build/debug --target puzzles -- --no-print-directory

debug_all: build/debug/Makefile
cmake --build build/debug -- --no-print-directory

release: build/release/Makefile
cmake --build build/release --target puzzles -- --no-print-directory

run: debug
./build/debug/puzzles

# TODO Find a better name than "full"
run_full: debug
./build/debug/puzzles full

.PHONY: all clean check debug debug_all release run
# Release targets
release: build/release/Makefile
cmake --build build/release --target puzzles -- --no-print-directory

run_release: release
./build/release/puzzles

.PHONY: all clean check debug debug_all release run run_release

# Specific file targets
build/debug/Makefile: CMakeLists.txt
Expand Down
2 changes: 2 additions & 0 deletions assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
words.txt

158 changes: 158 additions & 0 deletions src/common/arbitrary_container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright (c) 2019 Emanuel Machado da Silva
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#pragma once

#include <cassert>
#include <cmath>
#include <sys/types.h>
#include <vector>

namespace Puzzles {

typedef size_t value_t;

static constexpr inline size_t calculateValueBitLength(size_t value) {
assert(value > 0);
assert(value <= 0b11111111); // No real reason to stop there, I just haven't tested any further

auto digits = 0;
size_t next = 1;
while (value > 0) {
digits++;

if (next >= value) break;

value = value - next;
next = next * 2;
}

return digits;
}

template <value_t MAX_VALUE> struct ArbitraryContainer {

const size_t valueBitLength = calculateValueBitLength(MAX_VALUE);

// Constructors
ArbitraryContainer() = default;
ArbitraryContainer(const ArbitraryContainer<MAX_VALUE>&) = default;
explicit ArbitraryContainer(size_t count) : internal(count * valueBitLength, 0) { }

ArbitraryContainer(std::initializer_list<value_t> values) {
reserve(values.size());
for (auto value : values) {
push(value);
}
}

// Capacity
inline void reserve(size_t new_cap) { internal.reserve(new_cap * valueBitLength); }
inline void shrink_to_fit() { internal.shrink_to_fit(); }

// Insertion
inline void push(value_t value) {
assert(value <= MAX_VALUE);

std::vector<bool> results;
while (value > 0) {
results.push_back(value % 2);
value = value / 2;
}

for (size_t i = 0; i < (valueBitLength - results.size()); ++i) {
internal.push_back(0);
}

for (auto it = results.rbegin(); it != results.rend(); ++it) {
internal.push_back(*it);
}
}

// Retrieval
inline value_t at(size_t index) const {
assert(index < size());

value_t result = 0;
auto start = index * valueBitLength;

for (size_t i = 0; i < valueBitLength; ++i) {
if (internal[start + i]) {
result += std::pow(2, valueBitLength - 1 - i);
}
}

return result;
}
inline value_t operator[](size_t i) const { return at(i); }
inline size_t size() const { return internal.size() / valueBitLength; }

// Iterators
struct const_iterator {

const_iterator(const ArbitraryContainer<MAX_VALUE> *container, size_t position)
: container(container), position(position) {}

inline bool operator!=(const const_iterator &o) const { return container != o.container || position != o.position; }

inline const_iterator *operator++() {
position++;
return this;
}

inline value_t operator*() const {
assert(position < container->size());
return container->at(position);
}

private:
const ArbitraryContainer<MAX_VALUE> *container;
size_t position;
};

inline const_iterator begin() const { return const_iterator(this, 0); }
inline const_iterator end() const { return const_iterator(this, size()); }

// Operators
inline ArbitraryContainer<MAX_VALUE> &operator=(const ArbitraryContainer<MAX_VALUE> &other) {
internal = other.internal;
return *this;
}

inline bool operator<(const ArbitraryContainer<MAX_VALUE> &o) const {
if (size() != o.size()) {
return size() < o.size();
}

auto size = internal.size();
for (size_t i = 0; i < size; ++i) {
if (internal[i] != o.internal[i]) {
return internal[i] < o.internal[i];
}
}
return false;
}

private:
std::vector<bool> internal;
};
}
43 changes: 43 additions & 0 deletions src/common/collections.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2020 Emanuel Machado da Silva
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#pragma once

#include <queue>

namespace Puzzles {

template <typename T>
struct queue_inserter {
explicit queue_inserter(std::queue<T> &queue) : queue(queue) {}

queue_inserter operator++() { return *this; }
queue_inserter operator*() { return *this; }
queue_inserter &operator=(const char value) {
queue.push(value);
return *this;
}

private:
std::queue<T> &queue;
};
}
41 changes: 41 additions & 0 deletions src/common/file_reader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2020 Emanuel Machado da Silva
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "file_reader.h"

using namespace Puzzles;

FileReader::FileReader(const std::string &filename) {
input.open(filename);
}

FileReader::~FileReader() {
input.close();
}

std::istream *FileReader::istream() {
if (input) {
return &input;
} else {
return nullptr;
}
}
40 changes: 40 additions & 0 deletions src/common/file_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Emanuel Machado da Silva
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#pragma once

#include <fstream>
#include <istream>
#include <string>

namespace Puzzles {

struct FileReader {
explicit FileReader(const std::string &filename);
~FileReader();

std::istream *istream();

private:
std::fstream input;
};
}
Loading