From 89a0f11febd3735f1616ac9287dfd65504ea660c Mon Sep 17 00:00:00 2001 From: daleclack Date: Tue, 27 Sep 2022 14:50:29 +0800 Subject: [PATCH] Update gtk140 --- .../gtk140_minesweeper/.vscode/settings.json | 3 +- Gtkmm3/gtk140_minesweeper/src/InputBox.cc | 29 ++++++++++++++----- Gtkmm3/gtk140_minesweeper/src/InputBox.hh | 6 +++- Gtkmm3/gtk140_minesweeper/src/MineSweeper.cc | 7 +++++ Gtkmm3/gtk140_minesweeper/src/main.cc | 2 +- 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/Gtkmm3/gtk140_minesweeper/.vscode/settings.json b/Gtkmm3/gtk140_minesweeper/.vscode/settings.json index 4fb390d..5f25913 100644 --- a/Gtkmm3/gtk140_minesweeper/.vscode/settings.json +++ b/Gtkmm3/gtk140_minesweeper/.vscode/settings.json @@ -2,6 +2,7 @@ "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", "files.associations": { "iostream": "cpp", - "ostream": "cpp" + "ostream": "cpp", + "fstream": "cpp" } } \ No newline at end of file diff --git a/Gtkmm3/gtk140_minesweeper/src/InputBox.cc b/Gtkmm3/gtk140_minesweeper/src/InputBox.cc index 2726939..957f7b1 100644 --- a/Gtkmm3/gtk140_minesweeper/src/InputBox.cc +++ b/Gtkmm3/gtk140_minesweeper/src/InputBox.cc @@ -1,19 +1,32 @@ #include #include "InputBox.hh" -#include "jsonfile.hh" InputBox::InputBox(){ + main_label.set_label("Input your name"); +} +void InputBox::read_json_file(){ + // Try to read json data firstly + std::fstream json_file; + json_file.open("scores.json", std::ios_base::in); + if(json_file.is_open()){ + json data = json::parse(json_file); + scores_map = data["scores"]; + } + json_file.close(); } void InputBox::on_response(int response_id){ - // Read the original json file - std::fstream json_file; - json_file.open("score.json"); - // Append data to the json file - if(json_file.is_open()){ - - }else{ + // Put the data to a map container + std::map outdata; + outdata["scores"] = scores_map; + // Output data to json file + std::fstream outfile; + outfile.open("scores.json"); + json data = json::parse(outdata); + if(outfile.is_open()){ + outfile << data; } + outfile.close(); } diff --git a/Gtkmm3/gtk140_minesweeper/src/InputBox.hh b/Gtkmm3/gtk140_minesweeper/src/InputBox.hh index d840aea..d08f3cb 100644 --- a/Gtkmm3/gtk140_minesweeper/src/InputBox.hh +++ b/Gtkmm3/gtk140_minesweeper/src/InputBox.hh @@ -1,15 +1,19 @@ #pragma once #include +#include "jsonfile.hh" class InputBox : public Gtk::Dialog { public: InputBox(); + void read_json_file(); + times_map scores_map; protected: void on_response(int response_id); private: Gtk::Entry entry_name; -}; \ No newline at end of file + Gtk::Label main_label; +}; diff --git a/Gtkmm3/gtk140_minesweeper/src/MineSweeper.cc b/Gtkmm3/gtk140_minesweeper/src/MineSweeper.cc index e4fcacc..31fa7eb 100644 --- a/Gtkmm3/gtk140_minesweeper/src/MineSweeper.cc +++ b/Gtkmm3/gtk140_minesweeper/src/MineSweeper.cc @@ -85,6 +85,9 @@ void MineSweeper::reset_game() winned = true; status_label.set_label(" "); calc_mines(); + + // Read scores + win_input.read_json_file(); } void MineSweeper::calc_mines() @@ -179,10 +182,14 @@ void MineSweeper::cell_clicked(MineCell *cell) // If all the mines has cleared, you has winned if (mines_clear == 40) { + // Stop the game status_label.set_label("You winned!"); winned = true; game_ended = true; mytimer.disconnect(); + + // Save the time of game + } } } diff --git a/Gtkmm3/gtk140_minesweeper/src/main.cc b/Gtkmm3/gtk140_minesweeper/src/main.cc index 13a35c2..3c9cd8a 100644 --- a/Gtkmm3/gtk140_minesweeper/src/main.cc +++ b/Gtkmm3/gtk140_minesweeper/src/main.cc @@ -5,4 +5,4 @@ int main(int argc, char **argv){ auto app = Gtk::Application::create(argc,argv,"org.gtk.daleclack"); MineSweeper window; return app->run(window); -} \ No newline at end of file +}