A fun exploration of C programming through rectangle manipulation with physics! What started as a university assignment evolved into a comprehensive demonstration of modular programming, physics simulation, and professional development practices.
- ๐ Physics Engine: Automatic gravity system with realistic falling behavior
- ๐ฒ Random IDs: Each rectangle gets a unique random identifier (0-9)
- ๐ Collision Detection: Advanced collision detection for overlaps and lateral contacts
- ๐จ Visual Display: ASCII-based graphical representation with filled interiors
- โก Real-time Manipulation: Create, move, and delete rectangles with instant feedback
- ๐งช Unit Testing: Comprehensive test suite with 100% function coverage
- ๐๏ธ Modular Architecture: Clean separation of concerns across multiple modules
- ๐ Boundary Validation: Robust input validation and error handling
- GCC compiler (C99 standard or later)
- Unix-like system (Linux, macOS) or Windows with MinGW
# Clone the repository
git clone https://github.com/yourusername/playingwith_C_rectanglesgame.git
cd playingwith_C_rectanglesgame
# Build the project
make
# Run the program
make run
# Run unit tests
make testThe system follows a modular design pattern with clear separation of responsibilities:
๐ฆ Rectangle System
โโโ ๐ฎ main.c # Entry point and program orchestration
โโโ ๐ฏ controlo.c # Core logic: physics, collision detection, CRUD operations
โโโ ๐จ visualizacao.c # Display engine and ASCII rendering
โโโ ๐ analise.c # Command parsing and user interaction
โโโ ๐งช tests.c # Comprehensive unit test suite
โโโ ๐ retangulos.h # Public API definitions and data structures
| Module | Purpose | Key Functions |
|---|---|---|
| Control | Physics & State Management | add_rectangle(), apply_gravity(), check_collision() |
| Visualization | Rendering & Display | display_world(), ASCII art generation |
| Analysis | Command Processing | parse_command(), process_commands() |
| Testing | Quality Assurance | Unit tests for all core functions |
The program offers three operation modes:
- ๐ฏ Demo Mode: Runs predefined test scenarios
- โก Interactive Mode: Manual command input
- ๐งช Testing Mode: Automated unit test execution
| Command | Syntax | Description | Example |
|---|---|---|---|
| Create | create x,y+w,h |
Creates a rectangle at position (x,y) with width w and height h | create 5,10+8,4 |
| Move Right | moveright x,y+n |
Moves rectangle containing point (x,y) right by n positions | moveright 7,12+3 |
| Move Left | moveleft x,y+n |
Moves rectangle containing point (x,y) left by n positions | moveleft 15,8+2 |
| Delete | delete x,y |
Removes rectangle containing point (x,y) | delete 10,5 |
| Exit | exit |
Terminates interactive mode | exit |
- Origin: Bottom-left corner at
(1,1) - Dimensions: 80ร25 grid
- Gravity: Downward (toward y=1)
- Bounds: All rectangles must fit within the plane boundaries
Rectangles are displayed with ASCII characters:
xxxxxxxxxxxx
x++++5+++++x โ Border with 'x', interior filled with '+', ID displayed
x++++++++++x
x++++++++++x
xxxxxxxxxxxx
The system includes a comprehensive testing suite:
# Run all tests
make test
# Memory leak detection (requires Valgrind)
make memcheck
# Static code analysis (requires cppcheck)
make analyzeTest Coverage:
- โ Random ID generation and uniqueness
- โ Rectangle location algorithms
- โ Collision detection (overlap and lateral)
- โ Boundary condition validation
- โ Edge cases and error handling
Professional Makefile with multiple targets:
make debug # Debug build with symbols
make release # Optimized production build
make clean # Clean build artifacts
make install # System-wide installation
make help # Show all available commands- Gravity Simulation: Rectangles automatically fall until they hit the ground or another rectangle
- Collision Prevention: Movement commands are validated to prevent overlapping
- Lateral Contact Detection: System alerts when rectangles are touching side-by-side
- Language: ANSI C (C99 standard)
- Memory Management: Stack-based allocation with bounds checking
- Maximum Rectangles: 10 concurrent rectangles
- ID System: Random integers 0-9 with collision avoidance
- Coordinate Precision: Integer-based positioning
- Performance: O(nยฒ) collision detection, O(n) gravity application
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Rectangle Creation | O(n) | O(1) |
| Movement | O(n) | O(1) |
| Collision Detection | O(nยฒ) | O(1) |
| Gravity Application | O(nยฒ) | O(1) |
| Display Rendering | O(wรh) | O(wรh) |
# Development build with debugging
make debug
# Run with memory checking
make memcheck
# Generate documentation (requires Doxygen)
make docsThe project maintains high code quality standards:
- Static Analysis: cppcheck integration
- Memory Safety: Valgrind compatibility
- Modular Design: Clear separation of concerns
- Comprehensive Testing: Unit tests for all critical functions
- Documentation: Inline comments and external docs
Contributions are welcome! Please ensure:
- All unit tests pass (
make test) - Code follows existing style conventions
- New features include corresponding tests
- Memory leaks are avoided (
make memcheck)
$ make run
Rectangle Manipulation System
=============================
Choose an option:
1 - Run test example
2 - Interactive mode
3 - Run unit tests
Option: 2
Available commands:
- create x,y+l,h
- moveright x,y+p
- moveleft x,y+p
- delete x,y
- exit
Enter command: create 10,5+8,4
Rectangle created successfully (ID: 7)
Current world state:
xxxxxxxxxxxx
x++++7+++++x
x++++++++++x
xxxxxxxxxxxxThis project demonstrates:
- โจ Advanced C Programming: Modular architecture, proper memory management
- ๐งช Test-Driven Development: Comprehensive unit testing framework
- ๐ฏ Software Engineering: Clean code principles, separation of concerns
- ๐จ User Experience: Intuitive command interface, visual feedback
- ๐ง Professional Tooling: Modern build system, development workflow
Originally developed as part of a Programming Laboratory course assignment, this project showcases professional-level software development practices including modular design, comprehensive testing, and robust error handling.
This project is licensed under the MIT License - see the LICENSE file for details.
Maria Sarabando
Computer Science Student
โญ Star this repository if you found it helpful!
Built with โค๏ธ and lots of โ