示例#1
0
 public void MoveTo(int moveNo)
 {
     Notations.MoveTo(moveNo);
     Book.MoveTo(moveNo);
     SetCapturedPiecesParameters();
     CapturedPieces.MoveTo(moveNo);
 }
示例#2
0
        public void MoveTo(Move m)
        {
            if (m == null)
            {
                return;
            }

            if (m.Id == CurrentMove.Id)
            {
                return;
            }

            if (BeforeMoveTo != null)
            {
                BeforeMoveTo(this, new MoveToEventArgs(m));
            }

            Flags.IsRetracMove = false;

            CurrentMove = m.Clone();

            Clock.MoveTo(CurrentMove);
            Notations.MoveTo(CurrentMove);
            Book.MoveTo(CurrentMove);
            CapturedPieces.MoveTo(CurrentMove);

            SetFen(CurrentMove.Fen);
            SwapPlayersIfNeeded();

            if (AfterMoveTo != null)
            {
                AfterMoveTo(this, new MoveToEventArgs(CurrentMove));
            }
        }
示例#3
0
        public Game()
        {
            RootMove    = GetRootMove();
            CurrentMove = RootMove.Clone();

            Flags = new GameFlags(this);
            //PlayingMode = new PlayingMode(this);
            Clipboard      = new Clipboard(this);
            Notations      = new Notations(this);
            GameTime       = new GameTime(this);
            GameData       = new GameData(this);
            Book           = new Book(this);
            Clock          = new Clock(this);
            CapturedPieces = new CapturedPieces(this);
            Moves          = new Moves(this);
            GameValidator  = new GameW(this.InitialBoardFen);

            Player1 = new Player(this, PlayerColorE.White);
            Player2 = new Player(this, PlayerColorE.Black);
        }
示例#4
0
        private void DoNewGame(string fen, GameMode gameMode, GameType gameType, OptionsBlitzClock blitzClock, OptionsLongClock longClock)
        {
            NewGameEventArgs args = new NewGameEventArgs();

            if (BeforeNewGame != null)
            {
                BeforeNewGame(this, args);
            }

            if (args.Cancel)
            {
                return;
            }

            if (SaveDocking != null)
            {
                SaveDocking(this, EventArgs.Empty);
            }

            GameStartTimeLocal = DateTime.Now;

            Ap.Options.CurrentGameGuid = string.Empty;
            Stop();

            InitialBoardFen = fen;

            PreviousGameMode   = GameMode;
            PreviousPonderMove = string.Empty;
            GameMode           = gameMode;
            GameType           = gameType;

            #region Init
            //GameValidator = new GameW(this.InitialBoardFen);
            GameValidator.SetFen(this.InitialBoardFen);

            SpaceBarCounter = 0;

            CloseEngines();

            if (GameType == GameType.Long)
            {
                GameTime.Init(longClock, true);
            }
            else
            {
                GameTime.Init(blitzClock, true);
            }

            if (Flags.IsBookLoadRequired)
            {
                Book.Load(null);
            }

            GameResult = GameResultE.InProgress;
            Reset();

            if (DbGame != null && DbGame.IsFinished)
            {
                this.GameResult = DbGame.GameResultIDE;
                this.Flags.SetFlags(DbGame.GameFlags);
            }

            CurrentMove = RootMove.Clone();
            Moves.Clear();
            CurrentLine = null;
            #endregion

            #region Switch GameMode
            switch (GameMode)
            {
            case GameMode.None:
                break;

            case GameMode.HumanVsHuman:
                #region HumanVsHuman
                GameTime.TimeMin     = 0;
                GameTime.GainPerMove = 0;

                Player1.Engine = null;
                Player2.Engine = null;

                Player2.Book = Book;

                Player1.PlayerTitle = Ap.UserProfile.LastName;
                Player2.PlayerTitle = "InfinityChess";

                GameData.SetPlayers(Player1.PlayerTitle, Ap.UserProfile.FirstName, Player2.PlayerTitle, "");
                gameTypeTitle = Ap.GetGameTypeTitle(GameType);
                #endregion
                break;

            case GameMode.HumanVsEngine:
                #region HumanVsEngine
                Player2EngineFileName = InfinitySettings.Settings.DefaultEngineXml.FilePath;
                Player1.Engine        = null;
                if (DefaultEngine == null)
                {
                    Player2.Engine = UCIEngine.Load(Player2EngineFileName, Options.DefaultHashTableSize, this);
                    Player2.Engine.UseTablebases = Ap.EngineOptions.UseTablebases;

                    DefaultEngine = Player2.Engine;
                }
                else
                {
                    Player2.Engine = DefaultEngine;
                }

                Player2.Book = Book;

                DefaultBook = Player2.Book;

                Player1.PlayerTitle = Ap.UserProfile.LastName;
                Player2.PlayerTitle = Player2.Engine.EngineName;

                GameData.SetPlayers(Player1.PlayerTitle, Ap.UserProfile.FirstName, Player2.PlayerTitle, "");
                gameTypeTitle = Ap.GetGameTypeTitle(GameType);
                #endregion
                break;

            case GameMode.EngineVsEngine:
                #region EngineVsEngine
                Player1.Engine = UCIEngine.Load(Player1EngineFileName, Player1EngineHashTableSize, this);
                Player1.Engine.UseTablebases = Ap.EngineOptions.UseTablebases;

                Player2.Engine = UCIEngine.Load(Player2EngineFileName, Player2EngineHashTableSize, this);
                Player2.Engine.UseTablebases = Ap.EngineOptions.UseTablebases;

                Player1.PlayerTitle = Player1.Engine.EngineName;
                Player2.PlayerTitle = Player2.Engine.EngineName;

                if (Player1.Book != null)
                {
                    Player1.Book.NewGame();
                }
                if (Player2.Book != null)
                {
                    Player2.Book.NewGame();
                }

                GameData.SetPlayers(Player1.PlayerTitle, "", Player2.PlayerTitle, "");
                gameTypeTitle = Ap.GetGameTypeTitle(GameType);

                InitE2eResult();
                #endregion
                break;

            case GameMode.OnlineHumanVsHuman:
                #region OnlineHumanVsHuman
                StartOnlineH2HGame();
                #endregion
                break;

            case GameMode.OnlineHumanVsEngine:
                #region OnlineHumanVsEngine
                gameTypeTitle = Ap.GetGameTypeTitle(GameType);
                #endregion
                break;

            case GameMode.OnlineEngineVsEngine:
                #region OnlineEngineVsEngine
                if (Ap.PlayingMode.SelectedEngine == null)
                {
                    StartOnlineH2HGame();
                }
                else
                {
                    StartOnlineE2EGame();
                }
                #endregion
                break;

            case GameMode.Kibitzer:
                #region Kibitzer
                GameTime.Set(DbGame);
                GameResult = DbGame.GameResultIDE;

                Player1.PlayerTitle = DbGame.WhiteUser.UserName + " " + DbGame.WhiteUser.Engine.Name;
                Player2.PlayerTitle = DbGame.BlackUser.UserName + " " + DbGame.BlackUser.Engine.Name;

                if (DbGame.IsRated)
                {
                    gameTypeTitle = GameType.ToString() + " " + DbGame.TimeMin.ToString() + "' + " + DbGame.GainPerMoveMin.ToString() + "'', " + "Rated";

                    if (DbGame.EloBlackBefore != 0 && !DbGame.BlackUser.IsGuest)
                    {
                        Player2.PlayerTitle += " " + DbGame.EloBlackBefore.ToString();
                    }

                    if (DbGame.EloWhiteBefore != 0 && !DbGame.WhiteUser.IsGuest)
                    {
                        Player1.PlayerTitle += " " + DbGame.EloWhiteBefore.ToString();
                    }
                }
                else
                {
                    gameTypeTitle = GameType.ToString() + " " + DbGame.TimeMin.ToString() + "' + " + DbGame.GainPerMoveMin.ToString() + "'', " + "Unrated";
                }

                GameData.SetPlayers(Player1.PlayerTitle, "", Player2.PlayerTitle, "");

                #endregion
                break;

            default:
                break;
            }
            #endregion

            if (DefaultEngine != null)
            {
                if (DefaultEngine.IsClosed)
                {
                    DefaultEngine.Load();
                }
            }

            Flags.IsEngineBlack = true;

            #region Start

            Clock.NewGame();
            Notations.NewGame();

            if (Flags.IsBookLoadRequired)
            {
                Book.NewGame();
            }

            CapturedPieces.NewGame();
            GameData.NewGame();

            Clipboard.Reset();

            GameData.Tournament = GameTypeTitle;

            Ap.Options.GameType = gameType;
            Ap.Options.Save();

            #endregion

            if (CreateDocking != null)
            {
                CreateDocking(this, EventArgs.Empty);
            }

            Flags.IsInfiniteAnalysisOn = false;
            MediaPlayer.PlaySound(SoundFileNameE.SetPieces);
            SetFen(fen);

            Flags.IsBoardSetByFen = fen != ChessLibrary.FenParser.InitialBoardFen;

            if (Flags.IsOnline && DbGame != null)
            {
                Flags.IsChallengerSendsGame = DbGame.IsChallengerSendsGame;
            }

            SetSuddenDeathMatchTime();

            if (AfterNewGame != null)
            {
                AfterNewGame(this, EventArgs.Empty);
            }
        }
示例#5
0
        public void MoveTo(MoveToE moveTo, bool moveFromBook)
        {
            Move m = null;

            if ((Flags.IsFirstMoveSelected || Flags.IsRootMoveSelected) && (moveTo == MoveToE.Previous || moveTo == MoveToE.First))
            {
                if (Flags.IsRootMoveSelected)
                {
                    return;
                }

                SetInitialPosition();
                return;
            }

            if (moveFromBook && moveTo == MoveToE.Next)
            {
                Book.MoveTo(moveTo, true);
                return;
            }

            if (Flags.IsFirtMove && moveTo == MoveToE.Next)
            {
                return;
            }

            switch (moveTo)
            {
            case MoveToE.First:
                m = Moves.First;
                break;

            case MoveToE.Last:
                m = Moves.Last;
                break;

            case MoveToE.Next:
                m = GetNextMove(CurrentMove);
                break;

            case MoveToE.Previous:
                m = Moves.Prev(CurrentMove);
                break;

            case MoveToE.Up:
                break;

            case MoveToE.Down:
                break;

            default:
                break;
            }


            Flags.IsRetracMove = false;

            CurrentMove = m.Clone();

            Clock.MoveTo(moveTo);
            Notations.MoveTo(moveTo);
            Book.MoveTo(moveTo, false);
            CapturedPieces.MoveTo(moveTo);

            SetFen(CurrentMove.Fen);
            SwapPlayersIfNeeded();
        }
示例#6
0
        public Move AddMove(string from, string to, Pieces fromPiece, string fen, Move m, bool isSetFen)
        {
            #region On Before
            if (m == null)
            {
                return(null);
            }

            if (BeforeAddMove != null)
            {
                EngineMoveEventArgs em = new EngineMoveEventArgs();
                em.BestMove   = from + to;
                em.PonderMove = PonderMove;
                BeforeAddMove(this, em);
            }
            #endregion

            #region Before CurrentMove Import
            if (Flags.IsOnline && Flags.IsGameFinished) // due to time expire etc.
            {
                return(null);
            }

            if (Flags.IsOnline && (Flags.IsNotMyTurn || Flags.IsGameFinished))
            {
                Clock.ToggleClock(m);
            }
            else
            {
                #region Prepare Move
                if (!m.Flags.IsPromotion) // promoted piece is already assigned in promotion dialog
                {
                    m.Piece = fromPiece;
                }

                m.Id      = NextMoveId;
                m.Pid     = NextMovePid;
                m.MoveNo  = NextMoveNo;
                m.IsWhite = NextMoveIsWhite;
                m.From    = from;
                m.To      = to;
                m.Fen     = fen;

                if (Flags.IsEvalRequired)
                {
                    if (!String.IsNullOrEmpty(CurrentPlayer.Engine.Points) && !String.IsNullOrEmpty(CurrentPlayer.Engine.Depth))
                    {
                        m.SetEval(CurrentPlayer.Engine.Points, CurrentPlayer.Engine.Depth);
                    }
                }

                if (Flags.IsExpectedMoveRequired)
                {
                    if (!String.IsNullOrEmpty(CurrentPlayer.Engine.ExpectedMove))
                    {
                        m.ExpectedMove = CurrentPlayer.Engine.ExpectedMove;
                    }
                }
                #endregion

                #region IsVariation
                if (AddVariationMove(m))
                {
                    PlaySounds(m);
                    return(null);
                }
                else
                {
                    m.Flags.VariationType = this.VariationType;
                }
                #endregion

                #region Set Clock
                if (Flags.IsOnline)
                {
                    if (!m.IsWhite)
                    {
                        Clock.WhiteTime += DbGame.GainPerMoveMin;
                    }
                    else
                    {
                        Clock.BlackTime += DbGame.GainPerMoveMin;
                    }
                }
                else
                {
                    if (m.IsWhite)
                    {
                        Clock.BlackTime += GameTime.GainPerMove;
                    }
                    else
                    {
                        Clock.WhiteTime += GameTime.GainPerMove;
                    }
                }

                m.MoveTimeWhite = Clock.WhiteTime;
                m.MoveTimeBlack = Clock.BlackTime;

                m.MoveTime = Clock.MoveTime;

                if (this.Flags.IsFirtMove && this.Flags.IsInfiniteAnalysisOff)
                {
                    Clock.Start();
                }

                #endregion
            }

            #endregion

            Notations.AddMove(m);
            CurrentMove = m.Clone();
            Moves.Import(CurrentMove);

            #region After CurrentMove Import

            SetCurrentLine(CurrentMove);
            CheckNextLongGamePhase(CurrentMove);
            CheckThreefoldRepetition();
            SetCastlingFlags(CurrentMove.Piece, CurrentMove.From, CurrentMove.To);

            Flags.IsPieceMovedSuccessfully = true;

            #region Point Book
            if (GameMode != GameMode.EngineVsEngine && GameMode != GameMode.OnlineEngineVsEngine)
            {
                Book.PointTo(CurrentMove);
            }

            #endregion

            #region Update Captured Pieces

            if (CurrentMove.Flags.IsCapture)
            {
                CapturedPieces.Update(CurrentMove);
            }

            #endregion

            Flags.IsMoveInProgress    = false;
            Flags.IsForceEngineToMove = false;

            #region Check Game Finish
            if (!Flags.IsDatabaseGame)
            {
                #region Mated
                if (Flags.IsMated)
                {
                    Mated();
                    Flags.IsMoveInProgress = true;
                }

                #endregion

                #region StaleMated
                if (Flags.IsStaleMated)
                {
                    StaleMated();
                }

                #endregion

                #region ThreefoldRepetition
                if (Flags.IsThreeFoldRepetition)
                {
                    ThreefoldRepetition(false);
                }
                #endregion

                #region InsufficientMaterial
                if (Flags.IsInsufficientMaterial)
                {
                    InsufficientMaterial();
                }
                #endregion
            }
            #endregion

            #region SetFen
            if (isSetFen)
            {
                SetFen(CurrentMove.Fen);
            }

            #endregion

            #region FifityMoves
            if (this.HalfMovesCounter >= 100)
            {
                FifityMoves();
            }
            #endregion

            #region Set ECO, Play Sound etc.
            SearchEco();
            CheckMoveLimit();
            PlaySounds(m);
            Clock.ResetMoveTime();
            VariationType = VariationTypeE.None;

            if (Flags.IsClockStartRequired)
            {
                Clock.Start();
            }

            #endregion

            #region Send Move To Opponent
            if (Flags.IsUpdateGameRequired)
            {
                SocketClient.UpdateGameDataByGameID(DbGame.GameID, GetLastMoveXml(), GameResult, Flags.Flags, DbGame.OpponentUserID);
            }
            #endregion

            #endregion

            #region On After

            if (AfterAddMove != null)
            {
                AfterAddMove(this, EventArgs.Empty);
            }

            #endregion

            return(CurrentMove);
        }
示例#7
0
        public void NewGame(string fen, GameMode gameMode, GameType gameType, OptionsBlitzClock blitzClock, OptionsLongClock longClock)
        {
            Stop();

            InitialBoardFen = fen;
            GameMode        = gameMode;
            GameType        = gameType;

            if (BeforeNewGame != null)
            {
                BeforeNewGame(this, EventArgs.Empty);
            }

            #region Init
            GameValidator = new GameW(this.InitialBoardFen);

            SpaceBarCounter = 0;

            CloseEngines();

            if (GameType == GameType.LongGame)
            {
                GameTime.Init(longClock, true);
            }
            else
            {
                GameTime.Init(blitzClock, true);
            }

            GameResult  = GameResultE.InProgress;
            CurrentMove = null;

            Flags.Reset();
            #endregion

            #region Switch gameMode
            switch (GameMode)
            {
            case GameMode.None:
                break;

            case GameMode.HumanVsHuman:
                Player1.PlayerType = PlayerType.Human;
                Player2.PlayerType = PlayerType.Human;

                Player1.Engine = null;
                Player2.Engine = null;

                Player1.PlayerTitle = GameData.WhiteTitle;
                Player2.PlayerTitle = GameData.BlackTitle;
                break;

            case GameMode.HumanVsEngine:
                Player1.PlayerType    = PlayerType.Human;
                Player2.PlayerType    = PlayerType.Engine;
                Player2EngineFileName = InfinitySettings.Settings.DefaultEngineXml.FilePath;
                Player1.Engine        = null;
                if (DefaultEngine == null)
                {
                    Player2.Engine = UCIEngine.Load(Player2EngineFileName, Options.DefaultHashTableSize);
                    Player2.Engine.UseTablebases = EngineParameters.UseTablebases;

                    DefaultEngine = Player2.Engine;
                }
                else
                {
                    Player2.Engine = DefaultEngine;
                }

                Player2.Book = Book;
                DefaultBook  = Player2.Book;

                Player1.PlayerTitle = Ap.UserProfile.LastName;
                Player2.PlayerTitle = Player2.Engine.EngineName;

                break;

            case GameMode.EngineVsEngine:
                Player1.PlayerType = PlayerType.Engine;
                Player2.PlayerType = PlayerType.Engine;

                Player1.Engine = UCIEngine.Load(Player1EngineFileName, Player1EngineHashTableSize);
                Player1.Engine.UseTablebases = EngineParameters.UseTablebases;

                Player2.Engine = UCIEngine.Load(Player2EngineFileName, Player2EngineHashTableSize);
                Player2.Engine.UseTablebases = EngineParameters.UseTablebases;

                Player1.PlayerTitle = Player1.Engine.EngineName;
                Player2.PlayerTitle = Player2.Engine.EngineName;

                break;

            case GameMode.OnlineHumanVsHuman:
                #region OnlineHumanVsHuman
                StartOnlineH2HGame();
                #endregion
                break;

            case GameMode.OnlineHumanVsEngine:
                break;

            case GameMode.OnlineEngineVsEngine:

                if (PlayingMode.SelectedEngine == null)
                {
                    StartOnlineH2HGame();
                }
                else
                {
                    StartOnlineE2EGame();
                }

                break;

            case GameMode.Kibitzer:
                #region Kibitzer
                GameTime.Set(DbGame);
                GameResult = DbGame.GameResultIDE;     //GameResultE.InProgress;

                Player1.PlayerType  = PlayerType.Human;
                Player1.PlayerTitle = DbGame.WhiteUser.UserName;
                Player2.PlayerType  = PlayerType.Human;
                Player2.PlayerTitle = DbGame.BlackUser.UserName;

                if (DbGame.IsRated)
                {
                    gameTypeTitle = GameType.ToString() + " " + DbGame.TimeMin.ToString() + "m + " + DbGame.GainPerMoveMin.ToString() + "s, " + "Rated";

                    if (DbGame.EloBlackBefore != 0 && !DbGame.BlackUser.IsGuest)
                    {
                        Player2.PlayerTitle += " " + DbGame.EloBlackBefore.ToString();
                    }

                    if (DbGame.EloWhiteBefore != 0 && !DbGame.WhiteUser.IsGuest)
                    {
                        Player1.PlayerTitle += " " + DbGame.EloWhiteBefore.ToString();
                    }
                }
                else
                {
                    gameTypeTitle = GameType.ToString() + " " + DbGame.TimeMin.ToString() + "m + " + DbGame.GainPerMoveMin.ToString() + "s, " + "Unrated";
                }
                #endregion
                break;

            default:
                break;
            }
            #endregion

            if (DefaultEngine != null)
            {
                if (DefaultEngine.IsClosed)
                {
                    DefaultEngine.Load();
                }
            }

            Flags.IsEngineBlack = true;

            #region Start

            gameTypeTitle = Ap.GetGameTypeTitle(GameType);

            Player1.Active = true;
            Player2.Active = false;

            Notations.SetupNewGame();
            CapturedPieces.SetupNewGame();
            Clock.SetupNewGame();
            Book.SetupNewGame();

            Clipboard.Reset();

            CurrentPlayer = Player1;

            GameData.SetupNewGame();


            if (Flags.IsChangeNamesAllowed)
            {
                GameData.White1 = Player1.PlayerTitle;
                GameData.White2 = Ap.UserProfile.FirstName;
                GameData.Black1 = Player2.PlayerTitle;
            }
            GameData.Tournament = GameTypeTitle;

            InitTablebases();

            Ap.Options.GameType = gameType;
            Ap.Options.Save();


            #endregion

            if (CreateDocking != null)
            {
                CreateDocking(this, EventArgs.Empty);
            }

            Flags.IsInfiniteAnalysisOn = false;
            SetFen(fen);

            if (Flags.IsOnline)
            {
                // Flags.IsChallengerSendsGame = Flags.IsPositionSetupAllowed;
            }

            if (AfterNewGame != null)
            {
                AfterNewGame(this, EventArgs.Empty);
            }
        }
示例#8
0
        public void MoveTo(MoveToE moveTo)
        {
            Move m = null;

            switch (moveTo)
            {
            case MoveToE.First:
                m = Moves.First;
                //Notations.MoveTo(moveTo);
                break;

            case MoveToE.Last:
                m = Moves.Last;
                break;

            case MoveToE.Next:
                m = Moves.Next(CurrentMove);
                //Notations.MoveTo(moveTo);
                break;

            case MoveToE.Previous:
                m = Moves.Prev(CurrentMove);
                //Notations.MoveTo(moveTo);
                break;

            case MoveToE.Up:
                break;

            case MoveToE.Down:
                break;

            default:
                break;
            }


            if (m == null && moveTo == MoveToE.Previous)
            {
                return;
            }

            if (m == null)
            {
                Book.MoveTo(moveTo, true);
                return;
            }

            if (CurrentMove.Id == m.Id)
            {
                Book.MoveTo(moveTo, true);
            }
            else
            {
                CurrentMove = m.Clone();

                Flags.IsMoveInProgress = true;
                Flags.IsClickedByUser  = true;

                Clock.Stop();
                Clock.SetClock(CurrentMove);

                Flags.IsRetracMove     = false;
                Flags.IsMoveInProgress = false;

                Notations.Set();
                Book.MoveTo(moveTo, false);
                SetCapturedPiecesParameters();
                CapturedPieces.MoveTo(moveTo);

                SetFen(CurrentMove.Fen);
            }
        }