Beispiel #1
0
        /// <summary>
        /// 获得当前五子棋策略的下一步落棋位置。
        /// </summary>
        /// <param name="chessBoard">五子棋棋盘对象。</param>
        /// <param name="pieceType">棋子的类型。</param>
        /// <param name="stepIndex">下一步落子操作的序号。</param>
        /// <param name="prevStep">包含对方上一步落子操作信息的走步对象。</param>
        /// <returns>下一步落棋的位置。</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/3/23 9:40    创建
        /// </history>
        public ChessBoardPoint GetNextStep( IChessBoard chessBoard, PieceTypeEnum pieceType, int stepIndex, PlayStep? prevStep )
        {
            //判断是否为黑方的第一手棋
            if (pieceType == PieceTypeEnum.Black && stepIndex == 0)
            {
                //如果是黑方的第一手棋,则总是返回天元
                return ChessBoardPoint.TENGEN;
            }
            //用于保存落子位置的行序号
            int rowIndex = 0;
            //用于保存落子位置的列序号
            int columnIndex = 0;

            //创建随机数生成器
            Random random = new Random();

            //循环直到获得可以落子的位置
            while (true)
            {
                //生成随机行坐标序号(有效的行序号为0~14)
                rowIndex = random.Next() % 15;

                //生成随机列坐标序号(有效的列序号为0~14)
                columnIndex = random.Next() % 15;

                //判断随机落子点是否已经存在棋子
                if (chessBoard.GetPointState( rowIndex, columnIndex ) == PointStateEnum.Blank)
                {
                    //如果随机落子点为空白点,则返回随机落子点
                    return new ChessBoardPoint( rowIndex, columnIndex );
                }
            }
        }
Beispiel #2
0
        public void MoveUp(PlayStep step)
        {
            var oldIndex = Play.Steps.IndexOf(step);

            if (oldIndex > 0)
            {
                Play.Steps.Move(oldIndex, oldIndex - 1);
            }
            Normalize();
        }
Beispiel #3
0
        public void MoveDown(PlayStep step)
        {
            var oldIndex = Play.Steps.IndexOf(step);

            if (oldIndex < Play.Steps.Count - 1)
            {
                Play.Steps.Move(oldIndex, oldIndex + 1);
            }
            Normalize();
        }
Beispiel #4
0
        public void AddStep()
        {
            var s = new PlayStep()
            {
                Actor = "A"
            };

            Play.Steps.Add(s);
            s.PropertyChanged += Step_PropertyChanged;
            Normalize();
        }
Beispiel #5
0
        public PlayEditorViewModel(Client apiClient, Tags tags, SectionDetail section, Func <Task> onSave, PlayDetail play, bool isCopy) : this(apiClient, tags, section, onSave)
        {
            Play.AGuardKey         = play.AGuardKey;
            Play.PGuardKey         = play.PGuardKey;
            Play.AGuardModifierKey = play.AGuardModifierKey;
            Play.PGuardModifierKey = play.PGuardModifierKey;
            Play.MeasureKey        = play.MeasureKey;

            if (!isCopy)
            {
                Play.PlayKey     = play.PlayKey;
                Play.VariantName = play.VariantName;
            }
            else
            {
                if (Play.SectionKey == play.SectionKey) //copy into same section
                {
                    Play.VariantName = "Copy of " + play.VariantName;
                }
                else //copy into different section
                {
                    Play.VariantName = play.VariantName;
                }
            }

            foreach (var step in play.Steps)
            {
                PlayStep item = new PlayStep()
                {
                    Actor                        = step.Actor,
                    FootworkKey                  = step.FootworkKey,
                    GuardKey                     = step.GuardKey,
                    GuardModifierKey             = step.GuardModifierKey,
                    IntermediateGuardKey         = step.IntermediateGuardKey,
                    IntermediateGuardModifierKey = step.IntermediateGuardModifierKey,
                    Notes                        = step.Notes,
                    TargetKey1                   = step.TargetKey1,
                    TargetKey2                   = step.TargetKey2,
                    TargetKey3                   = step.TargetKey3,
                    TechniqueKey1                = step.TechniqueKey1,
                    TechniqueKey2                = step.TechniqueKey2,
                    TechniqueKey3                = step.TechniqueKey3,
                    TempoNumber                  = step.TempoNumber
                };
                Play.Steps.Add(item);

                item.PropertyChanged += Step_PropertyChanged;
            }
        }
Beispiel #6
0
        public ChessBoardPoint GetNextStep(IChessBoard chessBoard, PieceTypeEnum pieceType, int stepIndex, PlayStep? prevStep)
        {
            int[] tupleScoreTable = new int[11] { 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            //initialize tuple score table
            if (stepIndex % 2 == 0)//even step is first hand,i.e. the black side
            {
                if (stepIndex == 0)
                {
                    return ChessBoardPoint.TENGEN;
                }
                else if(stepIndex==2)
                {
                    Position[][] classicalOpen = new Position[8][];
                    //0:white is on left side of Tengen
                    classicalOpen[0]=new Position[21]{
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};
                    //1:white is on up side of Tengen
                    classicalOpen[1]=new Position[21]{
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};
                    //2:white is on right side of Tengen
                    classicalOpen[2]=new Position[21]{
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};
                    //3:white is on bottom side of Tengen
                    classicalOpen[3]=new Position[21]{
                        new Position(8,8),new Position(9,7),new Position(7,8),new Position(7,9),
                        new Position(8,9),new Position(6,9),new Position(5,8),new Position(6,8),
                        new Position(6,7),new Position(5,7),new Position(9,9),new Position(5,5),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};
                    //4:white is on topleft corner of Tengen
                    classicalOpen[4]=new Position[25]{
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};
                    //5:white is on topright corner of Tengen
                    classicalOpen[5]=new Position[25]{
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};
                    //6:white is on bottomright corner of Tengen
                    classicalOpen[6]=new Position[25]{
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};
                    //7:white is on bottomleft corner of Tengen
                    classicalOpen[7] = new Position[25]{
                        new Position(6,6),new Position(7,5),new Position(7,6),new Position(6,5),
                        new Position(8,5),new Position(6,8),new Position(9,5),new Position(7,9),
                        new Position(5,5),new Position(5,6),new Position(7,8),new Position(5,8),
                        new Position(5,9),new Position(8,5),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position(),new Position(),new Position(),new Position(),
                        new Position()};

                    Position whiteFirst = new Position(14-prevStep.Value.Location.RowIndex, prevStep.Value.Location.ColumnIndex);
                    switch ()
                    {
                    case :
                        break;
                    }
                }
                tupleScoreTable[1] = 35;
                tupleScoreTable[2] = 800;
                tupleScoreTable[3] = 15000;
                tupleScoreTable[4] = 800000;
                tupleScoreTable[5] = 15;
                tupleScoreTable[6] = 400;
                tupleScoreTable[7] = 1800;
                tupleScoreTable[8] = 100000;
            }
            else//odd step is back hand,i.e. the white side
            {
                tupleScoreTable[1] = 15;
                tupleScoreTable[2] = 400;
                tupleScoreTable[3] = 1800;
                tupleScoreTable[4] = 100000;
                tupleScoreTable[5] = 35;
                tupleScoreTable[6] = 800;
                tupleScoreTable[7] = 15000;
                tupleScoreTable[8] = 800000;
            }

            //extended board,with virtual points
            ExtendedPointStateEnum[,] exPointStates = new ExtendedPointStateEnum[23, 23];
            for (int row = 0; row < 23; row++)
            {
                for (int col = 0; col < 23; col++)
                {
                    if (row < 4 || row > 18 || col < 4 || col > 18)
                    {
                        exPointStates[row, col] = ExtendedPointStateEnum.Virtual;
                    }
                    else
                    {
                        exPointStates[row, col] = (ExtendedPointStateEnum)chessBoard.GetPointState(14 - (row - 4), col - 4);
                    }
                }
            }

            int[,] scoreTable = new int[15, 15];

            /// <summary>calculate type of every tuple</summary>
            /// <description>In order to give a clear train of thought,
            /// I used an intuitionistic method to do this work.
            /// But this results in not efficient.
            /// Every tuple is calculated for twice.
            /// But it's easy to modify it:two ends of a tuple own the same tuple
            /// I'll modify it in next version ,where efficiency becomes bottleneck.
            /// </description>
            //every point indexs 8 tuples around it
            TupleType[, ,] tupleTable = new TupleType[15, 15, 8];
            for (int row = 4; row < 19; row++)
            {
                for (int col = 4; col < 19; col++)
                {
                    int[] white = new int[8];//white points in a tuple
                    int[] black = new int[8];//black points in a tuple
                    #region ---check tuples of every direction
                    //left ,index 0
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row, col - i] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 0] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row, col - i] == ExtendedPointStateEnum.Black)
                        {
                            black[0]++;
                        }
                        else if (exPointStates[row, col - i] == ExtendedPointStateEnum.White)
                        {
                            white[0]++;
                        }
                    }
                    //top left,index 1
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row - i, col - i] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 1] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row - i, col - i] == ExtendedPointStateEnum.Black)
                        {
                            black[1]++;
                        }
                        else if (exPointStates[row - i, col - i] == ExtendedPointStateEnum.White)
                        {
                            white[1]++;
                        }
                    }
                    //up ,index 2
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row - i, col] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 2] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row - i, col] == ExtendedPointStateEnum.Black)
                        {
                            black[2]++;
                        }
                        else if (exPointStates[row - i, col] == ExtendedPointStateEnum.White)
                        {
                            white[2]++;
                        }
                    }
                    //top right,index 3
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row - i, col + i] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 3] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row - i, col + i] == ExtendedPointStateEnum.Black)
                        {
                            black[3]++;
                        }
                        else if (exPointStates[row - i, col + i] == ExtendedPointStateEnum.White)
                        {
                            white[3]++;
                        }
                    }
                    //right,index 4
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row, col + i] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 4] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row, col + i] == ExtendedPointStateEnum.Black)
                        {
                            black[4]++;
                        }
                        else if (exPointStates[row, col + i] == ExtendedPointStateEnum.White)
                        {
                            white[4]++;
                        }
                    }
                    //bottom right,index 5
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row + i, col + i] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 5] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row + i, col + i] == ExtendedPointStateEnum.Black)
                        {
                            black[5]++;
                        }
                        else if (exPointStates[row + i, col + i] == ExtendedPointStateEnum.White)
                        {
                            white[5]++;
                        }
                    }
                    //bottom,index 6
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row + i, col] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 6] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row + i, col] == ExtendedPointStateEnum.Black)
                        {
                            black[6]++;
                        }
                        else if (exPointStates[row + i, col] == ExtendedPointStateEnum.White)
                        {
                            white[6]++;
                        }
                    }
                    //bottom left,index 7
                    for (int i = 0; i < 5; i++)
                    {
                        if (exPointStates[row + i, col - i] == ExtendedPointStateEnum.Virtual)
                        {
                            tupleTable[row - 4, col - 4, 7] = TupleType.Virtual;
                            break;
                        }
                        else if (exPointStates[row + i, col - i] == ExtendedPointStateEnum.Black)
                        {
                            black[7]++;
                        }
                        else if (exPointStates[row + i, col - i] == ExtendedPointStateEnum.White)
                        {
                            white[7]++;
                        }
                    }
                    #endregion //check tuples of every direction

                    //decide tuple type
                    for (int i = 0; i < 8; i++)
                    {
                        //already assigned
                        if (tupleTable[row - 4, col - 4, i] == TupleType.Virtual)
                        {
                            continue;
                        }
                        if (white[i] > 0 && black[i] > 0)
                        {
                            tupleTable[row - 4, col - 4, i] = TupleType.Polluted;
                        }
                        else if (white[i] == 0 && black[i] == 0)
                        {
                            tupleTable[row - 4, col - 4, i] = TupleType.Blank;
                        }
                        else if (white[i] == 0)
                        {
                            tupleTable[row - 4, col - 4, i] = (TupleType)black[i];
                        }
                        else
                        {
                            tupleTable[row - 4, col - 4, i] = (TupleType)(white[i] + 4);
                        }
                    }
                }
            }
            #region ---scoreTable calculate
            //calculate score table . using symmetry
            //top left corner
            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    if (exPointStates[row + 4, col + 4] != ExtendedPointStateEnum.Blank)
                    {
                        //this situation has been considered
                        //scoreTable[row,col]=0;
                        continue;
                    }
                    for (int m = 0; m < 5; m++)
                    {
                        if (row >= m)//top right
                        {
                            scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row - m, col + m, 7]];
                        }
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row + m, col, 2]];//bottom
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row + m, col + m, 1]];//bottom right
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row, col + m, 0]];//right
                    }
                }
            }
            //top right corner
            for (int row = 0; row < 8; row++)
            {
                for (int col = 8; col < 15; col++)
                {
                    if (exPointStates[row + 4, col + 4] != ExtendedPointStateEnum.Blank)
                    {
                        //this situation has been considered
                        //scoreTable[row,col]=0;
                        continue;
                    }
                    for (int m = 0; m < 5; m++)
                    {
                        if (row >= m)//top left
                        {
                            scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row - m, col - m, 5]];
                        }
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row + m, col, 2]];//bottom
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row + m, col - m, 3]];//bottom left
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row, col - m, 4]];//left
                    }
                }
            }
            //bottom left corner
            for (int row = 8; row < 15; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    if (exPointStates[row + 4, col + 4] != ExtendedPointStateEnum.Blank)
                    {
                        //this situation has been considered
                        //scoreTable[row,col]=0;
                        continue;
                    }
                    for (int m = 0; m < 5; m++)
                    {
                        if (row + m < 15)//bottom right
                        {
                            scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row + m, col + m, 1]];
                        }
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row - m, col, 6]];//top
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row - m, col + m, 7]];//top right
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row, col + m, 0]];//right
                    }
                }
            }
            //bottom right corner
            for (int row = 8; row < 15; row++)
            {
                for (int col = 8; col < 15; col++)
                {
                    if (exPointStates[row + 4, col + 4] != ExtendedPointStateEnum.Blank)
                    {
                        //this situation has been considered
                        //scoreTable[row,col]=0;
                        continue;
                    }
                    for (int m = 0; m < 5; m++)
                    {
                        if (row + m < 15)//bottom left
                        {
                            scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row + m, col - m, 3]];
                        }
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row - m, col, 6]];//top
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row - m, col - m, 5]];//top left
                        scoreTable[row, col] += tupleScoreTable[(int)tupleTable[row, col - m, 4]];//left
                    }
                }
            }
            #endregion //scoreTable

            //select best position
            List<Position> bestList = new List<Position>();

            //select first valid point
            Position first = new Position(0, 0);
            //all the point is forbidden.connot happen
            //while (IsFobidden(first))
            //{
            //    while (IsFobidden(first))
            //    {
            //        if (first.Col<14)
            //        {
            //            first.Col++;
            //        }
            //        else
            //        {
            //            break;
            //        }
            //    }
            //    if (first.Row<14)
            //    {
            //        first.Row++;
            //    }
            //    else
            //    {
            //        break;
            //    }
            //}
            while (IsFobidden(ref chessBoard, first, pieceType))
            {
                if (first.Col < 14)
                {
                    first.Col++;
                }
                else if (first.Row < 14)
                {
                    first.Row++;
                    first.Col = 0;
                }
                else
                {
                    return new ChessBoardPoint(-1, -1);
                }
            }
            bestList.Add(first);

            Referee checkWin = new Referee();
            //select best points
            for (int row = 0; row < 15; row++)
            {
                for (int col = 0; col < 15; col++)
                {
                    if (scoreTable[row, col] > scoreTable[bestList[0].Row, bestList[0].Col])
                    {
                        Position best = new Position(row, col);
                        if (!IsFobidden(ref chessBoard, best, pieceType))
                        {
                            bestList.Clear();
                            bestList.Add(best);
                        }

                    }
                    else if (scoreTable[row, col] == scoreTable[bestList[0].Row, bestList[0].Col])
                    {
                        Position best = new Position(row, col);
                        if (!IsFobidden(ref chessBoard, best, pieceType))
                        {
                            bestList.Add(best);
                        }
                    }
                }
            }
            //there is no best .connot happen
            if (bestList.Count == 0)
            {
                return new ChessBoardPoint(-1, -1);
            }
            Position ret = bestList[(new Random()).Next(bestList.Count)];
            return new ChessBoardPoint(14 - ret.Row, ret.Col);
        }
Beispiel #7
0
        /// <summary>
        /// 获得当前五子棋策略的下一步落棋位置。
        /// </summary>
        /// <param name="chessBoard">五子棋棋盘对象。</param>
        /// <param name="pieceType">棋子的类型。</param>
        /// <param name="stepIndex">下一步落子操作的序号。</param>
        /// <param name="prevStep">包含对方上一步落子操作信息的走步对象。</param>
        /// <returns>下一步落棋的位置。</returns>
        public ChessBoardPoint GetNextStep(IChessBoard chessBoard, PieceTypeEnum pieceType, int stepIndex, PlayStep? prevStep)
        {
            //change the frame of axes to our(   |_  to |-  )
            //判断是否为黑方的第一手棋
            if (stepIndex == 0&&pieceType == PieceTypeEnum.Black )
            {
                // update score table
                UpdateScoreTable(ref chessBoard,new Position(7,7),PieceTypeEnum.Black);

                //如果是黑方的第一手棋,则总是返回天元
                return ChessBoardPoint.TENGEN;
            }else if(stepIndex==1&&pieceType==PieceTypeEnum.White){
                //change scoreTable to defensive order
                for (int i = 1; i < 5;i++ )
                {
                    int tmp = tupleScoreTable[i];
                    tupleScoreTable[i] = tupleScoreTable[i + 4];
                    tupleScoreTable[i + 4] = tmp;
                }
            }

            Position preStepPos = new Position(14 - prevStep.Value.Location.RowIndex, prevStep.Value.Location.ColumnIndex);

            //update score table for changes caused by previous step
            UpdateScoreTable(ref chessBoard, preStepPos,prevStep.Value.PieceType);

            // position to play chess
            Position bestPos = FindBestPos(ref chessBoard, prevStep.Value.PieceType);

            //update score table for changes caused by this step
            UpdateScoreTable(ref chessBoard,bestPos,prevStep.Value.PieceType==PieceTypeEnum.Black?PieceTypeEnum.White:PieceTypeEnum.Black);

            return new ChessBoardPoint(14-bestPos.Row,bestPos.Col);
        }
Beispiel #8
0
 public void DeleteStep(PlayStep step)
 {
     step.PropertyChanged -= Step_PropertyChanged;
     Play.Steps.Remove(step);
     Normalize();
 }