Skip to content

Commit

Permalink
Merge branch 'main' into dev-sagara
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrwaaa committed Feb 10, 2024
2 parents 2b44e5f + 7d5b361 commit 257f11e
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Assets/Prefabs/Shogi/Piece.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ RectTransform:
m_Children: []
m_Father: {fileID: 7220746899906434829}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &7068772002625486135
CanvasRenderer:
Expand Down
55 changes: 31 additions & 24 deletions Assets/Scripts/Data/PieceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,50 +99,57 @@ public static string PieceTypeToStr(PieceType pieceType)
return "none";
}
}

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

public static PieceType GetCapturePieceType(PieceType pieceType)
{
// ここで取った駒を取ったプレイヤーの駒に変換する
// 例: PieceType.WhitePawn -> PieceType.BlackPawn
switch (pieceType)
{
case PieceType.WhitePawn:
return "black_pawn";
return PieceType.BlackPawn;
case PieceType.WhiteLance:
return "black_lance";
return PieceType.BlackLance;
case PieceType.WhiteKnight:
return "black_knight";
return PieceType.BlackKnight;
case PieceType.WhiteSilver:
return "black_silver";
return PieceType.BlackSilver;
case PieceType.WhiteGold:
return "black_gold";
return PieceType.BlackGold;
case PieceType.WhiteBishop:
return "black_bishop";
return PieceType.BlackBishop;
case PieceType.WhiteRook:
return "black_rook";
return PieceType.BlackRook;
case PieceType.WhiteKing:
return "black_king";
return PieceType.BlackKing;
case PieceType.BlackPawn:
return "white_pawn";
return PieceType.WhitePawn;
case PieceType.BlackLance:
return "white_lance";
return PieceType.WhiteLance;
case PieceType.BlackKnight:
return "white_knight";
return PieceType.WhiteKnight;
case PieceType.BlackSilver:
return "white_silver";
return PieceType.WhiteSilver;
case PieceType.BlackGold:
return "white_gold";
return PieceType.WhiteGold;
case PieceType.BlackBishop:
return "white_bishop";
return PieceType.WhiteBishop;
case PieceType.BlackRook:
return "white_rook";
return PieceType.WhiteRook;
case PieceType.BlackKing:
return "white_king";
return PieceType.WhiteKing;
default:
return "none";
return PieceType.None;
}
}

/// <summary>
/// 取った駒を取ったプレイヤーの駒に変換する
/// </summary>
/// <param name="pieceType"></param>
/// <returns></returns>
public static string CapturePieceTypeToStr(PieceType pieceType)
{
return PieceTypeToStr(GetCapturePieceType(pieceType));
}
}
37 changes: 31 additions & 6 deletions Assets/Scripts/GameSceneScreen/GameSceneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ private void SelectPiece(Piece piece)
Debug.Log("選択した駒:" + piece.ToString());
return;
}

// 持ち駒を他の駒がある位置に置こうとした場合
if (selectedPiece.IsCaptured())
{
Debug.Log("不正な手です");
isPieceSelected = false;
selectedPiece.Outline.SetActive(false);
selectedPiece = null;
return;
}

var from = selectedPiece.SqPos;
var to = piece.SqPos;
Expand Down Expand Up @@ -182,12 +192,22 @@ private void MovePiece(Cell cell)
{
return;
}

var from = selectedPiece.SqPos;
var to = cell.SqPos;


Move move;
if (selectedPiece.IsCaptured())
{
var pt = Converter.PieceTypeToDropPiece(selectedPiece.pieceType);
var to = cell.SqPos;
move = Util.MakeMoveDrop(pt, to);
}
else
{
var from = selectedPiece.SqPos;
var to = cell.SqPos;
move = Util.MakeMove(from, to);
}

// 合法手かどうかを判定する
var move = Util.MakeMove(from, to);
Debug.Log("指し手:" + move.Pretty());
if (!gameState.IsValidMove(move))
{
Expand Down Expand Up @@ -226,7 +246,12 @@ private void CapturePiece(Piece piece, bool isBlack)
Debug.Log("取った駒:" + pieceType);
var capturePiece = Instantiate(piecePrefab, capturePieceArea.transform);
capturePiece.GetComponent<Image>().sprite = Resources.Load<Sprite>("ShogiUI/Piece/" + PieceData.CapturePieceTypeToStr(pieceType));
capturePiece.GetComponent<Piece>().pieceType = pieceType;
capturePiece.GetComponent<Piece>().pieceType = PieceData.GetCapturePieceType(pieceType);
capturePiece.GetComponent<Piece>().piecePotition = new PieceData.PiecePotition(-1, -1);
capturePiece.GetComponent<Piece>().OnClickAction += () =>
{
SelectPiece(capturePiece.GetComponent<Piece>());
};
// 取った駒を消去する
Destroy(piece.gameObject);
}
Expand Down
6 changes: 6 additions & 0 deletions Assets/Scripts/Shogi/Piece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Piece : MonoBehaviour, IPointerClickHandler
public PieceType pieceType;
public PieceData.PiecePotition piecePotition;
public UnityAction OnClickAction;
public bool isPromoted = false;

public Square SqPos => Converter.PosToSquare(piecePotition.x, piecePotition.y);
public GameObject Outline => outline;
Expand All @@ -32,6 +33,11 @@ public bool IsTurnPlayerPiece(bool isBlackTurn)
return pieceTypeNum < 9;
}

public bool IsCaptured()
{
return piecePotition.x == -1 && piecePotition.y == -1;
}

public override string ToString()
{
return Converter.PosToSign(this.piecePotition.x, this.piecePotition.y) + " " + this.pieceType.ToString() +
Expand Down
113 changes: 113 additions & 0 deletions Assets/Scripts/Utils/Converter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using MyShogi.Model.Shogi.Core;
using MSPiece = MyShogi.Model.Shogi.Core.Piece;

public class Converter
{
Expand Down Expand Up @@ -58,6 +60,117 @@ public static string NumToKanji(int num)
return "";
}
}

/// <summary>
/// PieceTypeをMyShogiのPieceに変換する
/// </summary>
/// <param name="pieceType"></param>
/// <param name="isPromoted"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static MSPiece PieceTypeToPiece(PieceType pieceType, bool isPromoted)
{
if (isPromoted)
{
switch (pieceType)
{
case PieceType.BlackPawn:
return MSPiece.B_PRO_PAWN;
case PieceType.BlackLance:
return MSPiece.B_PRO_LANCE;
case PieceType.BlackKnight:
return MSPiece.B_PRO_KNIGHT;
case PieceType.BlackSilver:
return MSPiece.B_PRO_SILVER;
case PieceType.BlackRook:
return MSPiece.B_DRAGON;
case PieceType.BlackBishop:
return MSPiece.B_HORSE;
case PieceType.WhitePawn:
return MSPiece.W_PRO_PAWN;
case PieceType.WhiteLance:
return MSPiece.W_PRO_LANCE;
case PieceType.WhiteKnight:
return MSPiece.W_PRO_KNIGHT;
case PieceType.WhiteSilver:
return MSPiece.W_PRO_SILVER;
case PieceType.WhiteRook:
return MSPiece.W_DRAGON;
case PieceType.WhiteBishop:
return MSPiece.W_HORSE;
default:
throw new Exception("Invalid piece type");
}
}

switch (pieceType)
{
case PieceType.BlackPawn:
return MSPiece.B_PAWN;
case PieceType.BlackLance:
return MSPiece.B_LANCE;
case PieceType.BlackKnight:
return MSPiece.B_KNIGHT;
case PieceType.BlackSilver:
return MSPiece.B_SILVER;
case PieceType.BlackGold:
return MSPiece.B_GOLD;
case PieceType.BlackRook:
return MSPiece.B_ROOK;
case PieceType.BlackBishop:
return MSPiece.B_BISHOP;
case PieceType.BlackKing:
return MSPiece.B_KING;
case PieceType.WhitePawn:
return MSPiece.W_PAWN;
case PieceType.WhiteLance:
return MSPiece.W_LANCE;
case PieceType.WhiteKnight:
return MSPiece.W_KNIGHT;
case PieceType.WhiteSilver:
return MSPiece.W_SILVER;
case PieceType.WhiteGold:
return MSPiece.W_GOLD;
case PieceType.WhiteRook:
return MSPiece.W_ROOK;
case PieceType.WhiteBishop:
return MSPiece.W_BISHOP;
case PieceType.WhiteKing:
return MSPiece.W_KING;
default:
throw new Exception("Invalid piece type");
}
}

public static MSPiece PieceTypeToDropPiece(PieceType pieceType)
{
switch (pieceType)
{
case PieceType.BlackPawn:
case PieceType.WhitePawn:
return MSPiece.PAWN;
case PieceType.BlackLance:
case PieceType.WhiteLance:
return MSPiece.LANCE;
case PieceType.BlackKnight:
case PieceType.WhiteKnight:
return MSPiece.KNIGHT;
case PieceType.BlackSilver:
case PieceType.WhiteSilver:
return MSPiece.SILVER;
case PieceType.BlackGold:
case PieceType.WhiteGold:
return MSPiece.GOLD;
case PieceType.BlackBishop:
case PieceType.WhiteBishop:
return MSPiece.BISHOP;
case PieceType.BlackRook:
case PieceType.WhiteRook:
return MSPiece.ROOK;
default:
throw new Exception("Invalid piece type");
}
}

public static Square PosToSquare(int x, int y)
{
Expand Down

0 comments on commit 257f11e

Please sign in to comment.