Recent Entries 10
- pattern minor 112d agoTicTacToe with MiniMax algorithmI just got into C++ a few days ago and decided to make a MiniMax based TicTacToe game, I don't really know much about the conventions and best uses of the C++ language so I'd really appreciate any kind of feedback. Brief summary: - Board: has all the information pertinent to the game. Is used by the main method to paint the window and by the Brain to decide which move to make. - Brain: Container for the MiniMax algorithm with helper methods. The Board is passed as a pointer to the Brain during construction (I don't know how wise this is but it's how I did things in Java). - Main: mainly contains functions to draw the board and get inputs from the user. Spacebar clears the board, F1 asks for a move, click performs a move. Board.h - Most of the comments are not in the header files ``` #ifndef BOARD_H_ #define BOARD_H_ #include typedef enum {empty, x, o} cellState; // The 3 possible states a cell can be in class Board { public: Board(); static const unsigned NUMBER_OF_CELLS = 9; static const unsigned CELLS_PER_ROW = 3; Board getClone(); cellState getCell(int n); std::vector getLegalMoves(); int getWinner(); bool isGameOver(); bool isXTurn(); void applyMove(unsigned m); void clear(); void print(); private: std::vector cells; cellState winner; bool xToMove; bool gameOver; unsigned movesMade; void setMark(int p, cellState m); void update(); void xWon(); void oWon(); void draw(); }; #endif ``` Board.cpp ``` #include #include "Board.h" Board::Board() { clear(); } void Board::clear() { // Resets the game cells.clear(); for (unsigned i = 0; i Board::getLegalMoves() { // Gets a vector of all the empty cells std::vector moves; for (unsigned i = 0; i = NUMBER_OF_CELLS || isGameOver()) return; if (xToMove) setMark(p, x); else setMark(p, o); } void Board::print() { // Prints the board to the console for bugtesting purposes
- pattern minor 112d agoCursor module for a text-editorA few weeks ago, I had started to write a text-editor in C++ using SFML library. The work is now paused pretty much because I am a lazy freak. So, I thought I might use this passive time to get reviews on what I have done so far. As of now, I want to get reviews on the cursor module (for displaying a blinking cursor), I wrote for the text-editor. I am posting the code as it was written and it was written without many comments. (I am not sure if I am supposed to add comments before putting it up for review on the site.) Cursor.h: ``` #ifndef CURSOR_H_INCLUDED #define CURSOR_H_INCLUDED #include #include class Cursor{ private: sf::Color color; sf::RectangleShape cursorLine; sf::Clock clock; int col, line; int txtX, txtY; int maxCol; float thickness, length; int blinkPeriod, lastTime; bool visible; public: float getCol(); float getLine(); float getLength(); float getThickness(); void move(int, int); void setPosition(int,int); void setDefaultValues(); Cursor(); Cursor(float, float, float, float, int, int, int); void draw(sf::RenderWindow *); void update(); }; #endif // CURSOR_H_INCLUDED ``` Cursor.cpp: ``` #include "Cursor.h" void Cursor::setDefaultValues(){ col = 1; line = 1; lastTime = 0; thickness = 2; length = 20; blinkPeriod = 400; visible = true; color = sf::Color(0,255,0); } Cursor::Cursor(){ setDefaultValues(); } Cursor::Cursor(float x, float y, float len, float thick, int txX, int txY, int maxCol){ setDefaultValues(); this->maxCol = maxCol; txtX = txX; txtY = txY; col = x; line = y; length = len; thickness = thick; } float Cursor::getCol(){ return col; } float Cursor::getLine(){ return line; } float Cursor::getLength(){ return length; } float Cursor::getThickness(){ return thickness; } void Cursor::move(int dx, int dy){ if(dx>0){ if(col+dx0) col +=dx; else{
- pattern minor 112d agoRendering the periodic table in C++ using SFMLI have created this as an example. It uses SFML as its core dependency along with JSON for modern C++ to load the elements from a file. Basically what happens is when an object that contains an instance of `sf::Drawable` is created, its constructor will be responsible for adding it to the draw list. Its destructor is responsible for removing it from the list. I am interested in if this is a good approach to dealing with the objects in my program that will need to be rendered or if I should be extending `sf::Drawable` class directly. I am also interested in the overall code quality of the entire project. I am aware of the assignment and copy constructors that do nothing. I am relatively new to C++ (1 semester). Drawables.cpp ``` #include "Drawables.hpp" namespace { std::vector> drawList; } std::vector>& getDrawList() { return drawList; } ``` Element.cpp constructor and destructor ``` #include "Element.hpp" #include Element::Element() { drawable = std::shared_ptr( new sf::RectangleShape(sf::Vector2f{49, 49}) ); drawable->setFillColor(sf::Color::Green); getDrawList().push_back(drawable); } Element::~Element() { // Remove it's drawable from the drawList for(int i = getDrawList().size() - 1; i >= 0; --i) { if(dynamic_cast(getDrawList()[i].get()) == drawable.get()) { getDrawList().erase(getDrawList().begin() + i); } } } ``` Client.cpp draw function ``` void Client::draw() { while(window.isOpen()) { window.clear(); for(std::shared_ptr obj : getDrawList()) { window.draw(*obj); } window.display(); } } ```
- pattern minor 112d agoBuilding a 2D Game Engine Using SFMLI've always loved making games and using SFML, and up until a few days ago, I've had to re-write code over and over again. So I decided to write a game engine so I can reuse my code. I've never done something like this before. I've read about it, and I watched a few videos. I'd love if you could look around, give me some ideas. Keep in mind I've only worked on this for less than a week. GitHub So far, I've just re-skinned SFML functions and classes using my own classes, but I've changed some functions around and made things a bit simpler. Is it, for example, any good creating my own `Sprite` class that is basically a re-skin of the SFML `sprite` class, with perhaps some added functionality, or should my resource manager just return an SFML `sprite`, which the user of the engine can just use all the in-built functionality for the `sf::Sprite`? Here is a very simple program that creates a sprite and a text and draws them on the screen. And we can use WASD to move the sprite. ``` #include #include #include #include #include #include #include #include #include int screenWidth = 1600; int screenHeight = 640; t2d::Clock _clock; t2d::GameState gameState; int main() { t2d::Window _window; _window.create(screenWidth, screenHeight, "BELLO!"); gameState = t2d::GameState::PLAY; // Text t2d::ResourceManager::createText("Text", "Fonts/times.ttf"); t2d::ResourceManager::getText("Text").setCharacterSize(50); t2d::ResourceManager::getText("Text").setStyle(t2d::TextStyle::Bold); t2d::ResourceManager::getText("Text").setColor(t2d::Color::Magenta); t2d::ResourceManager::getText("Text").setPosition(screenWidth / 4.0f, screenHeight / 2.0f); t2d::ResourceManager::getText("Text").setString("Hello World"); // Sprite t2d::ResourceManager::createSprite("RedPlayer", "Sprites/RedPlayer.png"); t2d::ResourceManager::getSprite("RedPlayer").setPosition(screenWidth / 4.0f, screenHeight / 2.0f); while (gameState != t2d::G
- pattern minor 112d agoSketch of Chutes and Ladders gameThe classic Chutes and Ladders game has a grid of 100 squares with various "chutes" (which send a player backwards) and "ladders" (which promote a player forward). The goal here was to randomly create a series of these chutes and ladders such that the net effect is that there are 50 more squares that may be lost than gained. I had considered finishing this off to a complete and playable game, but it doesn't seem to be a sufficiently interesting game to warrant the small additional effort. Here's a screenshot of the result: distrib.cpp ``` #include #include #include #include #include #include #include "distrib.h" int makeDeltaValues(std::vector &chutes, std::vector &ladders, int target) { std::random_device rd; std::mt19937 gen(rd()); std::weibull_distribution<> d{2.0, 20}; auto badvalue = [](int a){ return (aboxcount*2/3);}; auto next{[&](){ int m; do { m=d(gen)+2; } while(badvalue(m)); return m;}}; int iterations = 0; do { ++iterations; std::generate(ladders.begin(), ladders.end(), next); std::generate(chutes.begin(), chutes.end(), next); int lsum = std::accumulate(ladders.begin(), ladders.end(), 0); int csum = std::accumulate(chutes.begin(), chutes.end(), 0); // lsum must always be smaller or equal if (lsum > csum) { std::swap(csum, lsum); std::swap(chutes, ladders); } int delta = target - csum+lsum; // need to add delta distributed over chutes for (auto &v : chutes) { if (!badvalue(v+delta)) { v += delta; delta = 0; } } // force remainder into first element (usually zero) chutes.front() += delta; } while (badvalue(chutes.front())); return iterations; } std::vector> createLinks() { std::vector ladders(9); std::vector chutes(9); std::vector> links; links.reserve(ladders.size() + chutes.size()); makeDeltaVa
- pattern major 112d agoMandelbrot image generator and viewerThis is a C++ program which outputs a Mandelbrot fractal image in a graphics window and lets the user zoom and pan around the image, generating a new image each time the user does so. I'm using SFML for the graphics part. Because it generates a new image after every zoom or pan, it is pretty slow, especially with a 1000x600 image (it can take up to half a second for the new image to load). ``` #include #include const int IMAGE_WIDTH = 1000; const int IMAGE_HEIGHT = 600; double zoom = 0.004; // allow the user to zoom in and out... double offsetX = -0.7; // and move around double offsetY = 0.0; const int MAX = 127; // maximum number of iterations for mandelbrot() // don't increase MAX or the colouring will look strange int mandelbrot(double, double, int); sf::Color getColor(int); int main() { sf::RenderWindow window(sf::VideoMode(IMAGE_WIDTH, IMAGE_HEIGHT), "Mandelbrot"); window.setFramerateLimit(30); sf::Image image; image.create(IMAGE_WIDTH, IMAGE_HEIGHT, sf::Color(0, 0, 0)); sf::Texture texture; sf::Sprite sprite; bool stateChanged = true; // track whether the image needs to be regenerated while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: window.close(); break; case sf::Event::KeyPressed: stateChanged = true; // image needs to be recreated when the user changes zoom or offset switch (event.key.code) { case sf::Keyboard::Escape: window.close(); break; case sf::Keyboard::Equal: zoom *= 0.9; break; case sf::Keyboard::Dash: zoom /= 0.9; break;
- pattern moderate 112d agoPong game in C++This is my second SFML game, which is Pong. Please tell me what you think. ``` #include #include int player1score = 0; int player2score = 0; bool intersects(sf::CircleShape& c1, sf::RectangleShape& rect2){ sf::FloatRect circle = c1.getGlobalBounds(); sf::FloatRect rectangle = rect2.getGlobalBounds(); return circle.intersects(rectangle); } int main(){ sf::VideoMode videomode(400, 400); sf::RenderWindow window(videomode, "PONG"); sf::CircleShape Ball; Ball.setFillColor(sf::Color::Red); Ball.setRadius(10); Ball.setPosition(100, 200); sf::RectangleShape firstPlayer; firstPlayer.setFillColor(sf::Color::Black); firstPlayer.setSize(sf::Vector2f(10, 50)); firstPlayer.setPosition(30, 200); sf::RectangleShape secondPlayer; secondPlayer.setFillColor(sf::Color::Black); secondPlayer.setSize(sf::Vector2f(10, 50)); secondPlayer.setPosition(370, 200); sf::RectangleShape outleft; outleft.setFillColor(sf::Color::Black); outleft.setSize(sf::Vector2f(10, 400)); outleft.setPosition(390, 0); sf::RectangleShape outright; outright.setFillColor(sf::Color::Black); outright.setSize(sf::Vector2f(10, 400)); outright.setPosition(0,0); sf::RectangleShape mid; mid.setFillColor(sf::Color::Blue); mid.setSize(sf::Vector2f(20, 400)); mid.setPosition(200, 0); sf::RectangleShape up; up.setFillColor(sf::Color::Blue); up.setSize(sf::Vector2f(20, 400)); up.setPosition(400, 0); up.rotate(90); sf::RectangleShape down; down.setFillColor(sf::Color::Blue); down.setSize(sf::Vector2f(20, 400)); down.setPosition(400, 380); down.rotate(90); sf::RectangleShape left; sf::RectangleShape right; sf::Vector2ballSpeed(0.1, 0.1); while (window.isOpen()){ window.clear(sf::Color::Yellow); window.draw(mid); window.draw(Ball); window.draw(secondPlayer); window.draw(down); window.draw(up); window.draw(firstPlayer); window.draw(outleft); window.draw(outright); window.display(); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Eve
- pattern minor 112d agoSFML game using moving circlesThis is my first SFML game. There are 2 players: the red circle and the yellow circle. The red circle is controlled by Up,down,left and right keys, the yellow one is controlled by WASD keys. The players are supposed to keep on trying to get behind each other in order to move them and get them inside the black circle, the player who gets in the circle first loses. ``` #include #include bool intersects(const sf::CircleShape& c1, const sf::CircleShape& c2){ sf::FloatRect circ1 = c1.getGlobalBounds(); sf::FloatRect circ2 = c2.getGlobalBounds(); return circ1.intersects(circ2); } int main(){ sf::VideoMode videomode(400, 400); sf::RenderWindow window(videomode, "GAME"); sf::CircleShape circle; circle.setFillColor(sf::Color::Red); circle.setPosition(100, 100); circle.setRadius(20); sf::CircleShape circle2; circle2.setFillColor(sf::Color::Yellow); circle2.setPosition(200, 200); circle2.setRadius(20); sf::CircleShape circle3; circle3.setFillColor(sf::Color::Black); circle3.setPosition(300, 300); circle3.setRadius(50); while (window.isOpen()&&(!intersects(circle,circle3))&&(!intersects(circle2,circle3))){ window.clear(sf::Color::Blue); window.draw(circle); window.draw(circle2); window.draw(circle3); window.display(); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Up)){ circle.move(0, -0.5); } else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Down)){ circle.move(0, 0.5); } else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right)){ circle.move(0.5, 0); } else if ((event.type == sf::Event::KeyPressed) &&
- pattern minor 112d agoSnake game in C++ (using SFML)I haven't done any programming in a while, and I'm getting back into it. I made this Snake clone to refresh me a bit, and I would like to get some feedback in the "elegance" and "well-done"-ness of the code. main.cpp ``` #include "Game.h" int main() { Game::run(); return 0; } ``` Game.h ``` #ifndef __GAME_H__ #define __GAME_H__ #include #include #include #include enum class Direction {UP, RIGHT, DOWN, LEFT}; enum class MoveEvent {EAT, COLLIDE}; class Game { public: static void run(); private: static sf::RenderWindow * window; static int score; static int level; static bool alive; static std::pair foodLocation; // Random Number Generation for X static const int rangex_from; static const int rangex_to; static std::random_device rand_devX; static std::mt19937 generatorX; static std::uniform_int_distribution distrX; static const int rangey_from; static const int rangey_to; static std::random_device rand_devY; static std::mt19937 generatorY; static std::uniform_int_distribution distrY; static std::vector> snake_body; static Direction snake_direction; static Direction new_direction; static std::pair getNewFoodLocation(); static void moveSnake(); static void draw(); static bool collides(std::pair, std::vector>); static sf::Texture tile; static sf::Texture body; static sf::Texture food; static sf::Sprite tile_spr; static sf::Sprite body_sp
- pattern minor 112d agoSimple Snake GameI was working on this game for while and I have made it for education purposes by using SFML. The game works fine but I would like to know how I can improve it. ``` #include #include #include #include #include #include #include #include namespace { const sf::Vector2f WindowSize(640, 480); constexpr auto BlockSize = 16.f; constexpr auto SnakeSpeed = 5.f; const auto TimeStep = sf::seconds(1 / SnakeSpeed); auto randomEngine() { std::array seed_data; thread_local std::random_device source; std::generate(std::begin(seed_data), std::end(seed_data), std::ref(source)); std::seed_seq seeds(std::begin(seed_data), std::end(seed_data)); thread_local std::mt19937 seeded_engine(seeds); return seeded_engine; } auto random(const std::uniform_real_distribution& dist) { static auto& RandomEngine = randomEngine(); return dist(RandomEngine); } } class Snake : public sf::Drawable, sf::NonCopyable { public: enum Direction { None, Up, Down, Left, Right }; private: using SnakeContainer = std::vector; public: explicit Snake(const sf::Font& font) { reset(); mLivesText.setFont(font); mLivesText.setStyle(sf::Text::Bold); mLivesText.setCharacterSize(30); mLivesText.setColor(sf::Color::White); mLivesText.setPosition(WindowSize.x - 160.f, 0.f); } void reset() { mSnakeBody.clear(); sf::RectangleShape shape({ BlockSize - 1, BlockSize - 1 }); shape.setPosition(70 + BlockSize, 70 + 3 * BlockSize); shape.setFillColor(sf::Color(211, 211, 211)); mSnakeBody.push_back(shape); shape.setPosition(70 + BlockSize, 70 + 2 * BlockSize); mSnakeBody.push_back(shape); shape.setPosition(70 + BlockSize, 70 + BlockSize); mSnakeBody.push_back(shape); mDirection = Direction::None; mLives