private void HandleResetGame(GameState gameState)
 {
     if (GameReset != null)
         _dispatcher.Dispatch(() => GameReset(this, new GameResetEventArgs(gameState)));
 }
 public GameResetEventArgs(GameState gameState)
 {
     GameState = gameState;
 }
 private void HandleAddedToGame(GameState gameState)
 {
     if (AddedToGame != null)
         _dispatcher.BeginInvoke(() => AddedToGame(this, new AddedToGameEventArgs(gameState)));
     }
 public AddedToGameEventArgs(GameState gameState)
 {
     GameState = gameState;
 }
        private void CreateWorld(GameState state)
        {
            _model = new GameModel(state.Size.Item1, state.Size.Item2);
            _players = new ObservableCollection<Player>(state.Players);
            PlayerList.ItemsSource = _players;

            _gameGrid = new Grid();
            _lineButtons = new List<Button>();
            _squareGrids = new List<Grid>();

            SetRowDefinitions();
            SetColumnsDefinitions();

            var lst = _model.GetAllElements().ToList();

            foreach (var piece in lst)
            {
                var element = GetElementForPiece(piece);
                element.SetValue(Grid.RowProperty, piece.Row);
                element.SetValue(Grid.ColumnProperty, piece.Column);
                element.Tag = piece;
                _gameGrid.Children.Add(element);

                if (element is Button)
                {
                    _lineButtons.Add(element as Button);
                }

                if (piece is Square)
                {
                    var square = piece as Square;
                    square.Completed += SquareCompleted;

                    _squareGrids.Add(element as Grid);
                }
            }

            Board.Children.Clear();
            Board.Children.Add(_gameGrid);

            foreach (var button in _lineButtons)
                SetPieceColor(button);

            foreach (var occupied in state.OccupiedLines)
            {
                var line = _model.GetElementAt(occupied.Row, occupied.Column) as Line;
                if (line != null) line.Occupy(occupied.PlayerId);
                var button = FindButton(occupied.Row, occupied.Column);
                SetPieceColor(button);
                var player = GetPlayer(occupied.PlayerId);
                if (player != null) player.Score++;
            }
        }
 public void StartNewGame()
 {
     _gameModel = new GameModel(Height, Width);
     var players = _state.Players;
     _state = new GameState { Size = new Tuple<int, int>(Height, Width), Players = players };
 }