// Undo the last move public void UndoMove() { IsOver = false; // Reset the is running variable Sounds.PlayNormalMove(); // check if the last move was promo move Move move = ChessGame.GetLastMove(); // get the last move if (ChessGame.UnDoMove()) { LogUserMove("Undo Move"); // Log the user action // Only remove the item from capture bar, if it was a capture move if (move.IsCaptureMove()) { ParentForm.ChessCaptureBar.RemoveLast(); } } // If computer is playing do the double undo if (ChessGame.ActivePlay.IsComputer()) { move = ChessGame.GetLastMove(); // get the last move ChessGame.UnDoMove(); // Only remove the item from capture bar, if it was a capture move if (move.IsCaptureMove()) { ParentForm.ChessCaptureBar.RemoveLast(); } } RedrawBoard(); // Refresh the board }
/// <summary> /// Load the current game state from the given file path /// </summary> /// <param name="filePath"></param> public void LoadGame() { // Show the File Save as dialog and get the target file path OpenFileDialog openDialog = new OpenFileDialog(); openDialog.Title = "Load CSChess file..."; openDialog.Filter = "CSChess File (*.qcf)|*.qcf"; openDialog.RestoreDirectory = true; if (openDialog.ShowDialog() == DialogResult.OK) { ChessGame = new Game(); ChessGame.Reset(); // Reset the game board ParentForm.ChessCaptureBar.Clear(); IsRunning = true; LogCounter = 0; // Handle the events fired by the library ChessGame.ComputerThinking += new ChessLibrary.Game.ChessComputerThinking(ComputerThinking); // Save the file at the given path ChessGame.LoadGame(openDialog.FileName); // Show the player info InitPlayers(); ParentForm.BlackPlayerTime.Text = ChessGame.BlackPlayer.ThinkTime; ParentForm.WhitePlayerTime.Text = ChessGame.WhitePlayer.ThinkTime; // Restore the Log and Capture bar items object[] moves = ChessGame.MoveHistory.ToArray(); for (int i = moves.Length - 1; i >= 0; i--) { Move move = (Move)moves[i]; // Log this user move LogUserMove(move.ToString()); // Add to the capture list if (move.IsCaptureMove()) { ParentForm.ChessCaptureBar.Add(ChessImages.GetImageForPiece(move.CapturedPiece)); } } // Restore the menu state ParentForm.EnableSaveMenu(); ParentForm.SetGamePrefrencesMenu(); RedrawBoard(); // Make the chess board visible on screen NextPlayerTurn(); // When the both players are computer this start the game } }
// 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 // 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 checkmate situation if (ChessGame.IsCheckMate(/*ChessGame.*/ Game.GameTurn)) { IsOver = true; MessageBox.Show(ChessGame.GetPlayerBySide(/*ChessGame.*/ Game.GameTurn).Name + " is checkmate.", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } // check for the statemate situation if (ChessGame.IsStaleMate(/*ChessGame.*/ Game.GameTurn)) { IsOver = true; MessageBox.Show(ChessGame.GetPlayerBySide(/*ChessGame.*/ Game.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); }
// Redo the move from redo history public void RedoMove() { Sounds.PlayNormalMove(); if (ChessGame.ReDoMove()) { LogUserMove("Redo Move"); // Log the user action // check if the last move was promo move Move move = ChessGame.GetLastMove(); // get the last move // Add to the capture list if (move.IsCaptureMove()) { ParentForm.ChessCaptureBar.Add(ChessImages.GetImageForPiece(move.CapturedPiece)); } } RedrawBoard(); // Refresh the board }
// 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); }