Exemplo n.º 1
0
 /// <summary>
 /// Print Board in Console
 /// </summary>
 /// <param name="board"></param>
 /// <param name="writer"></param>
 private void drawBoard(Board board, TextWriter writer)
 {
     var buf1 = new StringBuilder();
     var buf2 = new StringBuilder();
     writer.WriteLine("+--------------------------------+       +--------------------------------+");
     for (int k = 1; k <= 32; k++)
     {
         for (int i = 8; i > 0; i--)
         {
             int j = 1;
             if (i%2 == 0)
             {
                 j = 2;
                 buf1.AppendFormat("    ");
             }
             int shift;
             for (shift = 3; (j <= 8 && shift >= 0); j += 2, shift--)
             {
                 int cellNum = i*4 - shift;
                 var coord = board[i, j];
                 string soldierColor;
                 if (board.IsBlack(coord))
                 {
                     soldierColor = "b";
                     if (board.IsKing(coord)) soldierColor = "bk";
                 }
                 else if (board.IsWhite(coord))
                 {
                     soldierColor = "w";
                     if (board.IsKing(coord)) soldierColor = "wk";
                 }
                 else
                 {
                     soldierColor = ".";
                 }
                 k++;
                 buf1.AppendFormat(" | {0} |  ", soldierColor);
                 if (cellNum >= 1 && cellNum <= 9)
                 {
                     buf2.AppendFormat("  | {0} | ", cellNum);
                 }
                 else
                 {
                     buf2.AppendFormat("  | {0}| ", cellNum);
                 }
             }
             writer.WriteLine("{0}        {1}", buf1, buf2);
             writer.WriteLine("+--------------------------------+       +--------------------------------+");
             buf1.Length = 0;
             buf2.Length = 0;
         }
     }
     buf1.Append("|");
     buf2.Append("|");
 }
Exemplo n.º 2
0
 /// <summary>
 /// Checks if draw and return number of moves (draw - if there are 15 king's moves with no captures of both opponent and player)
 /// </summary>
 /// <param name="board"></param>
 /// <param name="coordinate"></param>
 /// <param name="canCapture"></param>
 /// <param name="count"></param>
 public GameState CheckDraw(Board board, Coordinate coordinate, bool canCapture, ref int count)
 {
     if (board.IsKing(coordinate) && !canCapture)
     {
         count++;
         if (count == 15)
         {
             return GameState.Draw;
         }
     }
     else
     {
         count = 0;
     }
     return GameState.Undetermined;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Checks if draw and return number of moves (draw - if there are 15 king's moves with no captures of both opponent and player)
 /// </summary>
 /// <param name="board"></param>
 /// <param name="coordinate"></param>
 /// <param name="captured"></param>
 public GameState CheckDraw(Board board, Coordinate coordinate, bool captured)
 {
     if (board.IsKing(coordinate) && !captured)
     {
         countMove++;
         if (countMove == 15)
         {
             return GameState.Draw;
         }
     }
     else
     {
         countMove = 0;
     }
         return GameState.Undetermined;
 }
Exemplo n.º 4
0
        /// <summary>
        /// returns the number of ways in which soldier on coord can be captured;
        /// </summary>
        /// <param name="board"></param>
        /// <param name="coord"></param>
        /// <param name="player"></param>
        /// <returns></returns>
        public int CanBeCaptured(Board board, Coordinate coord, Player player)
        {
            int num = 0;
            Rules rule = new Rules();
            IList<Coordinate> optionalCoords = rule.OptionalMoves(board, coord, player);
            IList<Coordinate> coordsInDir = rule.GetMovesInDirection(board, coord, player);

            //collect all coords behind coord
            IList<Coordinate> coordsfrombehind = optionalCoords.Where(opCor => !coordsInDir.Contains(opCor)).ToList();
            foreach (var cid in coordsInDir)
            {
                if (board.GetPlayer(board[cid.X, cid.Y]) == board.GetOpponent(player) &&
                    rule.CoordsToCaptureAndDest(board, cid, coord, board.GetOpponent(player)).Count > 0)
                    num++;
            }

            foreach (var cfb in coordsfrombehind)
            {
                if (board.GetPlayer(board[cfb.X, cfb.Y]) == board.GetOpponent(player) && board.IsKing(coord) &&
                    rule.CoordsToCaptureAndDest(board, cfb, coord, board.GetOpponent(player)).Count > 0)
                    num++;
            }
            return num;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Find captures from a given source coord and opponent coord
        /// 
        /// </summary>
        /// <param name="board"></param>
        /// <param name="srcCoordinate"></param>
        /// <param name="oponentCoordinate"></param>
        /// <param name="player"></param>
        /// <returns> a dictionary of dest coord and lists of captures</returns>
        public IDictionary<IList<Coordinate>, Coordinate> CoordsToCaptureAndDest(Board board, Coordinate srcCoordinate,
            Coordinate oponentCoordinate,
            Player player)
        {
            IDictionary<IList<Coordinate>, Coordinate> map = new Dictionary<IList<Coordinate>, Coordinate>();
            int srcX = srcCoordinate.X;
            int srcY = srcCoordinate.Y;
            int oponentX = oponentCoordinate.X;
            int oponentY = oponentCoordinate.Y;
            Coordinate dest;
            Piece opponentPiece = new Piece();
            int destX, destY;

            //find the direction of the optional capture and set destination accordingly
            if (srcX < oponentX) destX = oponentX + 1;
            else destX = oponentX - 1;

            if (srcY < oponentY) destY = oponentY + 1;
            else destY = oponentY - 1;
            if (InBounds(board, destX, destY))
            {
                dest = new Coordinate(board[destX, destY]);
            }
            else
            {
                return map;
            }
            if (dest.Status != Piece.None)
                return map;
            dest.Status = srcCoordinate.Status;

            IsBecameAKing(board, dest);
            if (board.IsKing(dest))
            {
                opponentPiece = board[oponentCoordinate.X, oponentCoordinate.Y].Status;
                board[oponentCoordinate.X, oponentCoordinate.Y].Status = Piece.None;
            }
            //find coordinates if we can continue capture from dest
            IList<Coordinate> moreOptionalDirCaptures = GetMovesInDirection(board, dest, player);
            map.Add(new List<Coordinate> {oponentCoordinate}, dest);
            if (moreOptionalDirCaptures.Count == 0)
            {
                return map;
            }
            foreach (var cid in moreOptionalDirCaptures)
            {
                if (board.IsOpponentPiece(player, cid))
                {
                    IDictionary<IList<Coordinate>, Coordinate> temp = CoordsToCaptureAndDest(board, dest, cid, player);
                    if (temp.Keys.Count() > map.Keys.Count())
                    {
                        map = temp;
                        if (map.Values.Contains(dest))
                        {
                            var item = map.FirstOrDefault((x => x.Value.X == dest.X && x.Value.Y == dest.Y));
                            map.Remove(item.Key);
                        }
                    }
                    else if (temp.Keys.Count() == map.Keys.Count())
                    {
                        map = map.Concat(temp).ToDictionary(pair => pair.Key, pair => pair.Value);
                        if (map.Values.Contains(dest))
                        {
                            var item = map.FirstOrDefault((x => x.Value.X == dest.X && x.Value.Y == dest.Y));
                            map.Remove(item.Key);
                        }
                    }
                }
            }
            if (!map.Values.Contains(dest))
            {
                foreach (var item in map)
                {
                    item.Key.Add(oponentCoordinate);
                }
            }
            if (board[oponentCoordinate.X, oponentCoordinate.Y].Status == Piece.None)
            {
                board[oponentCoordinate.X, oponentCoordinate.Y].Status = opponentPiece;
            }
            return map;
        }