Skip to content

Commit

Permalink
AIが先手のときの挙動調査中
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrwaaa committed Feb 17, 2024
1 parent 6c2fee4 commit 0e6473d
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 223 deletions.
40 changes: 31 additions & 9 deletions Assets/Scripts/GameSceneScreen/GameSceneController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void SetCells()
}
}

private async UniTask InitBoard(string boardJsonPath = "", BoardType boardType = BoardType.NoHandicap, bool isBlackTurn = true)
private async UniTask InitBoard(string boardJsonPath = "", BoardType boardType = BoardType.NoHandicap, bool isAIFirst = false)
{
ClearPieces();
capturePieceAreaData = new CapturePieceAreaData();
Expand All @@ -112,7 +112,8 @@ private async UniTask InitBoard(string boardJsonPath = "", BoardType boardType =
gameState = new GameState(boardType);
gameState.ShowBoard();
battleAI = new RandomAI();
this.isBlackTurn = isBlackTurn;
this.isBlackTurn = true;
this.isAIFirst = isAIFirst;

if (String.IsNullOrEmpty(boardJsonPath))
{
Expand All @@ -127,7 +128,16 @@ private async UniTask InitBoard(string boardJsonPath = "", BoardType boardType =
var piece = Instantiate(piecePrefab, cells[data.y, data.x].transform);
piece.GetComponent<Image>().sprite = Resources.Load<Sprite>("ShogiUI/Piece/" + data.pieceType);
piece.GetComponent<Piece>().pieceType = PieceData.StrToPieceType(data.pieceType);
piece.GetComponent<Piece>().piecePotition = new PieceData.PiecePotition(data.x, data.y);
if(isAIFirst)
{
// AIが先手の場合、blackpieceとwhitepieceの配置を逆にする
piece.GetComponent<Piece>().piecePotition = new PieceData.PiecePotition(8 - data.x, 8 - data.y);
}
else
{
piece.GetComponent<Piece>().piecePotition = new PieceData.PiecePotition(data.x, data.y);
}

piece.GetComponent<Piece>().OnClickAction += UniTask.UnityAction(async () =>
{
if (!IsPlayerTurn())
Expand All @@ -139,7 +149,7 @@ private async UniTask InitBoard(string boardJsonPath = "", BoardType boardType =

}

if (!isBlackTurn)
if (isAIFirst)
{
await GetAIAction();
}
Expand Down Expand Up @@ -191,8 +201,8 @@ private async UniTask SelectPiece(Piece piece)
return;
}

var from = selectedPiece.SqPos;
var to = piece.SqPos;
var from = selectedPiece.SqPos(isAIFirst);
var to = piece.SqPos(isAIFirst);

// 合法手かどうかを判定する
var move = Util.MakeMove(from, to);
Expand Down Expand Up @@ -287,15 +297,15 @@ private async UniTask MovePiece(Cell cell, bool isBlack)
if (selectedPiece.IsCaptured())
{
var pt = Converter.PieceTypeToDropPiece(selectedPiece.pieceType);
var to = cell.SqPos;
var to = cell.SqPos(isAIFirst);
move = Util.MakeMoveDrop(pt, to);
decidedMove = move;
}
else
{
// 盤上の駒を移動する場合
var from = selectedPiece.SqPos;
var to = cell.SqPos;
var from = selectedPiece.SqPos(isAIFirst);
var to = cell.SqPos(isAIFirst);
move = Util.MakeMove(from, to);
movePromote = Util.MakeMovePromote(from, to);
decidedMove = move;
Expand Down Expand Up @@ -451,6 +461,7 @@ private Piece GetCapturedPiece(PieceType pieceType, bool isBlack)

private bool IsPlayerTurn()
{
// 「先手のターンかつAIが後手」「後手のターンかつAIが先手」の場合、プレイヤーは手番を持っている
return isBlackTurn != isAIFirst;
}

Expand All @@ -468,9 +479,20 @@ private async UniTask GetAIAction()
var to = move.To();
var toX = Converter.SquareToX(to);
var toY = Converter.SquareToY(to);

Debug.Log("fromX:" + fromX + " fromY:" + fromY + " toX:" + toX + " toY:" + toY);
isPieceSelected = true;

/*
if(isAIFirst)
{
fromX = 8 - fromX;
fromY = 8 - fromY;
toX = 8 - toX;
toY = 8 - toY;
}
*/

// 駒を打つ場合
if (move.IsDrop())
{
Expand Down
10 changes: 5 additions & 5 deletions Assets/Scripts/GameSceneScreen/Popup/ConfigPopupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ConfigPopupController : MonoBehaviour
[SerializeField] private Button closeButton;
[SerializeField] private Button firstPlayerButton;
[SerializeField] private Button secondPlayerButton;
private bool isBlackTurn = true;
private bool isAIFirst = false;

public UnityAction<string, BoardType, bool> action;

Expand Down Expand Up @@ -96,7 +96,7 @@ private void StartGame()
boardType = BoardType.NoHandicap;
break;
}
action.Invoke(boardJsonPath, boardType, isBlackTurn);
action.Invoke(boardJsonPath, boardType, isAIFirst);
ClosePanel();
}

Expand All @@ -108,7 +108,7 @@ private void ClosePanel()

private void ChooseFirstPlayer()
{
isBlackTurn = true;
isAIFirst = false;
firstPlayerButton.image.color = new Color32(101, 173, 211, 255);
secondPlayerButton.image.color = new Color32(255, 255, 255, 255);
firstPlayer.text = "▲先手";
Expand All @@ -117,7 +117,7 @@ private void ChooseFirstPlayer()

private void ChooseSecondPlayer()
{
isBlackTurn = false;
isAIFirst = true;
firstPlayerButton.image.color = new Color32(255, 255, 255, 255);
secondPlayerButton.image.color = new Color32(101, 173, 211, 255);
firstPlayer.text = "△後手";
Expand All @@ -141,7 +141,7 @@ private void DropdownValueChanged(TMP_Dropdown change)
secondPlayerButton.gameObject.SetActive(false);
firstPlayer.text = "△後手";
secondPlayer.text = "▲先手";
isBlackTurn = true;
isAIFirst = true;
}
}
}
5 changes: 4 additions & 1 deletion Assets/Scripts/Shogi/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public class Cell : MonoBehaviour, IPointerClickHandler
public int y;
public UnityAction OnClickAction;

public Square SqPos => Converter.PosToSquare(x, y);
public Square SqPos(bool isAIFirst)
{
return Converter.PosToSquare(x, y, isAIFirst);
}


public void OnPointerClick(PointerEventData eventData)
Expand Down
7 changes: 5 additions & 2 deletions Assets/Scripts/Shogi/Piece.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ public class Piece : MonoBehaviour, IPointerClickHandler
public UnityAction OnClickAction;
public bool isPromoted = false;

public Square SqPos => Converter.PosToSquare(piecePotition.x, piecePotition.y);
public GameObject Outline => outline;


public Square SqPos(bool isAIFirst)
{
return Converter.PosToSquare(piecePotition.x, piecePotition.y, isAIFirst);
}

public void OnPointerClick(PointerEventData eventData)
{
Expand Down
Loading

0 comments on commit 0e6473d

Please sign in to comment.