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); }
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; }