Skip to content

refactor: improve C++ code quality and add Vec2 type#21

Merged
YJack0000 merged 2 commits into
mainfrom
refactor/code-quality
Feb 23, 2026
Merged

refactor: improve C++ code quality and add Vec2 type#21
YJack0000 merged 2 commits into
mainfrom
refactor/code-quality

Conversation

@YJack0000

Copy link
Copy Markdown
Owner

Summary

  • Add Vec2 struct to replace std::pair<float,float> for positions/movement — improves readability (pos.x vs pos.first)
  • Fix EnvironmentObject: make position protected, remove unnecessary virtual from getPosition/setPosition
  • Fix double memcpy bug in Genes constructor
  • Fix calculateDistance taking shared_ptr by value (unnecessary ref count bump)
  • Make Food::canBeEaten() const, add Food(int energy) constructor for configurable energy
  • Remove all commented-out printf debug statements (~20 lines)
  • Use C++17 structured bindings in Environment iteration loops
  • Pre-compute organism list before thread dispatch, skip thread creation for numThreads=1

Discussion Points

  • Vec2 提供了 operator std::pair<float,float>() 以維持向後相容。Python bindings 仍然回傳 tuple,不需要改動 Python 端程式碼
  • EnvironmentObject::position 改為 protected,讓 Organism 可以直接存取而不需要透過 virtual dispatch
  • Food 新增 Food(int energy) constructor,但預設仍然是 500,不會影響現有行為
  • SpatialIndexType enum 已加入但 constructor 仍接受 string 以維持 Python API 相容性

Test plan

  • All 3 existing Python tests pass
  • Verify Python examples still work correctly
  • Verify Food(energy) constructor works from Python

🤖 Generated with Claude Code

YJack0000 and others added 2 commits February 24, 2026 04:39
- Add Vec2 struct to replace std::pair<float,float> for positions and movement
- Fix EnvironmentObject: make position protected, remove unnecessary virtual from getPosition/setPosition
- Fix double memcpy bug in Genes constructor (delegating ctor already copies)
- Fix calculateDistance: take shared_ptr by const ref instead of by value
- Make react()/interact() take const vector references
- Make Food::canBeEaten() const, add configurable energy via Food(int energy) constructor
- Make RNG thread_local in Organism::makeMove() and Genes::defaultMutationLogic()
- Remove dead removeDeadOrganisms() declaration from Environment
- Pre-compute organism list before thread dispatch in handleReactions
- Skip thread creation when numThreads=1
- Make handleInteractions single-threaded (mutates shared state)
- Remove all commented-out printf debug statements
- Use structured bindings in Environment iteration loops
- Initialize foodConsumption to 0 in header
- Expose Food energy constructor in Python bindings

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@YJack0000
YJack0000 force-pushed the refactor/code-quality branch from b256cb9 to 41e91ac Compare February 23, 2026 20:41
@YJack0000
YJack0000 merged commit 30bf58e into main Feb 23, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant