AR(名刺に記載)
.png)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CellGenerate : MonoBehaviour
{
public enum CellState
{
Empty,
Black,
White
}
private CellState[,] boardState = new CellState[8,8];
public GameObject CellPrefab1;
public GameObject BlackPiecePrefab;
public GameObject WhitePiecePrefab;
public float cellSpacing = 1.1f;
public CellState CurrentTurn;
// ボードのサイズ
public int boardSize = 8;
//instance化したオブジェクトを配列から管理するための保存先
private GameObject[,] CellObjects = new GameObject[8,8];
=========一部割愛しております。=========
public void PieceChange(int x, int y, CellState state)
{
// 8方向の探索
int[] dx = { -1, 0, 1, -1, 1, -1, 0, 1 };
int[] dy = { -1, -1, -1, 0, 0, 1, 1, 1 };
bool validMoveFound = false;
for (int i = 0; i < 8; i++)
{
List<(int, int)> piecesToFlip = new List<(int, int)>();
int curX = x + dx[i];
int curY = y + dy[i];
while(0 <= curX && curX < boardSize && 0 <= curY && curY < boardSize)
{
print(boardState[curX,curY] + ":" + curX + "," + curY);
if(boardState[curX,curY] == CellState.Empty)
{
// 空のセルに遭遇したら探索終了
break;
}
else if(boardState[curX,curY] == state)
{
// 置いた色と同じ色に遭遇したら、間の石を反転させる
if(piecesToFlip.Count < 1) // リストに要素がある場合のみ反転処理を行う
{
print("設置できない!");
break;
}
else{
foreach (var piece in piecesToFlip)
{
FlipPiece(piece.Item1, piece.Item2, state);
}
}
break;
}
else
{
// 異なる色の石に遭遇したらリストに追加
piecesToFlip.Add((curX, curY));
}
curX += dx[i];
curY += dy[i];
}
}
}
//実際にひっくり返す処理を行う
private void FlipPiece(int x, int y, CellState newState)
{
// 既存の子オブジェクト(駒)を破棄
foreach (Transform child in CellObjects[x, y].transform)
{
Destroy(child.gameObject); // 子オブジェクトを破棄
}
CellState State = newState == CellState.Black ? CellState.Black : CellState.White;
// 新しい色のPrefabを選択
GameObject piecePrefab = newState == CellState.Black ? BlackPiecePrefab : WhitePiecePrefab;
//ボードを変更する
boardState[x,y] = State;
// 新しい駒をインスタンス化して、CellObjectsの子オブジェクトとして配置
GameObject newPiece = Instantiate(piecePrefab, CellObjects[x, y].transform.position, Quaternion.identity, CellObjects[x, y].transform);
}