Skip to content
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build*
.vscode
CMakeUserPresets.json
env
24 changes: 3 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,12 @@ project(stratego LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(
stratego-src
src/AttackInfoBox.cpp
src/BigButton.cpp
src/Board.cpp
src/Box.cpp
src/Button.cpp
src/ButtonInterface.cpp
src/Game.cpp
src/GameController.cpp
src/GameView.cpp
src/MainMenu.cpp
src/Movable.cpp
src/Player.cpp)

target_include_directories(stratego-src PUBLIC include)

add_executable(${PROJECT_NAME} src/main.cpp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(SFML 2.6.1 REQUIRED system window graphics CONFIG)

target_link_libraries(stratego-src PUBLIC sfml-system sfml-window sfml-graphics)
target_link_libraries(${PROJECT_NAME} stratego-src)
add_subdirectory(src)
add_subdirectory(include)

install(DIRECTORY font DESTINATION ${CMAKE_BINARY_DIR})
install(DIRECTORY images DESTINATION ${CMAKE_BINARY_DIR})
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ Created with the help of SFML documentation and sources:
- [Setting The Board](https://user-images.githubusercontent.com/62173407/85401972-2f64ad80-b55b-11ea-81bb-2f78c8d047ed.png)
- [Scout Move](https://user-images.githubusercontent.com/62173407/85402027-44d9d780-b55b-11ea-9de2-3ff096b19e8b.png)
- [Attack Info Box](https://user-images.githubusercontent.com/62173407/85402062-5622e400-b55b-11ea-9d96-9cd896a91d29.png)


## Contributing
In case you would like to contribue, please make sure that you format code.

1. Make sure you are in repo root
2. Install clang-format using pip:
```bash
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
```

3. Now use the formatting script:
```bash
bash infra/scripts/apply_formatting.sh .
```

4. You can also install pre-commit hook that will automatically check for correct code-formatting:
```bash
cp infra/hooks/pre-commit .git/hooks/pre-commit
```
187 changes: 78 additions & 109 deletions include/Array2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,140 +6,109 @@
#include <stdexcept>
#include <vector>

template <typename T>
class Array
{
std::vector<T> data;
void check_range(int idx) const
{
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid COLUMN access");
}
template <typename T> class Array {
std::vector<T> data;
void check_range(int idx) const {
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid COLUMN access");
}
}

public:
explicit Array(int size) : data(size)
{
}
T &operator[](int idx)
{
check_range(idx);
return data[idx];
}
const T &operator[](int idx) const
{
check_range(idx);
return data[idx];
}
explicit Array(int size) : data(size) {}
T &operator[](int idx) {
check_range(idx);
return data[idx];
}
const T &operator[](int idx) const {
check_range(idx);
return data[idx];
}
};

template <typename T>
class Array2D
{
std::vector<Array<T>> data;
void check_range(int idx) const
{
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid ROW access");
}
template <typename T> class Array2D {
std::vector<Array<T>> data;
void check_range(int idx) const {
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid ROW access");
}
}

public:
explicit Array2D(int width, int height) : data(height, Array<T>(width)) {}
Array<T> &operator[](int idx)
{
check_range(idx);
return data[idx];
}
const Array<T> &operator[](int idx) const
{
check_range(idx);
return data[idx];
}
explicit Array2D(int width, int height) : data(height, Array<T>(width)) {}
Array<T> &operator[](int idx) {
check_range(idx);
return data[idx];
}
const Array<T> &operator[](int idx) const {
check_range(idx);
return data[idx];
}
};

template <>
class Array<bool>
{
template <> class Array<bool> {
public:
class my_bool
{
public:
bool value;
my_bool(bool val = false) : value(val){};
inline friend std::ostream &operator<<(std::ostream &stream, my_bool rhs);
inline operator bool() const;
inline operator bool();

};
class my_bool {
public:
bool value;
my_bool(bool val = false) : value(val) {};
inline friend std::ostream &operator<<(std::ostream &stream, my_bool rhs);
inline operator bool() const;
inline operator bool();
};

private:
std::vector<my_bool> data;
void check_range(int idx) const
{
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid COLUMN access");
}
std::vector<my_bool> data;
void check_range(int idx) const {
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid COLUMN access");
}
}

public:
explicit Array(int size) : data(size)
{
}
my_bool &operator[](int idx)
{
check_range(idx);
return data[idx];
}
const my_bool &operator[](int idx) const
{
check_range(idx);
return data[idx];
}
explicit Array(int size) : data(size) {}
my_bool &operator[](int idx) {
check_range(idx);
return data[idx];
}
const my_bool &operator[](int idx) const {
check_range(idx);
return data[idx];
}
};

std::ostream &operator<<(std::ostream &stream, Array<bool>::my_bool rhs)
{
stream << rhs.value;
return stream;
}

Array<bool>::my_bool::operator bool() const {
return (const bool)value;
std::ostream &operator<<(std::ostream &stream, Array<bool>::my_bool rhs) {
stream << rhs.value;
return stream;
}

Array<bool>::my_bool::operator bool() {
return value;
}
Array<bool>::my_bool::operator bool() const { return (const bool)value; }

Array<bool>::my_bool::operator bool() { return value; }

template <>
class Array2D<bool>
{
std::vector<Array<Array<bool>::my_bool>> data;
void check_range(int idx) const
{
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid ROW access");
}
template <> class Array2D<bool> {
std::vector<Array<Array<bool>::my_bool>> data;
void check_range(int idx) const {
if (idx < 0 || static_cast<size_t>(idx) >= data.size()) {
std::cerr << "KABOOM !" << std::endl;
throw std::out_of_range("Invalid ROW access");
}
}

public:
explicit Array2D(int width, int height) : data(height, Array<Array<bool>::my_bool>(width)) {}
Array<Array<bool>::my_bool> &operator[](int idx)
{
check_range(idx);
return data[idx];
}
const Array<Array<bool>::my_bool> &operator[](int idx) const
{
check_range(idx);
return data[idx];
}
explicit Array2D(int width, int height)
: data(height, Array<Array<bool>::my_bool>(width)) {}
Array<Array<bool>::my_bool> &operator[](int idx) {
check_range(idx);
return data[idx];
}
const Array<Array<bool>::my_bool> &operator[](int idx) const {
check_range(idx);
return data[idx];
}
};

#endif /* ARRAY2D_H_ */
79 changes: 40 additions & 39 deletions include/AttackInfoBox.hpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
#pragma once

#include <memory>
#include <SFML/Graphics.hpp>
#include <array>
#include <memory>

#include "unit/Unit.hpp"

#include "Unit.hpp"
class AttackInfoBox {
const std::string path_to_textures;
float attacking_unit_pos_x;
float attacking_unit_pos_y;
float attacked_unit_pos_x;
float attacked_unit_pos_y;
std::array<sf::Sprite *, 12> red_units_sprites_ptrs;
std::array<sf::Sprite *, 12> blue_units_sprites_ptrs;
sf::Texture box_texture;
sf::Sprite box_sprite;
sf::Font box_font;
sf::Text box_text;
sf::Sprite attacking_unit;
sf::Sprite attacked_unit;
sf::Sprite &winner_highlight;
const Unit *attacker_ptr;
const Unit *attacked_ptr;

class AttackInfoBox
{
const std::string path_to_textures;
int attacking_unit_pos_x;
int attacking_unit_pos_y;
int attacked_unit_pos_x;
int attacked_unit_pos_y;
std::array<sf::Sprite*, 12> red_units_sprites_ptrs;
std::array<sf::Sprite*, 12> blue_units_sprites_ptrs;
sf::Texture box_texture;
sf::Sprite box_sprite;
sf::Font box_font;
sf::Text box_text;
sf::Sprite attacking_unit;
sf::Sprite attacked_unit;
sf::Sprite& winner_highlight;
std::shared_ptr<Unit> attacker_ptr;
std::shared_ptr<Unit> attacked_ptr;
int get_unit_sprite_idx(const Unit &unit);
void load_box_texture();
void load_font();
void update_attacking_unit_pos();
void update_attacked_unit_pos();
void update_box_text_pos();
const Unit *get_winner();
void set_winner_highlight();
void draw_winner_highlight(sf::RenderWindow &win);

int get_unit_sprite_idx(const std::shared_ptr<Unit>& unit);
void load_box_texture();
void load_font();
void update_attacking_unit_pos();
void update_attacked_unit_pos();
void update_box_text_pos();
std::shared_ptr<Unit> get_winner();
void set_winner_highlight();
void draw_winner_highlight(sf::RenderWindow& win);
public:
AttackInfoBox(std::array<sf::Sprite, 12>& red_units_sprites, std::array<sf::Sprite, 12>& blue_units_sprites, sf::Sprite& winning_unit_highlight);
void draw(sf::RenderWindow& win);
void set_position(int x, int y);
void set_attacking_unit(const std::shared_ptr<Unit>& attacker);
void set_attacked_unit(const std::shared_ptr<Unit>& victim);
float get_height() const { return box_sprite.getGlobalBounds().height; };
float get_width() const { return box_sprite.getGlobalBounds().width; };


public:
AttackInfoBox(std::array<sf::Sprite, 12> &red_units_sprites,
std::array<sf::Sprite, 12> &blue_units_sprites,
sf::Sprite &winning_unit_highlight);
void draw(sf::RenderWindow &win);
void set_position(int x, int y);
void set_attacking_unit(const std::optional<Unit> &attacker);
void set_attacked_unit(const std::optional<Unit> &victim);
float get_height() const { return box_sprite.getGlobalBounds().height; };
float get_width() const { return box_sprite.getGlobalBounds().width; };
};
21 changes: 10 additions & 11 deletions include/BigButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

#include "ButtonInterface.hpp"

class BigButton : public ButtonInterface
{
protected:
sf::Texture button_texture;
sf::Texture highlighted_button_texture;
class BigButton : public ButtonInterface {
protected:
sf::Texture button_texture;
sf::Texture highlighted_button_texture;

void load_button_texture();
void load_highlighted_button_texture();
public:
explicit BigButton(const std::string& button_text = "default");
void draw(sf::RenderWindow& win);
void load_button_texture();
void load_highlighted_button_texture();

public:
explicit BigButton(const std::string &button_text = "default");
void draw(sf::RenderWindow &win);
};
Loading