protected virtual List <ChessPosition> Vertical(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); ChessColor color = Map_Chess[pos_start.X, pos_start.Y].Color; ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType; if (color == ChessColor.Die) { return(list); } for (int index = 0; index < 2; index++) { int val = maps_vertical_n_horizontal[index]; while (pos_start.Y + val >= 0 && pos_start.Y + val <= 7) { if (Map_Chess[pos_start.X, pos_start.Y + val].Color == color) { break; // if ally -> can't move } else { list.Add(new ChessPosition() { X = pos_start.X, Y = pos_start.Y + val }); if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die) { break; // if enemy -> eat } } val = val + maps_vertical_n_horizontal[index]; } } return(list); }
private void Pic_MouseDown(object sender, MouseEventArgs e) { isMove = true; temp_p = e.Location; index_control = ChessBoard.Controls.IndexOf((PictureBox)sender); ChessPosition pos_chess = GetPosMapsChess(((PictureBox)sender).Location); if (round != Map_Chess[pos_chess.X, pos_chess.Y].Color) { isMove = false; return; } ((PictureBox)sender).Visible = false; temp_pic = LoadPic(Map_Chess[pos_chess.X, pos_chess.Y].Color, Map_Chess[pos_chess.X, pos_chess.Y].ChessType, ((PictureBox)sender).Location, false); ChessBoard.Controls.Add(temp_pic); temp_pic.BringToFront(); listcanmove = new List <ChessPosition>(); listcanmove = Map_Chess[pos_chess.X, pos_chess.Y].ListCanMove(pos_chess, Map_Chess); foreach (ChessPosition pos in listcanmove) { foreach (PictureBox pic in GetPictureBoxFromPosChess(pos)) { pic.BackColor = canmove_color; } } }
public static List <ChessPosition> ListCanMove(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); list.AddRange(diagonal(pos_start, Map_Chess)); list.AddRange(vertical(pos_start, Map_Chess)); list.AddRange(horizontal(pos_start, Map_Chess)); return(list); }
private static List <ChessPosition> horizontal(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); ChessColor color = Map_Chess[pos_start.X, pos_start.Y].Color; if (color == ChessColor.Die) { return(list); } ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType; if (type == ChessEnum.Knight | type == ChessEnum.Bishop | type == ChessEnum.Pawn) { return(list); //Knight & Bishop & Pawn can't move horizontal } for (int index = 0; index < 2; index++) { int val = VerticalandHorizontal[index]; while (pos_start.X + val >= 0 & pos_start.X + val <= 7) { if (Map_Chess[pos_start.X + val, pos_start.Y].Color == color) { break; // if ally -> can't move } else { list.Add(new ChessPosition() { X = pos_start.X + val, Y = pos_start.Y }); if (Map_Chess[pos_start.X + val, pos_start.Y].Color != ChessColor.Die) { break; // if enemy -> eat } } if (type == ChessEnum.King) { break; //king max 1 step } val = val + VerticalandHorizontal[index]; } } return(list); }
private List <PictureBox> GetPictureBoxFromPosChess(ChessPosition pos) { List <PictureBox> list = new List <PictureBox>(); foreach (PictureBox pic in listchess) { if (pic.Location == new Point(pos.X * ChessEdge, pos.Y * ChessEdge)) { list.Add(pic); } } foreach (PictureBox pic in listBackground) { if (pic.Location == new Point(pos.X * ChessEdge, pos.Y * ChessEdge)) { list.Add(pic); } } return(list); }
protected virtual List <ChessPosition> Diagonal(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> List = new List <ChessPosition>(); ChessColor color = Map_Chess[pos_start.X, pos_start.Y].Color; if (color == ChessColor.Die) { return(List); } ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType; for (int val = 0; val < 4; val++) { int _x = maps_diagonal[val, 0]; int _y = maps_diagonal[val, 1]; while (pos_start.X + _x >= 0 && pos_start.X + _x <= 7 && pos_start.Y + _y >= 0 && pos_start.Y + _y <= 7) { if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color == color) { break; // if ally -> can't move } else { List.Add(new ChessPosition() { X = pos_start.X + _x, Y = pos_start.Y + _y }); if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color != ChessColor.Die) { break; // if enemy -> eat } } _x = _x + maps_diagonal[val, 0]; _y = _y + maps_diagonal[val, 1]; } } return(List); }
private static List <ChessPosition> vertical(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); ChessColor color = Map_Chess[pos_start.X, pos_start.Y].Color; ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType; if (color == ChessColor.Die) { return(list); } if (type == ChessEnum.Knight | type == ChessEnum.Bishop) { return(list); //Knight & Bishop can't move vertical } for (int index = 0; index < 2; index++) { int val = VerticalandHorizontal[index]; while (pos_start.Y + val >= 0 & pos_start.Y + val <= 7) { if (type == ChessEnum.Pawn && (int)color == VerticalandHorizontal[index]) { break; //Pawn can't move behind } if (Map_Chess[pos_start.X, pos_start.Y + val].Color == color) { break; // if ally -> can't move } else { if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die & type == ChessEnum.Pawn) { break; // Pawn can't move } list.Add(new ChessPosition() { X = pos_start.X, Y = pos_start.Y + val }); if (Map_Chess[pos_start.X, pos_start.Y + val].Color != ChessColor.Die) { break; // if enemy -> eat } } if (type == ChessEnum.King) { break; //king max 1 step } if (type == ChessEnum.Pawn) { if (val == 2 | val == -2) { break; //Pawn max 2 step } int line = color == ChessColor.Black ? 1 : 6; if (pos_start.Y != line) { break; } } val = val + VerticalandHorizontal[index]; } } return(list); }
private static List <ChessPosition> diagonal(ChessPosition pos_start, ChessPiece[,] Map_Chess) { List <ChessPosition> list = new List <ChessPosition>(); ChessColor color = Map_Chess[pos_start.X, pos_start.Y].Color; if (color == ChessColor.Die) { return(list); } ChessEnum type = Map_Chess[pos_start.X, pos_start.Y].ChessType; if (type == ChessEnum.Knight | type == ChessEnum.Castle) { return(list); //Knight & Castle can't move diagonal } for (int val = 0; val < 4; val++) { int _x = Diagonal[val, 0]; int _y = Diagonal[val, 1]; while (pos_start.X + _x >= 0 & pos_start.X + _x <= 7 & pos_start.Y + _y >= 0 & pos_start.Y + _y <= 7) { if (type == ChessEnum.Pawn && (int)color == Diagonal[val, 1]) { break; //Pawn can't move behind } if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color == color) { break; // if ally -> can't move } else { if (type == ChessEnum.Pawn) { if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color != ChessColor.Die) { list.Add(new ChessPosition() { X = pos_start.X + _x, Y = pos_start.Y + _y }); } } else { list.Add(new ChessPosition() { X = pos_start.X + _x, Y = pos_start.Y + _y }); if (Map_Chess[pos_start.X + _x, pos_start.Y + _y].Color != ChessColor.Die) { break; // if enemy -> eat } } } if (type == ChessEnum.King | type == ChessEnum.Pawn) { break; //King & Pawn max 1 step } _x = _x + Diagonal[val, 0]; _y = _y + Diagonal[val, 1]; } } return(list); }
public abstract List <ChessPosition> ListCanMove(ChessPosition pos_start, ChessPiece[,] Map_Chess);
private void Pic_MouseUp(object sender, MouseEventArgs e) { if (isMove) { isMove = false; } else { return; } int _x = 0, _y = 0; Point pos = new Point(); ChessColor lose = ChessColor.Die; if (CheckDropTrueSquare(ref _x, ref _y) && CheckDropTruePosAllow(_x, _y, ref pos)) { //if eat foreach (PictureBox pic in listchess) { if (pic.Location == pos) { pic.Visible = false; pic.Dispose(); ChessBoard.Controls.Remove(pic); listchess.Remove(pic); if (Map_Chess[pos.X / 57, pos.Y / 57].ChessType != ChessEnum.King) { CheckPieceDie(Map_Chess[pos.X / 57, pos.Y / 57]); } break; } } ChessPosition pos_map = GetPosMapsChess(((PictureBox)sender).Location); ChessPiece map = Map_Chess[pos_map.X, pos_map.Y]; ((PictureBox)sender).Location = pos; ChessPosition pos_map_ = GetPosMapsChess(((PictureBox)sender).Location); //check king if (Map_Chess[pos_map_.X, pos_map_.Y].ChessType == ChessEnum.King) { lose = Map_Chess[pos_map_.X, pos_map_.Y].Color; } //check pawn if (Map_Chess[pos_map.X, pos_map.Y].ChessType == ChessEnum.Pawn) { if (pos_map_.Y == 0 || pos_map_.Y == 7) { PawnUnderPromotion nform = new PawnUnderPromotion(Map_Chess[pos_map.X, pos_map.Y].Color); nform.ShowDialog(); switch (nform.chess) { case ChessEnum.Bishop: map = new PieceClass.Bishop(Map_Chess[pos_map.X, pos_map.Y].Color); break; case ChessEnum.Rook: map = new PieceClass.Rook(Map_Chess[pos_map.X, pos_map.Y].Color); break; case ChessEnum.Knight: map = new PieceClass.Knight(Map_Chess[pos_map.X, pos_map.Y].Color); break; case ChessEnum.Queen: map = new PieceClass.Queen(Map_Chess[pos_map.X, pos_map.Y].Color); break; } ((PictureBox)sender).Image = Image.FromFile(FolderImage + Map_Chess[pos_map.X, pos_map.Y].Color.ToString() + "\\" + nform.chess.ToString() + ".png"); ((PictureBox)sender).BringToFront(); } } //overwrite new pos Map_Chess[pos_map_.X, pos_map_.Y] = map; //clear old pos maps Map_Chess[pos_map.X, pos_map.Y] = new PieceClass.None(); reportmove(pos_map.X, pos_map.Y, pos_map_.X, pos_map_.Y, map); //change round round = round == ChessColor.Black ? ChessColor.White : ChessColor.Black; } //clean ((PictureBox)sender).Visible = true; ChessBoard.Controls.Remove(temp_pic); temp_pic.Dispose(); temp_pic = null; foreach (PictureBox pic in listchess) { if (pic.BackColor == canmove_color) { pic.BackColor = DefaultColor; } } foreach (PictureBox pic in listBackground) { if (pic.BackColor == canmove_color) { pic.BackColor = ((pic.Location.X / 57) + (pic.Location.Y / 57)) % 2 == 0 ? Color.Gray : Color.DarkGray; } } if (lose != ChessColor.Die) { MessageBox.Show(lose.ToString() + " lose."); MakeNewRound(); Application.Exit(); return; } playerturn(); GC.Collect(); }