27 #include "../include/game.h" 44 make7window (), make7scores (), make7message (), make7error_board (),
45 make7confirm (), make7askname (), make7credits (),
47 headerbar (), appmenu (), newgame (), scores (), credits (), quit (),
49 button_cancel (), button_credits (), button_help (),
50 button_new (), button_next (), button_quit (), button_rotate (),
51 button_save (), button_scores (), button_erase (),
53 nnw (), nn (), nne (), nww (), nw (), ne (), nee (), ww (), w (), m (), e (), ee (), sww (),
54 sw (), se (), see (), ssw (), ss (), sse (),
56 mainNw (), mainNe (), mainW (), mainM (), mainE (), mainSw (), mainSe (),
58 current_score (), box_score (), user_entry (),
60 Board (), SelectionArea (), BoardSnapshotMap (), Colors (), BoardKeys (), SelectionAreaKeys (), ColorsKeys (),
61 pos2 (), max (), valHexa1 (), valHexa2 (), score (), nMatch (), oldValHexa1 (), oldValHexa2 (),
66 31, 32, 33, 34, 35, 41, 42, 43,
70 Colors = {{0,
"nocolor"}, {1,
"purple"}, {2,
"orange"}, {3,
"red"},
71 {4,
"blue"}, {5,
"green"}, {6,
"yellow"}, {7,
"multicolor"}};
75 (Glib::build_filename (UI_DIR,
"theme.css")))
77 std::cout <<
"Failed to load CSS file" << std::endl;
104 newgame->signal_activate ().connect
107 scores->signal_activate ().connect
110 credits->signal_activate ().connect
113 quit->signal_activate ().connect
121 Board [key]->signal_clicked ().connect
134 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
136 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
138 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
139 quit->add_accelerator (
"activate",
make7window->get_accel_group (),
'q',
140 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
142 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
144 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
146 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
148 Gdk::CONTROL_MASK, Gtk::ACCEL_VISIBLE);
151 Glib::RefPtr<Gdk::Pixbuf> icon =
152 Gdk::Pixbuf::create_from_file (ICON_DIR
"/32x32/apps/make7libre.png");
155 Glib::RefPtr<Gtk::StyleContext> WinContext =
158 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
167 Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file
168 (UI_DIR
"/basic.glade");
171 make7window->add_accel_group (Gtk::AccelGroup::create ());
179 builder->get_widget (
"headerbar",
headerbar);
181 builder->get_widget (
"newgame",
newgame);
182 builder->get_widget (
"scores",
scores);
183 builder->get_widget (
"credits",
credits);
184 builder->get_widget (
"quit",
quit);
185 builder->get_widget (
"appmenu",
appmenu);
193 builder->get_widget (
"button_apply_save",
button_save);
196 builder->get_widget (
"box_score",
box_score);
197 builder->get_widget (
"user_entry",
user_entry);
200 builder->get_widget (
"nnw",
nnw);
201 builder->get_widget (
"nn",
nn);
202 builder->get_widget (
"nne",
nne);
203 builder->get_widget (
"nww",
nww);
204 builder->get_widget (
"nw",
nw);
205 builder->get_widget (
"ne",
ne);
206 builder->get_widget (
"nee",
nee);
207 builder->get_widget (
"ww",
ww);
208 builder->get_widget (
"w",
w);
209 builder->get_widget (
"m",
m);
210 builder->get_widget (
"e",
e);
211 builder->get_widget (
"ee",
ee);
212 builder->get_widget (
"sww",
sww);
213 builder->get_widget (
"sw",
sw);
214 builder->get_widget (
"se",
se);
215 builder->get_widget (
"see",
see);
216 builder->get_widget (
"ssw",
ssw);
217 builder->get_widget (
"ss",
ss);
218 builder->get_widget (
"sse",
sse);
220 builder->get_widget (
"mainNw",
mainNw);
221 builder->get_widget (
"mainNe",
mainNe);
222 builder->get_widget (
"mainW",
mainW);
223 builder->get_widget (
"mainM",
mainM);
224 builder->get_widget (
"mainE",
mainE);
225 builder->get_widget (
"mainSw",
mainSw);
226 builder->get_widget (
"mainSe",
mainSe);
230 {32,
w}, {33,
m}, {34,
e}, {35,
ee}, {41,
sww},
232 {52,
ss}, {53,
sse} };
265 for (
int hexa : hexas)
270 if (nb != -42 &&
Board[nb]->get_label () !=
"")
272 Board[nb]->set_label (
"");
283 int nFull = 0, nEmpty = 0;
284 for (
auto const& hexa : tmpBoard)
286 if (hexa.second ==
"")
293 make7message->set_message (
"Congratulation, you win !");
306 make7message->set_message (
"You lose, no more place available !\nTry again !");
320 Glib::ustring message;
322 message =
"Error ! This location is not empty !";
324 message =
"Error ! One of the hexa is out of the board !";
326 message =
"Error ! You have not enough score points !";
328 message =
"Error ! There is nothing here !";
330 message =
"Error undefined !";
339 Board[ref]->set_label (valref);
342 Glib::signal_timeout ().connect
350 if (
Board[ref]->get_label () ==
"")
356 Board[ref]->set_label (
"");
366 std::map<int, Glib::ustring>
369 std::map<int, Glib::ustring> map;
371 map[i] =
Board[i]->get_label ();
388 else if (
Board[ref]->get_label () ==
"")
390 bool there_is_a_second_hexa_to_define =
false;
400 else if (
Board[nb]->get_label () !=
"")
408 there_is_a_second_hexa_to_define =
true;
412 if (there_is_a_second_hexa_to_define)
425 Glib::ustring configdir = Glib::get_user_config_dir () +
"/make7libre";
426 Glib::ustring filepath = configdir +
"/history";
427 if (file_test (filepath, Glib::FILE_TEST_IS_REGULAR))
432 mkdir (configdir.c_str (), 0755);
433 Glib::KeyFile *new_config_file =
new Glib::KeyFile ();
435 Glib::TimeVal *cur_time =
new Glib::TimeVal;
436 cur_time->assign_current_time ();
437 Glib::ustring top_comment =
" This file was firstly created by Make7Libre, the " 438 + cur_time->as_iso8601 ();
440 new_config_file->set_comment (top_comment);
441 new_config_file->save_to_file (filepath);
465 return mainM->get_label ();
474 if ((rel == -10 && (ref == 15 || ref == 14 || ref == 13 ||
475 ref == 22 || ref == 31))
477 (rel == -9 && (ref == 13 || ref == 14 || ref == 15 ||
478 ref == 25 || ref == 35))
480 (rel == -1 && (ref == 13 || ref == 22 || ref == 31 ||
481 ref == 41 || ref == 51))
483 (rel == 1 && (ref == 15 || ref == 25 || ref == 35 ||
484 ref == 44 || ref == 53))
486 (rel == 9 && (ref == 31 || ref == 41 || ref == 51 ||
487 ref == 52 || ref == 53))
489 (rel == 10 && (ref == 51 || ref == 52 || ref == 53 ||
490 ref == 44 || ref == 35)))
506 int nHexa = rand () % 2;
524 Glib::KeyFile *saved_scores =
new Glib::KeyFile ();
525 saved_scores->load_from_file (config_file);
527 std::vector<Gtk::Widget*> old_score_vect =
box_score->get_children();
528 for (Gtk::Widget* old_score : old_score_vect)
532 if (saved_scores->has_group (
"Scores"))
534 Glib::ArrayHandle<Glib::ustring> names = saved_scores->get_keys (
"Scores");
535 for (Glib::ustring user : names)
537 std::list<int> user_scores = saved_scores->get_integer_list (
"Scores", user);
538 for (
int u_score : user_scores)
540 Gtk::Label *user_score_label =
new Gtk::Label ();
541 Glib::ustring line = user +
"\t: " + std::to_string (u_score);
542 user_score_label->set_text (line);
543 box_score->pack_start (*user_score_label);
555 return "nosecondhexa";
562 for (
int i : oldHexas)
566 Board[i]->set_label (
"");
571 if (
Board[newHexa]->get_label () !=
"")
573 int val = std::stoi (
Board[newHexa]->get_label ()) + 1;
574 if (val >
max && val < 8)
583 Board[newHexa]->set_label (std::to_string (val));
586 std::cerr <<
"No label !" << std::endl;
628 std::cout <<
"Event enter !!!" << std::endl;
670 Glib::ustring Sscore = std::to_string (Iscore) +
" pts";
694 Board[key]->set_label (
"");
705 Glib::PropertyProxy<Gtk::ReliefStyle> prop =
707 prop.set_value (Gtk::RELIEF_NONE);
736 if (actualPos2 == -10)
738 if (actualPos2 == -9)
742 if (actualPos2 == 10)
746 if (actualPos2 == -1)
756 if (event->button == 3)
758 if (actualPos2 == -10)
760 if (actualPos2 == -9)
764 if (actualPos2 == 10)
768 if (actualPos2 == -1)
791 for (
int i : oldRefs)
797 if (
Board[ref]->get_label () !=
"" &&
798 Board[Nb]->get_label () ==
Board[ref]->get_label ())
801 oldRefs.push_back (Nb);
811 mainM->set_label (Snum);
812 Glib::PropertyProxy<Gtk::ReliefStyle> prop =
813 mainM->property_relief ();
814 prop.set_value (Gtk::RELIEF_NORMAL);
815 mainM->set_sensitive (
true);
832 Glib::PropertyProxy<Gtk::ReliefStyle> prop =
834 prop.set_value (Gtk::RELIEF_NORMAL);
852 std::list<int> matchingNbs;
856 if (matchingNbs.size () > 2)
861 nMatch = matchingNbs.size () * 10;
875 Glib::RefPtr<Gtk::StyleContext> HexaContext;
887 HexaContext =
Board[hexa]->get_style_context();
888 if (
Board[hexa]->get_label () !=
"")
889 val = std::stoi (
Board[hexa]->get_label ());
893 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
896 HexaContext->remove_class (
Colors [key]);
899 HexaContext->add_class (
Colors [val]);
901 HexaContext->add_class (
"nocolor");
909 Glib::KeyFile *saved_scores;
910 saved_scores =
new Glib::KeyFile ();
911 saved_scores->load_from_file (config_file);
916 Glib::ustring username =
user_entry->get_text();
918 std::list<int> Lscore;
919 if (saved_scores->has_group(
"Scores"))
921 if (saved_scores->has_key(
"Scores", username))
922 Lscore = saved_scores->get_integer_list (
"Scores", username);
926 saved_scores->set_integer_list (
"Scores", username, Lscore);
928 saved_scores->save_to_file (config_file);
void big_badaboom(std::list< int > hexas)
Erase all hexas around three (or more) matching "7" hexas.
int get_pos2()
Returns pos2 (not very useful)
int get_neighbour(int rel, int givenRef)
Calculates the position of a neighbor button.
Gtk::Window * make7window
Gtk::AboutDialog * make7credits
Glib::ustring get_config_file()
Create config file if it doesn't exist, then return its path.
Gtk::MessageDialog * make7message
void on_display_help_activate()
Signal function activated by button_help.
void set_main_hexa(Glib::ustring Snum)
Put the string to the label of the main hexa in SelectionArea.
bool triggers(int ref)
Deal with a new hexa on the board.
void import_every_widget()
Function containing a lot of lines too.
std::map< int, Glib::ustring > Colors
std::map< int, Gtk::Button * > Board
std::map< int, Glib::ustring > BoardSnapshotMap
void on_close_scores_activate()
Signal function activated by button_close_scores.
void reset_board()
Erase values and colors of all hexas on the Board.
void get_random_hexas()
Generate random numbers and fill valHexa1 and valHexa2 with them.
void game_button_selected(int ref)
Called when any Board button is clicked.
std::list< int > SelectionAreaKeys
Gtk::HeaderBar * headerbar
Gtk::MessageDialog * make7confirm
int refresh_score()
Update the amount of points in the score label.
void refresh_selection_area()
Re-display the content of the SelectionArea.
std::map< int, Gtk::Button * > SelectionArea
Gtk::MessageDialog * make7askname
Glib::ustring get_main_hexa()
Returns the value of the main hexa button label.
Gtk::ToggleButton * button_erase
void on_quit_game_activate()
Signal function activated by button_quit.
Glib::ustring oldValHexa2
void display_error(int i)
Spawns a Gtk::MessageDialog containing an appropriated message.
void connect_every_button()
Function containing a lot of lines.
Gtk::Label * current_score
void erase_hexa(int ref)
Erase value and color of the given hexa.
void get_scores()
Read users scores from saved file.
std::list< int > BoardKeys
Gtk::Button * button_save
bool get_confirmation()
Ask user to confirm the quitting.
std::list< int > ColorsKeys
void merge_hexas(int newHexa, std::list< int > oldHexas)
Erase values and colors of matching neighbors.
Glib::ustring get_second_hexa()
Same as get_main_hexa () but with the second.
bool rotate_hexas_right(GdkEventButton *event)
Signal function activated when user right-clicks on main hexa.
void set_second_hexa(Glib::ustring Snum)
Same as set_main_hexa (), but with the second.
void reset_snapshot()
Re-display an old configuration of Board and SelectionArea, using the std::map created by get_snapsho...
void rotate_hexas_left()
Signal function activated when user left-clicks on main hexa.
Gtk::Window * make7scores
Glib::RefPtr< Gtk::CssProvider > CssProvider
void set_pos2(int givenPos2)
Set pos2 to the given value (not very useful)
void on_cancel_action_activate()
Signal function activated by button_cancel.
void write_scores()
Save the score in a file.
void launch_game()
All initializations needed at the beginning of a new game.
Gtk::Button * button_cancel
void reset_selection_area()
Erase values and colors of all hexas on the SelectionArea.
bool on_make7window_hide(_GdkEventAny *event)
Signal function activated when user wants to quit.
void define_hexa(int ref, Glib::ustring valref)
Define the new hexa then check neighbors while there are.
Gtk::Button * button_help
Gtk::Button * button_next
bool scan_neighbours(int ref, std::list< int > &oldRefs)
Check if the neighbors of a given hexa have the same value as it.
void on_new_game_activate()
Signal function activated by button_new.
void on_entry_editing_done()
Activates button_apply_save when user hits 'Enter'.
void update_hexa_color(int hexa)
Change the color of a given hexa.
void display(Glib::RefPtr< Gtk::Application > app)
Here is actually displayed the window.
Gtk::Button * button_close_scores
void on_next_hexa_activate()
Signal function activated by button_next.
void on_display_credits_activate()
Signal function activated by button_credits.
void on_display_scores_activate()
Signal function activated by button_scores.
Glib::ustring oldValHexa1
Gtk::MessageDialog * make7error_board
std::map< int, Glib::ustring > get_board_snapshot()
Backup the whole Board and SelectionArea in a std::map.