Exemplo n.º 1
0
        public static Boolean[][] KnightStep(ChessState state, int col, int row)
        {
            Boolean[][] ret = new Boolean[8][];
            for (int i = 0; i < 8; ++i) ret[i] = new Boolean[8];
            for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) ret[i][j] = false;

            int[] dc = { 1, -1, -2, -2, -1, 1, 2, 2 };
            int[] dr = { -2, -2, -1, 1, 2, 2, 1, -1 };
            int tcol, trow;

            for (int i = 0; i < 8; i++)
            {
                tcol = col + dc[i]; trow = row + dr[i];
                if (tcol >= 0 && tcol < 8 && trow >= 0 && trow < 8)
                {
                    //白马
                    if (state.State[row][col] == ChessType.WKnight)
                    {
                        if (state.State[trow][tcol] == ChessType.None || state.State[trow][tcol] >= ChessType.BPawn && state.State[trow][tcol] <= ChessType.BKing)
                            ret[trow][tcol] = true;
                    }
                    //黑马
                    if (state.State[row][col] == ChessType.BKnight)
                    {
                        if (state.State[trow][tcol] == ChessType.None || state.State[trow][tcol] >= ChessType.WPawn && state.State[trow][tcol] <= ChessType.WKing)
                            ret[trow][tcol] = true;
                    }
                }
            }

            return ret;
        }
Exemplo n.º 2
0
        /// <summary>
        /// OnPlayerPlayChess回调
        /// </summary>
        /// <param name="obj"></param>
        private void ChessBoard_OnPlayerPlayChess(Vector2Int index, ChessState chessState)
        {
            //TODO判定是否是否胜利
            if (JudeWinOrContinue(index, chessState) == true)
            {
                Debug.Log(chessState + "Win");
                if (OnWin != null)
                {
                    OnWin.Invoke();//执行结束的事件 剔除棋盘格子的射线事件
                }
                UIManager.Instance.chessState = chessState;
                UIManager.Instance.gameStatus = GameStatus.Over;
                return;
            }

            playerIndex++;//增加回合

            #region 轮询逻辑
            if (OnTurn != null)
            {
                OnTurn.Invoke(playerIndex);//暂停事件
            }

            #endregion



            //TODO增加栈 来储存10步内的悔棋。
        }
Exemplo n.º 3
0
 public Chess(ChessOwner co, int pos, int n)
 {
     owner    = co;
     posId    = pos;
     chessNum = n;
     state    = ChessState.ALIVE;
 }
Exemplo n.º 4
0
        double VPaiNextState(ChessState state, StrategyState strategy)
        {
            int Tot = 0; bool isKing = state.State[strategy.SlcR][strategy.SlcC] == ChessType.BKing;
            string mstate = ChessState.StateToStr(state);
            string astrategy = StrategyState.StaToStr(strategy);

            //插入决策
            DataOperation.InsertStrategy(astrategy);

            VSTimes[] NsT = new VSTimes[10000];
            NsT = DataOperation.SelectProbTimes(mstate, astrategy);

            //计算V(s+1)
            double NS = 0.0;
            for (int i = 0; i < NsT.Count(); i++)
            {
                NS += NsT[i].Vs * NsT[i].times;
                Tot += NsT[i].times;
            }
            if (Tot > 0) NS /= Tot;

            //从数据库里读出v(s)
            double vs = DataOperation.SelectVState(mstate);

            //从数据库读出瞬时回报值
            double r = DataOperation.SelectReward(mstate, astrategy, isKing);

            //计算现在的v(s)
            vs = vs + a * (r + u * NS - vs);

            return vs;
        }
Exemplo n.º 5
0
        public static String GenerateBoard(ChessState gameState)
        {
            var result = new StringBuilder();

            int emptySpaces = 0;
            int line = 0;

            foreach (ChessPiece p in gameState)
            {
                if (p.IsEmpty)
                    ++emptySpaces;
                else
                {
                    AddEmptySpaces(result, ref emptySpaces);
                    result.Append(Presentation.GetPresentation(p));
                }

                if (++line == 8)
                {
                    AddEmptySpaces(result, ref emptySpaces);
                    result.Append("/");
                    line = 0;
                }
            }

            result.Remove(result.Length - 1, 1);

            return result.ToString();
        }
Exemplo n.º 6
0
        public static Boolean[][] PawnStep(ChessState state, int col, int row)
        {
            Boolean[][] ret = new Boolean[8][];
            for (int i = 0; i < 8; ++i) ret[i] = new Boolean[8];
            for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) ret[i][j] = false;

            //白棋小兵的第一步
            if (row == 1 && state.State[row][col] == ChessType.WPawn)
            {
                if (state.State[row + 1][col] == ChessType.None) ret[row + 1][col] = true;
                if (ret[row + 1][col] && state.State[row + 2][col] == ChessType.None) ret[row + 2][col] = true;
            }
            //黑棋小兵的第一步
            if (row == 6 && state.State[row][col] == ChessType.BPawn)
            {
                if (state.State[row - 1][col] == ChessType.None) ret[row - 1][col] = true;
                if (ret[row - 1][col] && state.State[row - 2][col] == ChessType.None) ret[row - 2][col] = true;
            }
            //白棋小兵的中间步
            if (row < 7 && state.State[row][col] == ChessType.WPawn)
            {
                if (state.State[row + 1][col] == ChessType.None) ret[row + 1][col] = true;
                if (col > 0 && state.State[row + 1][col - 1] >= ChessType.BPawn && state.State[row + 1][col - 1] <= ChessType.BKing) ret[row + 1][col - 1] = true;
                if (col < 7 && state.State[row + 1][col + 1] >= ChessType.BPawn && state.State[row + 1][col + 1] <= ChessType.BKing) ret[row + 1][col + 1] = true;
            }
            //黑棋小兵的中间步
            if (row > 0 && state.State[row][col] == ChessType.BPawn)
            {
                if (state.State[row - 1][col] == ChessType.None) ret[row - 1][col] = true;
                if (col > 0 && state.State[row - 1][col - 1] >= ChessType.WPawn && state.State[row - 1][col - 1] <= ChessType.WKing) ret[row - 1][col - 1] = true;
                if (col < 7 && state.State[row - 1][col + 1] >= ChessType.WPawn && state.State[row - 1][col + 1] <= ChessType.WKing) ret[row - 1][col + 1] = true;
            }
            return ret;
        }
Exemplo n.º 7
0
 public void TransitionToState(ChessState nextState)
 {
     if (nextState != remainState)
     {
         currentState = nextState;
         OnExitState();
     }
 }
Exemplo n.º 8
0
 static void Main(string[] args)
 {
     ChessAIService chess = new ChessAIService();
     ChessState state = new ChessState();
     state.SetupNewGame();
     chess.GameStart();
     string str = chess.GetStrategy(state, true);
     Console.WriteLine(str);
 }
Exemplo n.º 9
0
 // Generation
 public static String Generate(ChessState gameState)
 {
     return String.Format("{0} {1} {2} {3} {4} {5}",
                          GenerateBoard(gameState), gameState.Player,
                          (gameState.Castling.KingsideWhite ? "K" : "") +
                          (gameState.Castling.QueensideWhite ? "Q" : "") +
                          (gameState.Castling.KingsideBlack ? "k" : "") +
                          (gameState.Castling.QueensideBlack ? "q" : ""),
                          gameState.EnPassant == null ? "-" : gameState.EnPassant.ToString(),
                          gameState.HalfMoves,
                          gameState.FullMoves);
 }
Exemplo n.º 10
0
 void SetState(ChessState _state)
 {
     state = _state;
     if (state == ChessState.MANAGE)
     {
         hpBar.SetActive(false);
     }
     else
     {
         hpBar.SetActive(true);
     }
 }
Exemplo n.º 11
0
 private void ShowResultPanel(ChessState curState)
 {
     if (curState == ChessState.BlackChess)
     {
         Instantiate(_blackWinPrefab);
         Debug.Log("Player Win");
     }
     else if (curState == ChessState.WhiteChess)
     {
         Instantiate(_whiteWinPrefab);
         Debug.Log("AI Win");
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// 取消选中
 /// </summary>
 public void CancelChoose()
 {
     if (chessState == ChessState.beChoosed)
     {
         //将被选中时的所有变化还原
         transform.FindChild("白边").gameObject.SetActive(false);
         for (int i = 0; i < GameController.grids.Length; i++)
         {
             GameObject.Find("Grids").transform.GetChild(i).GetComponent <Image>().enabled = false;
         }
         chessState = ChessState.idle;
     }
 }
Exemplo n.º 13
0
        public void Init(ChessState state)
        {
            _chessState = state;

            _chessState.Engine.HumanPlayer = OnlineManager.PlayerColor;
            _chessState.PlayerColor        = OnlineManager.PlayerColor;
            OnlineManager.OnOpponentMove  += OnlineManagerOnOnOpponentMove;
            OnlineManager.OnEndGame       += OnlineManagerOnEndGame;
            OnlineManager.OnSessionClosed += OnlineManagerOnSessionClosed;
            if (_chessState.PlayerColor == ChessPieceColor.Black)
            {
                PlayerLock.GameLock = true;
            }
        }
Exemplo n.º 14
0
 void CommonGetStra(Boolean[][] whtmp, int i, int j, ref double max, ref double[] VSmax, ref string[] Stra, ref int count, ChessState state)
 {
     for (int row = 0; row < 8; row++)
     {
         for (int col = 0; col < 8; col++)
         {
             if (whtmp[row][col])
             {
                 StrategyState ss = new StrategyState(i, j, row, col);
                 double Ns = VPaiNextState(state, ss);
                 if (Ns > max) { max = Ns; count = 1; VSmax[0] = Ns; Stra[0] = StrategyState.StaToStr(ss); }
                 else if (Ns == max) { VSmax[count] = Ns; Stra[count++] = StrategyState.StaToStr(ss); }
             }
         }
     }
 }
Exemplo n.º 15
0
        public void Start()
        {
            Debug.Assert(State == ChessState.WaitingForPlayers);
            State = ChessState.InProgress;

            foreach (var side in Sides)
            {
                if (side.IsAi())
                {
                    continue;
                }

                var player = side.GetPlayer();
                Debug.Assert(player != null);
                SendStartGame(player, Logic.Turn);
            }
        }
Exemplo n.º 16
0
    /// <summary>
    /// 被选中
    /// </summary>
    public void BeChoosed()
    {
        //有白边圈住
        transform.FindChild("白边").gameObject.SetActive(true);

        //棋子稍微上升
        //这里先更新棋盘,测试用
        GameController.UpdateChessGame();


        //可以提示出该棋子能移动的所有位置
        Vector2[] canMovePoints = CanMovePoints().ToArray();
        for (int i = 0; i < canMovePoints.Length; i++)
        {
            GameController.vector2Grids[canMovePoints[i]].GetComponent <Image>().enabled = true;
        }
        chessState = ChessState.beChoosed;
    }
Exemplo n.º 17
0
        double VPaiNextState(ChessState state, StrategyState strategy)
        {
            //int Tot = 0; 
            bool isKing = state.State[strategy.SlcR][strategy.SlcC] == ChessType.WKing;
            string mstate = ChessState.StateToStr(state);
            string astrategy = StrategyState.StaToStr(strategy);

            //插入决策
            DataOperation.InsertStrategy(astrategy);

            // VSTimes[] NsT = new VSTimes[10000];
            //NsT = DataOperation.SelectProbTimes(mstate, astrategy);

            ////计算V(s+1)
            //double NS = 0.0;
            //for (int i = 0; i < NsT.Count(); i++)
            //{
            //    NS += NsT[i].Vs * NsT[i].times;
            //    Tot += NsT[i].times;
            //}
            //if (Tot > 0) NS /= Tot;

            //从数据库里读出Q(s,a)
            double qs = DataOperation.SelectQState(mstate, astrategy);

            //从数据库读出瞬时回报值
            double r = DataOperation.SelectReward(mstate, astrategy, isKing);

            //计算现在的q(s,a)
            qs = qs + a * (r + u * DataOperation.SelectVState(mstate) - qs);

            //把得到的qs存入数据库
            //DataOperation.InsertQState(mstate, astrategy, qs);

            //把得到的qs存入数组
            QState[qcount] = mstate;
            QStrategy[qcount++] = astrategy;

            return qs;
        }
Exemplo n.º 18
0
        public static Boolean[][] BishopStep(ChessState state, int col, int row)
        {
            Boolean[][] ret = new Boolean[8][];
            for (int i = 0; i < 8; ++i) ret[i] = new Boolean[8];
            for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) ret[i][j] = false;

            int[] dc = { 1, -1, -1, 1 };
            int[] dr = { -1, -1, 1, 1 };

            int tcol, trow;
  
            for (int i = 0; i < 4; i++)
            {
                tcol = col; trow = row;
                for (int j = 1; j < 8; j++)
                {
                    tcol += dc[i]; trow += dr[i];
                    if (tcol >= 0 && tcol <= 7 && trow >= 0 && trow <= 7)
                    {
                        //白主教
                        if (state.State[row][col] == ChessType.WBishop)
                        {
                            if (state.State[trow][tcol] == ChessType.None || state.State[trow][tcol] >= ChessType.BPawn && state.State[trow][tcol] <= ChessType.BKing)
                                ret[trow][tcol] = true;
                            if (state.State[trow][tcol] != ChessType.None) break;
                        }
                        //黑主教
                        if (state.State[row][col] == ChessType.BBishop)
                        {
                            if (state.State[trow][tcol] == ChessType.None || state.State[trow][tcol] >= ChessType.WPawn && state.State[trow][tcol] <= ChessType.WKing)
                                ret[trow][tcol] = true;
                            if (state.State[trow][tcol] != ChessType.None) break;
                        }
                    }
                    else break;
                }
            }
            return ret;
        }
Exemplo n.º 19
0
        /// <summary>
        /// 用于放置棋
        /// </summary>
        /// <param name="eventData"></param>
        public void OnPointerClick(PointerEventData eventData)
        {
            if (isOver)
            {
                return;
            }

            //TODO 将点击事件触发给board做下子的触发
            if (isPlaced)
            {
                Debug.Log(string.Concat("该位置已下", index));
                return;
            }
            //  Debug.Log(string.Concat("下棋点击事件", index));

            if (board.playerIndex % 2 == 0)
            {
                //白棋
                chessState     = ChessState.WhiteChess;
                weightedValues = 1;
                //该位置绘制一个白棋
            }
            else
            {
                //黑棋
                chessState     = ChessState.BlackChess;
                weightedValues = 2;
                //该位置绘制一个黑棋
            }
            if (board != null)
            {
                board.OnPlayerPlayChess(index, chessState);
            }
            DrawSpere();//绘制棋牌
            isPlaced = true;
        }
Exemplo n.º 20
0
        public string GetStrategy(ChessState state, Boolean isWhite)
        {
            int i, j, count = 0;
            double max = 0.0;
            double[] VSmax = new double[10000];
            string[] Stra = new string[10000];
            Boolean[][] whtmp;

            //插入状态
            DataOperation.InsertState(ChessState.StateToStr(state));

            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    if (state.State[i][j] >= ChessType.BPawn && state.State[i][j] <= ChessType.BKing)
                    {
                        switch (state.State[i][j])
                        {
                            case ChessType.BPawn:
                                whtmp = ChessLawExe.PawnStep(state, j, i);
                                for (int row = 0; row < 8; row++)
                                {
                                    for (int col = 0; col < 8; col++)
                                    {
                                        if (whtmp[row][col])
                                        {
                                            StrategyState ss = new StrategyState(i, j, row, col);
                                            if (row == 7) ss.Conv = ChessType.BQueen;
                                            double Ns = VPaiNextState(state, ss);
                                            if (Ns > max) { max = Ns; count = 1; VSmax[0] = Ns; Stra[0] = StrategyState.StaToStr(ss); }
                                            else if (Ns == max) { VSmax[count] = Ns; Stra[count++] = StrategyState.StaToStr(ss); }

                                            if (row == 7) ss.Conv = ChessType.BKnight;
                                            Ns = VPaiNextState(state, ss);
                                            if (Ns > max) { max = Ns; count = 1; VSmax[0] = Ns; Stra[0] = StrategyState.StaToStr(ss); }
                                            else if (Ns == max) { VSmax[count] = Ns; Stra[count++] = StrategyState.StaToStr(ss); }
                                        }
                                    }
                                }
                                break;

                            case ChessType.BKnight:
                                whtmp = ChessLawExe.KnightStep(state, j, i);
                                CommonGetStra(whtmp, i, j, ref max, ref VSmax, ref Stra, ref count, state);
                                break;

                            case ChessType.BBishop:
                                whtmp = ChessLawExe.BishopStep(state, j, i);
                                CommonGetStra(whtmp, i, j, ref max, ref VSmax, ref Stra, ref count, state);
                                break;

                            case ChessType.BRook:
                                whtmp = ChessLawExe.RookStep(state, j, i);
                                CommonGetStra(whtmp, i, j, ref max, ref VSmax, ref Stra, ref count, state);
                                break;

                            case ChessType.BQueen:
                                whtmp = ChessLawExe.QueenStep(state, j, i);
                                CommonGetStra(whtmp, i, j, ref max, ref VSmax, ref Stra, ref count, state);
                                break;

                            case ChessType.BKing:
                                whtmp = ChessLawExe.KingStep(state, j, i);
                                CommonGetStra(whtmp, i, j, ref max, ref VSmax, ref Stra, ref count, state);
                                break;
                        }
                    }
                }
            }

            Random rand = new Random();
            int key = rand.Next(0, count);

            double CurrVs = VSmax[key];
            string CurrStr = Stra[key];

            //把得到的qs存入数据库
            for (i = 0; i < qcount; i++)
                DataOperation.InsertQState(QState[i], QStrategy[i], CurrVs);

            QState = new string[10000];
            QStrategy = new string[10000];
            qcount = 0;

            //把状态和决策存入数组
            State[tcount] = ChessState.StateToStr(state);
            Strategy[tcount++] = CurrStr;

            return CurrStr;
        }
Exemplo n.º 21
0
        /// <summary>
        /// 判定是否胜利
        /// </summary>
        private bool JudeWinOrContinue(Vector2Int index, ChessState chess)
        {
            int x = index.x;
            int y = index.y;
            int i = index.x, j = index.y;
            int count = 0; //棋子计数器

            /*计算水平方向连续棋子个数*/
            while (i > -1 && grids[i, j].chessState == chess)
            {
                i--;
                count++; //累加左侧
            }
            i = x + 1;
            while (i < 15 && grids[i, j].chessState == chess)
            {
                i++;
                count++; //累加右侧
            }
            if (count >= ConstKey.WinNum)
            {
                return(true); //获胜
            }
            /*计算竖直方向连续棋子个数*/
            i     = x;
            count = 0;
            while (j > -1 && grids[i, j].chessState == chess)
            {
                j--;
                count++; //累加上方
            }
            j = y + 1;
            while (j < 15 && grids[i, j].chessState == chess)
            {
                j++;
                count++; //累加下方
            }
            if (count >= ConstKey.WinNum)
            {
                return(true); //获胜
            }
            /*计算左上右下方向连续棋子个数*/
            j     = y;
            count = 0;
            while (i > -1 && j > -1 && grids[i, j].chessState == chess)
            {
                i--;
                j--;
                count++; //累加左上
            }
            i = x + 1;
            j = y + 1;
            while (i < 15 && j < 15 && grids[i, j].chessState == chess)
            {
                i++;
                j++;
                count++; //累加右下
            }
            if (count >= ConstKey.WinNum)
            {
                return(true); //获胜
            }
            /*计算右上左下方向连续棋子个数*/
            i     = x;
            j     = y;
            count = 0;
            while (i < 15 && j > -1 && grids[i, j].chessState == chess)
            {
                i++;
                j--;
                count++; //累加右上
            }
            i = x - 1;
            j = y + 1;
            while (i > -1 && j < 15 && grids[i, j].chessState == chess)
            {
                i--;
                j++;
                count++; //累加左下
            }
            if (count >= ConstKey.WinNum)
            {
                return(true); //获胜
            }
            return(false);    //该步没有取胜
        }
Exemplo n.º 22
0
    protected ChessState chessState;    //棋子状态

    public virtual void Awake()
    {
        chessState   = ChessState.idle;
        ChooseEvent += new ChooseEventHandler(CancelChoose); //订阅事件
        EatEvent    += new EatEventHandler(Eat);             //订阅吃事件
    }
Exemplo n.º 23
0
        public ChessState ParseBoard(String s, List<Player> players = null)
        {
            if (players == null) players = GetMockPlayers();
            var gs = new ChessState();

            int file = 1, rank = 8;
            foreach (Char? ch in GetBoardLetters(s))
            {
                var p = ChessPieceFactory.CreatePiece(ch, Presentation, players);
                var pos = new Position(file, rank);
                gs[pos] = p;
                p.Move(pos);

                if (++file > 8) { --rank; file = 1; }
            }

            return gs;
        }
Exemplo n.º 24
0
        public void Finish(int winner)
        {
            if (State != ChessState.WaitingForPlayers && State != ChessState.InProgress)
            {
                return;
            }

            foreach (var side in Sides)
            {
                if (side == null)
                {
                    continue;
                }

                if (side.IsAi())
                {
                    continue;
                }

                var    player       = PlayerManager.FindByGuid(side.PlayerGuid, out bool isOnline);
                Player onlinePlayer = null;
                if (isOnline)
                {
                    onlinePlayer = PlayerManager.GetOnlinePlayer(side.PlayerGuid);
                    SendGameOver(onlinePlayer, winner);
                }

                if (winner != Chess.ChessWinnerEndGame)
                {
                    var totalGames = (player.GetProperty(PropertyInt.ChessTotalGames) ?? 0) + 1;
                    player.SetProperty(PropertyInt.ChessTotalGames, totalGames);

                    if (isOnline)
                    {
                        onlinePlayer.Session.Network.EnqueueSend(new GameMessagePrivateUpdatePropertyInt(onlinePlayer, PropertyInt.ChessTotalGames, totalGames));
                    }
                }

                if (winner >= 0)
                {
                    var winnerColor = (ChessColor)winner;
                    if (winnerColor == side.Color)
                    {
                        var won = (player.GetProperty(PropertyInt.ChessGamesWon) ?? 0) + 1;
                        player.SetProperty(PropertyInt.ChessGamesWon, won);

                        if (isOnline)
                        {
                            onlinePlayer.Session.Network.EnqueueSend(new GameMessagePrivateUpdatePropertyInt(onlinePlayer, PropertyInt.ChessGamesWon, won));
                        }
                    }
                    else
                    {
                        var lost = (player.GetProperty(PropertyInt.ChessGamesLost) ?? 0) + 1;
                        player.SetProperty(PropertyInt.ChessGamesLost, lost);

                        if (isOnline)
                        {
                            onlinePlayer.Session.Network.EnqueueSend(new GameMessagePrivateUpdatePropertyInt(onlinePlayer, PropertyInt.ChessGamesLost, lost));
                        }
                    }
                }

                if (isOnline)
                {
                    onlinePlayer.ChessMatch = null;
                }
            }

            if (winner >= 0)
            {
                // adjust player ranks
                var playerGuid   = Sides[0].PlayerGuid;
                var opponentGuid = Sides[1].PlayerGuid;
                var winnerGuid   = winner == 0 ? playerGuid : opponentGuid;

                AdjustPlayerRanks(playerGuid, opponentGuid, winnerGuid);
            }

            Actions.Clear();

            Logic.WalkPieces((piece) =>
            {
                RemoveWeeniePiece(piece);
            });

            State          = ChessState.Finished;
            NextRangeCheck = null;

            ChessBoard.ChessMatch = null;
        }
Exemplo n.º 25
0
        private void btn_Options_Click(object sender, EventArgs e)
        {
            if (this._computerMoving == true)
            {
                return;
            }

            Form_Options form_temp = new Form_Options(this._language);

            if (form_temp.ShowDialog() == DialogResult.OK && board != null)
            {
                //Save this game

                ChessState[,] SaveThisState = new ChessState[10, 10];

                int i, j;
                for (i = 0; i < 10; i++)
                {
                    for (j = 0; j < 10; j++)
                    {
                        SaveThisState[i, j] = new ChessState(board.arrState[i, j].Type, board.arrState[i, j].Side, board.arrState[i, j].Moves);
                    }
                }

                eGameStatus       gameStatus         = board.GameStatus;
                eGameEndReason    gameStatusReason   = board.GameStatusReason;
                eChessSide        whoTurn            = board.WhoTurn;
                ArrayList         arrWhoCheck        = board.arrWhoCheck;
                Stack <ChessMove> stkUndo            = board.stkUndo;
                Stack <ChessMove> stkRedo            = board.stkRedo;
                Stack <String>    stkChessMoveString = board.stkChessMoveString;
                Stack <Bitmap_Side_ChessPieceEated> stkChessPieceEated = board.stkChessPieceEated;
                bool  clearStackRedo   = board.clear_Stack_Redo;
                Point positionLastMove = new Point(Uc_ChessBoard.PositionLastMove.X, Uc_ChessBoard.PositionLastMove.Y);
                Point positionChoosen  = new Point(board.PositionChoosen.X, board.PositionChoosen.Y);
                Dictionary <string, int> arrPosition = board.arrPosition;

                if (this._gameMode == eGameMode.VsComputer)
                {
                    CreateChessBoard(this._ownSide, this._gameMode, this._gameDifficulty, SaveThisState);
                }
                else
                {
                    CreateChessBoard(this._ownSide, this._gameMode, SaveThisState);
                }

                //Unload this game
                board.GameStatus               = gameStatus;
                board.GameStatusReason         = gameStatusReason;
                board.WhoTurn                  = whoTurn;
                board.arrWhoCheck              = arrWhoCheck;
                board.stkUndo                  = stkUndo;
                board.stkRedo                  = stkRedo;
                board.stkChessMoveString       = stkChessMoveString;
                board.clear_Stack_Redo         = clearStackRedo;
                Uc_ChessBoard.PositionLastMove = new Point(positionLastMove.X, positionLastMove.Y);
                board.PositionChoosen          = new Point(positionChoosen.X, positionChoosen.Y);
                board.arrPosition              = arrPosition;
                board.stkChessPieceEated       = stkChessPieceEated;
                ucChessPieceEated1.LoadChessPieces(board.stkChessPieceEated);
                ucChessPieceEated2.LoadChessPieces(board.stkChessPieceEated);
            }
        }
Exemplo n.º 26
0
        public static Boolean[][] KingStep(ChessState state, int col, int row)
        {
            Boolean[][] ret = new Boolean[8][];
            for (int i = 0; i < 8; ++i) ret[i] = new Boolean[8];
            for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) ret[i][j] = false;

            int[] dc = { 1, -1, -1, 1, 1, 0, -1, 0 };
            int[] dr = { -1, -1, 1, 1, 0, -1, 0, 1 };

            int tcol, trow;

            for (int i = 0; i < 8; i++)
            {
                tcol = col + dc[i]; trow = row + dr[i];
                if (tcol >= 0 && tcol <= 7 && trow >= 0 && trow <= 7)
                {
                    //白王
                    if (state.State[row][col] == ChessType.WKing)
                    {
                        if (state.State[trow][tcol] == ChessType.None || state.State[trow][tcol] >= ChessType.BPawn && state.State[trow][tcol] <= ChessType.BKing)
                            ret[trow][tcol] = true;
                        
                    }
                    //黑王
                    if (state.State[row][col] == ChessType.BKing)
                    {
                        if (state.State[trow][tcol] == ChessType.None || state.State[trow][tcol] >= ChessType.WPawn && state.State[trow][tcol] <= ChessType.WKing)
                            ret[trow][tcol] = true;
                    }
                }
            }

            if (KingRookSwap(state, row, col, row, 0)) ret[row][col - 2] = true;
            else if (KingRookSwap(state, row, col, row, 7)) ret[row][col + 2] = true;

            return ret;
        }
Exemplo n.º 27
0
        public static bool CheckMate(ChessState state, int kr, int kc)
        {
            bool ret = false;
            ChessType Pawn, King;
            if (state.State[kr][kc] == ChessType.BKing) { Pawn = ChessType.WPawn; King = ChessType.WKing; }
            else { Pawn = ChessType.BPawn; King = ChessType.BKing; }

            Boolean[][] Step;

            for (int i = 6; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (state.State[i][j] >= Pawn && state.State[i][j] <= King)
                    {
                        switch (state.State[i][j])
                        {
                            case ChessType.WPawn:
                            case ChessType.BPawn:
                                Step = PawnStep(state, i, j);
                                if (Step[kr][kc]) return ret = true;
                                break;
                            case ChessType.WKnight:
                            case ChessType.BKnight:
                                Step = KnightStep(state, i, j);
                                if (Step[kr][kc]) return ret = true;
                                break;
                            case ChessType.WBishop:
                            case ChessType.BBishop:
                                Step = BishopStep(state, i, j);
                                if (Step[kr][kc]) return ret = true;
                                break;
                            case ChessType.WRook:
                            case ChessType.BRook:
                                Step = RookStep(state, i, j);
                                if (Step[kr][kc]) return ret = true;
                                break;
                            case ChessType.WQueen:
                            case ChessType.BQueen:
                                Step = QueenStep(state, i, j);
                                if (Step[kr][kc]) return ret = true;
                                break;
                            case ChessType.WKing:
                            case ChessType.BKing:
                                Step = KingStep1(state, i, j);
                                if (Step[kr][kc]) return ret = true;
                                break;
                        }
                    }
                }
            }
            return ret;
        }
Exemplo n.º 28
0
        public static Boolean KingRookSwap(ChessState state, int kr, int kc, int rr, int rc)
        {
            Boolean ret = true;

            //条件1 是否动过
            bool move = true;
            if (state.InitState[kr][kc] && state.InitState[rr][rc]) move = false;
            if (move) return ret = false;
            
            //条件2 之间位置是否是空
            int cs, ct;
            bool empty = true;
            if (kc > rc) { cs = rc; ct = kc; }
            else { cs = kc; ct = rc; }
            for (int i = cs + 1; i < ct; i++)
                if (state.State[kr][i] != ChessType.None) empty = false;
            if (!empty) return ret = false;

            //条件3是否被将军
            if (CheckMate(state, kr, kc)) return ret = false;

            return ret;
        }
Exemplo n.º 29
0
 /// <summary>
 /// 悔棋 如果是该步骤实现的则执行此方法
 /// </summary>
 public void Regret()
 {
     isPlaced   = false;
     chessState = ChessState.None;
     //清除绘制
 }
Exemplo n.º 30
0
        private int GetChessValue(Chess chesscolor, int x, int y)
        {                                                                         //求(x,y)點的權值
            int totalmarks;                                                       //總分

            ChessState Cstate = new ChessState();                                 //new 一個 ChessState

            Point left, right, top, down, leftTop, rightTop, leftDown, rightDown; //八個方向

            int temp, connectCount;

            //計算八個方向的坐標
            left = new Point(Math.Max(0, x - 4), y);

            right = new Point(Math.Min(14, x + 4), y);

            top = new Point(x, Math.Max(0, y - 4));

            down = new Point(x, Math.Min(14, y + 4));

            temp = Math.Min(x - left.X, y - top.Y);

            leftTop = new Point(x - temp, y - temp);

            temp = Math.Min(x - left.X, down.Y - y);

            leftDown = new Point(x - temp, y + temp);

            temp = Math.Min(right.X - x, y - top.Y);

            rightTop = new Point(x + temp, y - temp);

            temp = Math.Min(right.X - x, down.Y - y);

            rightDown = new Point(x + temp, y + temp);

            if (chesscolor == Chess.Black)          //下的是黑子
            {
                if (VirtualBox[x, y] != Chess.none) //已經有棋子
                {
                    return(-2);
                }

                else
                {
                    //處理黑棋連子情況

                    VirtualBox[x, y] = Chess.Black;

                    //左右方向

                    connectCount = ConnectCount(Chess.Black, left, right);

                    Cstate.blackConnect[connectCount]++;                     //黑連子

                    if (ActiveThree(Chess.Black, connectCount, left, right)) //有黑活三
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    //上下方向

                    connectCount = ConnectCount(Chess.Black, top, down);

                    Cstate.blackConnect[connectCount]++;

                    if (ActiveThree(Chess.Black, connectCount, top, down))
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    //左上 右下方向

                    connectCount = ConnectCount(Chess.Black, leftTop, rightDown);

                    Cstate.blackConnect[connectCount]++;

                    if (ActiveThree(Chess.Black, connectCount, leftTop, rightDown))
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    //左下 右上方向

                    connectCount = ConnectCount(Chess.Black, leftDown, rightTop);

                    Cstate.blackConnect[connectCount]++;

                    if (ActiveThree(Chess.Black, connectCount, leftDown, rightTop))
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    VirtualBox[x, y] = Chess.none;


                    //處理白棋連子情況

                    VirtualBox[x, y] = Chess.White;

                    //左右方向

                    connectCount = ConnectCount(Chess.White, left, right);

                    Cstate.whiteConnect[connectCount]++;

                    if (BreakActiveThree(Chess.White, connectCount, x, y, left, right))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    //上下方向

                    connectCount = ConnectCount(Chess.White, top, down);

                    Cstate.whiteConnect[connectCount]++;

                    if (BreakActiveThree(Chess.White, connectCount, x, y, top, down))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    //左上_右下方向

                    connectCount = ConnectCount(Chess.White, leftTop, rightDown);

                    Cstate.whiteConnect[connectCount]++;

                    if (BreakActiveThree(Chess.White, connectCount, x, y, leftTop, rightDown))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    //左下_右上方向

                    connectCount = ConnectCount(Chess.White, leftDown, rightTop);

                    Cstate.whiteConnect[connectCount]++;

                    if (BreakActiveThree(Chess.White, connectCount, x, y, leftDown, rightTop))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    if (ActiveThree(Chess.White, 3, left, right) && ConnectCount(Chess.White, left, right) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    if (ActiveThree(Chess.White, 3, top, down) && ConnectCount(Chess.White, top, down) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    if (ActiveThree(Chess.White, 3, leftTop, rightDown) && ConnectCount(Chess.White, leftTop, rightDown) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    if (ActiveThree(Chess.White, 3, leftDown, rightTop) && ConnectCount(Chess.White, leftDown, rightTop) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    VirtualBox[x, y] = Chess.none;

                    //開始求權值


                    if (Cstate.blackActive[3] > 1 || Cstate.blackActive[4] > 1 || SixConnect(x, y)) //禁手

                    {
                        return(-1);
                    }

                    else if (Cstate.blackConnect[5] > 0) //黑五連子

                    {
                        return(150000);
                    }

                    else if (Cstate.whiteConnect[5] > 0) //白五連子

                    {
                        return(140000);
                    }

                    else if (Cstate.blackActive[4] > 0 || Cstate.blackConnect[4] > 1) //黑連四/黑四子

                    {
                        return(130000);
                    }

                    else if (Cstate.blackConnect[4] == 1 && Cstate.blackActive[3] == 1) //黑四子/黑活三

                    {
                        return(120000);
                    }

                    else if (Cstate.blackConnect[4] == 1 && Cstate.blackConnect[3] > 0)//黑四子/黑三字

                    {
                        return(110000);
                    }

                    else if (Cstate.whiteActive[4] > 0 || Cstate.whiteConnect[4] > 1) //白連四/白四子

                    {
                        return(100000);
                    }

                    else if (Cstate.whiteConnect[4] == 1 && Cstate.tempActive3 == 1)
                    {
                        return(90000);
                    }

                    else if (Cstate.whiteActive[3] > 1)
                    {
                        return(80000);
                    }

                    else if (Cstate.whiteConnect[4] == 1 && Cstate.whiteConnect[3] > 0)
                    {
                        return(70000);
                    }

                    else
                    {
                        totalmarks = (Cstate.blackConnect[4] + Cstate.blackActive[3]) * 6250 + (Cstate.blackConnect[3] + Cstate.blackActive[2] + Cstate.whiteConnect[4] + Cstate.whiteActive[3]) * 1250

                                     + (Cstate.blackConnect[2] + Cstate.whiteConnect[3] + Cstate.whiteActive[2]) * 250 + Cstate.blackActive[1] * 50 + (Cstate.blackConnect[1] + Cstate.whiteConnect[2] + Cstate.whiteActive[1]) * 10 + Cstate.whiteConnect[1] * 2;

                        return(totalmarks); //回傳總分
                    }
                }
            }

            else //下白棋
            {
                if (VirtualBox[x, y] != Chess.none)
                {
                    return(-2);
                }

                else
                {
                    //處理黑子連子情況

                    VirtualBox[x, y] = Chess.Black;

                    //左右方向

                    connectCount = ConnectCount(Chess.Black, left, right);

                    Cstate.blackConnect[connectCount]++;

                    if (BreakActiveThree(Chess.Black, connectCount, x, y, left, right))
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    //上下方向

                    connectCount = ConnectCount(Chess.Black, top, down);

                    Cstate.blackConnect[connectCount]++;

                    if (BreakActiveThree(Chess.Black, connectCount, x, y, top, down))
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    //左上 右下方向

                    connectCount = ConnectCount(Chess.Black, leftTop, rightDown);

                    Cstate.blackConnect[connectCount]++;

                    if (BreakActiveThree(Chess.Black, connectCount, x, y, leftTop, rightDown))
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    //左下 右上方向

                    connectCount = ConnectCount(Chess.Black, leftDown, rightTop);

                    Cstate.blackConnect[connectCount]++;

                    if (BreakActiveThree(Chess.Black, connectCount, x, y, leftDown, rightTop))
                    {
                        Cstate.blackConnect[connectCount]--;

                        Cstate.blackActive[connectCount]++;
                    }

                    if (ActiveThree(Chess.Black, 3, left, right) && ConnectCount(Chess.Black, left, right) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    if (ActiveThree(Chess.Black, 3, top, down) && ConnectCount(Chess.Black, top, down) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    if (ActiveThree(Chess.Black, 3, leftTop, rightDown) && ConnectCount(Chess.Black, leftTop, rightDown) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    if (ActiveThree(Chess.Black, 3, leftDown, rightTop) && ConnectCount(Chess.Black, leftDown, rightTop) <= 3)
                    {
                        Cstate.tempActive3++;
                    }

                    VirtualBox[x, y] = Chess.none;

                    //處理白子連子情況

                    if (x == 6 && y == 9)
                    {
                        x = 6;
                    }

                    VirtualBox[x, y] = Chess.White;

                    //左右方向

                    connectCount = ConnectCount(Chess.White, left, right);

                    Cstate.whiteConnect[connectCount]++;

                    if (ActiveThree(Chess.White, connectCount, left, right))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    //上下方向

                    connectCount = ConnectCount(Chess.White, top, down);

                    Cstate.whiteConnect[connectCount]++;

                    if (ActiveThree(Chess.White, connectCount, top, down))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    //左上 右下方向

                    connectCount = ConnectCount(Chess.White, leftTop, rightDown);

                    Cstate.whiteConnect[connectCount]++;

                    if (ActiveThree(Chess.White, connectCount, leftTop, rightDown))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    //左下 右上方向

                    connectCount = ConnectCount(Chess.White, leftDown, rightTop);

                    Cstate.whiteConnect[connectCount]++;

                    if (ActiveThree(Chess.White, connectCount, leftDown, rightTop))
                    {
                        Cstate.whiteConnect[connectCount]--;

                        Cstate.whiteActive[connectCount]++;
                    }

                    VirtualBox[x, y] = Chess.none;

                    //開始求權值


                    bool BlackForbiden = (Cstate.tempActive3 > 1 || Cstate.blackActive[4] > 1 || SixConnect(x, y)); //判斷黑子是否禁手

                    if (Cstate.whiteConnect[5] > 0)
                    {
                        return(150000);
                    }

                    else if (Cstate.blackConnect[5] > 0 && !BlackForbiden)
                    {
                        return(140000);
                    }

                    else if (Cstate.whiteActive[4] > 0 || Cstate.whiteConnect[4] > 1)
                    {
                        return(130000);
                    }

                    else if (Cstate.whiteConnect[4] == 1 && Cstate.whiteActive[3] > 0)
                    {
                        return(120000);
                    }

                    else if (Cstate.blackActive[4] == 1 && !BlackForbiden || Cstate.blackConnect[4] > 1 && !BlackForbiden)
                    {
                        return(110000);
                    }

                    else if (Cstate.whiteConnect[4] == 1 && Cstate.whiteConnect[3] > 0)
                    {
                        return(100000);
                    }

                    else if (Cstate.blackConnect[4] > 0 && Cstate.tempActive3 == 1 && !BlackForbiden)
                    {
                        return(90000);
                    }

                    else if (Cstate.whiteActive[3] > 1)
                    {
                        return(80000);
                    }

                    else if (Cstate.blackConnect[4] > 0 && Cstate.blackConnect[3] > 0 && !BlackForbiden)
                    {
                        return(70000);
                    }

                    else
                    {
                        totalmarks = (Cstate.whiteConnect[4] + Cstate.whiteActive[3]) * 6250 + (Cstate.whiteConnect[3] + Cstate.whiteActive[2] + Cstate.blackConnect[4] + Cstate.blackActive[3]) * 1250

                                     + (Cstate.whiteConnect[2] + Cstate.blackConnect[3] + Cstate.blackActive[2]) * 250 + Cstate.whiteActive[1] * 50 + (Cstate.whiteConnect[1] + Cstate.blackConnect[2] + Cstate.blackActive[1]) * 10 + Cstate.blackConnect[1] * 2;

                        return(totalmarks);
                    }
                }
            }
        }
Exemplo n.º 31
0
        public void Init(ChessState state)
        {
            _chessState = state;

            _engineMoveJob = new EngineMoveJob(_chessState.Engine);
        }
Exemplo n.º 32
0
        public static WinnerType GetGameResult(ChessState state)
        {
            WinnerType ret = WinnerType.None;
            bool ew = false,eb = false;

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (state.State[i][j] == ChessType.WKing) ew = true;
                    if (state.State[i][j] == ChessType.BKing) eb = true;
                }
            }
            if (ew && eb) ret = WinnerType.None;
            if (ew && !eb) ret = WinnerType.WhiteWin;
            if (!ew && eb) ret = WinnerType.BlackWin;

            return ret;
        }
Exemplo n.º 33
0
 public void Init(ChessState state)
 {
     _cameraManager = Object.FindObjectOfType <CameraManager>();
     _chessState    = state;
 }
Exemplo n.º 34
0
    /// <summary>
    /// 判断当前棋手是否获胜
    /// </summary>
    private bool IsWin(int x, int y, ChessState chess)
    {
        int i = x, j = y;
        int count = 0; //棋子计数器

        /*计算水平方向连续棋子个数*/
        while (i > -1 && _chess[i, j].CurChessState == chess)
        {
            i--;
            count++; //累加左侧
        }
        i = x + 1;
        while (i < 15 && _chess[i, j].CurChessState == chess)
        {
            i++;
            count++; //累加右侧
        }
        if (count >= ConstKey.WinNum)
        {
            return(true); //获胜
        }
        /*计算竖直方向连续棋子个数*/
        i     = x;
        count = 0;
        while (j > -1 && _chess[i, j].CurChessState == chess)
        {
            j--;
            count++; //累加上方
        }
        j = y + 1;
        while (j < 15 && _chess[i, j].CurChessState == chess)
        {
            j++;
            count++; //累加下方
        }
        if (count >= ConstKey.WinNum)
        {
            return(true); //获胜
        }
        /*计算左上右下方向连续棋子个数*/
        j     = y;
        count = 0;
        while (i > -1 && j > -1 && _chess[i, j].CurChessState == chess)
        {
            i--;
            j--;
            count++; //累加左上
        }
        i = x + 1;
        j = y + 1;
        while (i < 15 && j < 15 && _chess[i, j].CurChessState == chess)
        {
            i++;
            j++;
            count++; //累加右下
        }
        if (count >= ConstKey.WinNum)
        {
            return(true); //获胜
        }
        /*计算右上左下方向连续棋子个数*/
        i     = x;
        j     = y;
        count = 0;
        while (i < 15 && j > -1 && _chess[i, j].CurChessState == chess)
        {
            i++;
            j--;
            count++; //累加右上
        }
        i = x - 1;
        j = y + 1;
        while (i > -1 && j < 15 && _chess[i, j].CurChessState == chess)
        {
            i--;
            j++;
            count++; //累加左下
        }
        if (count >= ConstKey.WinNum)
        {
            return(true); //获胜
        }
        return(false);    //该步没有取胜
    }