ライフゲーム
提供:kuhalaboWiki
Cellクラスを作る
- Cell.h
#pragma once #include "ofMain.h" class Cell { public: int currState; //現在の状態 int nextState; //次回の状態 int activeNeighbors; //近傍の生命の数 ofColor color; //生きているセルの色 ofColor gcolor; //グリッドの色 Cell(); void update(); void draw(float x, float y, float w, float h); void clear(); };
- Cell.cpp
#include "Cell.h" //初期状態の設定 Cell::Cell(){ currState = 0; nextState = 0; color = ofColor::black; // cell color gcolor = ofColor(150, 150, 150); // grid color } //セルの状態変異測 void Cell::update(){ if (currState) { switch(activeNeighbors){ case 0: case 1: nextState = 0; break; case 2: case 3: nextState = 1; color = ofColor::black; // live cell color break; case 4: case 5: case 6: case 7: case 8: nextState = 0; break; } } else{ switch(activeNeighbors){ case 3: nextState = 1; color = ofColor::green; // born cell color break; default: nextState = false; break; } } } //セルを描画 void Cell::draw(float x, float y, float w, float h){ ofSetColor(gcolor); ofNoFill(); ofRect(x, y, w, h); if (currState) { ofSetColor(color); ofFill(); ofRect(x, y, w, h); ofNoFill(); } } void Cell::clear() { currState = 0; nextState = 0; color = ofColor::black; }