/// <summary> /// Cancel a piece selection /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCancelSelect_Click(object sender, RoutedEventArgs e) { if (pieceMoving != null) { moving = false; pieceMoving = null; originMoving = null; this.chessBoardUC.clean(game.Background); } }
/// <summary> /// mouseLeftButtonDown : Actions when player clic on the chessboard in the game window /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void mouseLeftButtonDown(object sender, MouseButtonEventArgs e) { StackPanel stackPanel = (StackPanel)sender; int col = Grid.GetColumn(stackPanel); int row = Grid.GetRow(stackPanel); logger.Log("clic on col=" + col + ", row=" + row + "; stackPanel=" + stackPanel.Name); if (!chessBoardUC.ChessBoard.KingInCheck) { this.mainGame.btnCancelSelect.IsEnabled = true; } else { this.mainGame.btnCancelSelect.IsEnabled = false; } if (!moving && stackPanel.Children.Count == 0) { return; } // clic to select a piece... else if (!moving && stackPanel.Children.Count > 0) { pieceMoving = (PieceUserControl)stackPanel.Children[0]; if (pieceMoving.Piece.Player != game.CurrentPlayer) { System.Windows.Forms.MessageBox.Show("This piece is not one of yours!", "Wrong piece", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); pieceMoving = null; return; } stackPanel.Background = Brushes.Green; moving = true; logger.Log("Piece selected=" + pieceMoving.Piece.Name + ", x=" + pieceMoving.Piece.XPosition + ", y=" + pieceMoving.Piece.YPosition); originMoving = stackPanel; if (game.CurrentPlayer.Help == true) { foreach (StackPanel sp in chessBoardUC.grid.Children) { Case spCase = chessBoardUC.ChessBoard.GetCase(Grid.GetColumn(sp), (7 - Grid.GetRow(sp))); if (chessBoardUC.ChessBoard.MovePossible(pieceMoving.Piece, spCase)) { sp.Background = Brushes.Aquamarine; } } } } //clic to move to a case else if (moving) { // if King is moving we check if the case where he's going would not put him in check if (pieceMoving.Piece.GetType().Equals(typeof(King))) { //here we have to move the king for real to enable movePossible for a pawn (special case of eating...) Piece tmp = null; if (chessBoardUC.ChessBoard.GetCase(col, 7 - row).Piece != null) { tmp = chessBoardUC.ChessBoard.GetCase(col, 7 - row).Piece; Piece piece = chessBoardUC.ChessBoard.GetPiece(col, (7 - row)); piece.State = State.DEAD; } chessBoardUC.ChessBoard.GetCase(col, 7 - row).Piece = pieceMoving.Piece; Case spCase = chessBoardUC.ChessBoard.GetCase(Grid.GetColumn(originMoving), (7 - Grid.GetRow(originMoving))); spCase.Piece = null; if (chessBoardUC.ChessBoard.PutKingInCheck(chessBoardUC.ChessBoard.GetCase(col, 7 - row), game.CurrentPlayer)) { System.Windows.Forms.MessageBox.Show("You can't move your king to this case, it would be in check!", "Case not allowed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); if (tmp != null) { // kind of rollback chessBoardUC.ChessBoard.GetCase(col, 7 - row).Piece = tmp; Piece piece = chessBoardUC.ChessBoard.GetPiece(col, (7 - row)); piece.State = State.ALIVE; tmp = null; spCase.Piece = pieceMoving.Piece; } else { chessBoardUC.ChessBoard.GetCase(col, 7 - row).Piece = null; } return; } if (tmp != null) { // kind of rollback chessBoardUC.ChessBoard.GetCase(col, 7 - row).Piece = tmp; Piece piece = chessBoardUC.ChessBoard.GetPiece(col, (7 - row)); piece.State = State.ALIVE; tmp = null; spCase.Piece = pieceMoving.Piece; } else { chessBoardUC.ChessBoard.GetCase(col, 7 - row).Piece = null; } } if (chessBoardUC.ChessBoard.MovePossible(pieceMoving.Piece, chessBoardUC.ChessBoard.GetCase(col, 7 - row))) { if (stackPanel.Children.Count > 0) { PieceUserControl pieceToKill = (PieceUserControl)stackPanel.Children[0]; if (chessBoardUC.ChessBoard.Kill(pieceToKill.Piece)) { stackPanel.Children.Remove(pieceToKill); } } } if (chessBoardUC.ChessBoard.Move(pieceMoving.Piece, chessBoardUC.ChessBoard.GetCase(col, 7 - row))) { originMoving.Children.Remove(pieceMoving); stackPanel.Children.Add(pieceMoving); if (pieceMoving.Piece.GetType().Equals(typeof(King))) { if (game.CurrentPlayer == game.Player1) { chessBoardUC.ChessBoard.P1KingCase = chessBoardUC.ChessBoard.GetCase(col, 7 - row); } else { chessBoardUC.ChessBoard.P2KingCase = chessBoardUC.ChessBoard.GetCase(col, 7 - row); } } moving = false; pieceMoving = null; originMoving = null; chessBoardUC.clean(game.Background); // TODO gérer ça comme il faut if (game.CurrentPlayer == game.Player1) { // check if King in check chessBoardUC.ChessBoard.KingInCheck = chessBoardUC.ChessBoard.PutKingInCheck(chessBoardUC.ChessBoard.P2KingCase, game.Player2); if (chessBoardUC.ChessBoard.KingInCheck) { // in case of KingInCheck, we select the other player KingUC for next move StackPanel sP = (StackPanel)chessBoardUC.GetGridElement(chessBoardUC.grid, chessBoardUC.ChessBoard.P2KingCase.XPosition, chessBoardUC.ChessBoard.P2KingCase.YPosition); sP.Background = Brushes.Red; pieceMoving = (PieceUserControl)sP.Children[0]; originMoving = sP; moving = true; this.mainGame.btnCancelSelect.IsEnabled = false; bool checkmate = false; foreach (Case caseItem in chessBoardUC.ChessBoard.Cases) { if (chessBoardUC.ChessBoard.MovePossible(pieceMoving.Piece, caseItem)) { checkmate = true; } } if (!checkmate || game.ChessBoard.P2KingCase.Piece == null || game.ChessBoard.P2KingCase.Piece.State == State.DEAD) { game.Player2.State = State.DEAD; EndGame(game.Player1); } else { System.Windows.Forms.MessageBox.Show("Player 2, your King is in Check", "Player2 King In Check", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); if (game.CurrentPlayer.Help == true) { foreach (StackPanel sp in chessBoardUC.grid.Children) { Case spCase = chessBoardUC.ChessBoard.GetCase(Grid.GetColumn(sp), (7 - Grid.GetRow(sp))); if (chessBoardUC.ChessBoard.MovePossible(pieceMoving.Piece, spCase)) { sp.Background = Brushes.Aquamarine; } } } } } } else { chessBoardUC.ChessBoard.KingInCheck = chessBoardUC.ChessBoard.PutKingInCheck(chessBoardUC.ChessBoard.P1KingCase, game.Player1); if (chessBoardUC.ChessBoard.KingInCheck) { // in case of KingInCheck, we select the other player KingUC for next move StackPanel sP = (StackPanel)chessBoardUC.GetGridElement(chessBoardUC.grid, chessBoardUC.ChessBoard.P1KingCase.XPosition, chessBoardUC.ChessBoard.P1KingCase.YPosition); pieceMoving = (PieceUserControl)sP.Children[0]; originMoving = sP; moving = true; this.mainGame.btnCancelSelect.IsEnabled = false; bool checkmate = false; foreach (Case caseItem in chessBoardUC.ChessBoard.Cases) { if (chessBoardUC.ChessBoard.MovePossible(pieceMoving.Piece, caseItem)) { checkmate = true; } } if (!checkmate || game.ChessBoard.P1KingCase.Piece == null || game.ChessBoard.P1KingCase.Piece.State == State.DEAD) { game.Player1.State = State.DEAD; EndGame(game.Player2); } else { System.Windows.Forms.MessageBox.Show("Player 1, your King is in Check", "Player1 King In Check", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); if (game.CurrentPlayer.Help == true) { foreach (StackPanel sp in chessBoardUC.grid.Children) { Case spCase = chessBoardUC.ChessBoard.GetCase(Grid.GetColumn(sp), (7 - Grid.GetRow(sp))); if (chessBoardUC.ChessBoard.MovePossible(pieceMoving.Piece, spCase)) { sp.Background = Brushes.Aquamarine; } } } } } } } if (game.CurrentPlayer == game.Player1) { game.CurrentPlayer = game.Player2; } else { game.CurrentPlayer = game.Player1; } } e.Handled = true; }