/// <summary> /// 石をひっくり返すコルーチンを呼ぶためのコルーチンです。 /// </summary> IEnumerator CallTurnStoneCoroutine(Game_Cell cell) { //誤操作防止のため全マスをクリック不可にする foreach (Game_Cell c in cells) { c.SetClickable(false); } //8方向それぞれに対し、ひっくり返す処理を実行 possibleCoroutineCount = 8; StartCoroutine(TurnStoneCoroutine(cell, -1, -1)); StartCoroutine(TurnStoneCoroutine(cell, -1, 0)); StartCoroutine(TurnStoneCoroutine(cell, -1, 1)); StartCoroutine(TurnStoneCoroutine(cell, 0, -1)); StartCoroutine(TurnStoneCoroutine(cell, 0, 1)); StartCoroutine(TurnStoneCoroutine(cell, 1, -1)); StartCoroutine(TurnStoneCoroutine(cell, 1, 0)); StartCoroutine(TurnStoneCoroutine(cell, 1, 1)); while (possibleCoroutineCount > 0) { yield return(new WaitForSeconds(0.1f)); } yield return(new WaitForSeconds(0.5f)); Game_SceneController.Instance.OnTurnStoneFinished(); }
/// <summary> /// 石をひっくり返すコルーチンです。(指定した1方向に対して処理をかける実働メソッド) /// </summary> IEnumerator TurnStoneCoroutine(Game_Cell cell, int xDirection, int yDirection) { //Tuencheckメソッドにより挟めるかどうか判定、挟める場合のみ続行 if (!Turncheck(cell, cell.GetStoneColor(), xDirection, yDirection)) { possibleCoroutineCount--; yield break; } int x = cell.X; int y = cell.Y; while (true) { x += xDirection; y += yDirection; Game_Cell targetCell = GetCell(x, y); //targetCellがnullもしくは任意のセルと同じ色になった場合、処理を終了 if (null == targetCell || targetCell.GetStoneColor() == cell.GetStoneColor()) { possibleCoroutineCount--; break; } else //相手セルの色を任意のセルと同じ色に変更 { yield return(new WaitForSeconds(0.5f)); targetCell.SetStoneColor(cell.GetStoneColor()); Game_SoundManager.Instance.turn.Play(); } } }
/// <summary> /// マスがクリックされた時の処理です /// </summary> /// <param name="cell">Cell.</param> public void OnCellClick(Game_Cell cell) { field.Lock(); cell.StoneColor = Instance.CurrentPlayerStoneColor; Game_SoundManager.Instance.put.Play(); field.TurnOverStoneIfPossible(cell); }
/// <summary> /// 任意のマスから指定された方向に対し、相手の石を挟める場所に自分の石が配置されているかチェックします /// </summary> bool Turncheck(Game_Cell cell, StoneColor stoneColor, int xDirection, int yDirection) { int x = cell.X; int y = cell.Y; //相手の石を挟めるかどうか bool existEnemyStone = false; while (true) { //各座標をそれぞれのDirectionずつずらす x = x + xDirection; y = y + yDirection; //(x,y)座標のcellを取得 Game_Cell targetCell = GetCell(x, y); //targetCellがnullまたは、色が無い場合、false if (targetCell == null || targetCell.GetStoneColor() == StoneColor.None) { return(false); } //targetCellと任意のマスの色が同じだった場合、その時点でのexistEnemyStoneを返す else if (targetCell.GetStoneColor() == stoneColor) { return(existEnemyStone); } //targetCellと任意のマスの色が違う場合、existEnemyStoneをtrueに else { existEnemyStone = true; } } }
/// <summary> /// 任意のマスから指定された方向に対し、相手の石を挟める場所に自分の石が配置されているかチェックします /// </summary> /// <returns><c>true</c>, if own stone at the other side of enemy stone for direction was existsed, <c>false</c> otherwise.</returns> /// <param name="cell">Cell.</param> /// <param name="xDirection">X direction.</param> /// <param name="yDirection">Y direction.</param> bool ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(Game_Cell cell, StoneColor stoneColor, int xDirection, int yDirection) { var x = cell.X; var y = cell.Y; var existsEnemyStone = false; while (true) { x += xDirection; y += yDirection; var targetCell = GetCell(x, y); if (null == targetCell || targetCell.StoneColor == StoneColor.None) { return(false); } else if (targetCell.StoneColor == stoneColor) { return(existsEnemyStone); } else { existsEnemyStone = true; } } }
/// <summary> /// 石をひっくり返すコルーチンです(指定した1方向に対して処理をかける実働メソッド) /// </summary> /// <returns>The stone for direction if possible coroutine.</returns> /// <param name="cell">Cell.</param> /// <param name="xDirection">X direction.</param> /// <param name="yDirection">Y direction.</param> IEnumerator TurnStoneForDirectionIfPossibleCoroutine(Game_Cell cell, int xDirection, int yDirection) { if (!ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, cell.StoneColor, xDirection, yDirection)) { turnStoneForDirectionIfPossibleCoroutineCount--; yield break; } var x = cell.X; var y = cell.Y; while (true) { x += xDirection; y += yDirection; var targetCell = GetCell(x, y); if (null == targetCell || targetCell.StoneColor == cell.StoneColor) { turnStoneForDirectionIfPossibleCoroutineCount--; yield break; } else { yield return(new WaitForSeconds(0.5f)); targetCell.StoneColor = cell.StoneColor; Game_SoundManager.Instance.turn.Play(); } } }
/// <summary> /// 石をひっくり返すコルーチンです。(指定した1方向に対して処理をかける実働メソッド) /// </summary> IEnumerator TurnStoneCoroutine(Game_Cell cell, int xDirection, int yDirection) { if (!Turncheck(cell, cell.GetStoneColor(), xDirection, yDirection)) { possibleCoroutineCount--; yield break; } int x = cell.X; int y = cell.Y; while (true) { x += xDirection; y += yDirection; Game_Cell targetCell = GetCell(x, y); if (null == targetCell || targetCell.GetStoneColor() == cell.GetStoneColor()) { possibleCoroutineCount--; break; } else { yield return(new WaitForSeconds(0.5f)); targetCell.SetStoneColor(cell.GetStoneColor()); Game_SoundManager.Instance.turn.Play(); } } }
/// <summary> /// 任意のマスから指定された方向に対し、相手の石を挟める場所に自分の石が配置されているかチェックします /// </summary> bool Turncheck(Game_Cell cell, StoneColor stoneColor, int xDirection, int yDirection) { int x = cell.X; int y = cell.Y; bool existEnemyStone = false; while (true) { x = x + xDirection; y = y + yDirection; Game_Cell targetCell = GetCell(x, y); if (targetCell == null || targetCell.GetStoneColor() == StoneColor.None) { return(false); } else if (targetCell.GetStoneColor() == stoneColor) { return(existEnemyStone); } else { existEnemyStone = true; } } }
/// <summary> /// 指定場所のマスを取得します /// </summary> Game_Cell GetCell(int x, int y) { Game_Cell target_Cell = new Game_Cell(); foreach (Game_Cell cell in cells) { if (cell.X == x && cell.Y == y) { target_Cell = cell; } } return(target_Cell); }
protected override void Awake() { base.Awake(); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { Game_Cell cell = Instantiate(cellPrefab); cell.transform.SetParent(transform); cell.transform.localScale = Vector3.one; cell.Initialize(x, y); cells.Add(cell); } } }
/// <summary> /// 石が配置可能なマスかどうか確認します /// </summary> bool IsStonePuttableCell(Game_Cell cell, StoneColor stoneColor) { if (cell.GetStoneColor() == StoneColor.None) { return(Turncheck(cell, stoneColor, -1, -1) || Turncheck(cell, stoneColor, -1, 0) || Turncheck(cell, stoneColor, -1, 1) || Turncheck(cell, stoneColor, 0, -1) || Turncheck(cell, stoneColor, 0, 1) || Turncheck(cell, stoneColor, 1, -1) || Turncheck(cell, stoneColor, 1, 0) || Turncheck(cell, stoneColor, 1, 1)); } else { return(false); } }
/// <summary> /// 石が配置可能なマスかどうか確認します /// </summary> /// <returns><c>true</c> if this instance is stone puttable cell the specified cell; otherwise, <c>false</c>.</returns> /// <param name="cell">Cell.</param> bool IsStonePuttableCell(Game_Cell cell, StoneColor stoneColor) { if (cell.StoneColor == StoneColor.None) { // マスが空の場合は、該当マスから8方向に対して相手の石を挟める状態かどうかチェック return(ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, -1, -1) || ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, -1, 0) || ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, -1, 1) || ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, 0, -1) || ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, 0, 1) || ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, 1, -1) || ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, 1, 0) || ExistsOwnStoneAtTheOtherSideOfEnemyStoneForDirection(cell, stoneColor, 1, 1)); } else { return(false); } }
/// <summary> /// 石をひっくり返すコルーチンです /// </summary> /// <returns>The over stone if possible coroutine.</returns> /// <param name="cell">Cell.</param> IEnumerator TurnOverStoneIfPossibleCoroutine(Game_Cell cell) { // 誤操作防止のため全マスをクリック不可にする Lock(); // 8方向それぞれに対し、ひっくり返す処理を実行 turnStoneForDirectionIfPossibleCoroutineCount = 8; StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, -1, -1)); StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, -1, 0)); StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, -1, 1)); StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, 0, -1)); StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, 0, 1)); StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, 1, -1)); StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, 1, 0)); StartCoroutine(TurnStoneForDirectionIfPossibleCoroutine(cell, 1, 1)); while (turnStoneForDirectionIfPossibleCoroutineCount > 0) { yield return(new WaitForSeconds(0.1f)); } yield return(new WaitForSeconds(0.5f)); Game_SceneController.Instance.OnTurnStoneFinished(); }
/// <summary> /// 指定したマスを起点に、可能であれば石をひっくり返します /// </summary> public void TurnOverStoneIfPossible(Game_Cell cell) { StartCoroutine(CallTurnStoneCoroutine(cell)); }