source: branches/openyahtzee1.7PE/src/MainFrame.cpp @ 79

Last change on this file since 79 was 79, checked in by guyru, 7 years ago

Patched source-code for portable edition

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.2 KB
Line 
1// $Header$
2/***************************************************************************
3 *   Copyright (C) 2006 by Guy Rutenberg   *
4 *   guyrutenberg@gmail.com   *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 *   This program is distributed in the hope that it will be useful,       *
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14 *   GNU General Public License for more details.                          *
15 *                                                                         *
16 *   You should have received a copy of the GNU General Public License     *
17 *   along with this program; if not, write to the                         *
18 *   Free Software Foundation, Inc.,                                       *
19 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20 ***************************************************************************/
21
22 /***********************************************
23 *      This File contains the definitions      *
24 *      of MainFrame's functions                *
25 ***********************************************/
26
27// #define DEBUG
28
29#include <wx/wx.h>
30
31#include "MainFrame.h"
32#include "wxDynamicBitmap.h"
33#include "ObjectsID.h"
34#include "HighScoreDialog.h"
35#include "SettingsDialog.h"
36#include <iostream>
37#include <sstream>
38#include <cstdlib>
39#include <wx/version.h>
40
41//include the images for the dice
42#include "one.xpm"
43#include "two.xpm"
44#include "three.xpm"
45#include "four.xpm"
46#include "five.xpm"
47#include "six.xpm"
48
49//include the icon file
50#include "Icon.h"
51
52//default values
53#define SPACE_SIZE 1
54#define DEF_HIGHSCORESIZE 20
55#define OY_VERSION "1.7.0"
56
57MainFrame::MainFrame(const wxString& title, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE)
58        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size, style)
59{
60        //give the frame an icon
61        SetIcon(wxIcon(ICON));
62
63
64        std::ostringstream sstr;
65
66        m_settingsdb = new SettingsDB(); //Get the settings database connection
67        m_highscoredb = new HighScoreTableDB();
68
69        //DATABASE initialization
70        if (m_settingsdb->GetKey("highscoresize") == "") { //check if we need to create a new high score table
71                m_highscoredb->SetSize(DEF_HIGHSCORESIZE);
72                sstr<<DEF_HIGHSCORESIZE<<std::flush;
73                m_settingsdb->SetKey("highscoresize", sstr.str());
74        } else {
75                int highscoresize = atoi((m_settingsdb->GetKey("highscoresize")).c_str());
76                //m_highscoredb->SetSize((highscoresize>0)?highscoresize:DEF_HIGHSCORESIZE);
77                m_highscoredb->SetSize(highscoresize);
78        }
79       
80        if (m_settingsdb->GetKey("animate") == "Yes") {
81                m_animate = true;
82        } else if (m_settingsdb->GetKey("animate") == "No") {
83                m_animate = false;
84        } else {
85                m_settingsdb->SetKey("animate", "Yes");
86                m_animate = true;
87        }
88        if (m_settingsdb->GetKey("calculatesubtotal") == "Yes") {
89                m_calculatesubtotal = true;
90        } else if (m_settingsdb->GetKey("calculatesubtotal") == "No") {
91                m_calculatesubtotal = false;
92        } else {
93                m_settingsdb->SetKey("calculatesubtotal", "Yes");
94                m_calculatesubtotal = true;
95        }
96
97        // END Database initialization
98
99        bitmap_dices[0] = new wxBitmap(one_xpm);
100        bitmap_dices[1] = new wxBitmap(two_xpm);
101        bitmap_dices[2] = new wxBitmap(three_xpm);
102        bitmap_dices[3] = new wxBitmap(four_xpm);
103        bitmap_dices[4] = new wxBitmap(five_xpm);
104        bitmap_dices[5] = new wxBitmap(six_xpm);
105       
106        //randomize the random-number generator based on the time
107        srand( (unsigned)time( NULL ) );
108
109
110        /*****Create and initialize the menu bar******/
111
112        /*Create the menus*/
113        wxMenu *gameMenu = new wxMenu;  //create File menu
114        wxMenu *helpMenu = new wxMenu;  //create Help menu
115       
116       
117        //insert menu items into menu Help
118        helpMenu->Append(ID_CHECK_FOR_UPDATES, wxT("&Check for Updates"),
119                        wxT("Check for new version of the game via the web"));
120        helpMenu->AppendSeparator();
121        helpMenu->Append(ID_SENDCOMMENT, wxT("&Send a Comment to Developers"),
122                        wxT("Send a comment to the developers of the game"));
123        helpMenu->AppendSeparator();
124        helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
125                        wxT("Show about dialog"));
126
127        //insert menu items into menu File
128        gameMenu->Append(ID_NEWGAME,wxT("&New Game\tF2"),wxT("Start a new game"));
129        //create the undo button and make it disabled
130        gameMenu->Append(ID_UNDO,wxT("&Undo\tCTRL+Z"),wxT("Undo the last move"));
131        gameMenu->Append(ID_SHOWHIGHSCORE,wxT("High &Scores"),wxT("Show high-scores table"));
132        gameMenu->Append(ID_SETTINGS,wxT("Settings"),wxT("Show settings dialog"));
133        gameMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),
134                        wxT("Quit this program"));
135       
136        // Declare the menu-bar and append the freshly created menus to the menu bar...
137        wxMenuBar *menuBar = new wxMenuBar();
138        menuBar->Append(gameMenu, wxT("&Game"));
139        menuBar->Append(helpMenu, wxT("&Help"));
140       
141        // ... and attach this menu bar to the frame
142        SetMenuBar(menuBar);
143       
144        /***End menu-bar ***/
145        wxPanel* panel = new wxPanel(this, ID_PANEL,
146                wxDefaultPosition, wxDefaultSize);
147
148        wxBoxSizer *topSizer = new wxBoxSizer( wxHORIZONTAL );
149        wxBoxSizer *sectionsSizer = new wxBoxSizer( wxVERTICAL );
150        wxBoxSizer *diceSizer = new wxBoxSizer( wxVERTICAL );
151
152        wxSizer *uppersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Upper Section") ), wxVERTICAL);
153        wxSizer *lowersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Lower Section") ), wxVERTICAL);
154       
155        wxFlexGridSizer* uppergrid = new wxFlexGridSizer(2, 0, 10);
156        wxFlexGridSizer* lowergrid = new wxFlexGridSizer(2, 0, 10);
157
158       
159        uppergrid->Add(new wxButton(panel,ID_ACES,wxT("Aces")),0,wxALL,SPACE_SIZE);
160        uppergrid->Add(new wxTextCtrl(panel, ID_ACESTEXT),1,wxALL,SPACE_SIZE);
161        uppergrid->Add(new wxButton(panel,ID_TWOS,wxT("Twos")),0,wxALL,SPACE_SIZE);
162        uppergrid->Add(new wxTextCtrl(panel, ID_TWOSTEXT),1,wxALL,SPACE_SIZE);
163        uppergrid->Add(new wxButton(panel,ID_THREES,wxT("Threes")),0,wxALL,SPACE_SIZE);
164        uppergrid->Add(new wxTextCtrl(panel, ID_THREESTEXT),1,wxALL,SPACE_SIZE);
165        uppergrid->Add(new wxButton(panel,ID_FOURS,wxT("Fours")),0,wxALL,SPACE_SIZE);
166        uppergrid->Add(new wxTextCtrl(panel, ID_FOURSTEXT),1,wxALL,SPACE_SIZE);
167        uppergrid->Add(new wxButton(panel,ID_FIVES,wxT("Fives")),0,wxALL,SPACE_SIZE);
168        uppergrid->Add(new wxTextCtrl(panel, ID_FIVESTEXT),1,wxALL,SPACE_SIZE);
169        uppergrid->Add(new wxButton(panel,ID_SIXES,wxT("Sixes")),0,wxALL,SPACE_SIZE);
170        uppergrid->Add(new wxTextCtrl(panel, ID_SIXESTEXT),1,wxALL,SPACE_SIZE);
171        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total score:")),0,wxALL,SPACE_SIZE);
172        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERSECTIONTOTAL),1,wxALL,SPACE_SIZE);
173        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Bonus:")),0,wxALL,SPACE_SIZE);
174        uppergrid->Add(new wxTextCtrl(panel, ID_BONUS),1,wxALL,SPACE_SIZE);
175        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of upper section:")),0,wxALL,SPACE_SIZE);
176        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERTOTAL),1,wxALL,SPACE_SIZE);
177
178
179        lowergrid->Add(new wxButton(panel,ID_THREEOFAKIND,wxT("3 of a kind")),0,wxALL,SPACE_SIZE);
180        lowergrid->Add(new wxTextCtrl(panel, ID_THREEOFAKINDTEXT),1,wxALL,SPACE_SIZE);
181        lowergrid->Add(new wxButton(panel,ID_FOUROFAKIND,wxT("4 of a kind")),0,wxALL,SPACE_SIZE);
182        lowergrid->Add(new wxTextCtrl(panel, ID_FOUROFAKINDTEXT),1,wxALL,SPACE_SIZE);
183        lowergrid->Add(new wxButton(panel,ID_FULLHOUSE,wxT("Full House")),0,wxALL,SPACE_SIZE);
184        lowergrid->Add(new wxTextCtrl(panel, ID_FULLHOUSETEXT),1,wxALL,SPACE_SIZE);
185        lowergrid->Add(new wxButton(panel,ID_SMALLSEQUENCE,wxT("Sequence of 4")),0,wxALL,SPACE_SIZE);
186        lowergrid->Add(new wxTextCtrl(panel, ID_SMALLSEQUENCETEXT),1,wxALL,SPACE_SIZE);
187        lowergrid->Add(new wxButton(panel,ID_LARGESEQUENCE,wxT("Sequence of 5")),0,wxALL,SPACE_SIZE);
188        lowergrid->Add(new wxTextCtrl(panel, ID_LARGESEQUENCETEXT),1,wxALL,SPACE_SIZE);
189        lowergrid->Add(new wxButton(panel,ID_YAHTZEE,wxT("Yahtzee")),0,wxALL,SPACE_SIZE);
190        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEETEXT),1,wxALL,SPACE_SIZE);
191        lowergrid->Add(new wxButton(panel,ID_CHANCE,wxT("Chance")),0,wxALL,SPACE_SIZE);
192        lowergrid->Add(new wxTextCtrl(panel, ID_CHANCETEXT),1,wxALL,SPACE_SIZE);
193        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Yahtzee Bonus")),0,wxALL,SPACE_SIZE);
194        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEEBONUSTEXT),1,wxALL,SPACE_SIZE);
195        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of lower section:")),0,wxALL,SPACE_SIZE);
196        lowergrid->Add(new wxTextCtrl(panel, ID_LOWERTOTAL),1,wxALL,SPACE_SIZE);
197        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Grand Total:")),0,wxALL,SPACE_SIZE);
198        lowergrid->Add(new wxTextCtrl(panel, ID_GRANDTOTAL),1,wxALL,SPACE_SIZE);
199
200        uppersection->Add(uppergrid);
201        lowersection->Add(lowergrid);
202        sectionsSizer->Add(uppersection,0,wxALL,5);
203        sectionsSizer->Add(lowersection,0,wxALL,5);
204
205        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE1,*bitmap_dices[0]),0,wxALL,3);
206        diceSizer->Add(new wxCheckBox(panel, ID_DICE1KEEP, wxT("Keep")),0,wxBOTTOM,10);
207        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE2,*bitmap_dices[1]),0,wxALL,3);
208        diceSizer->Add(new wxCheckBox(panel, ID_DICE2KEEP, wxT("Keep")),0,wxBOTTOM,10);
209        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE3,*bitmap_dices[2]),0,wxALL,3);
210        diceSizer->Add(new wxCheckBox(panel, ID_DICE3KEEP, wxT("Keep")),0,wxBOTTOM,10);
211        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE4,*bitmap_dices[3]),0,wxALL,3);
212        diceSizer->Add(new wxCheckBox(panel, ID_DICE4KEEP, wxT("Keep")),0,wxBOTTOM,10);
213        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE5,*bitmap_dices[4]),0,wxALL,3);
214        diceSizer->Add(new wxCheckBox(panel, ID_DICE5KEEP, wxT("Keep")),0,wxBOTTOM,10);
215        diceSizer->Add(new wxButton(panel, ID_ROLL, wxT("Roll")),0,wxALL,3);
216
217       
218        topSizer->Add(sectionsSizer);
219        topSizer->Add(diceSizer);       
220
221        panel->SetSizer(topSizer);
222       
223        topSizer->Fit(this);
224        topSizer->SetSizeHints(this);
225       
226        //make the text boxes uneditable to the user   
227        wxTextCtrl *textctrl;
228        for(int i = ID_ACESTEXT;i<=ID_GRANDTOTAL;i++) {
229                textctrl = (wxTextCtrl*) FindWindow(i);
230                textctrl->SetEditable(false);
231        }
232       
233        //disable the undo button
234        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
235
236
237       
238
239        /***************************************/
240        /********* Declare Event Table *********/
241        /***************************************/
242       
243        /***Connect Menu items***/
244        Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnQuit));
245        Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAbout));
246        Connect(ID_CHECK_FOR_UPDATES, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnCheckForUpdates));
247        Connect(ID_SENDCOMMENT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSendComment));
248        Connect(ID_NEWGAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnNewGame));
249        Connect(ID_UNDO, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnUndo));
250        Connect(ID_SHOWHIGHSCORE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnShowHighscore));
251        Connect(ID_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSettings));
252        Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnRollButton));
253
254        Connect(ID_ACES,ID_SIXES, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnUpperButtons));
255        Connect(ID_THREEOFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On3ofakindButton));
256        Connect(ID_FOUROFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On4ofakindButton));
257        Connect(ID_FULLHOUSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnFullHouseButton));
258        Connect(ID_SMALLSEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnSmallSequenceButton));
259        Connect(ID_LARGESEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnLargeSequenceButton));
260        Connect(ID_YAHTZEE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnYahtzeeButton));
261        Connect(ID_CHANCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnChanceButton));
262       
263        /*** End of Event Table ***/
264
265        ResetRolls();
266        ClearDiceHash();
267        m_yahtzee = false;
268        m_yahtzeebonus = false;
269        m_numofplaysleft = 13;
270
271 }
272
273/*********EVENT PROCCESSING FUNCTIONS********/
274
275
276void MainFrame::OnAbout(wxCommandEvent& event)
277{
278        wxString msg;
279        wxString sqliteversion = wxString(sqlite3_version,wxConvUTF8);
280        msg.Printf(wxT("Open Yahtzee %s Portable Edition\nCopyright (C) 2006 by Guy Rutenberg.\nDice design by Seamus McGill\n\nThis program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.\n\nOpen Yahtzee was built against:\nwxWidgets %i.%i\n"),wxT(OY_VERSION),wxMAJOR_VERSION,wxMINOR_VERSION);
281        msg += wxT("SQLite ") + sqliteversion;
282               
283        wxMessageBox(msg, wxT("About Open Yahtzee PE"), wxOK | wxICON_INFORMATION, this);
284}
285
286void MainFrame::OnCheckForUpdates (wxCommandEvent& event){
287       
288        wxString link = wxT("http://openyahtzee.sourceforge.net/update.php?version=");
289       
290        link += wxT(OY_VERSION);
291
292        LaunchBrowser(link);
293}
294
295void MainFrame::OnSendComment (wxCommandEvent& event){
296       
297        wxString link = wxT("http://openyahtzee.sourceforge.net/feedback.php");
298       
299        LaunchBrowser(link);
300}
301
302void MainFrame::OnQuit(wxCommandEvent& event)
303{
304        // Destroy the frame
305        Close();
306}
307
308void MainFrame::OnNewGame(wxCommandEvent& event)
309{
310        //disable the undo button so it won't be enabled when a new game is started.
311        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
312
313        ResetRolls();
314        ClearDiceHash();
315        m_yahtzee = false;
316        m_numofplaysleft = 13;
317       
318        for (int i = ID_ACESTEXT; i<= ID_GRANDTOTAL; i++)
319                ((wxTextCtrl*) FindWindow(i))->Clear();
320        for (int i = ID_ACES; i<= ID_CHANCE; i++)
321                ((wxButton*) FindWindow(i))->Enable(true);
322}
323
324///This function handles the undo events
325void MainFrame::OnUndo(wxCommandEvent& event)
326{
327        m_rolls = m_rollsundo;
328
329        //after the user scored the button was enabled, check if it should be disabled
330        if (m_rolls <= 0) //we don't have remaining rolls
331                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
332
333        //restore the 'keep' checkboxes
334        for (int i=0; i<5; i++)
335                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
336       
337        //reset the users last choice
338        FindWindow(m_lastmove)->Enable(true);
339
340        //clear the score;
341        ((wxTextCtrl*)FindWindow(ID_ACESTEXT + (m_lastmove - ID_ACES)))->SetValue(wxT(""));
342
343        //undo also the yahtzee bonus if needed
344        if (m_yahtzeebonus) {
345                long temp;
346                wxString tempstr;
347       
348                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
349                tempstr.ToLong(&temp,10);
350                temp -= 100; //this line reduces the points given for the yahtzee bonus
351                tempstr.Printf(wxT("%i"),temp);
352                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
353        }
354
355        //recalculate the subtotals
356        CalculateSubTotal();
357
358        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
359        //cancel the counting for the choice that was canceled
360        m_numofplaysleft++;
361}
362///This function enables the undo button and stores the last move
363inline void MainFrame::EnableUndo(int id)
364{
365        if (m_numofplaysleft) {
366                (GetMenuBar()->FindItem(ID_UNDO))->Enable(true);
367                m_lastmove = id;
368        }
369}
370
371void MainFrame::OnShowHighscore(wxCommandEvent& event)
372{
373        HighScoreDialog *dialog = new HighScoreDialog(this,wxID_ANY,m_highscoredb);
374        dialog->ShowModal();
375}
376
377void MainFrame::OnSettings( wxCommandEvent& event)
378{
379        SettingsDialog *dialog = new SettingsDialog(this,wxID_ANY);
380        SettingsDialogData data;
381        std::ostringstream sstr;
382       
383        data.highscoresize = m_highscoredb->GetSize();
384
385        data.animate = (m_settingsdb->GetKey("animate")=="Yes")?true:false;
386        data.subtotal = (m_settingsdb->GetKey("calculatesubtotal")=="Yes")?true:false;
387       
388        dialog->SetData(data);
389        if(dialog->ShowModal()==wxID_OK) { //user saved Changes
390                data = dialog->GetData();
391
392                if(data.reset)
393                        m_highscoredb->SetSize(0);
394               
395                sstr<<data.highscoresize<<std::flush;
396                m_settingsdb->SetKey("highscoresize",sstr.str());
397                               
398                m_highscoredb->SetSize(data.highscoresize);
399               
400                if (data.animate){
401                        m_settingsdb->SetKey("animate","Yes");
402                        m_animate = true;
403                       
404                } else {
405                        m_settingsdb->SetKey("animate","No");
406                        m_animate = false;
407                }
408                if (data.subtotal){
409                        m_settingsdb->SetKey("calculatesubtotal","Yes");
410                        m_calculatesubtotal = true;
411                       
412                } else {
413                        m_settingsdb->SetKey("calculatesubtotal","No");
414                        m_calculatesubtotal = false;
415                }
416
417        }
418}
419
420void MainFrame::OnRollButton (wxCommandEvent& event)
421{
422        //roll the dice...
423        if (m_animate) {
424                int dice_throws[5] = {0,0,0,0,0};
425                for (int i=0; i<5; i++) { //set the number of rolls for each dice
426                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
427                                dice_throws[i] = (rand()%15)+3; //ensures the number is at least one.
428                        }
429                }
430                while (dice_throws[0] || dice_throws[1] || dice_throws[2] || dice_throws[3] || dice_throws[4]) {
431                        for (int i=0 ; i<5; i++){
432                                if(dice_throws[i]){
433                                        dice_throws[i]--;
434                                        dice[i] = rand()%6;
435                                        ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
436                                }
437                        }
438                        ::wxMilliSleep(100);
439                }
440        } else {
441                for (int i=0; i<5; i++) {
442                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
443                                dice[i] = rand()%6;
444                                ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
445                        }
446                }
447        }
448       
449        //Clear old dice-hash and create a new one
450        ClearDiceHash();
451        for (int i=0; i<5; i++)
452                dicehash[dice[i]] += 1;
453
454        //if out of rolls disable the roll butoon
455        m_rolls -= 1;
456        #ifndef DEBUG
457        if (m_rolls <= 0)
458                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
459        #endif
460       
461        //enable the keep checkboxes
462        for (int i=0; i<5; i++)
463                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
464       
465        //we rolled the dices so undoing isn't allowed
466        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
467        m_yahtzeebonus = false; //if we scored yahtzee bonus before we don't care anymore.
468
469}
470
471void MainFrame::OnUpperButtons (wxCommandEvent& event)
472{
473        wxString out;
474        int temp;
475        if(m_rolls < 3){
476                YahtzeeBonus();
477                temp = dicehash[(event.GetId() - ID_ACES)] * (event.GetId() - ID_ACES + 1);
478       
479                out.Printf(wxT("%i"),temp);
480                ((wxTextCtrl*) FindWindow(event.GetId() - ID_ACES + ID_ACESTEXT))->SetValue(out);
481               
482                PostScore(event.GetId());
483        }
484        else
485                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
486}
487
488void MainFrame::On3ofakindButton(wxCommandEvent& event)
489{
490        if(m_rolls>=3) {
491                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
492                return;
493        }
494        YahtzeeBonus();
495        bool three = false;
496        wxString out;
497        int temp = 0;   
498
499        //check for the conditions of scoring
500        for (int i=0; i<6; i++)
501                if (dicehash[i] >= 3)
502                        three = true;
503        if (three){
504                for(int i = 0; i<5; i++)
505                        temp += dice[i]+1;
506       
507                out.Printf(wxT("%i"),temp);
508                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(out);
509        } else
510                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(wxT("0"));
511       
512        PostScore(event.GetId());
513}
514
515void MainFrame::On4ofakindButton(wxCommandEvent& event)
516{
517        if(m_rolls>=3) {
518                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
519                return;
520        }
521        YahtzeeBonus();
522        bool four = false;
523        wxString out;
524        int temp = 0;   
525
526        //check for the conditions of scoring
527        for (int i=0; i<6; i++)
528                if (dicehash[i] >= 4)
529                        four = true;
530        if (four){
531                for(int i = 0; i<5; i++)
532                        temp += dice[i]+1;
533       
534                out.Printf(wxT("%i"),temp);
535                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(out);
536        } else
537                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(wxT("0"));
538       
539        PostScore(event.GetId());
540}
541
542void MainFrame::OnFullHouseButton(wxCommandEvent& event)
543{
544        if(m_rolls>=3) {
545                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
546                return;
547        }
548        YahtzeeBonus();
549        bool two = false;
550        bool three = false;
551
552        //check for the conditions of scoring
553        for (int i=0; i<6; i++)
554                if (dicehash[i] == 2)
555                        two = true;
556        for (int i=0; i<6; i++)
557                if (dicehash[i] == 3)
558                        three = true;
559        if ((two && three) || YahtzeeJoker())
560                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("25"));
561        else
562                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("0"));
563       
564        PostScore(event.GetId());
565}
566
567void MainFrame::OnSmallSequenceButton(wxCommandEvent& event)
568{
569        if(m_rolls>=3) {
570                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
571                return;
572        }
573
574        YahtzeeBonus();
575        bool sequence = false;
576        //check for the conditions of scoring
577        if ( (dicehash[0]>=1 && dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1) ||
578                (dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1) ||
579                (dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1 && dicehash[5]>=1))
580                        sequence = true;
581        if (sequence || YahtzeeJoker())
582                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("30"));
583        else
584                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("0"));
585       
586        PostScore(event.GetId());
587}
588
589void MainFrame::OnLargeSequenceButton(wxCommandEvent& event)
590{
591        if(m_rolls>=3) {
592                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
593                return;
594        }
595
596        YahtzeeBonus();
597        bool sequence = false;
598        //check for the conditions of scoring
599        if ( (dicehash[0]==1 && dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1) ||
600                (dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1 && dicehash[5]==1))
601                        sequence = true;
602        if (sequence || YahtzeeJoker())
603                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("40"));
604        else
605                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("0"));
606       
607        PostScore(event.GetId());
608}
609
610void MainFrame::OnYahtzeeButton(wxCommandEvent& event)
611{
612        if(m_rolls>=3) {
613                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
614                return;
615        }
616        //give the score
617        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])){
618                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("50"));
619                m_yahtzee=true;
620        } else
621                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("0"));
622
623        PostScore(event.GetId());
624}
625
626void MainFrame::OnChanceButton (wxCommandEvent& event)
627{
628        wxString out;
629        int temp = 0;
630        if(m_rolls < 3){
631                YahtzeeBonus();
632                for(int i = 0; i<5; i++)
633                        temp += dice[i]+1;
634       
635                out.Printf(wxT("%i"),temp);
636                ((wxTextCtrl*) FindWindow(ID_CHANCETEXT))->SetValue(out);
637               
638                PostScore(event.GetId());
639        }
640        else
641                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
642
643}
644
645//********************************************
646//******        General Functions       ******
647//********************************************
648
649void MainFrame::ClearDiceHash()
650{
651        for (int i=0; i<6; i++)
652                dicehash[i] = 0;
653}
654
655void MainFrame::ResetRolls()
656{
657        m_rollsundo = m_rolls;
658        m_rolls = 3;
659        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(true);
660        for (int i=0; i<5; i++){
661                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> SetValue(false);
662                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(false);
663        }
664}
665
666void MainFrame::YahtzeeBonus()
667{
668        long temp;
669        wxString tempstr;
670       
671        //if the player didn't have any yathzees yet he can't have the bonus;
672        if (!m_yahtzee)
673                return;
674        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])) {
675                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
676                tempstr.ToLong(&temp,10);
677                temp += 100;
678                tempstr.Printf(wxT("%i"),temp);
679                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
680                m_yahtzeebonus = true;
681        }       
682}
683
684bool MainFrame::YahtzeeJoker()
685{
686        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4]) && !(FindWindow(ID_ACES+dice[0])->IsEnabled()) && !(FindWindow(ID_YAHTZEE)->IsEnabled())) {
687                return true;
688        }
689        return false;
690}
691
692void MainFrame::EndofGame()
693{
694        if(m_numofplaysleft>0)
695                return;
696       
697        wxString tempstr;
698        long temp;
699        long upperscore = 0;
700        long lowerscore = 0;
701
702        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
703                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
704                tempstr.ToLong(&temp,10);
705                upperscore +=temp;
706        }
707       
708        tempstr.Printf(wxT("%i"),upperscore);
709        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
710       
711        //check for bonus
712        if(upperscore>=63) {
713                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("35"));
714                upperscore +=35;
715        } else
716                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("0"));
717       
718        tempstr.Printf(wxT("%i"),upperscore);
719        ((wxTextCtrl*) FindWindow(ID_UPPERTOTAL)) -> SetValue(tempstr);
720       
721        //calculate total on lower section
722        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
723                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
724                tempstr.ToLong(&temp,10);
725                lowerscore +=temp;
726        }
727       
728        tempstr.Printf(wxT("%i"),lowerscore);
729        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
730        tempstr.Printf(wxT("%i"),upperscore + lowerscore);
731        ((wxTextCtrl*) FindWindow(ID_GRANDTOTAL)) -> SetValue(tempstr);
732
733        //disable the roll button;
734        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
735
736        tempstr.Printf(wxT("Your final score is %i points!"),lowerscore+upperscore);
737        wxMessageBox(tempstr, wxT("Game Ended"), wxOK | wxICON_INFORMATION, this);
738
739        //submit to high score
740        HighScoreHandler(lowerscore+upperscore);
741       
742}
743
744void MainFrame::HighScoreHandler(int score)
745{
746        int place;
747        std::string name,date;
748        wxCommandEvent newevent;
749
750
751        place = m_highscoredb->IsHighScore(score);
752       
753        if(!place)  //if the score didn't make it to the highscore table do nothing
754                return;
755        HighScoreInfo *infodialog = new HighScoreInfo(this,place);
756        infodialog->ShowModal();
757
758        name = infodialog->GetName().mb_str();
759
760        //get the date
761        wxDateTime now = wxDateTime::Now();
762        date = now.FormatISOTime().mb_str();
763        date +=" ";
764        date += now.FormatDate().mb_str();
765
766        m_highscoredb->SendHighScore(name,date,score);
767
768        //now show the high score table
769        newevent.SetId(ID_SHOWHIGHSCORE);
770        newevent.SetEventType(wxEVT_COMMAND_MENU_SELECTED);
771        ProcessEvent(newevent);
772
773}
774
775///this function handles all the post scoring stuff such as disabling the right button.
776void MainFrame::PostScore(int id)
777{
778        //now after the scoring reset the rolls
779        ResetRolls();
780
781        CalculateSubTotal();
782
783        //and disable the button
784        FindWindow(id)->Enable(false);
785        m_numofplaysleft--;
786        EnableUndo(id);
787        EndofGame();
788}
789
790void MainFrame::CalculateSubTotal()
791{
792        if (!m_calculatesubtotal)
793                return;
794        long upperscore = 0;
795        long lowerscore = 0;
796        wxString tempstr;
797        long temp;
798
799
800        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
801                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
802                tempstr.ToLong(&temp,10);
803                upperscore +=temp;
804        }
805       
806        tempstr.Printf(wxT("%i"),upperscore);
807        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
808
809        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
810                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
811                tempstr.ToLong(&temp,10);
812                lowerscore +=temp;
813        }
814       
815        tempstr.Printf(wxT("%i"),lowerscore);
816        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
817}
818
819void MainFrame::LaunchBrowser (wxString link){
820        if (!wxLaunchDefaultBrowser(link)){
821                 //if builtin function doesn't work search for couple of browsers manually. see
822                 //http://linux-consulting.buanzo.com.ar/2006/05/wxwidgets-code-to-launch-browser.html
823
824                // variable declarations
825                wxArrayString browsers;
826                wxPathList path_list;
827                bool BrowserWasFound = false;
828                unsigned int i = 0;
829                wxString path;
830
831                // Add directories to wxPathList's search path from PATH environment variable
832                path_list.AddEnvList(wxT("PATH"));
833               
834                // Add browsers filenames. First item = most priority
835                browsers.Add(wxT("firefox"));
836                browsers.Add(wxT("firefox-bin"));
837                browsers.Add(wxT("mozilla"));
838                browsers.Add(wxT("mozilla-bin"));
839                browsers.Add(wxT("opera"));
840                browsers.Add(wxT("konqueror"));
841                browsers.Add(wxT("epiphany"));
842               
843                for (i = 0; i < browsers.GetCount(); i++) {
844                        path = path_list.FindAbsoluteValidPath(browsers[i]);
845                        if (path.IsEmpty()) {
846                                continue;
847                        } else {
848                                BrowserWasFound = true;
849                                break;
850                        }
851                }
852               
853                browsers.Clear();
854               
855                if (BrowserWasFound) {
856                        path += wxT(" ");
857                        path += link;
858                        ::wxExecute(path);
859                } else {
860                        wxMessageBox(wxT("No browser has been found."),wxT("OpenYahtzee"));
861                }
862        }
863}
Note: See TracBrowser for help on using the repository browser.