/// <summary>
        /// Constructor for the MonopolyWindowViewModel.
        /// The game board used must be passed as a parameter.
        /// </summary>
        /// <param name="savefile">The savefile to be used if selected.</param>
        /// <param name="boardDirectory">Directory of the game board to be used.</param>
        /// <param name="players">List of the players in the game.</param>
        /// <param name="settings">Dictionary of the settings to be used.</param>
        public MonopolyWindowViewModel(IDialogCoordinator instance, MonopolyHandler savefile, string boardDirectory = null, List <Player> players = null, Dictionary <string, object> settings = null)
        {
            Dialogs = instance; // Set the DialogCoordinator to the one define in App.xaml.cs
            // If a savefile exists, load the savefile.
            var alreadyStarted = false;

            if (savefile != null)
            {
                // Load the boardgame and get underway.
                alreadyStarted    = true;
                Handler           = savefile;
                Handler.ViewModel = this;
            }
            else
            {
                // This is a new game, hurray!
                // The new handler...
                Handler = new MonopolyHandler(this, settings);
                // ...must load the selected board.
                Handler.BoardConfiguration = DeserialisePathIntoGameboard(boardDirectory);
                // Then, the deseralised BoardConfiguration must be formatted for view. This must be set to equal the Board in the view.
                Handler.Board = Handler.BoardConfiguration.FormatForView();
                // It's time to establish the players.
                foreach (Player player in players)
                {
                    Handler.InitPlayer(player);
                }
                // Before we start, we must also load and deseralise the chance and community chest cards.
                // As these are stored in the game resources and made by the programmer, we can assume they do not need error checking.
                Handler.ChanceCards         = JsonConvert.DeserializeObject <Queue <Card> >(File.ReadAllText(Environment.CurrentDirectory + @"\Resources\ChanceCards.json"));
                Handler.CommunityChestCards = JsonConvert.DeserializeObject <Queue <Card> >(File.ReadAllText(Environment.CurrentDirectory + @"\Resources\CommunityChestCards.json"));
            }
            // Establish visual configuration - board scale, rotations, etc.
            // We zoom in by 1.05x to allow for movement correlating with the most position.
            BoardScale           = 1.05;
            RotateButtonRotation = 360;
            // The board is now ready to go, and the game can begin.
            Handler.Start(alreadyStarted);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Start the game!
        /// </summary>
        /// <param name="sender"></param>
        private async void StartGame(object sender)
        {
            // Check if we are starting a new game or loading an old game.
            if (Players.Count == 0)
            {
                // We are loading an old game.
                // Open the file dialog within a try-catch (to detect for improper closure).
                try
                {
                    OpenFileDialog openSavegame = new OpenFileDialog();
                    openSavegame.Filter           = "Savegame files (*.monopoly)|*.monopoly";
                    openSavegame.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Monopoly\\Saves";
                    openSavegame.ShowDialog();
                    if (openSavegame.FileName == null)
                    {
                        return;
                    }
                    FileStream      savegame  = File.Open(openSavegame.FileName, FileMode.Open);
                    BinaryFormatter formatter = new BinaryFormatter();
                    //It serialize the employee object

                    object          newObject = formatter.Deserialize(savegame);
                    MonopolyHandler handler   = (MonopolyHandler)newObject;
                    // Create the new window
                    MonopolyWindow NewMonopolyWindow = new MonopolyWindow();
                    // Use the deserialised handler to launch back into the game
                    NewMonopolyWindow.DataContext = new MonopolyWindowViewModel(DialogCoordinator.Instance, handler);
                    // Let's go!
                    NewMonopolyWindow.Show();
                    await Task.Delay(1000);

                    if (NewMonopolyWindow.IsActive)
                    {
                        Close();
                    }
                    else
                    {
                        await Dialogs.ShowMessageAsync(this, "", "That savefile appears corrupted or otherwise incompatible with this version of Monopoly. Please try again, load a different savefile, or begin a new game.", MessageDialogStyle.Affirmative);
                    }
                    savegame.Close();
                }
                catch (ArgumentException)
                {
                    await Dialogs.ShowMessageAsync(this, "", "Please ensure you have selected a savefile compatible with this version of Monopoly.", MessageDialogStyle.Affirmative);
                }
            }
            else if (Players.Count == 1)
            {
                return; // Not enough players to begin!
            }
            else
            {
                // Check if a board is selected.
                if (BoardDirectory == null)
                {
                    await Dialogs.ShowMessageAsync(this, "", "You haven't selected a board to play on yet!");

                    return;
                }
                MonopolyWindow NewMonopolyWindow = new MonopolyWindow();
                // Assign DataContext, within the MVVM pattern
                NewMonopolyWindow.DataContext = new MonopolyWindowViewModel(DialogCoordinator.Instance, null, BoardDirectory, Players.ToList <Player>(), Settings);
                NewMonopolyWindow.Show();
                Close();
            }
        }