public void MachinePiecePositionChangeHandler(MachineMoveMessage action) { int from_loca_index = action.From_File * 10 + (7 - action.From_Rank); int to_loca_index = action.To_File * 10 + (7 - action.To_Rank); ChessPiece moved = this.pieces_dict[from_loca_index]; //this is all for castling move updating on frontend if (action.MKC) { if(action.From_File==4) //means machine use black { ChessPiece rook_king_side = this.pieces_dict[70]; moved.Pos_X = 6; rook_king_side.Pos_X = 5; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(70); this.pieces_dict.Add(60, moved); this.pieces_dict.Add(50, rook_king_side); } else //means machine use white { ChessPiece rook_king_side = this.pieces_dict[0]; moved.Pos_X = 1; rook_king_side.Pos_X = 2; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(0); this.pieces_dict.Add(10, moved); this.pieces_dict.Add(20, rook_king_side); } return ; } if (action.MQC) { if (action.From_File == 4) //means machine use black { ChessPiece rook_king_side = this.pieces_dict[0]; moved.Pos_X = 2; rook_king_side.Pos_X = 3; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(0); this.pieces_dict.Add(20, moved); this.pieces_dict.Add(30, rook_king_side); } else //means machine use white { ChessPiece rook_king_side = this.pieces_dict[70]; moved.Pos_X = 5; rook_king_side.Pos_X = 4; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(70); this.pieces_dict.Add(50, moved); this.pieces_dict.Add(40, rook_king_side); } return; } if (this.pieces_dict.ContainsKey(to_loca_index)) { ChessPiece to_piece_location = this.pieces_dict[to_loca_index]; Application.Current.Dispatcher.Invoke((Action)(() => this.pieces_collection.Remove(to_piece_location))); this.pieces_dict.Remove(to_loca_index); Application.Current.Dispatcher.Invoke((Action)(() => { String cap_piece_img = "/PieceImg/chess_piece_" + to_piece_location.Player.ToString() + "_" + to_piece_location.Type.ToString()+".png"; //Console.WriteLine(cap_piece_img); Uri uri_cap_piece_img = new Uri(cap_piece_img, UriKind.Relative); BitmapImage hm_cap_img = new BitmapImage(); // BitmapImage resized_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(); human_capture.CapturedPiecesCollection.Add(hm_cap_img); })); } moved.Pos_X = action.To_File; moved.Pos_Y = 7 - action.To_Rank; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Add(to_loca_index, moved); //unlock human player pieces so he can go on foreach (KeyValuePair<int, ChessPiece> item in this.pieces_dict) { if (item.Value.Player == Player.White && MoveGenerator.player_color) { item.Value.Ownership = action.Turn; } else if (item.Value.Player == Player.Black && !MoveGenerator.player_color) { item.Value.Ownership = action.Turn; } } this.HumanTimer.startClock(); }
public void MachinePiecePositionChangeHandler(MachineMoveMessage action) { int from_loca_index = action.From_File * 10 + (7 - action.From_Rank); int to_loca_index = action.To_File * 10 + (7 - action.To_Rank); ChessPiece moved = this.pieces_dict[from_loca_index]; //unlock human player pieces so he can go on foreach (KeyValuePair<int, ChessPiece> item in this.pieces_dict) { if (item.Value.Player == Player.White && MoveGenerator.player_color) { item.Value.Ownership = action.Turn; } else if (item.Value.Player == Player.Black && !MoveGenerator.player_color) { item.Value.Ownership = action.Turn; } } //this is all for castling move updating on frontend if (action.MKC) { if(action.From_File==4) //means machine use black { ChessPiece rook_king_side = this.pieces_dict[70]; moved.Pos_X = 6; rook_king_side.Pos_X = 5; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(70); this.pieces_dict.Add(60, moved); this.pieces_dict.Add(50, rook_king_side); } else //means machine use white { ChessPiece rook_king_side = this.pieces_dict[0]; moved.Pos_X = 1; rook_king_side.Pos_X = 2; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(0); this.pieces_dict.Add(10, moved); this.pieces_dict.Add(20, rook_king_side); } this.HumanTimer.startClock(); return ; } if (action.MQC) { if (action.From_File == 4) //means machine use black { ChessPiece rook_king_side = this.pieces_dict[0]; moved.Pos_X = 2; rook_king_side.Pos_X = 3; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(0); this.pieces_dict.Add(20, moved); this.pieces_dict.Add(30, rook_king_side); } else //means machine use white { ChessPiece rook_king_side = this.pieces_dict[70]; moved.Pos_X = 5; rook_king_side.Pos_X = 4; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(70); this.pieces_dict.Add(50, moved); this.pieces_dict.Add(40, rook_king_side); } this.HumanTimer.startClock(); return; } if (action.PQC) { if (action.From_File == 4) //means machine use black { ChessPiece rook_queen_side = this.pieces_dict[7]; moved.Pos_X = 2; rook_queen_side.Pos_X = 3; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(0); this.pieces_dict.Add(27, moved); this.pieces_dict.Add(37, rook_queen_side); } else //means machine use white { ChessPiece rook_queen_side = this.pieces_dict[77]; moved.Pos_X = 5; rook_queen_side.Pos_X = 4; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(77); this.pieces_dict.Add(57, moved); this.pieces_dict.Add(47, rook_queen_side); } this.HumanTimer.startClock(); return; } if (action.PKC) { if (action.From_File == 4) //means machine use black { ChessPiece rook_king_side = this.pieces_dict[77]; moved.Pos_X = 6; rook_king_side.Pos_X = 5; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(77); this.pieces_dict.Add(67, moved); this.pieces_dict.Add(57, rook_king_side); } else //means machine use white { ChessPiece rook_king_side = this.pieces_dict[7]; moved.Pos_X = 1; rook_king_side.Pos_X = 2; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Remove(7); this.pieces_dict.Add(17, moved); this.pieces_dict.Add(27, rook_king_side); } this.HumanTimer.startClock(); return; } if (action.Promotion) { moved.Type = PieceType.Queen; } //this is capture ChessPiece to_piece_location= null; if (this.pieces_dict.ContainsKey(to_loca_index)) { to_piece_location = this.pieces_dict[to_loca_index]; Application.Current.Dispatcher.Invoke((Action)(() => this.pieces_collection.Remove(to_piece_location))); this.pieces_dict.Remove(to_loca_index); 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(); human_capture.CapturedPiecesCollection.Add(hm_cap_img); })); } moved.Pos_X = action.To_File; moved.Pos_Y = 7 - action.To_Rank; this.pieces_dict.Remove(from_loca_index); this.pieces_dict.Add(to_loca_index, moved); if (MoveGenerator.isPlayerKingCheckmate()) { MessageBoxResult result = MessageBox.Show("Player lost the game", "Confirmation", MessageBoxButton.OK); if (result == MessageBoxResult.OK) { Application.Current.Dispatcher.Invoke((Action)(() => { Application.Current.Shutdown(); })); } } this.HumanTimer.startClock(); }