Exemplo n.º 1
0
        /// <summary>
        /// Saves the game to the file.
        /// </summary>
        /// <param name="filePathAndName"></param>
        /// <param name="game"></param>
        public static void SaveGameToFile(string filePathAndName, Tetris game)
        {
            Gamefile file     = new Gamefile(game.Fieldgrid, game.Score, game.Level, game.RowsRemovedInTotal, game.RowsRemovedAtCurrentLevel);
            string   gameFile = file.SerializeGame();

            try
            {
                System.IO.File.WriteAllText(filePathAndName, $"{gameFile}\n");
#pragma warning disable CS0168 // Variable is declared but never used
            }
            catch (System.IO.DirectoryNotFoundException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Could not find directory.");
#pragma warning disable CS0168 // Variable is declared but never used
            }
            catch (System.IO.IOException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Something went wrong.");
#pragma warning disable CS0168 // Variable is declared but never used
            }
            catch (UnauthorizedAccessException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Unauthorized access.");
            }
#pragma warning disable CS0168 // Variable is declared but never used
            catch (System.Security.SecurityException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Sevurity Error.");
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Compares two tetris games
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public override bool Equals(object obj)
 {
     if (obj is Tetris other)
     {
         Gamefile game1 = new Gamefile(Fieldgrid, Score, Level, RowsRemovedInTotal, RowsRemovedAtCurrentLevel);
         Gamefile game2 = new Gamefile(other.Fieldgrid, other.Score, other.Level, other.RowsRemovedInTotal, other.RowsRemovedAtCurrentLevel);
         return(game1.Equals(game2));
     }
     return(false);
 }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the game on a string form.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            Gamefile game = new Gamefile(Fieldgrid, Score, Level, RowsRemovedInTotal, RowsRemovedAtCurrentLevel);

            return(game.ToString());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Loads a game from file.
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static Tetris LoadGameFromFile(string filename)
        {
            try
            {
                string   file             = System.IO.File.ReadAllText(filename);
                Gamefile gameDeserialized = JsonConvert.DeserializeObject <Gamefile>(file);
                Tetris   loadedGame       = new Tetris(gameDeserialized.Board, gameDeserialized.Level, gameDeserialized.Score, gameDeserialized.RowsRemovedInTotal, gameDeserialized.RowsRemovedAtCurrentLevel);
                if (GameIsValid(loadedGame))
                {
                    return(loadedGame);
                }
                else
                {
                    return(null);
                }
            }
#pragma warning disable CS0168 // Variable is declared but never used
            catch (System.IO.DirectoryNotFoundException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Could not find directory.");
            }
#pragma warning disable CS0168 // Variable is declared but never used
            catch (System.IO.IOException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Something went wrong.");
            }
#pragma warning disable CS0168 // Variable is declared but never used
            catch (UnauthorizedAccessException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Unauthorized access.");
            }
#pragma warning disable CS0168 // Variable is declared but never used
            catch (System.Security.SecurityException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                Console.WriteLine("Sevurity Error.");
#pragma warning disable CS0168 // Variable is declared but never used
            }
            catch (JsonReaderException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                //Could not interprate file as game
#pragma warning disable CS0168 // Variable is declared but never used
            }
            catch (JsonSerializationException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                //Could not interprate file as game
#pragma warning disable CS0168 // Variable is declared but never used
            }
            catch (JsonWriterException e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                //Could not interprate file as game
            }
#pragma warning disable CS0168 // Variable is declared but never used
            catch (Exception e)
#pragma warning restore CS0168 // Variable is declared but never used
            {
                //Other exceptions that might occurr
            }
            return(null);
        }