public void HumanPiecePositionChangeHandler(HumanMoveMessage action) { this.HumanTimer.stopClock(); //lock the entire board so that use cannot click on piece when it is machine's turn. foreach (KeyValuePair<int, ChessPiece> item in this.pieces_dict) { if (item.Value.Player == Player.White && MoveGenerator.player_color) { item.Value.Ownership = false; } else if (item.Value.Player == Player.Black && !MoveGenerator.player_color) { item.Value.Ownership = false; } } // some bit operations to get the bit }
public void HumanPiecePositionChangeHandler(HumanMoveMessage action) { this.HumanTimer.stopClock(); //lock the entire board so that use cannot click on piece when it is machine's turn. foreach (KeyValuePair<int, ChessPiece> item in this.pieces_dict) { item.Value.Ownership = false; } }
public void HumanPiecePositionChangeHandler(HumanMoveMessage action) { Console.WriteLine("Player moved From {0} to {1}", action.FromPoint, action.ToPoint); ChessPiece moved=this.pieces_dict[(int)action.FromPoint.X * 10 + (int)action.FromPoint.Y]; int to_location = (int)action.ToPoint.X * 10 + (int)action.ToPoint.Y; //player castling move if (action.Castling) { int from_dic_index = (int)action.FromPoint.X * 10 + (int)action.FromPoint.Y; int to_dic_index = (int)action.ToPoint.X * 10 + (int)action.ToPoint.Y; //Console.WriteLine("from value is " + from_dic_index); ChessPiece king = this.pieces_dict[from_dic_index]; ChessPiece rook = this.pieces_dict[to_dic_index]; if (rook.Pri_Coor_X > king.Pri_Coor_X) { king.Pos_X = king.Pri_Coor_X + 2; king.Pos_Y = 7; rook.Pos_X = king.Pri_Coor_X + 1; rook.Pos_Y = 7; MoveGenerator.updateCastlingMovedBitboard(king.Pri_Coor_X + 1, true); //true =right side rook } else { king.Pos_X = king.Pri_Coor_X - 2; king.Pos_Y = 7; rook.Pos_X = king.Pri_Coor_X - 1; rook.Pos_Y = 7; MoveGenerator.updateCastlingMovedBitboard(king.Pri_Coor_X - 1, false); //false=left side rook } Console.WriteLine("Player made an castling"); this.pieces_dict.Remove(to_dic_index); this.pieces_dict.Remove(from_dic_index); this.pieces_dict.Add(king.Coor_X * 10 + king.Coor_Y, king); this.pieces_dict.Add(rook.Coor_X * 10 + rook.Coor_Y, rook); MoveGenerator.PKC = false; MoveGenerator.PQC = false; } // en passent move else if (action.AnPassent) { Console.WriteLine("Player made an passent capture"); int en_passent_cap_index = (int)action.ToPoint.X * 10 + (int)action.ToPoint.Y + 1; ChessPiece to_piece_location = this.pieces_dict[en_passent_cap_index]; int cap_index = (7 - (int)action.ToPoint.Y - 1) * 8 + (int)action.ToPoint.X; ulong passent_cap_place = 0x0000000000000001; passent_cap_place = (passent_cap_place << (cap_index)); this.pieces_collection.Remove(to_piece_location); this.pieces_dict.Remove(to_location); int from_index = (7 - (int)action.FromPoint.Y) * 8 + (int)action.FromPoint.X; int to_index = (7 - (int)action.ToPoint.Y) * 8 + (int)action.ToPoint.X; ulong moved_place = 0x0000000000000001; ulong new_place = 0x0000000000000001; moved_place = ~(moved_place << (from_index)); //can be optimised new_place = (new_place << (to_index)); MoveGenerator.UpdateAnyMovedBitboard(PieceType.Pawn, moved_place, new_place); MoveGenerator.UpdateAnyCapturedBitboard(PieceType.Pawn, passent_cap_place); Application.Current.Dispatcher.Invoke((Action)(() => { String cap_piece_img = "/PieceImg/chess_piece_" + to_piece_location.Player.ToString() + "_" + to_piece_location.Type.ToString() + ".png"; Uri uri_cap_piece_img = new Uri(cap_piece_img, UriKind.Relative); BitmapImage hm_cap_img = new BitmapImage(); hm_cap_img.BeginInit(); hm_cap_img.UriSource = uri_cap_piece_img; hm_cap_img.DecodePixelHeight = 70; hm_cap_img.DecodePixelWidth = 70; hm_cap_img.EndInit(); machine_stack.CapturedPiecesCollection.Add(hm_cap_img); })); this.pieces_dict.Add((moved.Coor_X * 10 + moved.Coor_Y), moved); this.pieces_dict.Remove((int)action.FromPoint.X * 10 + (int)action.FromPoint.Y); } //normal move else { int from_index = (7 - (int)action.FromPoint.Y) * 8 + (int)action.FromPoint.X; int to_index = (7 - (int)action.ToPoint.Y) * 8 + (int)action.ToPoint.X; ulong moved_place = 0x0000000000000001; ulong new_place = 0x0000000000000001; moved_place = MoveGenerator.full_occupied & ~(moved_place << (from_index)); //can be optimised new_place = (new_place << (to_index)); MoveGenerator.UpdateAnyMovedBitboard(action.Type, moved_place, new_place); MoveGenerator.setCurrentBitboardsHistoryMove(new Move((7 - (int)action.FromPoint.Y), (int)action.FromPoint.X, (7 - (int)action.ToPoint.Y), (int)action.ToPoint.X, action.Type, false)); if (this.pieces_dict.ContainsKey(to_location)) { // if two piece is same color,then it is a castling. Console.WriteLine("Player made a capture move"); ChessPiece to_piece_location = this.pieces_dict[to_location]; ulong removed_place = 0x0000000000000001; removed_place = (removed_place << (to_index)); this.pieces_collection.Remove(to_piece_location); this.pieces_dict.Remove(to_location); MoveGenerator.UpdateAnyCapturedBitboard(to_piece_location.Type, removed_place); Application.Current.Dispatcher.Invoke((Action)(() => { String cap_piece_img = "/PieceImg/chess_piece_" + to_piece_location.Player.ToString() + "_" + to_piece_location.Type.ToString() + ".png"; Uri uri_cap_piece_img = new Uri(cap_piece_img, UriKind.Relative); BitmapImage hm_cap_img = new BitmapImage(); hm_cap_img.BeginInit(); hm_cap_img.UriSource = uri_cap_piece_img; hm_cap_img.DecodePixelHeight = 70; hm_cap_img.DecodePixelWidth = 70; hm_cap_img.EndInit(); machine_stack.CapturedPiecesCollection.Add(hm_cap_img); })); } else { Console.WriteLine("Player made a normal move"); } this.pieces_dict.Add(to_location, moved); this.pieces_dict.Remove((int)action.FromPoint.X * 10 + (int)action.FromPoint.Y); //Console.WriteLine("Replace "+((int)action.FromPoint.X * 10 + (int)action.FromPoint.Y) + " with "+ to_location); } //code belwo is for castling move if (action.Type == PieceType.King) { MoveGenerator.PKC = false; MoveGenerator.PQC = false; } if (action.Type == PieceType.Rook && action.FromPoint.X == 0 && MoveGenerator.player_color) MoveGenerator.PQC = false; if (action.Type == PieceType.Rook && action.FromPoint.X == 7 && MoveGenerator.player_color) MoveGenerator.PKC = false; if (action.Type == PieceType.Rook && action.FromPoint.X == 0 && !MoveGenerator.player_color) MoveGenerator.PKC = false; if (action.Type == PieceType.Rook && action.FromPoint.X == 7 && !MoveGenerator.player_color) MoveGenerator.PQC = false; Console.WriteLine("Moved piece is a {0} {1} placed at {2}", this.pieces_dict[(moved.Coor_X * 10 + moved.Coor_Y)].Player, this.pieces_dict[(moved.Coor_X * 10 + moved.Coor_Y)].Type, this.pieces_dict[(moved.Coor_X * 10 + moved.Coor_Y)].Pos); Console.WriteLine(" board state is " + Convert.ToString((long)MoveGenerator.pieces_occupied, 2)); //!!!!!!! this.turn = action.Turn; }