public static void Main(string[] args) { CurrentBoard = new Board(); CurrentGameStatus = GameStatus.Inactive; PieceMoves.InitiateChessPieceMoves(); PiecePseudoLegalMoves.GeneratePseudoLegalMoves(CurrentBoard); PieceLegalMoves.GenerateLegalMoves(CurrentBoard); EngineStopTokenSource = new CancellationTokenSource(); BoardOrientation = PieceColour.White; WhiteClock = new ChessClock(PieceColour.White, new TimeSpan(0, 30, 0)); BlackClock = new ChessClock(PieceColour.Black, new TimeSpan(0, 30, 0)); StrengthValue = 7; CurrentMode = GameMode.OnePlayer; CurrentGameHistory = new GameHistory(); Application.Init(); win = new MainWindow(); win.UpdateClock(WhiteClock); win.UpdateClock(BlackClock); win.InitWidgets(); win.Show(); Application.Run(); /* * UCITransceiver uci = new UCITransceiver ("./stockfish_6_x64"); * uci.Init (); * Console.WriteLine (uci.EngineName); * Console.WriteLine (uci.EngineAuthor); * uci.SendPosition ("rn2kbnr/ppq2pp1/2p1p2p/7P/3P4/3Q1NN1/PPP2PP1/R1B1K2R w KQkq - 0 11"); * uci.Go (); * Thread.Sleep (5000); * Console.WriteLine (uci.StopAndGetBestMove ()); * uci.Quit (); */ }
private void UpdateGameHistoryView() { Move move = MainClass.CurrentGameHistory.GetLastMove(); string moveOutput = ""; if (move.Colour == PieceColour.White) { moveOutput += " " + MainClass.CurrentGameHistory.MoveCount + ". "; } moveOutput += GameHistory.MoveToNotation(move) + " "; GameHistoryView.Buffer.Text += moveOutput; }
protected void OnImportPGN(object sender, EventArgs e) { var fc = new FileChooserDialog("Choose a PGN file to open.", this, FileChooserAction.Open, "Cancel", ResponseType.Cancel, "Open", ResponseType.Accept); if (fc.Run() == (int)ResponseType.Accept) { MainClass.CurrentGameHistory = GameHistory.importPGN(File.ReadAllText(fc.Filename)); string pgn = MainClass.CurrentGameHistory.ToPGNString(); int indexOfMovesStart = pgn.IndexOf("1."); if (indexOfMovesStart > 0) { GameHistoryView.Buffer.Text = pgn.Substring(indexOfMovesStart); } // Load the FEN from the last move FENParser parser = new FENParser(MainClass.CurrentGameHistory.GetLastMove().FEN); MainClass.CurrentBoard = parser.GetBoard(); MainClass.CurrentGameStatus = GameStatus.Inactive; PiecePseudoLegalMoves.GeneratePseudoLegalMoves(MainClass.CurrentBoard); PieceLegalMoves.GenerateLegalMoves(MainClass.CurrentBoard); Gtk.Application.Invoke(delegate { RedrawBoard(); }); MainClass.CurrentGameStatus = GameStatus.Inactive; GameStatus currentStatus = MainClass.CurrentBoard.CheckForMate(); if (currentStatus != GameStatus.Inactive && currentStatus != GameStatus.Active) { Gtk.Application.Invoke(delegate { ShowGameOverDialog(currentStatus); }); } MainClass.ResetClock(); UpdateMaterialDifference(MainClass.CurrentBoard); } fc.Destroy(); }
public static GameHistory importPGN(string PGN) { GameHistory newGameHistory = new GameHistory(); //Parse tag pairs first string startingFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; while (PGN.Contains("[")) { string tagPair = PGN.Substring(PGN.IndexOf('[') + 1, PGN.IndexOf(']') - PGN.IndexOf('[') - 1); PGN = PGN.Substring(PGN.IndexOf(']') + 1); switch (tagPair.Substring(0, tagPair.IndexOf(' '))) { case "Event": newGameHistory.Event = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; case "Site": newGameHistory.Site = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; case "Date": newGameHistory.Date = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; case "Round": newGameHistory.Round = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; case "White": newGameHistory.White = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; case "Black": newGameHistory.Black = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; case "Result": newGameHistory.Result = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; case "FEN": startingFEN = tagPair.Substring(tagPair.IndexOf('"') + 1).TrimEnd('"'); break; default: break; } } //Remove comments from PGN while (PGN.Contains("{")) { PGN = PGN.Remove(PGN.IndexOf('{'), PGN.IndexOf('}') - PGN.IndexOf('{') + 1); } //Take in moves one by one FENParser importFEN = new FENParser(startingFEN); Board gameBoard = importFEN.GetBoard(); PiecePseudoLegalMoves.GeneratePseudoLegalMoves(gameBoard); PieceLegalMoves.GenerateLegalMoves(gameBoard); List <Tuple <Move, string> > possibleMoveNotations = getPossibleMoveNotations(gameBoard); string[] tokens = PGN.Split(null); for (int i = 0; i < tokens.Length; i++) { char[] trimChars = { '!', '?' }; char[] checkChars = { '+', '#' }; string moveNotation = tokens[i].Trim(trimChars); if (moveNotation.Contains('.')) { moveNotation = moveNotation.Substring(moveNotation.IndexOf('.') + 1); } if (moveNotation.Length != 0) { //Console.WriteLine("***" + moveNotation + "***"); //debugging output for (int j = 0; j < possibleMoveNotations.Count; j++) { //Console.Write(possibleMoveNotations[j].Item2 + " "); if (moveNotation.Equals(possibleMoveNotations[j].Item2) || moveNotation.Equals(possibleMoveNotations[j].Item2.Trim(checkChars))) { gameBoard.MakeMove(possibleMoveNotations[j].Item1.Source, possibleMoveNotations[j].Item1.Destination, possibleMoveNotations[j].Item1.PromoteTo); newGameHistory.AddMove(possibleMoveNotations[j].Item1, gameBoard.ToFEN().Split(' ')[0]); possibleMoveNotations = getPossibleMoveNotations(gameBoard); break; } } //Console.WriteLine(""); //debugging output } } return(newGameHistory); }
protected void OnPieceClick(object o, ButtonPressEventArgs args) { double transx = Math.Abs((BoardArea.Allocation.Width - (boardBackground.Width * 0.75))) / 2; PointD clickLocation = new PointD(args.Event.X - transx, args.Event.Y - transx); if (clickLocation.X < 30 || clickLocation.Y < 30 || clickLocation.X > 522 || clickLocation.Y > 522) { return; } PointD pieceLocation = PieceDisplay.pieceCoordinates [0]; int pieceIndex = 0; for (int i = 0; i < PieceDisplay.pieceCoordinates.Length; i++) { PointD p = PieceDisplay.pieceCoordinates[i]; double x1 = p.X * 0.75; double y1 = p.Y * 0.75; double x2 = x1 + 61.5; double y2 = y1 + 61.5; if (x1 <= clickLocation.X && clickLocation.X <= x2) { if (y1 <= clickLocation.Y && clickLocation.Y <= y2) { pieceLocation = p; pieceIndex = MainClass.BoardOrientation == PieceColour.White ? i : Math.Abs(i - 63); break; } } } if (currentSelectionState == PieceSelectionState.None) { if (MainClass.CurrentBoard.Squares [pieceIndex].Piece == null) { return; } selectedPiece = (byte)pieceIndex; boardContext = Gdk.CairoHelper.Create(BoardArea.GdkWindow); boardContext.Translate(transx, 0); boardContext.Scale(0.75, 0.75); selectionBorder.Show(boardContext, pieceLocation.X + 1, pieceLocation.Y + 1); boardContext.Dispose(); currentSelectionState = PieceSelectionState.Selected; } else { if (!MainClass.CurrentBoard.IsMoveValid(selectedPiece, (byte)pieceIndex)) { currentSelectionState = PieceSelectionState.None; Gtk.Application.Invoke(delegate { RedrawBoard(); }); return; } if (MainClass.CurrentGameStatus != GameStatus.Active && MainClass.CurrentGameStatus != GameStatus.Inactive) { Console.Error.WriteLine("(EE) Attempted move during finished game."); MessageDialog errorDialog = new MessageDialog( this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, "The game is over!"); errorDialog.Run(); errorDialog.Destroy(); return; } // Handle pawn promotion PieceType?promoteTo = null; if (MainClass.CurrentBoard.Squares [selectedPiece].Piece.Type == PieceType.Pawn && MainClass.CurrentBoard.IsMoveValid(selectedPiece, (byte)pieceIndex) && Array.IndexOf(MainClass.CurrentBoard.pawnPromotionDestinations, (byte)pieceIndex) != -1) { PawnPromotionDialog dialog = new PawnPromotionDialog(); if (dialog.Run() == (int)Gtk.ResponseType.Ok) { promoteTo = dialog.PromoteTo; } else { dialog.Destroy(); return; } dialog.Destroy(); } try { SpecifierType specifierRequired = GameHistory.checkDisabiguationNeeded(MainClass.CurrentBoard, selectedPiece, (byte)pieceIndex); MoveResult result = MainClass.CurrentBoard.MakeMove(selectedPiece, (byte)pieceIndex, promoteTo); Piece movingPiece = null; if (promoteTo == null) { movingPiece = MainClass.CurrentBoard.Squares[(byte)pieceIndex].Piece; } else { movingPiece = new Piece(MainClass.CurrentBoard.Squares [(byte)pieceIndex].Piece.Colour, PieceType.Pawn); } if (result == MoveResult.Capture && movingPiece.Type == PieceType.Pawn) { specifierRequired = SpecifierType.File; } int checkOrCheckmate = 0; GameStatus mateState = MainClass.CurrentBoard.CheckForMate(); if (mateState == GameStatus.WhiteCheckmate || mateState == GameStatus.BlackCheckmate) { checkOrCheckmate = 2; } else if (MainClass.CurrentBoard.WhiteCheck || MainClass.CurrentBoard.BlackCheck) { checkOrCheckmate = 1; } string fenPosition = MainClass.CurrentBoard.ToFEN().Split(' ')[0]; MainClass.CurrentGameHistory.AddMove(new Move(selectedPiece, (byte)pieceIndex, MainClass.CurrentBoard.Squares [(byte)pieceIndex].Piece.Colour, movingPiece, result, MainClass.CurrentBoard.ToFEN(), checkOrCheckmate, specifierRequired, promoteTo), fenPosition); UpdateGameHistoryView(); if (MainClass.CurrentGameHistory.UpdateFiftyMoveCount(result) == GameStatus.DrawFifty) { MainClass.CurrentGameStatus = GameStatus.DrawFifty; } else if (MainClass.CurrentGameHistory.CheckThreefoldRepetition() == GameStatus.DrawRepetition) { MainClass.CurrentGameStatus = GameStatus.DrawRepetition; } } catch (InvalidOperationException) { } Gtk.Application.Invoke(delegate { RedrawBoard(); }); GameStatus isMate = MainClass.CurrentBoard.CheckForMate(); if (isMate != GameStatus.Active) { MainClass.CurrentGameStatus = isMate; } if (MainClass.CurrentGameStatus != GameStatus.Active && MainClass.CurrentGameStatus != GameStatus.Inactive) { ShowGameOverDialog(MainClass.CurrentGameStatus); } if (MainClass.CurrentGameStatus == GameStatus.Inactive) { MainClass.CurrentGameStatus = GameStatus.Active; } Gtk.Application.Invoke(delegate { MainClass.UpdateClock(); UpdatePlayerToMove(); }); currentSelectionState = PieceSelectionState.None; } }