示例#1
0
 static void Main(string[] args)
 {
     Console.OutputEncoding = Encoding.UTF8;
     var board = new Board();
     var bishop = new Bishop();
     var rook = new Rook();
     board.Set("e4", bishop);
     board.Set("f7", rook);
     while (true)
     {
         board.Show();
         Console.WriteLine("Blankt svar avslutter programmet. Ruter skrives som en bokstav og et tall, for eksempel \"e4\".");
         var positionFrom = Ask("Hvilken rute vil du flytte fra?");
         var positionTo = Ask("Hvilken rute vil du flytte til?");
         board.Move(positionFrom, positionTo);
     }
 }
示例#2
0
        private void InitializeSinglePiece(char i, int y, PieceType type, ChessColor color, Color col)
        {
            PictureBox chessPiece = new PictureBox {
                Location = Board[i][y].Location, Size = Board[i][y].Size, BackColor = Board[i][y].BackColor, BorderStyle = Board[i][y].BorderStyle, Image = PieceImages[type][col]
            };
            ChessPiece piece = null;

            switch (type)
            {
            case PieceType.PAWN: piece = new Pawn(i, y, color, chessPiece); break;

            case PieceType.ROOK: piece = new Rook(i, y, color, chessPiece); break;

            case PieceType.BISHOP: piece = new Bishop(i, y, color, chessPiece); break;

            case PieceType.KING: piece = new King(i, y, color, chessPiece); break;

            case PieceType.KNIGHT: piece = new Knight(i, y, color, chessPiece); break;

            case PieceType.QUEEN: piece = new Queen(i, y, color, chessPiece); break;
            }
            if (color == ChessColor.WHITE)
            {
                white_pieces.Add(piece);
            }
            else
            {
                black_pieces.Add(piece);
            }
            if (piece.Type == PieceType.KING)
            {
                Kings.Add(piece.Color, (King)piece);
            }
            BoardPieces.Add(chessPiece, piece);
            piecePositions[i][y].piece   = piece;
            piecePositions[i][y].ocupied = true;
            chessPiece.MouseClick       += Piece_MouseClick;
            Controls.Add(chessPiece);
            chessPiece.BringToFront();
        }
示例#3
0
        public Object InitializeTree(int depth, MovesNode node, bool turn, Dictionary <char, Dictionary <int, positionInfo> > pos)
        {
            if (depth == 0)
            {
                return(null);
            }
            List <ChessPiece> white_Pieces = new List <ChessPiece>();
            List <ChessPiece> black_Pieces = new List <ChessPiece>();

            foreach (var x in pos.Values)
            {
                foreach (var y in x.Values)
                {
                    if (y.ocupied)
                    {
                        if (y.piece.Color == ChessColor.WHITE)
                        {
                            white_Pieces.Add(y.piece);
                        }
                        else
                        {
                            black_Pieces.Add(y.piece);
                        }
                    }
                }
            }
            foreach (var piece in turn ? black_Pieces : white_Pieces)
            {
                foreach (var move in piece.getValidMoves(white_Pieces, black_Pieces, pos))
                {
                    Dictionary <char, Dictionary <int, positionInfo> > positions = new Dictionary <char, Dictionary <int, positionInfo> >();
                    foreach (var chr in pos.Keys)
                    {
                        positions.Add(chr, new Dictionary <int, positionInfo>());
                        foreach (var intr in pos[chr].Keys)
                        {
                            if (pos[chr][intr].ocupied)
                            {
                                ChessPiece newPiece = null;
                                switch (pos[chr][intr].piece.Type)
                                {
                                case PieceType.PAWN: newPiece = new Pawn(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); ((Pawn)newPiece).startingPos = ((Pawn)pos[chr][intr].piece).startingPos; break;

                                case PieceType.ROOK: newPiece = new Rook(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.KNIGHT: newPiece = new Knight(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.BISHOP: newPiece = new Bishop(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.QUEEN: newPiece = new Queen(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;

                                case PieceType.KING: newPiece = new King(pos[chr][intr].piece.Position.X, pos[chr][intr].piece.Position.Y, pos[chr][intr].piece.Color, pos[chr][intr].piece.Piece); break;
                                }
                                positions[chr].Add(intr, new positionInfo(newPiece, true));
                            }
                            else
                            {
                                positions[chr].Add(intr, new positionInfo(null, false));
                            }
                        }
                    }
                    List <ChessPiece> whitePieces = new List <ChessPiece>();
                    List <ChessPiece> blackPieces = new List <ChessPiece>();
                    foreach (var x in positions.Values)
                    {
                        foreach (var y in x.Values)
                        {
                            if (y.ocupied)
                            {
                                if (y.piece.Color == ChessColor.WHITE)
                                {
                                    whitePieces.Add(y.piece);
                                }
                                else
                                {
                                    blackPieces.Add(y.piece);
                                }
                            }
                        }
                    }
                    Moves movement = new Moves(positions[piece.Position.X][piece.Position.Y].piece, piece.Position.X, piece.Position.Y, move.X, move.Y);

                    if (positions[piece.Position.X][piece.Position.Y].ocupied && positions[move.X][move.Y].ocupied)
                    {
                        movement.PieceTaken = true;
                        movement.TakenType  = positions[move.X][move.Y].piece.Type.ToString();
                        if (positions[move.X][move.Y].piece.Color == ChessColor.WHITE)
                        {
                            whitePieces.Remove(positions[move.X][move.Y].piece);
                        }
                        else
                        {
                            whitePieces.Remove(positions[move.X][move.Y].piece);
                        }
                    }
                    if (positions[piece.Position.X][piece.Position.Y].ocupied)
                    {
                        positions[move.X][move.Y].ocupied = true;
                        positions[move.X][move.Y].piece   = positions[piece.Position.X][piece.Position.Y].piece;
                        positions[piece.Position.X][piece.Position.Y].ocupied = false;
                        positions[piece.Position.X][piece.Position.Y].piece   = null;
                        positions[move.X][move.Y].piece.MovePiece(move.X, move.Y);
                    }
                    if (random.Next(1, 10) < 6)
                    {
                        node.Children.Add(new MovesNode(positions, whitePieces, blackPieces, movement));
                    }
                }
            }
            if (!turn)
            {
                node.Children.Sort((i, j) => i.Value.CompareTo(j.Value));
            }
            else
            {
                node.Children.Sort((i, j) => j.Value.CompareTo(i.Value));
            }
            foreach (var child in node.Children)
            {
                InitializeTree(depth - 1, child, !turn, child.Board);
            }
            return(null);
        }
示例#4
0
        private void InitializeSingleChoice(int xpos, int ypos, ChessColor color, PieceType type)
        {
            PictureBox choicePiece = null;

            if (color == ChessColor.WHITE)
            {
                switch (type)
                {
                case PieceType.PAWN: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\White_Pawn.png")
                }; piece = new Pawn(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.ROOK: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\White_Rook.png")
                }; piece = new Rook(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.BISHOP: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\White_Bishop.png")
                }; piece = new Bishop(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.KING: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\White_King.png")
                }; piece = new King(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.KNIGHT: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\White_Knight.png")
                }; piece = new Knight(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.QUEEN: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\White_Queen.png")
                }; piece = new Queen(piece.Position.X, piece.Position.Y, color, choicePiece); break;
                }
            }
            else
            {
                switch (type)
                {
                case PieceType.PAWN: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\Black_Pawn.png")
                }; piece = new Pawn(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.ROOK: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\Black_Rook.png")
                }; piece = new Rook(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.BISHOP: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\Black_Bishop.png")
                }; piece = new Bishop(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.KING: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\Black_King.png")
                }; piece = new King(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.KNIGHT: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\Black_Knight.png")
                }; piece = new Knight(piece.Position.X, piece.Position.Y, color, choicePiece); break;

                case PieceType.QUEEN: choicePiece = new PictureBox {
                        Location = new Point(xpos, ypos), Size = new Size(75, 75), Image = Image.FromFile(@"..\..\Images\Black_Queen.png")
                }; piece = new Queen(piece.Position.X, piece.Position.Y, color, choicePiece); break;
                }
            }
            Controls.Add(choicePiece);
            choices.Add(choicePiece, piece);
            choicePiece.Click += choose_onClick;
        }