// A move is made by the player public bool UserMove(string source, string dest) { bool success = true; int MoveResult = ChessGame.DoMove(source, dest); RedrawBoard(); // Refresh the board switch (MoveResult) { case 0: // move was successfull; // check if the last move was promo move Move move = ChessGame.GetLastMove(); // get the last move // Play the sound if (ChessGame.IsUnderCheck()) { Sounds.PlayCheck(); // Player is under check } else if (move.Type == Move.MoveType.NormalMove || move.Type == Move.MoveType.TowerMove) { Sounds.PlayNormalMove(); } else { Sounds.PlayCaptureMove(); } // Add to the capture list if (move.IsCaptureMove()) { ParentForm.ChessCaptureBar.Add(ChessImages.GetImageForPiece(move.CapturedPiece)); } // If last move was a pawn promotion move, get the new selected piece from user if (move.IsPromoMove() && !ChessGame.ActivePlay.IsComputer()) { ChessGame.SetPromoPiece(GetPromoPiece(move.EndCell.piece.Side)); // Set the promo piece as selected by user } // check for the check mate situation if (ChessGame.IsCheckMate(ChessGame.GameTurn)) { Sounds.PlayGameOver(); IsOver = true; MessageBox.Show(ChessGame.GetPlayerBySide(ChessGame.GameTurn).Name + " is checkmate.", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } // check for the statemate situation if (ChessGame.IsStaleMate(ChessGame.GameTurn)) { Sounds.PlayGameOver(); IsOver = true; MessageBox.Show(ChessGame.GetPlayerBySide(ChessGame.GameTurn).Name + " is stalmate.", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } LogUserMove(move.ToString()); // Log the user action NextPlayerTurn(); break; default: success = false; break; } return(success); }
// Handle the computer thinking event public void ComputerThinking(int depth, int currentMove, int TotalMoves, int TotalAnalzyed, Move BestMove) { if (ShowComputerThinkingProgres) { // Update label and progress bar to display the computer think status ParentForm.PrgComputerThinkDepth.Maximum = TotalMoves; ParentForm.PrgComputerThinkDepth.Value = currentMove; ParentForm.LblComuterThinkLabel.Text = "Computer thinking at depth " + depth.ToString() + ". Total moves analyzed: " + TotalAnalzyed + ". "; if (BestMove != null) { ParentForm.LblComuterThinkLabel.Text += "Best move found so far is :" + BestMove.ToString(); } } }
// A move is made by the player public bool UserMove(string source, string dest) { sr.RecognizeAsyncStop(); bool success = true; int MoveResult = ChessGame.DoMove(source, dest); Console.Write(" --- MoveResult:: " + MoveResult + " --- "); RedrawBoard(); // Refresh the board switch (MoveResult) { case 0: // move was successful; // check if the last move was promo move Move move = ChessGame.GetLastMove(); // get the last move // Play the sound if (ChessGame.IsUnderCheck()) { Sounds.PlayCheck(); // Player is under check sSynth.SpeakAsync("Check."); // Speech indication of player being under check } else if (move.Type == Move.MoveType.NormalMove || move.Type == Move.MoveType.TowerMove) { Sounds.PlayNormalMove(); } else { Sounds.PlayCaptureMove(); } // Add to the capture list if (move.IsCaptureMove()) { ParentForm.ChessCaptureBar.Add(ChessImages.GetImageForPiece(move.CapturedPiece)); } // If last move was a pawn promotion move, get the new selected piece from user if (move.IsPromoMove() && !ChessGame.ActivePlay.IsComputer()) { ChessGame.SetPromoPiece(GetPromoPiece(move.EndCell.piece.Side)); // Set the promo piece as selected by user } // check for the check mate situation if (ChessGame.IsCheckMate(ChessGame.GameTurn)) { Sounds.PlayGameOver(); sSynth.SpeakAsync("Check mate."); sSynth.SpeakAsync("Game over."); IsOver = true; MessageBox.Show(ChessGame.GetPlayerBySide(ChessGame.GameTurn).Name + " is checkmate.", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } // check for the stalemate situation if (ChessGame.IsStaleMate(ChessGame.GameTurn)) { Sounds.PlayGameOver(); sSynth.SpeakAsync("Stale mate."); sSynth.SpeakAsync("Game over."); IsOver = true; MessageBox.Show(ChessGame.GetPlayerBySide(ChessGame.GameTurn).Name + " is stalemate.", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } String output = move.ToString(); StringBuilder bar = new StringBuilder(); foreach (char c in output) { if (Char.IsDigit(c)) { int val = (int)Char.GetNumericValue(c); int newVal = 8 - val + 1; bar.Append(newVal); } else { bar.Append(c); } } LogUserMove(bar.ToString()); Console.Write("---------"); Console.Write(move.ToString()); // Log the user action Console.Write(bar.ToString()); Console.Write("---------"); sSynth.SpeakAsync(bar.ToString()); // Speech indicates piece taken NextPlayerTurn(); break; default: success = false; break; } return(success); }