Esempio n. 1
0
 /// <summary>
 /// Begins the game
 /// </summary>
 private async void BeginGameplay()
 {
     // Each loop is one players turn
     while (User.Health > 0 && Computer.Health > 0)
     {
         // Computer's turn
         if (_currentPlayersTurn == Computer)
         {
             Shoot(_computerAlgorithm.NextShot(User.GameBoard), User);
             _currentPlayersTurn = User;
             OnPropertyChanged(nameof(UserHealth));
         }
         // User's turn
         else
         {
             // TODO: find alternative to try catch for TaskCanceledException
             try
             {
                 await WaitForUserPlay();
             }
             catch (TaskCanceledException) { }
             OnPropertyChanged(nameof(ComputerHealth));
         }
         _saveGameService.SaveGame(new GameModel(User, Computer), "BattleBoatsGame.json");
     }
     EndGameplay();
 }
Esempio n. 2
0
        public void SaveGame()
        {
            var data = new SaveGameData();

            data.ElapsedTime = TimeSpan.FromSeconds(Session.Time);

            data.Score     = Session.Score;
            data.Atoms     = Session.Atoms;
            data.Molecules = Session.Molecules;

            data.AddedAtoms = Session.AddedAtoms;

            data.CurrentAtom = Session.CurrentAtom.Electrons;
            data.NextAtom    = Session.NextAtom.Electrons;

            data.GridData = new SaveGameGridData[Grid.Width, Grid.Height];
            for (int gridX = 0; gridX < Grid.Width; gridX++)
            {
                for (int gridY = 0; gridY < Grid.Height; gridY++)
                {
                    var atom = Grid.GetAtom(gridX, gridY);
                    if (atom != null)
                    {
                        data.GridData[gridX, gridY] = new SaveGameGridData
                        {
                            Electrons       = atom.Electrons,
                            ConnectedLeft   = atom.LeftConnection != null,
                            ConnectedTop    = atom.TopConnection != null,
                            ConnectedRight  = atom.RightConnection != null,
                            ConnectedBottom = atom.BottomConnection != null
                        };
                    }
                }
            }

            _saves.SaveGame(AppConstants.LastSaveGameFilename, data);
        }