Skip to content

Commit

Permalink
Merge pull request #44 from BitBoard/fix/41
Browse files Browse the repository at this point in the history
持ち駒に関する様々な修正
  • Loading branch information
youkura-ikuzee authored Feb 18, 2024
2 parents 40feb18 + badcd10 commit c0f7002
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 53 deletions.
44 changes: 17 additions & 27 deletions Assets/Scripts/Data/PieceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,44 @@ public static PieceType StrToPieceType(string pieceTypeStr)
}
}

public static string PieceTypeToStr(PieceType pieceType)
public static string PieceTypeToStr(PieceType pieceType, bool isAIFirst)
{
// ここでPieceTypeから文字列に変換する
// 例: PieceType.WhitePawn -> "white_pawn"
switch (pieceType)
{
case PieceType.WhitePawn:
return "white_pawn";
return isAIFirst ? "white_pawn" : "black_pawn";
case PieceType.WhiteLance:
return "white_lance";
return isAIFirst ? "white_lance" : "black_lance";
case PieceType.WhiteKnight:
return "white_knight";
return isAIFirst ? "white_knight" : "black_knight";
case PieceType.WhiteSilver:
return "white_silver";
return isAIFirst ? "white_silver" : "black_silver";
case PieceType.WhiteGold:
return "white_gold";
return isAIFirst ? "white_gold" : "black_gold";
case PieceType.WhiteBishop:
return "white_bishop";
return isAIFirst ? "white_bishop" : "black_bishop";
case PieceType.WhiteRook:
return "white_rook";
return isAIFirst ? "white_rook" : "black_rook";
case PieceType.WhiteKing:
return "white_king";
return isAIFirst ? "white_king" : "black_king";
case PieceType.BlackPawn:
return "black_pawn";
return isAIFirst ? "black_pawn" : "white_pawn";
case PieceType.BlackLance:
return "black_lance";
return isAIFirst ? "black_lance" : "white_lance";
case PieceType.BlackKnight:
return "black_knight";
return isAIFirst ? "black_knight" : "white_knight";
case PieceType.BlackSilver:
return "black_silver";
return isAIFirst ? "black_silver" : "white_silver";
case PieceType.BlackGold:
return "black_gold";
return isAIFirst ? "black_gold" : "white_gold";
case PieceType.BlackBishop:
return "black_bishop";
return isAIFirst ? "black_bishop" : "white_bishop";
case PieceType.BlackRook:
return "black_rook";
return isAIFirst ? "black_rook" : "white_rook";
case PieceType.BlackKing:
return "black_king";
return isAIFirst ? "black_king" : "white_king";
default:
return "none";
}
Expand Down Expand Up @@ -183,16 +183,6 @@ public static PieceType GetCapturePieceType(PieceType pieceType)
}
}

/// <summary>
/// 取った駒を取ったプレイヤーの駒に変換する
/// </summary>
/// <param name="pieceType"></param>
/// <returns></returns>
public static string CapturePieceTypeToStr(PieceType pieceType)
{
return PieceTypeToStr(GetCapturePieceType(pieceType));
}

public static PieceType[] getPieceTypeList(bool isBlack)
{
return isBlack ?
Expand Down
75 changes: 49 additions & 26 deletions Assets/Scripts/GameSceneScreen/GameSceneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void SetCells()
{
return;
}
await MovePiece(cell, isBlackTurn);
await MovePiece(cell);
}
});

Expand Down Expand Up @@ -269,7 +269,7 @@ private async UniTask SelectPiece(Piece piece)
// 選択されている駒を移動させる
selectedPiece.transform.SetParent(piece.transform.parent);
// 移動先のマスの駒を取る
CapturePiece(piece, isBlackTurn);
CapturePiece(piece);
// 駒の位置を更新する
selectedPiece.transform.localPosition = Vector3.zero;
selectedPiece.piecePotition = new PieceData.PiecePotition(piece.piecePotition.x, piece.piecePotition.y);
Expand Down Expand Up @@ -301,7 +301,7 @@ private async UniTask SelectPiece(Piece piece)
/// </summary>
/// <param name="cell"></param>
/// <param name="isBlack"></param>
private async UniTask MovePiece(Cell cell, bool isBlack)
private async UniTask MovePiece(Cell cell)
{
if (!isPieceSelected)
{
Expand Down Expand Up @@ -380,24 +380,24 @@ private async UniTask MovePiece(Cell cell, bool isBlack)

// 持ち駒が消費されていた場合は持ち駒情報を更新する
if(selectedPiece.IsCaptured()) {
capturePieceAreaData.UpdateCapturePieceData(selectedPiece.pieceType, isBlack, selectedPiece.IsCaptured());
capturePieceAreaData.UpdateCapturePieceData(selectedPiece.pieceType, IsUpdateBlack(), selectedPiece.IsCaptured());
Debug.Log("持ち駒情報の更新 \n" + capturePieceAreaData);
// 駒数表示を非有効化
selectedPiece.IsActivePeiceNumText(0);

var capturePieceArea = isBlack ? view.BlackCapturePieceArea : view.WhiteCapturePieceArea;
var capturePieceArea = IsPlayerTurn() ? view.BlackCapturePieceArea : view.WhiteCapturePieceArea;

// 最新のcapturePieceAreaDataを反映する前に古い持ち駒の表示を削除
view.ClearCapturePieceArea(capturePieceArea.transform);

// 駒台を更新
foreach (PieceType pt in PieceData.getPieceTypeList(isBlack))
foreach (PieceType pt in PieceData.getPieceTypeList(IsUpdateBlack()))
{
var pieceNum = capturePieceAreaData.getPieceNum(pt, isBlack);
var pieceNum = capturePieceAreaData.getPieceNum(pt, IsUpdateBlack());
if (pieceNum == 0) continue;

var pieceByPrefab = Instantiate(piecePrefab, capturePieceArea.transform);
pieceByPrefab.GetComponent<Image>().sprite = Resources.Load<Sprite>("ShogiUI/Piece/" + PieceData.PieceTypeToStr(pt));
pieceByPrefab.GetComponent<Image>().sprite = Resources.Load<Sprite>("ShogiUI/Piece/" + PieceData.PieceTypeToStr(pt, IsPlayerBlack()));
pieceByPrefab.GetComponent<Piece>().piecePotition = new PieceData.PiecePotition(-1, -1);
pieceByPrefab.GetComponent<Piece>().pieceType = pt;
pieceByPrefab.GetComponent<Piece>().IsActivePeiceNumText(pieceNum);
Expand Down Expand Up @@ -443,25 +443,25 @@ private async UniTask MovePiece(Cell cell, bool isBlack)
/// </summary>
/// <param name="piece"></param>
/// <param name="isBlack"></param>
private void CapturePiece(Piece piece, bool isBlack)
private void CapturePiece(Piece piece)
{
var capturePieceArea = isBlack ? view.BlackCapturePieceArea : view.WhiteCapturePieceArea;
var capturePieceArea = IsPlayerTurn() ? view.BlackCapturePieceArea : view.WhiteCapturePieceArea;
var pieceType = piece.pieceType;
Debug.Log("取った駒:" + pieceType);

capturePieceAreaData.UpdateCapturePieceData(pieceType, isBlack);
capturePieceAreaData.UpdateCapturePieceData(pieceType, IsUpdateBlack());
Debug.Log("持ち駒情報の更新 \n" + capturePieceAreaData);

// 最新のcapturePieceAreaDataを反映する前に古い持ち駒の表示を削除
view.ClearCapturePieceArea(capturePieceArea.transform);

foreach (PieceType pt in PieceData.getPieceTypeList(isBlack))
foreach (PieceType pt in PieceData.getPieceTypeList(IsUpdateBlack()))
{
var pieceNum = capturePieceAreaData.getPieceNum(pt, isBlack);
var pieceNum = capturePieceAreaData.getPieceNum(pt, IsUpdateBlack());
if(pieceNum == 0) continue;

var pieceByPrefab = Instantiate(piecePrefab, capturePieceArea.transform);
pieceByPrefab.GetComponent<Image>().sprite = Resources.Load<Sprite>("ShogiUI/Piece/" + PieceData.PieceTypeToStr(pt));
pieceByPrefab.GetComponent<Image>().sprite = Resources.Load<Sprite>("ShogiUI/Piece/" + PieceData.PieceTypeToStr(pt, IsPlayerBlack()));
pieceByPrefab.GetComponent<Piece>().piecePotition = new PieceData.PiecePotition(-1, -1);
pieceByPrefab.GetComponent<Piece>().pieceType = pt;
pieceByPrefab.GetComponent<Piece>().IsActivePeiceNumText(pieceNum);
Expand Down Expand Up @@ -504,11 +504,20 @@ private Piece GetPieceOnBoard(int x, int y)
/// <param name="pieceType"></param>
/// <param name="isBlack"></param>
/// <returns></returns>
private Piece GetCapturedPiece(PieceType pieceType, bool isBlack)
private Piece GetCapturedPiece(PieceType pieceType)
{
var capturePieceArea = isBlack ? view.BlackCapturePieceArea : view.WhiteCapturePieceArea;
var piece = capturePieceArea.GetComponentInChildren<Piece>();
return piece;
var capturePieceArea = IsPlayerTurn() ? view.BlackCapturePieceArea : view.WhiteCapturePieceArea;
// 駒台にある駒で指定されたPieceTypeのものを取得
var pieceList = capturePieceArea.GetComponentsInChildren<Piece>();
foreach (var piece in pieceList)
{
if (piece.pieceType == pieceType)
{
return piece;
}
}

return null;
}

private bool IsPlayerTurn()
Expand All @@ -517,10 +526,22 @@ private bool IsPlayerTurn()
return isBlackTurn != isAIFirst;
}

/// <summary>
/// AIによる着手を行う
/// </summary>
private async UniTask GetAIAction()
private bool IsPlayerBlack()
{
// プレイヤーの持つ駒のPieceTypeがBlackかWhiteかを判定
return !isAIFirst;
}

private bool IsUpdateBlack()
{
// 更新したい情報に関連するPieceTypeがBlackに紐づいているかWhiteに紐づいているかを判定
return IsPlayerBlack() == IsPlayerTurn();
}

/// <summary>
/// AIによる着手を行う
/// </summary>
private async UniTask GetAIAction()
{
await UniTask.Delay(2000);
var move = battleAI.GetMove(gameState);
Expand All @@ -535,7 +556,8 @@ private async UniTask GetAIAction()
Debug.Log("fromX:" + fromX + " fromY:" + fromY + " toX:" + toX + " toY:" + toY);
isPieceSelected = true;

if(isAIFirst)
// AIが先手の場合は座標を反転させる
if(!IsPlayerBlack())
{
fromX = 8 - fromX;
fromY = 8 - fromY;
Expand All @@ -547,8 +569,9 @@ private async UniTask GetAIAction()
if (move.IsDrop())
{
var pieceType = Converter.DropPieceToPieceType(move.DroppedPiece(), isAIFirst);
selectedPiece = GetCapturedPiece(pieceType, isAIFirst);
await MovePiece(cells[toY, toX], isAIFirst);
Debug.Log("打つ駒:" + pieceType);
selectedPiece = GetCapturedPiece(pieceType);
await MovePiece(cells[toY, toX]);
return;
}

Expand All @@ -572,7 +595,7 @@ private async UniTask GetAIAction()
}
else
{
await MovePiece(cells[toY, toX], isAIFirst);
await MovePiece(cells[toY, toX]);
}

// 変数を初期化
Expand Down

0 comments on commit c0f7002

Please sign in to comment.