public void Register(ChessSettings settings)
        {
            _settings = settings;
            _board    = ViewPresenterManager.ResolveView(typeof(IChessBoardView))
                        as IChessBoardView;
            _main = MainPresenter.Get();

            _board.Start(settings);
            string gameType;

            if (_settings.Type == GameType.Single)
            {
                gameType = "single chess";
            }
            else if (_settings.Type == GameType.AgainstPC)
            {
                gameType = "bot chess";
            }
            else
            {
                gameType = "remote chess";
            }

            _game = GameFactory.GetGame(gameType);

            _game.MoveReceived = (move) => {
                _needMoveWhite = !_needMoveWhite;
                _countMoves++;
                _board.SetChange(move as Tuple <BPosition, BPosition>);
            };

            _game.MessagesReceived = (message) => {
                _main.AddChatMessage(message);
            };

            _game.Settings = settings;
            _game.Open();


            _main.QueryForSetupView((UserControl)_board);

            _board.SetupState(GameState.Initial, true);
            _board.GameReady      = GameStarted;
            _board.StateChanged   = GameStateChanged;
            _board.CancelLastMove = () => {
                return(_game.QueryCancelMove());
            };
            _board.SetText("Начало игры");
            _board.GetMovesFrom = (item) => _game.GetValidMoves(item).Select(x => (BPosition)x).ToList();

            if (_game.HaveChat)
            {
                _main.ShowChat();
            }
            else
            {
                _main.HideChat();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Draw the chess board
        /// </summary>
        /// <param name="o">Rendering object</param>
        void IChessBoardView.Render(object o)
        {
            // Dimensions and ViewData should be set before this call
            IChessBoardView view = (IChessBoardView)this;

            // Convert to the type we need for this implementation
            Graphics g = o as Graphics;

            // Draw the board (orientation does not matter)
            DrawBoard(g);

            // Draw the coordinates
            DrawBoardCoordinates(g);

            Rectangle s;
            Rectangle e;
            bool      drawLast = LastMoveRects(out s, out e);

            if (drawLast)
            {
                DrawLastMoveStart(g, s);
                DrawLastMoveEnd(g, e);
            }

            // Draw these over last moves if they exist
            DrawHighlightedSquares(g);

            // Draw the pieces (orientation does matter)
            foreach (ChessPiece piece in data.WhitePieces)
            {
                DrawPiece(g, piece);
            }

            foreach (ChessPiece piece in data.BlackPieces)
            {
                DrawPiece(g, piece);
            }

            if (drawLast)
            {
                DrawLastMoveLine(g, s, e);
            }

            // Render the move history - updates the text control
            DrawMoveHistory();

            // Get the control name w're using to output verbose for now (it's a label)
            Control verboseControl = viewForm.Controls[APMD_Form.EngineUpdateControlName];

            verboseControl.Text = data.ThinkingText;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a new ChessGame object
        /// </summary>
        /// <param name="engineView">IChessBoardView to use</param>
        /// <param name="fullPathToEngine">Full path the chess engine exe</param>
        /// <param name="engineLoader">object to load engine given path</param>
        /// <param name="reduceEngineStrength">true to make the engine weaker</param>
        /// <param name="cultureInfo">CultureInfo for main form</param>
        public ChessGame(IChessBoardView engineView, string fullPathToEngine,
                         IChessEngineProcessLoader engineLoader, bool reduceEngineStrength, CultureInfo cultureInfo)
        {
            // Save the view
            view = engineView;

            // Save culture for this thread
            currentCultureInfo = cultureInfo;
            Thread.CurrentThread.CurrentCulture   = currentCultureInfo;
            Thread.CurrentThread.CurrentUICulture = currentCultureInfo;

            ThinkingLocalized = Properties.Resources.Thinking;

            // Create legal move list
            legalMoves    = new List <BoardSquare>();
            selectedPiece = null;

            // Create the board, the view, and the engine
            // Create new UCI engine object
            engine = new UCIChessEngine(engineLoader);

            // Subscribe to events from the engine (commands and verbose)
            engine.OnChessEngineResponseReceived      += ChessEngineResponseReceivedEventHandler;
            engine.OnChessEngineVerboseOutputReceived += ChessEngineVerboseOutputReceivedEventHandler;

            // This will launch the process
            engine.LoadEngine(fullPathToEngine);

            // Start UCI
            engine.InitializeUciProtocol();

            // Initialize the chess engine with optional parameters
            // Note this is engine dependent and this version only works with stockfish (to my knowledge)
            // Other UCI engines use different options, several a combination of UCI_LimitStrength and UCI_ELO
            // Eventually this will need to be handled in some options dialog that will either
            // be customizable per engine, or provide a standard way to select things like play strength and time
            if (reduceEngineStrength)
            {
                string exeName = Path.GetFileName(fullPathToEngine);
                exeName = exeName.ToLower();
                string gimpedElo = String.Empty;

                //No Options listed (when tested at console)
                //===================
                //SOS 5 Arena
                //Anmon 5.75
                //Hermann28_32
                //Ruffian 105

                if (exeName.StartsWith("stockfish_8"))
                {
                    UciSetOptionCommand command = new UciSetOptionCommand("Skill Level", "0");
                    command.Execute(engine);
                }
                else if (exeName.StartsWith("rybkav2.3.2a"))
                {
                    gimpedElo = "1200";
                }
                else if (exeName.StartsWith("spike1.4"))
                {
                    gimpedElo = "1300";
                }
                //MadChess
                //option name UCI_LimitStrength type check default false
                //option name UCI_Elo type spin default 400 min 400 max 2200
                // Removed because it seems to take forever to return once this is
                // set with the engine.  The same behavior at the console, so
                // ignore it here

                if (gimpedElo != String.Empty)
                {
                    UciSetOptionCommand reduceEloCommand = new UciSetOptionCommand("UCI_LimitStrength", "true");
                    reduceEloCommand.Execute(engine);

                    UciSetOptionCommand setEloRating = new UciSetOptionCommand("UCI_Elo", gimpedElo);
                    setEloRating.Execute(engine);
                }
            }
        }