Exemple #1
0
        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            string imgPath = null;

            using (MarbleExplorer explorer = new MarbleExplorer(Environment.CurrentDirectory, cacheDirectory))
            {
                DialogResult result = explorer.ShowDialog();
                if (result == DialogResult.OK)
                {
                    try
                    {
                        imgPath       = explorer.returnDirectory + Path.DirectorySeparatorChar + "puzzle.jpg";
                        lastScorePath = explorer.returnDirectory + Path.DirectorySeparatorChar + "puzzle.bin";
                        lastMrbPath   = explorer.currentlySelectedItemPath;
                        img           = Image.FromFile(imgPath);
                        dataPath      = explorer.returnDirectory + Path.DirectorySeparatorChar + "puzzle.txt";

                        // Parse Score Data if available
                        if (File.Exists(lastScorePath))
                        {
                            latestScores = ReadScoresFromFile(lastScorePath, true);
                            if (latestScores.Any())
                            {
                                highScores.Text = "Scores" + Environment.NewLine;
                                latestScores.ForEach(s => highScores.Text += s + Environment.NewLine);
                            }
                            else
                            {
                                highScores.Text = "No Scores Available";
                            }
                        }
                        else
                        {
                            highScores.Text = "No Scores Available";
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Unable to parse data in cached directory '" + explorer.returnDirectory + "' -- Please verify file and try again");
                    }
                }
            }
            bool verifiedData = (img != null && imgPath != null && dataPath != null && File.Exists(dataPath) && File.Exists(imgPath));

            initButton.Enabled = verifiedData || initButton.Enabled;
            initButton.Text    = verifiedData ? "Start New Game" : initButton.Text;
        }
Exemple #2
0
        private void ValidateGame(List <GridBox> arr)
        {
            string timestamp = PadNumber(timerObj.currentHour, 2) + ":" + PadNumber(timerObj.currentMinute, 2) + ":" + PadNumber(timerObj.currentSeconds, 2);

            // Check loss condition
            for (int i = 0; i < arr.Count; i++)
            {
                if (arr[i].Item == -1)
                {
                    ToggleControls(false);
                    timerObj.PauseTiming();
                    initButton.Enabled = true;
                    playerName.Enabled = true;
                    initButton.Text    = "Restart";
                    MessageBox.Show("Game Over! You lasted " + timestamp + " with " + moves + " move(s)");
                    return;
                }
            }

            // Check win condition
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (GameBoard[i, j].Item != 0)
                    {
                        return;
                    }
                }
            }

            ToggleControls(false);
            timerObj.PauseTiming();
            initButton.Enabled = true;
            playerName.Enabled = true;
            initButton.Text    = "Restart";
            MessageBox.Show("You Won in " + timestamp + " with " + moves + " move(s)");

            // Save Data in Highscores and Refresh Layout
            Highscore newScore = new Highscore(playerName.Text + " " + moves + " " + timestamp);

            latestScores.Add(newScore);

            using (Stream stream = new FileStream(lastScorePath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, latestScores);
            }

            // Save Scores to ZIP File
            MarbleExplorer.AddFilesToZip(lastMrbPath, new[] { lastScorePath });

            // Read Data Copied from MarbleExplorer End Event
            latestScores = ReadScoresFromFile(lastScorePath, true);
            if (latestScores.Any())
            {
                highScores.Text = "Scores" + Environment.NewLine;
                latestScores.ForEach(s => highScores.Text += s + Environment.NewLine);
            }
            else
            {
                highScores.Text = "No Scores Available";
            }
        }