質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

1回答

463閲覧

lifegame を作ってます。GetIsAlive() と SetIsAlive() を消して IsAlive プロパティに統合したいのですがどのように書けばいいのかわかりません

kaitto

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2022/08/30 04:07

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cell : MonoBehaviour
{

SpriteRenderer _CellSprite; [SerializeField] bool _IsAlive = false; private void Awake() { _CellSprite = GetComponent<SpriteRenderer>(); } public bool GetIsAlive() { return _IsAlive; } /// <summary> /// 色を変える /// </summary> /// <param name="alive"></param> public void SetIsAlive(bool alive) { _IsAlive = alive; if (!_IsAlive) _CellSprite.color = Color.gray; else _CellSprite.color = Color.green; } public void SetScale(float scele) { transform.localScale = new Vector2(scele, scele); }

}

using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class CGOLGrid : MonoBehaviour
{
Vector2 GridStartPosition = new Vector2(-5.0f, -5.0f); // 最初のセルの開始位置
float CellSize;
// 個々のセルのサイズ

#region Grid [Header("Grid")] [Space(5)] [SerializeField] int GridCountMin = 50; [SerializeField] int GridCountMax = 150; [SerializeField][Range(50, 150)] int GridCountSize = 50; [SerializeField][Range(50, 150)] int GridCountCurrent = 50; [SerializeField] GameObject CellPrefab; Cell[,] CellArray; bool[,] NextCellArray; bool rand; #endregion #region Sim Speed [Header("Simulation Speed")] [Space(5)] [SerializeField] float SimSpeedMin = 1.0f; [SerializeField] float SimSpeedMax = 60.0f; [SerializeField][Range(1.0f, 60.0f)] float SimSpeed = 7.0f; bool IsPaused = true; bool clickedAlive = false; float Elapsed = 0.0f; #endregion #region UI [Header("UI")] [Space(5)] [SerializeField] TMP_Text PlayPauseText; [SerializeField] Slider SimSpeedSlider; [SerializeField] TMP_Text SimSpeedText; [SerializeField] Slider GridSizeSlider; [SerializeField] TMP_Text GridSizeText; [SerializeField] TMP_Text GridApplyText; #endregion private void Start() { //50 50 CellArray = new Cell[GridCountSize, GridCountSize]; //50 50 NextCellArray = new bool[GridCountSize, GridCountSize]; GridCountCurrent = GridCountSize;//50 InitialiseGrid(GridCountSize);//50 #region UI PlayPauseText.SetText("Play"); //size GridSizeSlider.minValue = GridCountMin;//50 GridSizeSlider.maxValue = GridCountMax;//150 GridSizeSlider.value = GridCountSize;//50 GridSizeSlider.onValueChanged.AddListener(_ => ChangedGridSize()); GridSizeText.SetText(GridCountCurrent.ToString("New Size: 0"));//50 GridApplyText.SetText(GridCountCurrent.ToString("Size: 0\nApply"));//50 //speed SimSpeedSlider.minValue = SimSpeedMin;//1.0f SimSpeedSlider.maxValue = SimSpeedMax;//60.0f SimSpeedSlider.value = 7.0f; SimSpeedText.SetText(SimSpeed.ToString("Sim Speed: 0.0/s")); #endregion } //セル数(行数 X 列数)や色など生成 void InitialiseGrid(int GridCount)//50 { CellSize = 10.0f / GridCount;// 10.0f / 50 = 0.2; for (int y = 0; y < GridCount; y++) { for (int x = 0; x < GridCount; x++) { // Vector2(-5.0f, -5.0f) + Vector2(0.2 * x, 0.2 * y) CellArray[x, y] = Instantiate(CellPrefab, GridStartPosition + new Vector2(CellSize * x, CellSize * y), Quaternion.identity).GetComponent<Cell>(); //各cellのScale CellArray[x, y].transform.localScale = new Vector2(CellSize, CellSize); //0から1 rand = (Random.value >= 0.5) ? false : true; //色を渡す50個 CellArray[x, y].SetIsAlive(rand); //次のcell50個 NextCellArray[x, y] = rand; } } } int GetLIvingNeighbours(int x,int y) { int count = 0; //左上のセルをチェック if(x != 0 && y != GridCountCurrent - 1) if (CellArray[x - 1, y + 1].GetIsAlive()) count++; //中央上部のセルをチェック if (y != GridCountCurrent - 1) if (CellArray[x, y + 1].GetIsAlive()) count++; //右上のセルをチェック if (x != GridCountCurrent - 1 && y != GridCountCurrent - 1) if (CellArray[x + 1, y + 1].GetIsAlive()) count++; //左のセルをチェック if (x != 0) if (CellArray[x - 1, y].GetIsAlive()) count++; //右側のセルをチェック if (x != GridCountCurrent - 1) if (CellArray[x + 1, y].GetIsAlive()) count++; //左下のセルをチェック if (x != 0 && y != 0) if (CellArray[x - 1, y - 1].GetIsAlive()) count++; //下中央のセルをチェック if (y != 0) if (CellArray[x, y - 1].GetIsAlive()) count++; // 右下のセルをチェック if (x != GridCountCurrent - 1 && y != 0) if (CellArray[x + 1, y - 1].GetIsAlive()) count++; return count; } void SetNextState(int xCount, int yCount) { for(int y = 0; y < yCount; y++) { for(int x = 0; x < xCount; x++) { CellArray[x, y].SetIsAlive(NextCellArray[x, y]); } } } void ChangeGridSize(int GridCountNew) { for(int y = 0; y < GridCountCurrent; y++) { for(int x = 0; x < GridCountCurrent; x++) { Destroy(CellArray[x, y].gameObject); CellArray[x, y] = null; NextCellArray[x, y] = false; } } CellArray = new Cell[GridCountNew, GridCountNew]; NextCellArray = new bool[GridCountNew, GridCountNew]; GridCountCurrent = GridCountNew; GridApplyText.SetText(GridCountCurrent.ToString("Size: 0\nApplied")); } //cellの大きさを変える void ChangedGridSize() { //sizebuttonのtext GridApplyText.SetText(GridCountCurrent.ToString("Size: 0\nApply")); } //play開始とpauseの制御ボタン public void ButtonPlayPause() { if(!IsPaused) { IsPaused = true; PlayPauseText.SetText("Play"); } else { IsPaused = false; PlayPauseText.SetText("Pause"); } } //ランダムに生成するボタン public void ButtonMapRandomise() { for(int y = 0; y < GridCountCurrent; y++)//50 { for(int x = 0; x < GridCountCurrent; x++) { rand = (Random.value >= 0.5) ? false: true; CellArray[x, y].SetIsAlive(rand); NextCellArray[x, y] = rand; } } } //リセットボタン public void ButtonMapReset() { for(int y = 0; y < GridCountCurrent; y++) { for(int x = 0; x < GridCountCurrent; x++) { CellArray[x, y].SetIsAlive(false); NextCellArray[x, y] = false; } } } public void SliderGridSiza() { GridCountSize = (int)GridSizeSlider.value; GridSizeText.SetText(GridCountSize.ToString("New Size: 0")); } public void ButtonGridResize() { ChangeGridSize(GridCountSize); } public void SliderSimSpeed() { SimSpeed = SimSpeedSlider.value; } public void ButtonQuit() { Application.Quit(); } private void Update() { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //マウスクリックされた時 if (Input.GetMouseButtonDown(0)) { if (hit.collider != null) { if (!hit.collider.gameObject.GetComponent<Cell>().GetIsAlive()) { hit.collider.gameObject.GetComponent<Cell>().SetIsAlive(true); clickedAlive = false; } else { hit.collider.gameObject.GetComponent<Cell>().SetIsAlive(false); clickedAlive = true; } } } //マウスクリックしてる間 if (Input.GetMouseButton(0) && !clickedAlive) { if (hit.collider != null) { hit.collider.gameObject.GetComponent<Cell>().SetIsAlive(true); } } else if (Input.GetMouseButton(0) && clickedAlive) { if (hit.collider != null) { hit.collider.gameObject.GetComponent<Cell>().SetIsAlive(false); } } SimSpeedText.SetText(SimSpeed.ToString("Sim Speed: 0.0/s")); //更新サイクルを遅くします Elapsed += Time.deltaTime; if (Elapsed < 1 / SimSpeed) return; Elapsed -= 1 / SimSpeed; if (!IsPaused) { for (int y = 0; y < GridCountCurrent; y++)//50 { for (int x = 0; x < GridCountCurrent; x++) { //セルの現在の状態を確認し、生きている隣人を数えます bool alive = CellArray[x, y].GetIsAlive(); int count = GetLIvingNeighbours(x, y); bool result = false; if (alive && count < 2) result = false; else if (alive & (count == 2 || count == 3)) result = true; else if (alive && count > 3) result = false; else if (!alive && count == 3) result = true; NextCellArray[x, y] = result; } } SetNextState(GridCountCurrent, GridCountCurrent); } }

}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YAmaGNZ

2022/08/30 04:44

「C# プロパティ 作成」とでも検索してみてはどうでしょうか
guest

回答1

0

C#

1public class Cell { 2 private bool _IsAlive = false; 3 4 private bool GetIsAlive() { 5 System.Console.WriteLine("GET"); 6 return _IsAlive; 7 } 8 private void SetIsAlive(bool alive) { 9 System.Console.WriteLine("SET"); 10 _IsAlive = alive; 11 } 12 13 public bool IsAlive { 14 get { return GetIsAlive(); } 15 set { SetIsAlive(value); } 16 } 17} 18 19// おためし 20class Program { 21 public static void Main() { 22 Cell c = new Cell(); 23 c.IsAlive = true; 24 bool b = c.IsAlive; 25 } 26}

投稿2022/08/30 22:51

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問