Пример #1
0
        /// <summary>
        /// This method inits some data for the first game turn.
        /// It looks for the possible moves and starts the timer.
        /// </summary>
        public void StartGame()
        {
            currentPossibleMoves = LogicalBoard.GetPossibleMoves(logicalBoard.BoardArray, whitePlayerTurn, BOARD_DIMENSIONS);

            UpdateHintsDisplay();
            if (windowView != null)
            {
                windowView.DisplayPlayerTurnHighlight(whitePlayerTurn);
            }

            GetPlayerStopwatch(whitePlayerTurn).Start();
            refreshTimer.Start();
        }
Пример #2
0
        /// <summary>
        /// This function changes the variables to change turn, calculates the possible moves for the current player and checks for game over.
        /// </summary>
        private void ChangeTurn()
        {
            GetPlayerStopwatch(whitePlayerTurn).Stop();
            whitePlayerTurn = !whitePlayerTurn;
            if (windowView != null)
            {
                windowView.DisplayPlayerTurnHighlight(whitePlayerTurn);
            }

            UpdateHintsDisplay(false);

            var nextPossibleMoves = LogicalBoard.GetPossibleMoves(logicalBoard.BoardArray, whitePlayerTurn, BOARD_DIMENSIONS);

            if (!nextPossibleMoves.Any())
            {
                // if the possible moves for the previous player and the current one are empty nobody can play anymore, it's the end of the game
                if (!currentPossibleMoves.Any())
                {
                    string playerName = ScoreWhite >= ScoreBlack ? "White player" : "Black player";
                    if (windowView != null && windowView.DisplayReplayDialog(playerName))
                    {
                        ResetGame();
                    }
                }
                // if only the current ones are empty it's to the other play again
                else
                {
                    currentPossibleMoves = nextPossibleMoves;
                    UpdateHintsDisplay();
                    ChangeTurn();
                    return;
                }
            }
            // otherwise we switch player turns regularly
            else
            {
                GetPlayerStopwatch(whitePlayerTurn).Start();
                currentPossibleMoves = nextPossibleMoves;
                UpdateHintsDisplay();
            }

            if (AI && whitePlayerTurn == false)
            {
                Tuple <int, int> movePos = GetNextMove(logicalBoard.BoardArray, 5, whitePlayerTurn);
                PlayMove(movePos.Item1, movePos.Item2, whitePlayerTurn);
                return;
            }
        }
Пример #3
0
        /// <summary>
        /// This constructor is called when the board is unserialized
        /// </summary>
        /// <param name="info">The list of parameters containing the serialized values</param>
        /// <param name="context">The stream where the data come from</param>
        private GameWithBoardInItsName(SerializationInfo info, StreamingContext context) : this()
        {
            // Add the saved play time of each player
            whiteTimeOffset = new TimeSpan().Add((TimeSpan)info.GetValue("WhiteTime", typeof(TimeSpan)));
            blackTimeOffset = new TimeSpan().Add((TimeSpan)info.GetValue("BlackTime", typeof(TimeSpan)));

            GetPlayerStopwatch(true).Reset();
            GetPlayerStopwatch(false).Reset();
            TimeWhite = (GetPlayerStopwatch(whitePlayerTurn).Elapsed + whiteTimeOffset).ToString(@"hh\:mm\:ss");
            TimeBlack = (GetPlayerStopwatch(whitePlayerTurn).Elapsed + blackTimeOffset).ToString(@"hh\:mm\:ss");

            whitePlayerTurn   = info.GetBoolean("Turn");
            this.logicalBoard = (LogicalBoard)info.GetValue("Board", typeof(LogicalBoard));

            ScoreWhite = info.GetInt32("ScoreWhite");
            ScoreBlack = info.GetInt32("ScoreBlack");
        }
Пример #4
0
        /// <summary>
        /// This constructor is the base to init different values called when the program is first started and when it loads a saved game
        /// It should be private since it's only used as a helper but the tournament needs a default constructor
        /// </summary>
        public GameWithBoardInItsName()
        {
            this.logicalBoard = new LogicalBoard(BOARD_DIMENSIONS.Width, BOARD_DIMENSIONS.Height);
            playedMovesStack  = new List <Move>();

            refreshTimer          = new DispatcherTimer();
            refreshTimer.Tick    += OnTimerEvent;
            refreshTimer.Interval = TimeSpan.FromSeconds(1);

            playersTimer    = new Stopwatch[2];
            playersTimer[0] = new Stopwatch();
            playersTimer[1] = new Stopwatch();

            blackTimeOffset = new TimeSpan();
            whiteTimeOffset = new TimeSpan();

            currentPossibleMoves = new List <Move>();
        }