Files
5.24BuYu/Assets/MyGame/aa/UI/Game/ChineseChess/ChessGame/ChessBoardManager.cs
T
2026-09-13 01:40:32 +08:00

115 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 棋盘数据管理(普通类):
/// 维护 9x10 棋子网格与全部棋子数据,负责落子/吃子/模拟回滚等纯数据操作
/// </summary>
public class ChessBoardManager
{
/// <summary>网格数据 grid[x,y](null = 空位)</summary>
private ChessPieceData[,] grid = new ChessPieceData[ChessConst.BoardWidth, ChessConst.BoardHeight];
/// <summary>全部棋子(含已被吃的,id 即添加顺序)</summary>
public List<ChessPieceData> allPieces = new List<ChessPieceData>();
public ChessPieceData GetPiece(int x, int y)
{
if (!ChessConst.InBoard(x, y)) return null;
return grid[x, y];
}
public ChessPieceData GetPiece(Vector2Int p)
{
return GetPiece(p.x, p.y);
}
/// <summary>初始化一副标准开局棋子</summary>
public void InitDefaultBoard()
{
grid = new ChessPieceData[ChessConst.BoardWidth, ChessConst.BoardHeight];
allPieces.Clear();
int id = 0;
// 底线:车马象士将士士象马车
var backRow = new[]
{
ChessPieceType.Ju, ChessPieceType.Ma, ChessPieceType.Xiang, ChessPieceType.Shi,
ChessPieceType.Jiang, ChessPieceType.Shi, ChessPieceType.Xiang, ChessPieceType.Ma, ChessPieceType.Ju
};
// 红方(下方 y0~4)
for (int x = 0; x < ChessConst.BoardWidth; x++)
AddPiece(id++, backRow[x], ChessCamp.Red, x, 0);
AddPiece(id++, ChessPieceType.Pao, ChessCamp.Red, 1, 2);
AddPiece(id++, ChessPieceType.Pao, ChessCamp.Red, 7, 2);
for (int x = 0; x < ChessConst.BoardWidth; x += 2)
AddPiece(id++, ChessPieceType.Bing, ChessCamp.Red, x, 3);
// 黑方(上方 y5~9)
for (int x = 0; x < ChessConst.BoardWidth; x++)
AddPiece(id++, backRow[x], ChessCamp.Black, x, 9);
AddPiece(id++, ChessPieceType.Pao, ChessCamp.Black, 1, 7);
AddPiece(id++, ChessPieceType.Pao, ChessCamp.Black, 7, 7);
for (int x = 0; x < ChessConst.BoardWidth; x += 2)
AddPiece(id++, ChessPieceType.Bing, ChessCamp.Black, x, 6);
}
private ChessPieceData AddPiece(int id, ChessPieceType type, ChessCamp camp, int x, int y)
{
var p = new ChessPieceData
{
id = id,
pieceType = type,
camp = camp,
pos = new Vector2Int(x, y),
isAlive = true
};
allPieces.Add(p);
grid[x, y] = p;
return p;
}
/// <summary>查找某方存活的将 / 帅</summary>
public ChessPieceData FindKing(ChessCamp camp)
{
for (int i = 0; i < allPieces.Count; i++)
{
var p = allPieces[i];
if (p.isAlive && p.camp == camp && p.pieceType == ChessPieceType.Jiang) return p;
}
return null;
}
/// <summary>
/// 真正执行移动(被吃的棋子标记 isAlive=false)。不做任何规则校验,调用方负责校验。
/// 返回被吃的棋子(没吃子返回 null)
/// </summary>
public ChessPieceData ApplyMove(ChessPieceData piece, Vector2Int to)
{
ChessPieceData captured = GetPiece(to);
if (captured != null) captured.isAlive = false;
grid[piece.pos.x, piece.pos.y] = null;
piece.pos = to;
grid[to.x, to.y] = piece;
return captured;
}
/// <summary>
/// 回退一步移动(配合悔棋 / 规则模拟):
/// 把 piece 移回 from,把 captured(可为 null)复活在原位置
/// </summary>
public void UndoMove(ChessPieceData piece, Vector2Int from, ChessPieceData captured)
{
Vector2Int to = piece.pos;
grid[to.x, to.y] = captured;
if (captured != null)
{
captured.isAlive = true;
captured.pos = to;
}
piece.pos = from;
grid[from.x, from.y] = piece;
}
}