void RemovePiece(object sender, EventArgs e) { if (capturedPiece != null) { capturedPiece.Visibility = Visibility.Hidden; if (capturedPiece is RedChecker) { RedChecker deadman = new RedChecker(); Storyboard AddToGraveyard = deadman.Resources["AddToGraveyard"] as Storyboard; this.pnlBlackGraveyard.Children.Add(deadman); AddToGraveyard.Begin(); } else if (capturedPiece is BlackChecker) { BlackChecker deadman = new BlackChecker(); Storyboard AddToGraveyard = deadman.Resources["AddToGraveyard"] as Storyboard; this.pnlRedGraveyard.Children.Add(deadman); AddToGraveyard.Begin(); } else if (capturedPiece is RedKingChecker) { RedKingChecker deadman = new RedKingChecker(); Storyboard AddToGraveyard = deadman.Resources["AddToGraveyard"] as Storyboard; this.pnlBlackGraveyard.Children.Add(deadman); AddToGraveyard.Begin(); } else if (capturedPiece is BlackKingChecker) { BlackKingChecker deadman = new BlackKingChecker(); Storyboard AddToGraveyard = deadman.Resources["AddToGraveyard"] as Storyboard; this.pnlRedGraveyard.Children.Add(deadman); AddToGraveyard.Begin(); } grdBoard.Children.Remove(capturedPiece); } }
// Here, the source is the drop target which in this case is a Label. This is needed to get // a reference to the underlying grid cell. That way we know the cell to which to add the new // image. void grdBoard_Drop(object sender, DragEventArgs e) { // /red is user string from = "", to = ""; //hit = ""; // use the label in the cell to get the current row and column EmptySpace l = e.Source as EmptySpace; int r = Grid.GetRow((EmptySpace)e.Source); int c = Grid.GetColumn((EmptySpace)e.Source); okToMove = false; from = currentPiece.row + "," + currentPiece.col; to = r + "," + c; CheckerPiece checker; if (currentPiece is RedChecker || currentPiece is RedKingChecker) { if (currentTurn != Turn.Red) { // Should never be here System.Windows.Forms.MessageBox.Show("It's not your turn"); return; } illegalTouchCounter = 0; // It's red's turn... if (currentPiece is RedKingChecker || (currentPiece is RedChecker && r == RED_LAST_ROW)) { checker = new RedKingChecker(); } else { checker = new RedChecker(); } if (currentPiece is RedChecker || currentPiece is RedKingChecker) { if ((l.row == currentPiece.row - 1 && (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1)) || ((l.row == currentPiece.row + 1 && currentPiece is RedKingChecker) && (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1))) { okToMove = true; } } //now check to see if user captured anything //if the logic is hard to follow, //it basically checks the location if the supposedly hit agent(black) checker //based on the fact that user (red) piece could be crown, and assign a //crowned or normal hit piece, if any, to the opponentPiece variable. //>> CheckerPiece opponentPiece = null; //was black if (c == currentPiece.col + 2) { if (r == currentPiece.row - 2) { if (grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col + 1)).SingleOrDefault() != null) { opponentPiece = grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col + 1)).SingleOrDefault(); } else { opponentPiece = grdBoard.Children.OfType <BlackKingChecker>() .Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col + 1)).SingleOrDefault(); } } else if (r == currentPiece.row + 2 && currentPiece is RedKingChecker) { if (grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col + 1)).SingleOrDefault() != null) { opponentPiece = grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col + 1)).SingleOrDefault(); } else { opponentPiece = grdBoard.Children.OfType <BlackKingChecker>() .Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col + 1)).SingleOrDefault(); } } } else if (c == currentPiece.col - 2) { if (r == currentPiece.row - 2) { if (grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col - 1)).SingleOrDefault() != null) { opponentPiece = grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col - 1)).SingleOrDefault(); } else { opponentPiece = grdBoard.Children.OfType <BlackKingChecker>() .Where(p => p.row == currentPiece.row - 1 && (p.col == currentPiece.col - 1)).SingleOrDefault(); } } else if ((r == currentPiece.row + 2 && currentPiece is RedKingChecker)) { if (grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col - 1)).SingleOrDefault() != null) { opponentPiece = grdBoard.Children.OfType <BlackChecker>() .Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col - 1)).SingleOrDefault(); } else { opponentPiece = grdBoard.Children.OfType <BlackKingChecker>() .Where(p => p.row == currentPiece.row + 1 && (p.col == currentPiece.col - 1)).SingleOrDefault(); } } } //<< //>> now remove if any black or black king was hit if (opponentPiece != null && Math.Abs(l.row - currentPiece.row) == 2) { int validCol = (opponentPiece.col > currentPiece.col) ? currentPiece.col + 2 : currentPiece.col - 2; if (((r == currentPiece.row - 2) || (Math.Abs(r - currentPiece.row) == 2 && currentPiece is RedKingChecker)) && c == validCol) { Storyboard PieceCaptured = opponentPiece.Resources["PieceCaptured"] as Storyboard; capturedPiece = opponentPiece; if (PieceCaptured != null) { PieceCaptured.Completed += new EventHandler(RemovePiece); PieceCaptured.Begin(); } okToMove = true; } } if (okToMove) { //currentTurn = Turn.Black; this.UserPlayed(this, new CheckerEventArg { moveDesc = from + "//" + to }); } } else // that is, if touched checker is black or black king <><><><><><><><><<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> { //when user touches agent stuff... if (currentTurn != Turn.Black) { this.UserTouchedAgentChecker(this, new UserTouchedAgentStuffEventArg { howManyTimes = ++illegalTouchCounter }); //System.Windows.Forms.MessageBox.Show("Wait, that's mine!"); return; } //should never be here //>>Few lines below only for debugging (until "<<") checker = new BlackChecker(); if (l.row == currentPiece.row + 1 && (l.col == currentPiece.col + 1 || l.col == currentPiece.col - 1)) { okToMove = true; } CaptureHumanCellInAgentMoveIfAny(r, c, l); if (okToMove) { currentTurn = Turn.Red; } //<< } //if (okToMove // && (checker is BlackChecker || checker is BlackKingChecker)) //{ // MoveChecker(r, c, checker); //} //not doing the move until confirmed by Java side if (okToMove && (checker is RedChecker || checker is RedKingChecker)) { latestR = r; latestC = c; LatestRedTryingToMove = checker; } }
/// <summary> /// This function loads the game pieces into the grid cells and prepares everything /// </summary> public void Reset() { currentTurn = Turn.Black; this.pnlBlackGraveyard.Children.Clear(); this.pnlRedGraveyard.Children.Clear(); int col = 0; for (int row = 0; row < grdBoard.RowDefinitions.Count; row++) { for (col = 0; col < grdBoard.ColumnDefinitions.Count; col++) { Border b = new Border(); b.BorderThickness = new Thickness(1, 1, 1, 1); b.BorderBrush = Brushes.Black; Grid.SetColumn(b, col); Grid.SetRow(b, row); this.grdBoard.Children.Add(b); } } for (int row = 0; row < grdBoard.RowDefinitions.Count; row++) { // put a piece in every other cell for (col = (col % 2 != 0 ? 0 : 1); col < grdBoard.ColumnDefinitions.Count; col += 2) { EmptySpace l = new EmptySpace(); l.Margin = new Thickness(0, 0, 0, 0); l.AllowDrop = true; l.Background = Brushes.Black; l.Name = "Label" + (row * col).ToString(); l.Drop += new DragEventHandler(grdBoard_Drop); l.col = col; l.row = row; Grid.SetColumn(l, col); Grid.SetRow(l, row); this.grdBoard.Children.Add(l); if (row >= grdBoard.RowDefinitions.Count - 3) { RedChecker redChecker = new RedChecker(); redChecker.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(grdBoard_PreviewMouseLeftButtonDown); redChecker.PreviewMouseMove += new MouseEventHandler(grdBoard_PreviewMouseMove); redChecker.Cursor = Cursors.Hand; redChecker.AllowDrop = false; redChecker.col = col; redChecker.row = row; Grid.SetColumn(redChecker, col); Grid.SetRow(redChecker, row); this.grdBoard.Children.Add(redChecker); } if (row < 3) { BlackChecker blackChecker = new BlackChecker(); blackChecker.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(grdBoard_PreviewMouseLeftButtonDown); blackChecker.PreviewMouseMove += new MouseEventHandler(grdBoard_PreviewMouseMove); blackChecker.Cursor = Cursors.Hand; blackChecker.AllowDrop = false; blackChecker.col = col; blackChecker.row = row; Grid.SetColumn(blackChecker, col); Grid.SetRow(blackChecker, row); this.grdBoard.Children.Add(blackChecker); } } } }