コード例 #1
0
        Chessboard CreateRecordChessboard(int recordIdx)
        {
            Chessboard recordChessBoard = Tools.Instance.DeepCopyByBinary <Chessboard>(chessboard);

            int tempRecordIdx = recordIdx;

            if (recordIdx >= recordMgr.recordList.Count)
            {
                tempRecordIdx = recordMgr.recordList.Count - 1;
            }

            int    i = -1;
            Record record;
            LinkedListNode <Record> node = recordMgr.recordList.First;

            for (; node != null; node = node.Next)
            {
                i++;
                record = node.Value;
                Piece orgPiece = recordChessBoard.GetPiece(record.orgBoardIdx);
                Piece dstPiece = recordChessBoard.GetPiece(record.dstBoardIdx);
                recordChessBoard.MovePiece(record.orgBoardIdx, record.dstBoardIdx);
                if (i == tempRecordIdx)
                {
                    recordNode = node;
                    break;
                }
            }

            return(recordChessBoard);
        }
コード例 #2
0
ファイル: Player.cs プロジェクト: cymheart/ChessAutoStepTest
        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);
        }
コード例 #3
0
        void AddRecordToListBox()
        {
            Chessboard tmpChessBoard = Tools.Instance.DeepCopyByBinary <Chessboard>(chessboard);

            listBoxRecord.Items.Clear();
            int    i = 0;
            Record record;
            LinkedListNode <Record> node = recordMgr.recordList.First;

            for (; node != null; node = node.Next)
            {
                i++;
                record = node.Value;
                Piece orgPiece = tmpChessBoard.GetPiece(record.orgBoardIdx);
                Piece dstPiece = tmpChessBoard.GetPiece(record.dstBoardIdx);
                tmpChessBoard.MovePiece(record.orgBoardIdx, record.dstBoardIdx);

                string orgIdxMsg = "(" + record.orgBoardIdx.x + "," + record.orgBoardIdx.y + ")";
                string dstIdxMsg = "(" + record.dstBoardIdx.x + "," + record.dstBoardIdx.y + ")";

                switch (record.type)
                {
                case ChessRecordType.Eat:
                {
                    listBoxRecord.Items.Add(i + ". " + orgPiece.Desc + orgIdxMsg + "吃" + dstPiece.Desc + dstIdxMsg + "   " + record.tips);
                }
                break;

                case ChessRecordType.Move:
                {
                    listBoxRecord.Items.Add(i + ". " + orgPiece.Desc + orgIdxMsg + "走到" + dstIdxMsg + "   " + record.tips);
                }
                break;
                }
            }

            string resultMsg = "平局";

            if (gameManager.winPlayerIdx >= 0)
            {
                ChessColor winColor = gameManager.playerPieceColor[gameManager.winPlayerIdx];
                if (winColor == ChessColor.White)
                {
                    resultMsg = "白子胜";
                }
                else
                {
                    resultMsg = "黑子胜";
                }
            }

            listBoxRecord.Items.Add(resultMsg);
        }
コード例 #4
0
ファイル: Piece.cs プロジェクト: cymheart/ChessAutoStepTest
        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());
        }
コード例 #5
0
        public void CreateBoardPieces(Chessboard chessBoard, int placePieceTimeMS = 1000)
        {
            this.chessBoard = chessBoard;

            Piece     piece;
            PieceView pieceView;

            for (int i = 0; i < chessBoard.XCount; i++)
            {
                for (int j = 0; j < chessBoard.YCount; j++)
                {
                    piece = chessBoard.GetPiece(i, j);
                    if (piece == null)
                    {
                        continue;
                    }
                    pieceView = CreatePieceView(piece.Type, piece.Color);
                    pieceView.MovedStopEvent = PieceMovedStoped;

                    boardPieceViews[i, j] = pieceView;
                }
            }

            placeAnim                 = new Animation(Chess.ChessView, animMS, false);
            placeAnim.DurationMS      = placePieceTimeMS;
            placeAnim.AnimationEvent += PlaceEvent;
            placeAnim.Start();
        }
コード例 #6
0
        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;
        }
コード例 #7
0
ファイル: Piece.cs プロジェクト: cymheart/ChessAutoStepTest
        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());
        }
コード例 #8
0
        /// <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, "(策略:没有吃的情况下,随机移动棋子)");
            }
        }