ぼく用あれこれまとめ
ライフゲーム
最終更新:
bokuyo
-
view
// // ライフゲームのソースコードだよ // #include <iostream> #include <string> // // セルクラス // class CellTable { public: CellTable(); int init(); int print(); int set_cell(int tate, int yoko); int set_cell_alive_state(int tate, int yoko); int update(); int update_cell(int x, int y); int check_cell(int x, int y); int set_glider(); int set_penta(); private: enum ENUM_CELL { CELL_BUFFER = 2, CELL_X = 32, CELL_Y = 16, }; bool cell_array[CELL_X][CELL_Y][CELL_BUFFER]; int current_cell; int past_cell; }; // // エントリーポイント // int main() { CellTable cell_table; std::string cell_command = "c"; std::cout << ".:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*.:*~*:._.:*~*:..:*~*:._.:*~*:. " << std::endl; std::cout << ".:・'゚♭:*・♪'゚。*# ★ Conway's Game of Life ★ :・'゚.'♪*'゚♭.:*・♪ " << std::endl; std::cout << ".:*~*:._.:*~*:._.:*~*:._.:*~*:._.:*.:*~*:._.:*~*:..:*~*:._.:*~*:. " << std::endl; while(cell_command != "exit") { if(cell_command == "c") { cell_table.init(); } else if(cell_command == "s") { int x, y; std::cout << "横の番号?> "; std::cin >> x; std::cout << "縦の番号?> "; std::cin >> y; if((x >= 0) && (y >= 0)) { cell_table.set_cell(x, y); } } else if(cell_command == "u") { cell_table.update(); } else if(cell_command == "g") { cell_table.set_glider(); } else if(cell_command == "p") { cell_table.set_penta(); } cell_table.print(); std::cout << std::endl; std::cout << "u: セルの更新, s: セルを置く, c: セルのクリア, exit: 終了" << std::endl; std::cout << "g: グライダーの設置, p: ペンタデスカトロンの設置" << std::endl; std::cout << "コマンドを入力してください> "; std::cin >> cell_command; } return 0; } CellTable::CellTable() { init(); } int CellTable::init() { current_cell = 0; for(int yoko = 0; yoko < CELL_X; ++yoko) { for(int tate = 0; tate < CELL_Y; ++tate) { cell_array[yoko][tate][current_cell] = false; } } return 0; } int CellTable::print() { for(int tate = -1; tate < CELL_Y; ++tate) { for(int yoko = -1; yoko < CELL_X; ++yoko) { if( (yoko == -1) && (tate == -1) ) { std::cout << " "; } else if( tate == -1 ) { printf("%2d", yoko); } else if( yoko == -1 ) { printf("%2d", tate); } else if( cell_array[yoko][tate][current_cell] == false) { std::cout << "□"; } else { std::cout << "■"; } } std::cout << "\n"; } return 0; } int CellTable::set_cell(int tate, int yoko) { tate = tate % CELL_X * ((tate < 0) ? -1: 1); yoko = yoko % CELL_Y * ((yoko < 0) ? -1: 1); cell_array[tate][yoko][current_cell] = (cell_array[tate][yoko][current_cell] == false) ? true : false; return 0; } int CellTable::set_cell_alive_state(int tate, int yoko) { cell_array[tate][yoko][current_cell] = true; return 0; } int CellTable::update() { past_cell = current_cell; current_cell = (current_cell + 1) % CELL_BUFFER; for(int tate = 0; tate < CELL_Y; ++tate) { for(int yoko = 0; yoko < CELL_X; ++yoko) { update_cell(yoko, tate); } } return 0; } int CellTable::update_cell(int x, int y) { int count = check_cell(x, y); if(cell_array[x][y][past_cell] == false) { if(count == 3) { cell_array[x][y][current_cell] = true; } else { cell_array[x][y][current_cell] = false; } } else { if( (count <= 1) || (count >= 4)) { cell_array[x][y][current_cell] = false; } else { cell_array[x][y][current_cell] = true; } } return 0; } int CellTable::check_cell(int x, int y) { int count = 0; if(cell_array[(x-1 < 0) ? CELL_X-1: x-1][(y-1 < 0) ? CELL_Y-1: y-1][past_cell] == true) { ++count; } if(cell_array[x][(y-1 < 0) ? CELL_Y-1: y-1][past_cell] == true) { ++count; } if(cell_array[(x+1 >= CELL_X) ? 0: x+1][(y-1 < 0) ? CELL_Y-1: y-1][past_cell] == true) { ++count; } if(cell_array[(x-1 < 0) ? CELL_X-1: x-1][y][past_cell] == true) { ++count; } if(cell_array[(x+1 >= CELL_X) ? 0: x+1][y][past_cell] == true) { ++count; } if(cell_array[(x-1 < 0) ? CELL_X-1: x-1][(y+1 >= CELL_Y) ? 0: y+1][past_cell] == true) { ++count; } if(cell_array[x][(y+1 >= CELL_Y) ? 0: y+1][past_cell] == true) { ++count; } if(cell_array[(x+1 >= CELL_X) ? 0: x+1][(y+1 >= CELL_Y) ? 0: y+1][past_cell] == true) { ++count; } return count; } int CellTable::set_glider() { set_cell_alive_state(1, 0); set_cell_alive_state(2, 1); set_cell_alive_state(0, 2); set_cell_alive_state(1, 2); set_cell_alive_state(2, 2); return 0; } int CellTable::set_penta() { set_cell_alive_state(17, 5); set_cell_alive_state(17, 6); set_cell_alive_state(17, 7); set_cell_alive_state(22, 5); set_cell_alive_state(22, 6); set_cell_alive_state(22, 7); set_cell_alive_state(16, 6); set_cell_alive_state(23, 6); return 0; }