Exemple #1
0
 public GameViewModel(Game game)
 {
     Game        = game;
     PlayerBoard = new BoardViewModel(Game.Player1.GameBoard);
     EnemyBoard  = new BoardViewModel(Game.Player1.EnemyBoard);
     Game.Player1.FireShot();
     StartGameCommand = new RelayCommand(StartGame);
     EnemyBoard.FieldClicked.Subscribe(HandlePlayer1Shot);
     Game.RoundCompleted.Subscribe(UpdateBoards);
     Game.GameFinished.Subscribe(GameFinished);
 }
        private void HandleChildEvent(object sender, RoutedEventArgs e)
        {
            Button b = e.OriginalSource as Button;
            m = this.DataContext as MainWindowViewModel;
            if (b.Name.Equals("new"))
            {
                shipmenu = new ShipMenu();
                bv = new BoardView(shipmenu);
                bvm = bv.DataContext as BoardViewModel;
                m.Menu = shipmenu;
                m.Content = bv;
                builder = new BattleshipBuilder(bvm, new Player(), new Player(), State.PLACE_BOAT_STATE);
            }

            else if (b.Name.Equals("load"))
            {
                loadGameView = new LoadGameView();
                m.Content = loadGameView;
                m.Menu = new LoadGameMenuView();
            }
            else if (b.Name.Equals("next"))
            {
                builder.progressGame();
                if (builder.CurrentState == State.PLACE_BOAT_STATE)
                {
                    shipmenu = new ShipMenu();
                    bv.resetShips(shipmenu);
                    m.Menu = shipmenu;
                }
                else if (builder.CurrentState == State.GAME_STATE)
                {
                    bv.resetBoard();
                    m.Menu = new GameMenu();
                }
                this.Title = builder.getPlayer();

            }
            else if (b.Name.Equals("nextPlayer"))
            {
                builder.progressGame();
                this.Title = builder.getPlayer();
            }
            else if (b.Name.Equals("saveGame"))
            {
                SavedGame save = new SavedGame("title", builder.Player1, builder.Player2);
                save.Save();
            }
            else if (b.Name.Equals("startLoadedGame"))
            {
                LoadGameViewModel loadViewModel = loadGameView.DataContext as LoadGameViewModel;
                SavedGame game = loadViewModel.getSelectedGame();

                if (shipmenu == null)
                {
                    shipmenu = new ShipMenu();
                    bv = new BoardView(shipmenu);
                }
                bvm = bv.DataContext as BoardViewModel;
                m.Menu = new GameMenu();
                m.Content = bv;
               // bvm.modelChanged(game.Player1.TargetBoard);
                builder = new BattleshipBuilder(bvm, game.Player1, game.Player2, State.GAME_STATE);
            }
            else if (b.Name.Equals("exitGame"))
            {
                m.Content = null;
                m.Menu = new MainMenu();
            }
            else if (b.Name.Equals("goBack")) {
                m.Content = null;
                m.Menu = new MainMenu();
            }
            e.Handled = true;
        }
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            model = this.DataContext as BoardViewModel;
            model.PropertyChanged += new PropertyChangedEventHandler(boardChanged);

            for (int i = 0; i < 10; i++)
            {
                mainGrid.RowDefinitions.Add(new RowDefinition());
                mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
            }

            initArray();
        }