Ejemplo n.º 1
0
        public TetrisGame(ILog logger, int row = 10, int column = 20, WeightedPool <BlockUnit> objectPool = null, Queue <BlockUnit> initialQueue = null)
        {
            this.logger = logger;
            logger.Debug($"TetrisInstance Creating : row{row},column{column}");

            TetrisConfig config = new TetrisConfig();

            config.Load();
            if (!config.Load())
            {
                config.Save();
            }
            if (objectPool == null && config.UseCustomObjectList)
            {
                var serializable = (SerializableObjectPool.Load(typeof(SerializableObjectPool), ConfigBase.Directory, config.ObjectListFile) as SerializableObjectPool);
                if (serializable == null)
                {
                    serializable = new SerializableObjectPool()
                    {
                        ObjectPool = DefaultObjectPool
                    };
                    serializable.Save(ConfigBase.Directory, config.ObjectListFile);
                }
                objectPool = serializable.ObjectPool;
            }

            timer          = new Timer();
            timer.Interval = TimerSpan;
            timer.Elapsed += new ElapsedEventHandler((object sender, ElapsedEventArgs e) => controller?.OnTimerTick());

            Setting = new GameSetting()
            {
                Row = row, Column = column
            };

            field = new Field(row, column);

            _state = new GameState()
            {
                Round = 0, Score = 0, RemovedLines = new Dictionary <int, int>()
            };

            _gameWatch = new Stopwatch();
            _playData  = new GamePlayData();

            ObjectPool   = objectPool ?? TetrisGame.DefaultObjectPool;
            _objectQueue = initialQueue ?? new Queue <BlockUnit>();

            field.OnBlockChanged += (object sender, Point point) =>
            {
                //logger.Debug($"Block was changed:{point}");
            };
            field.OnRoundStart += (object sender) =>
            {
                if (RecordPlayDataEnabled)
                {
                    _gameWatch.Restart();
                }
                lock (_objectQueue)
                {
                    field.SetObject(Dequeue());
                }
            };
            field.OnBlockPlaced += (object sender, BlockObject obj) =>
            {
                //logger.Debug("Block was placed");
                Draw();
            };
            field.OnLinesRemoved += (object sender, int[] lines, int eroded) =>
            {
                if (lines.Length != 0)
                {
                    if (!_state.RemovedLines.ContainsKey(lines.Length))
                    {
                        _state.RemovedLines.Add(lines.Length, 0);
                    }
                    _state.RemovedLines[lines.Length]++;
                }
            };
            field.OnRoundEnd += (object sender, RoundResult result) =>
            {
                logger.Debug($"Round {_state.Round} End");
                _state.Round++;
                _state.Score += result.Score;
                if (MaxRound > 0 && _state.Round >= MaxRound)
                {
                    timer.Stop();
                    OnGameEnd?.Invoke(this, new GameResult()
                    {
                        Score = State.Score, Round = State.Round
                    });
                    return;
                }
                if (RecordPlayDataEnabled)
                {
                    _playData.Save();
                }
                field.StartRound();
            };
            field.OnGameOver += (object sender) =>
            {
                logger.Debug("Game Over");
                timer.Stop();
                OnGameEnd?.Invoke(this, new GameResult()
                {
                    Score = State.Score, Round = State.Round
                });
            };
            OnGameEnd += (object sender, GameResult result) =>
            {
                logger.Debug(State.Score);
                _gameWatch.Stop();
            };
        }