Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DxLib.h"
- #define height 70
- #define width 80
- int red = GetColor(200, 0, 0);
- int green = GetColor(0, 200, 0);
- int blue = GetColor(0, 0, 200);
- void random_set(int **map){
- for (int i = 0; i < width; i++){
- for (int j = 0; j < height; j++){
- map[i][j] = (int)((double)rand() / RAND_MAX+0.5);
- }
- }
- }
- void write(int **map){
- for (int i = 0; i < width; i++){
- for (int j = 0; j < height; j++){
- if (map[i][j]){
- DrawFormatString(i * 10, j * 10, green, "*");
- }
- }
- }
- }
- void copy_map(int** map, int** temp_map){
- for (int i = 0; i < width; i++){
- for (int j = 0; j < height; j++){
- temp_map[i][j] = map[i][j];
- }
- }
- }
- void update(int** map, int** temp_map){
- int count;
- for (int i = 0; i < width; i++){
- for (int j = 0; j < height; j++){
- //生きているセルの個数を数える
- count = 0;
- //周りの8セルのチェック
- if (i != 0){ count += map[i - 1][j]; }
- if (i != 0 && j != height - 1){ count += map[i - 1][j + 1]; }
- if (j != height - 1){ count += map[i][j + 1]; }
- if (i != width - 1 && j != height - 1){ count += map[i + 1][j + 1]; }
- if (i != width - 1){ count += map[i + 1][j]; }
- if (i != width - 1 && j != 0){ count += map[i + 1][j - 1]; }
- if (j != 0){ count += map[i][j - 1]; }
- if (i != 0 && j != 0){ count += map[i - 1][j - 1]; }
- if ((map[i][j] && (count == 2 || count == 3)) || (!map[i][j] && count == 3)){
- temp_map[i][j] = 1;
- }
- else{
- temp_map[i][j] = 0;
- }
- }
- }
- //更新したtemp_mapをmapにコピー
- copy_map(temp_map, map);
- }
- int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int){
- //前処理
- ChangeWindowMode(true);
- SetAlwaysRunFlag(true);
- DxLib_Init();
- SetGraphMode(1000, 700, 8);
- SetDrawScreen(DX_SCREEN_BACK);
- //mapの宣言
- int **map=new int*[width];
- int **temp_map = new int*[width];
- for (int i = 0; i < width; i++){
- map[i] = new int[height];
- temp_map[i] = new int[height];
- }
- //第1世代をランダムにセット
- random_set(map);
- //世代用の変数
- int generation = 1;
- while (true){
- //画面をクリアする
- ClearDrawScreen();
- //世代を表示
- DrawFormatString(810, 50, red, "世代:%d", generation);
- //map表示
- write(map);
- //次の世代を計算
- update(map, temp_map);
- //0.1秒待って画面反転 世代を1進める
- WaitTimer(100);
- generation++;
- ScreenFlip();
- }
- //終了処理
- DxLib_End();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement