public BoardIdx?GetCanEatBoardIdx(BoardIdx beEatBoardIdx) { BoardIdx[] boardIdxes = GetCanEatBoardIdxs(); if (boardIdxes == null) { return(null); } for (int i = 0; i < boardIdxes.Length; i++) { Piece piece = chessBoard.GetPiece(boardIdxes[i]); BoardIdx[] canBeEatBoardIdxs = piece.ComputeEatPos(boardIdxes[i].x, boardIdxes[i].y, chessBoard); for (int j = 0; j < canBeEatBoardIdxs.Length; j++) { if (canBeEatBoardIdxs[j].x == beEatBoardIdx.x && canBeEatBoardIdxs[j].y == beEatBoardIdx.y) { return(boardIdxes[i]); } } } return(null); }
BoardIdx[] ComputeEatPosForPointType(int curtBoardX, int curtBoardY, Chessboard chessBoard) { List <BoardIdx> boardIdxList = new List <BoardIdx>(); BoardIdx boardIdx = new BoardIdx(); for (int i = 0; i < eatOffset.Length; i += 2) { boardIdx.x = curtBoardX + eatOffset[i]; boardIdx.y = curtBoardY + eatOffset[i + 1]; if (boardIdx.x < 0 || boardIdx.y < 0 || boardIdx.x >= chessBoard.XCount || boardIdx.y >= chessBoard.YCount) { continue; } if (chessBoard.IsHavPiece(boardIdx.x, boardIdx.y)) { Piece piece = chessBoard.GetPiece(boardIdx); if (piece.Color != Color) { boardIdxList.Add(boardIdx); } } } return(boardIdxList.ToArray()); }
BoardIdx[] ComputeMovePosForPointType(int curtBoardX, int curtBoardY, Chessboard chessBoard) { List <BoardIdx> boardIdxList = new List <BoardIdx>(); BoardIdx boardIdx = new BoardIdx(); for (int i = 0; i < moveOffset.Length; i += 2) { boardIdx.x = curtBoardX + moveOffset[i]; boardIdx.y = curtBoardY + moveOffset[i + 1]; if (boardIdx.x < 0 || boardIdx.y < 0 || boardIdx.x >= chessBoard.XCount || boardIdx.y >= chessBoard.YCount) { continue; } if (chessBoard.IsHavPiece(boardIdx.x, boardIdx.y)) { continue; } boardIdxList.Add(boardIdx); } return(boardIdxList.ToArray()); }
private void MovedEvent(Animation animation) { rect.X = linearPosX.GetCurtValue(); rect.Y = linearPosY.GetCurtValue(); if (linearPosX.IsStop && linearPosY.IsStop) { animation.Stop(); IsMoving = false; boardView.SetPiece(null, boardIdx.x, boardIdx.y); boardView.SetPiece(this, dstBoardIdx.x, dstBoardIdx.y); boardIdx = dstBoardIdx; Chess.ChessView.Refresh(); if (MovedStopEvent != null) { MovedStopEvent(this); } } else { Chess.ChessView.Refresh(); } }
BoardIdx GetPieceKeyBoardIdx(int pieceKey) { BoardIdx boardIdx = new BoardIdx(); boardIdx.x = pieceKey >> 12; boardIdx.y = pieceKey & 0xfff; return(boardIdx); }
public void MovePiece(BoardIdx orgBoardIdx, BoardIdx dstBoardIdx) { Piece piece = GetPiece(orgBoardIdx); piece.IsFirstMove = false; RemovePiece(orgBoardIdx.x, orgBoardIdx.y); LastActionPieceAtPrevBoardIdx = orgBoardIdx; AppendPiece(piece, dstBoardIdx.x, dstBoardIdx.y); }
/// <summary> /// 移动棋子 /// </summary> /// <param name="player">当前移子玩家</param> /// <param name="moveBoardIdx"></param> /// <param name="dstBoardIdx"></param> void Move(Player player, BoardIdx moveBoardIdx, BoardIdx dstBoardIdx, string tips = null) { int nextPlayerIdx = GetNextPlayerIdx(); recordMgr.AppendRecord(curtPlayPlayerIdx, nextPlayerIdx, moveBoardIdx, dstBoardIdx, tips, ChessRecordType.Move); player.EatOrMoveBoardPiece(moveBoardIdx, dstBoardIdx); }
public void Move(BoardIdx orgBoardIdx, BoardIdx dstBoardIdx) { PieceView orgPieceView = boardPieceViews[orgBoardIdx.x, orgBoardIdx.y]; if (orgPieceView == null) { return; } orgPieceView.StartMove(dstBoardIdx); }
/// <summary> /// 吃子 /// </summary> /// <param name="player">当前吃子玩家</param> /// <param name="eatBoardIdx"></param> /// <param name="beEatBoardIdx"></param> void Eat(Player player, BoardIdx eatBoardIdx, BoardIdx beEatBoardIdx, string tips = null) { int nextPlayerIdx = GetNextPlayerIdx(); recordMgr.AppendRecord( curtPlayPlayerIdx, nextPlayerIdx, eatBoardIdx, beEatBoardIdx, tips, ChessRecordType.Eat); player.EatOrMoveBoardPiece(eatBoardIdx, beEatBoardIdx); players[nextPlayerIdx].DelBoardPieceRef(beEatBoardIdx.x, beEatBoardIdx.y); }
/// <summary> /// 添加走子记录 /// </summary> public void AppendRecord( int orgPlayerIdx, int dstPlayerIdx, BoardIdx orgBoardIdx, BoardIdx dstBoardIdx, string tips, ChessRecordType type) { Record record = new Record( gameMgr.chessBoard, orgPlayerIdx, dstPlayerIdx, orgBoardIdx, dstBoardIdx, tips, type); Push(record); }
BoardIdx[] ComputeEatPosForLineType(int curtBoardX, int curtBoardY, Chessboard chessBoard) { List <BoardIdx> boardIdxList = new List <BoardIdx>(); int count = 0; int x; int y; BoardIdx boardIdx = new BoardIdx(); for (int i = 0; i < eatOffset.Length; i += 2) { count = 0; x = curtBoardX; y = curtBoardY; while (true) { count++; x += eatOffset[i]; y += eatOffset[i + 1]; if (moveLimitCount != -1 && count > moveLimitCount) { break; } if (x < 0 || y < 0 || x >= chessBoard.XCount || y >= chessBoard.YCount) { break; } if (chessBoard.IsHavPiece(x, y)) { boardIdx.x = x; boardIdx.y = y; Piece piece = chessBoard.GetPiece(boardIdx); if (piece.Color != Color) { boardIdxList.Add(boardIdx); } break; } } } return(boardIdxList.ToArray()); }
public void Create() { boardPieces = new Piece[XCount, YCount]; LastActionPieceAtBoardIdx = new BoardIdx() { x = -1, y = -1 }; LastActionPieceAtPrevBoardIdx = new BoardIdx() { x = -1, y = -1 }; }
public BoardIdx[] GetAllPieceBoardIdx() { List <BoardIdx> boardIdxList = new List <BoardIdx>(); LinkedListNode <int> node = chessBoardPiecePos.First; for (; node != null; node = node.Next) { BoardIdx boardIdx = GetPieceKeyBoardIdx(node.Value); boardIdxList.Add(boardIdx); } return(boardIdxList.ToArray()); }
/// <summary> /// 判断是否可以吃过路兵 /// </summary> /// <param name="curtRowIdx"></param> /// <param name="curtColIdx"></param> /// <param name="chessBoard"></param> /// <returns></returns> bool CanEatGuoLuPawn(int curtBoardX, int curtBoardY, Chessboard chessBoard) { Piece lastBoardPiece = chessBoard.GetLastActionPiece(); if (lastBoardPiece == null || lastBoardPiece.Color == Color) { return(false); } //1.吃过路兵是选择性的,若要进行,就要在对方走棋后的下一步马上进行,否则就失去机会 if (lastBoardPiece.Type != PieceType.Pawn) { return(false); } //2.要吃子的兵需在其第五行 if (PieceAtBoardDir == BoardDirection.Forward) { if (curtBoardY != 4) { return(false); } } else if (PieceAtBoardDir == BoardDirection.Reverse) { if (curtBoardY != 3) { return(false); } } //3.被吃子的兵需在相邻的列,而且一次就移动二格。 BoardIdx boardIdx = chessBoard.LastActionPieceAtBoardIdx; BoardIdx prevBoardIdx = chessBoard.LastActionPieceAtPrevBoardIdx; if (prevBoardIdx.y + 2 != boardIdx.y) //没有一次移动两格 { return(false); } //不在相邻位置上 if (!((boardIdx.x != curtBoardX - 1 && boardIdx.y != curtBoardY) || (boardIdx.x != curtBoardX + 1 && boardIdx.y != curtBoardY))) { return(false); } return(true); }
public void StartMove(BoardIdx dstBoardIdx) { this.dstBoardIdx = dstBoardIdx; RectangleF dstRect = boardView.GetTableCellRectByBoardIdx(dstBoardIdx); rect.Width = dstRect.Width; rect.Height = dstRect.Height; IsMoving = true; moveAnim.Stop(); linearPosX = new LinearAnimation(rect.X, dstRect.X, moveAnim); linearPosY = new LinearAnimation(rect.Y, dstRect.Y, moveAnim); moveAnim.Start(); }
public Record( Chessboard chessBoard, int orgPlayerIdx, int dstPlayerIdx, BoardIdx orgBoardIdx, BoardIdx dstBoardIdx, string tips, ChessRecordType type) { this.tips = tips; this.type = type; this.orgBoardIdx = orgBoardIdx; this.dstBoardIdx = dstBoardIdx; this.orgPlayerIdx = orgPlayerIdx; this.dstPlayerIdx = dstPlayerIdx; orgPiece = chessBoard.GetPiece(orgBoardIdx); dstPiece = chessBoard.GetPiece(dstBoardIdx); orgPieceIsFirstMove = orgPiece.IsFirstMove; lastActionPieceAtBoardIdx = chessBoard.LastActionPieceAtBoardIdx; lastActionPieceAtPrevBoardIdx = chessBoard.LastActionPieceAtPrevBoardIdx; }
public override BoardIdx[] ComputeEatPos(int curtBoardX, int curtBoardY, Chessboard chessBoard) { moveType = PieceMoveType.Point; BoardIdx[] eatBoardIdx = base.ComputeEatPos(curtBoardX, curtBoardY, chessBoard); //判断是否可以吃过路兵 if (EatGuoLuPawn) { bool canEatPawn = CanEatGuoLuPawn(curtBoardX, curtBoardY, chessBoard); if (canEatPawn) { BoardIdx boardIdx = chessBoard.LastActionPieceAtBoardIdx; List <BoardIdx> boardIdxList = new List <BoardIdx>(); boardIdxList.AddRange(eatBoardIdx); boardIdxList.Add(boardIdx); eatBoardIdx = boardIdxList.ToArray(); } } return(eatBoardIdx); }
public BoardIdx[] GetPieceBoardIdxsByType(PieceType type) { List <BoardIdx> boardIdxList = new List <BoardIdx>(); LinkedListNode <int> node = chessBoardPiecePos.First; for (; node != null; node = node.Next) { BoardIdx boardIdx = GetPieceKeyBoardIdx(node.Value); Piece piece = chessBoard.GetPiece(boardIdx); if (piece.Type == type) { boardIdxList.Add(boardIdx); } } if (boardIdxList.Count == 0) { return(null); } return(boardIdxList.ToArray()); }
public void SetBoardIdx(BoardIdx boardIdx) { this.boardIdx = boardIdx; }
/// <summary> /// 玩家策略走棋 /// </summary> /// <param name="player"></param> void AIPlay(Player player) { int nextPlayerIdx = GetNextPlayerIdx(); //1.优先吃对方的王 BoardIdx[] kingBoardIdx = players[nextPlayerIdx].GetPieceBoardIdxsByType(PieceType.King); BoardIdx? canEatKingBoardIdx = player.GetCanEatBoardIdx(kingBoardIdx[0]); if (canEatKingBoardIdx != null) { Eat(player, canEatKingBoardIdx.Value, kingBoardIdx[0], "(策略:优先吃对方的王)"); return; } //2.王下回合被吃的情况下,优先移动王 kingBoardIdx = player.GetPieceBoardIdxsByType(PieceType.King); canEatKingBoardIdx = players[nextPlayerIdx].GetCanEatBoardIdx(kingBoardIdx[0]); if (canEatKingBoardIdx != null) { Piece piece = chessBoard.GetPiece(kingBoardIdx[0]); BoardIdx[] eatPos = piece.ComputeEatPos(kingBoardIdx[0].x, kingBoardIdx[0].y, chessBoard); BoardIdx[] movePos = piece.ComputeMovePos(kingBoardIdx[0].x, kingBoardIdx[0].y, chessBoard); if (eatPos.Length != 0 || movePos.Length != 0) { for (int i = 0; i < eatPos.Length; i++) { Eat(player, kingBoardIdx[0], eatPos[i], "(策略:王下回合被吃的情况下,优先移动王吃子)"); canEatKingBoardIdx = players[nextPlayerIdx].GetCanEatBoardIdx(eatPos[i]); if (canEatKingBoardIdx == null) { return; } else { recordMgr.CancelRecord(); } } for (int i = 0; i < movePos.Length; i++) { Move(player, kingBoardIdx[0], movePos[i], "(策略:王下回合被吃的情况下,优先移动王)"); canEatKingBoardIdx = players[nextPlayerIdx].GetCanEatBoardIdx(movePos[i]); if (canEatKingBoardIdx == null) { return; } else { recordMgr.CancelRecord(); } } } } //3.吃棋子 BoardIdx?boardIdx = player.GetRandomCanEatBoardIdx(); if (boardIdx != null) { Piece piece = chessBoard.GetPiece(boardIdx.Value); BoardIdx[] canEatBoardIdxs = piece.ComputeEatPos(boardIdx.Value.x, boardIdx.Value.y, chessBoard); Random ra = Tools.Instance.Rand(); int eatIdx = ra.Next(0, canEatBoardIdxs.Length - 1); BoardIdx eatBoardIdx = canEatBoardIdxs[eatIdx]; Eat(player, boardIdx.Value, eatBoardIdx, "(策略:优先吃子)"); } else { //4.没有吃的情况下,随机移动棋子 boardIdx = player.GetRandomCanMoveBoardIdx(); Piece piece = chessBoard.GetPiece(boardIdx.Value); BoardIdx[] canMoveBoardIdxs = piece.ComputeMovePos(boardIdx.Value.x, boardIdx.Value.y, chessBoard); Random ra = Tools.Instance.Rand(); int moveIdx = ra.Next(0, canMoveBoardIdxs.Length - 1); BoardIdx moveBoardIdx = canMoveBoardIdxs[moveIdx]; Move(player, boardIdx.Value, moveBoardIdx, "(策略:没有吃的情况下,随机移动棋子)"); } }
public void AppendPiece(Piece piece, BoardIdx boardIdx) { AppendPiece(piece, boardIdx.x, boardIdx.y); }
public Piece GetPiece(BoardIdx boardIdx) { return(boardPieces[boardIdx.x, boardIdx.y]); }
public RectangleF GetTableCellRectByBoardIdx(BoardIdx boardIdx) { return(GetTableCellRectByBoardIdx(boardIdx.x, boardIdx.y)); }
public void EatOrMoveBoardPiece(BoardIdx playerBoardIdx, BoardIdx dstBoardIdx) { chessBoard.MovePiece(playerBoardIdx, dstBoardIdx); DelBoardPieceRef(playerBoardIdx.x, playerBoardIdx.y); AddBoardPieceRef(dstBoardIdx.x, dstBoardIdx.y); }