Exemple #1
0
        public Piece[,] createBoardFromFile()
        {
            var info     = new FileInfo("game.xml");
            var tempTime = info.LastWriteTime;

            if (DateTime.Compare(tempTime, lastWriteTime) > 0)
            {
                Piece[,] board = new Piece[8, 8];
                XDocument prevGame = XDocument.Load("game.xml");

                var lv0s = from lv0 in prevGame.Descendants("settings")
                           select new {
                    Turn         = lv0.Attribute("turn").Value,
                    White        = lv0.Attribute("white").Value,
                    Black        = lv0.Attribute("black").Value,
                    Whiteischeck = lv0.Attribute("whiteischeck").Value,
                    Blackischeck = lv0.Attribute("blackischeck").Value
                };

                foreach (var lv0 in lv0s)
                {
                    this.whiteIsCheck = Convert.ToBoolean(lv0.Whiteischeck);
                    this.blackIsCheck = Convert.ToBoolean(lv0.Blackischeck);
                    this.white        = lv0.White;
                    this.black        = lv0.Black;
                    this.turn         = (Piece.PieceColor)Enum.Parse(typeof(Piece.PieceColor), lv0.Turn);
                }

                var lv1s = from lv1 in prevGame.Descendants("square")
                           select new {
                    Type    = lv1.Attribute("type").Value,
                    Color   = lv1.Attribute("color").Value,
                    Coordx  = lv1.Attribute("coordx").Value,
                    Coordy  = lv1.Attribute("coordy").Value,
                    Texture = lv1.Attribute("texture").Value
                };

                int count = 0;
                foreach (var lv1 in lv1s)
                {
                    count += 1;

                    Piece.PieceType  type  = (Piece.PieceType)Enum.Parse(typeof(Piece.PieceType), lv1.Type);
                    Piece.PieceColor color = (Piece.PieceColor)Enum.Parse(typeof(Piece.PieceColor), lv1.Color);
                    int x   = Int32.Parse(lv1.Coordx);
                    int y   = Int32.Parse(lv1.Coordy);
                    int txt = Int32.Parse(lv1.Texture);

                    Piece temp = new Piece(type, x, y, color, txt);;
                    board [x, y] = temp;
                }

                lastWriteTime = info.LastWriteTime;
                return(board);
            }
            else
            {
                return(this.board);
            }
        }
Exemple #2
0
        public void SetPromotionPiece(int i)
        {
            switch (i)
            {
            case 0:
                aPromotionType = Piece.PieceType.BISHOP;
                break;

            case 1:
                aPromotionType = Piece.PieceType.KNIGHT;
                break;

            case 2:
                aPromotionType = Piece.PieceType.QUEEN;
                break;

            case 3:
                aPromotionType = Piece.PieceType.ROOK;
                break;

            default:
                aPromotionType = Piece.PieceType.QUEEN;
                break;
            }
        }
Exemple #3
0
 //if it's a castle move then StartSquare and TargetSquare correspond to king's start and end square
 public Move(Vector2Int start_square, Vector2Int target_square, bool is_castle_move, Piece.PieceType promote_to = Piece.PieceType.None)
 {
     StartSquare  = start_square;
     TargetSquare = target_square;
     IsCastleMove = is_castle_move;
     PromoteTo    = promote_to;
 }
Exemple #4
0
        public static bool isLegalMove(Piece[,] board, Coord fromPos, Coord toPos)
        {
            Piece.PieceType curType = board [fromPos.xpos, fromPos.ypos].type;
            switch (curType)
            {
            case Piece.PieceType.PAWN:
                return(Pawn(board, fromPos, toPos));

            case Piece.PieceType.ROOK:
                return(Rook(board, fromPos, toPos));

            case Piece.PieceType.KNIGHT:
                return(Knight(board, fromPos, toPos));

            case Piece.PieceType.BISHOP:
                return(Bishop(board, fromPos, toPos));

            case Piece.PieceType.QUEEN:
                return(Queen(board, fromPos, toPos));

            case Piece.PieceType.KING:
                return(King(board, fromPos, toPos));
            }
            return(false);
        }
Exemple #5
0
 internal void addToGrid(int x, int y, Player.EnumColor color, Piece.PieceType pieceType)
 {
     if (Grid[y, x] == null)
     {
         Grid[y, x] = PieceInfoToByte(color, pieceType);
     }
 }
Exemple #6
0
        public void Test_No_Empty_Pieces_Added()
        {
            // Arange
            Board board = new Board();

            // Act
            Piece.PieceType piece = (Piece.PieceType)board.GetRandomLocationForPiece(1, 6);

            // Assert
            Assert.AreNotEqual(Piece.PieceType.Empty, piece);
        }
 private IEnumerable <Cell> FindPieceLocations(Board board, Piece.PieceType type, Side side)
 {
     for (var q = 1; q <= 8; q++)
     {
         for (var w = 1; w <= 8; w++)
         {
             var place = board[q, w];
             var piece = place.piece;
             if (piece != null && piece.Side == side && piece.Type == type)
             {
                 yield return(place);
             }
         }
     }
 }
Exemple #8
0
    public override void HandleHover()
    {
        rememberPiece = Singletons.GRID.previewPiece.GetPieceType();

        Singletons.GRID.SetPreviewPiece (Piece.PieceType.Mirror);

        var preview = Singletons.GRID.previewPiece.GetComponent<Mirror> ();
        wasFlipped = preview.IsFlipped ();
        if (wasFlipped == this.flipped)
            preview.DoFlip ();
        Singletons.GRID.PreviewAt (this);

        Singletons.GRID.arrows.transform.position = this.transform.position;
        Singletons.GRID.arrows.SetActive (true);

        Singletons.GRID.ClearPreviews ();
        Singletons.GRID.PreviewLazers ();
    }
    public static void canMoveTo(Board board, Piece.PieceType type, Vector2 from, Vector2 to)
    {
        switch (type)
        {
        case Piece.PieceType.BATMAN:
        case Piece.PieceType.KNIGHT:
            break;

        case Piece.PieceType.BISHOP:
            break;

        case Piece.PieceType.KING:
            break;

        case Piece.PieceType.PAWN:
            break;

        case Piece.PieceType.QUEEN:
            break;
        }
    }
Exemple #10
0
    public void RenderBoard(IGameBoardState gameBoard)
    {
        int levels = gameBoard.GetNumLevels();
        int rows   = gameBoard.GetNumRows();
        int cols   = gameBoard.GetNumCols();

        for (int i = 0; i < levels; i++)
        {
            for (int j = 0; j < rows; j++)
            {
                for (int k = 0; k < cols; k++)
                {
                    Image img = squareLookup[i][j][k];

                    if (img != null)
                    {
                        if (gameBoard.GetSpaceState(i, j, k).IsOccupied())
                        {
                            Piece.PieceType pieceType = gameBoard.GetSpaceState(i, j, k).Occupier().GetPieceType();
                            img.sprite = PieceSprites[pieceType];

                            img.color = gameBoard.GetSpaceState(i, j, k).Occupier().GetPieceTint();
                            img.Desaturate();
                        }
                        else
                        {
                            img.sprite = BlankSprite;
                            img.color  = Color.white;
                        }
                    }
                    else
                    {
                        Debug.Log("Error: image not found at index " + i + " " + j + " " + k);
                    }
                }
            }
        }
    }
Exemple #11
0
        public void Randomize()
        {
            int random = 0;

            Piece.PieceType[] placedPieces = new Piece.PieceType[8]
            {
                Piece.PieceType.Empty,
                Piece.PieceType.Empty,
                Piece.PieceType.Empty,
                Piece.PieceType.Empty,
                Piece.PieceType.Empty,
                Piece.PieceType.Empty,
                Piece.PieceType.Empty,
                Piece.PieceType.Empty
            };

            Random rand = new Random();

            //Place Rooks (Cannot be right beside eachother or on same square)
            random = rand.Next(0, 8);
            placedPieces[random] = Piece.PieceType.Rook;

            int rook1pos = random;

            do
            {
                random = rand.Next(0, 8);
            }while (placedPieces[random] != Piece.PieceType.Empty || Math.Abs(rook1pos - random) <= 1);
            placedPieces[random] = Piece.PieceType.Rook;

            //Find the bounds for where king must be in between
            int lowerRook = (rook1pos < random) ? rook1pos : random;
            int upperRook = (rook1pos > random) ? rook1pos : random;

            //Place king
            placedPieces[rand.Next(lowerRook + 1, upperRook)] = Piece.PieceType.King;

            //Place bishop 1
            do
            {
                random = rand.Next(0, 7);
            }while (random % 2 == 1 || placedPieces[random] != Piece.PieceType.Empty);            //if odd AND if not empty
            placedPieces[random] = Piece.PieceType.Bishop;

            //Place bishop 2
            do
            {
                random = rand.Next(0, 7);
            }while (random % 2 == 0 || placedPieces[random] != Piece.PieceType.Empty);
            placedPieces[random] = Piece.PieceType.Bishop;

            //Place queen
            do
            {
                random = rand.Next(0, 7);
            }while (placedPieces[random] != Piece.PieceType.Empty);
            placedPieces[random] = Piece.PieceType.Queen;

            //Place knights
            for (int i = 0; i < placedPieces.Length; i++)
            {
                if (placedPieces[i] == Piece.PieceType.Empty)
                {
                    placedPieces[i] = Piece.PieceType.Knight;
                }
            }

            //Initialize black side
            char c = 'a';

            foreach (Piece.PieceType piece in placedPieces)
            {
                //int.TryParse('a', out int parsed);
                //char c = (char)((int)'a' + i);
                m_cells[c.ToString() + 1].piece = new Piece(piece, m_BlackSide);
                c++;
            }

            //Initialize white side
            c = 'a';
            foreach (Piece.PieceType piece in placedPieces)
            {
                //int.TryParse('a', out int parsed);
                //char c = (char)((int)'a' + i);
                m_cells[c.ToString() + 8].piece = new Piece(piece, m_WhiteSide);
                c++;
            }
        }
        public static bool TryParseMove(out Move move, string notation, Board board)
        {
            #region normal move

            Match match         = Regex.Match(notation, "^([a-h])([1-8]) ([a-h])([1-8])$");
            Match promote_match = Regex.Match(notation, "^([a-h])([1-8]) ([a-h])([1-8]) ([bnrq])$");

            if (match.Success)
            {
                var from = new Vector2Int(Array.IndexOf(Utils.Alphabet, char.Parse(match.Groups[1].Value)), Board.GridSize - int.Parse(match.Groups[2].ToString()));
                var to   = new Vector2Int(Array.IndexOf(Utils.Alphabet, char.Parse(match.Groups[3].Value)), Board.GridSize - int.Parse(match.Groups[4].ToString()));

                move = new Move(from, to, false);
                return(true);
            }
            else if (promote_match.Success)
            {
                var             from       = new Vector2Int(Array.IndexOf(Utils.Alphabet, char.Parse(promote_match.Groups[1].Value)), Board.GridSize - int.Parse(promote_match.Groups[2].ToString()));
                var             to         = new Vector2Int(Array.IndexOf(Utils.Alphabet, char.Parse(promote_match.Groups[3].Value)), Board.GridSize - int.Parse(promote_match.Groups[4].ToString()));
                Piece.PieceType promote_to = Board.CharToPieceType[char.Parse(promote_match.Groups[5].Value)];

                move = new Move(from, to, false, promote_to);
                return(true);
            }
            #endregion

            #region castle move
            if (notation == "0-0")
            {
                Vector2Int king_start_square = default;
                for (int y = 0; y < Board.GridSize; y++)
                {
                    for (int x = 0; x < Board.GridSize; x++)
                    {
                        Piece piece = board.Cells[x, y];
                        if (piece.MyPieceType == Piece.PieceType.King && piece.MyPieceColour == board.ColourToMove)
                        {
                            king_start_square = new Vector2Int(x, y);
                        }
                    }
                }

                var king_target_square = new Vector2Int(king_start_square.x + 2, king_start_square.y);

                move = new Move(king_start_square, king_target_square, true);
                return(true);
            }
            else if (notation == "0-0-0")
            {
                Vector2Int king_start_square = default;
                for (int y = 0; y < Board.GridSize; y++)
                {
                    for (int x = 0; x < Board.GridSize; x++)
                    {
                        Piece piece = board.Cells[x, y];
                        if (piece.MyPieceType == Piece.PieceType.King && piece.MyPieceColour == board.ColourToMove)
                        {
                            king_start_square = new Vector2Int(x, y);
                        }
                    }
                }

                var king_target_square = new Vector2Int(king_start_square.x - 2, king_start_square.y);

                move = new Move(king_start_square, king_target_square, true);
                return(true);
            }
            #endregion

            move = default;
            return(false);
        }
Exemple #13
0
 public PromotionManipulator()
 {
     aPromotionType = Piece.PieceType.QUEEN;
 }
Exemple #14
0
        //Calculate next AI move
        public void calculateMove(Board board)
        {
            moves      = new List <Coord[]> ();
            bestPoints = -1000;


            List <Coord> possibleMoves = new List <Coord> ();

            //Check which moves generate the most points
            foreach (Piece p in board.board)
            {
                if (p.color == color)
                {
                    possibleMoves = Rules.getLegalMoves(board.board, p.coord);
                    Coord currentPieceCoord = p.coord;
                    foreach (Coord c in possibleMoves)
                    {
                        if (causeCheck(board, currentPieceCoord, c))
                        {
                            continue;
                        }
                        else
                        {
                            Piece.PieceType pieceAtToPos = board.board [c.xpos, c.ypos].type;
                            //Check if move puts us in a vulnerable position
                            int deduct = reducePoints(board, p.coord, c);
                            switch (pieceAtToPos)
                            {
                            case Piece.PieceType.NONE:

                                isBetterMove(2 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.PAWN:
                                isBetterMove(6 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.ROOK:
                                isBetterMove(10 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.KNIGHT:
                                isBetterMove(10 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.BISHOP:
                                isBetterMove(10 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.QUEEN:
                                isBetterMove(15 - deduct, p.coord, c);
                                break;

                            case Piece.PieceType.KING:
                                isBetterMove(14 - deduct, p.coord, c);
                                break;
                            }
                        }
                    }
                }
            }

            //If we have severals moves with the same points, pick one at random
            Random rnd   = new Random();
            int    index = rnd.Next(0, moves.Count);

            if (moves.Count == 0)
            {
                Console.WriteLine("Out of moves!!");
            }
            else
            {
                board.tryMove(moves [index] [0], moves [index] [1]);
            }
        }
Exemple #15
0
 public void SetPromotion(Piece.PieceType promoteType)
 {
     PromoteTo = promoteType;
     SelectState = SelectionState.MoveSelected;
 }
Exemple #16
0
 public Chess960SetupCell(int p, int row, Piece.PieceType t)
 {
     pos  = ((char)('a' + p)).ToString() + row.ToString();
     type = t;
 }
 private int CountAllPieces(Board board, Piece.PieceType type, Side side)
 {
     return(FindPieceLocations(board, type, side).Count());
 }
Exemple #18
0
 internal void addPiece(byte x, byte y, Piece.PieceType typePiece)
 => DictPointPiece.Add(new Point(x, y), typePiece);
Exemple #19
0
 private byte PieceInfoToByte(Player.EnumColor color, Piece.PieceType pieceType) => byte.Parse($"{(int)color}{(int)pieceType}");