source: trunk/OpenYahtzee/src/MainFrame.cpp @ 51

Last change on this file since 51 was 51, checked in by guyru, 8 years ago

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.9 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
89        // END Database initialization
90
91        bitmap_dices[0] = new wxBitmap(one_xpm);
92        bitmap_dices[1] = new wxBitmap(two_xpm);
93        bitmap_dices[2] = new wxBitmap(three_xpm);
94        bitmap_dices[3] = new wxBitmap(four_xpm);
95        bitmap_dices[4] = new wxBitmap(five_xpm);
96        bitmap_dices[5] = new wxBitmap(six_xpm);
97       
98        //randomize the random-number generator based on the time
99        srand( (unsigned)time( NULL ) );
100
101
102        /*****Create and initialize the menu bar******/
103
104        /*Create the menus*/
105        wxMenu *gameMenu = new wxMenu;  //create File menu
106        wxMenu *helpMenu = new wxMenu;  //create Help menu
107       
108       
109        //insert menu items into menu Help
110        helpMenu->Append(ID_CHECK_FOR_UPDATES, wxT("&Check for Updates"),
111                        wxT("Check for new version of the game via the web"));
112        helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
113                        wxT("Show about dialog"));
114
115        //insert menu items into menu File
116        gameMenu->Append(ID_NEWGAME,wxT("&New Game\tF2"),wxT("Start a new game"));
117        //create the undo button and make it disabled
118        gameMenu->Append(ID_UNDO,wxT("&Undo"),wxT("Undo the last move"));
119        gameMenu->Append(ID_SHOWHIGHSCORE,wxT("High &Scores"),wxT("Show high-scores table"));
120        gameMenu->Append(ID_SETTINGS,wxT("Settings"),wxT("Show settings dialog"));
121        gameMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),
122                        wxT("Quit this program"));
123       
124        // Declare the menu-bar and append the freshly created menus to the menu bar...
125        wxMenuBar *menuBar = new wxMenuBar();
126        menuBar->Append(gameMenu, wxT("&Game"));
127        menuBar->Append(helpMenu, wxT("&Help"));
128       
129        // ... and attach this menu bar to the frame
130        SetMenuBar(menuBar);
131       
132        /***End menu-bar ***/
133        wxPanel* panel = new wxPanel(this, ID_PANEL,
134                wxDefaultPosition, wxDefaultSize);
135
136        wxBoxSizer *topSizer = new wxBoxSizer( wxHORIZONTAL );
137        wxBoxSizer *sectionsSizer = new wxBoxSizer( wxVERTICAL );
138        wxBoxSizer *diceSizer = new wxBoxSizer( wxVERTICAL );
139
140        wxSizer *uppersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Upper Section") ), wxVERTICAL);
141        wxSizer *lowersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Lower Section") ), wxVERTICAL);
142       
143        wxFlexGridSizer* uppergrid = new wxFlexGridSizer(9, 2, 0, 0);
144        wxFlexGridSizer* lowergrid = new wxFlexGridSizer(9, 2, 0, 0);
145
146       
147        uppergrid->Add(new wxButton(panel,ID_ACES,wxT("Aces")),0,wxALL,SPACE_SIZE);
148        uppergrid->Add(new wxTextCtrl(panel, ID_ACESTEXT),1,wxALL,SPACE_SIZE);
149        uppergrid->Add(new wxButton(panel,ID_TWOS,wxT("Twos")),0,wxALL,SPACE_SIZE);
150        uppergrid->Add(new wxTextCtrl(panel, ID_TWOSTEXT),1,wxALL,SPACE_SIZE);
151        uppergrid->Add(new wxButton(panel,ID_THREES,wxT("Threes")),0,wxALL,SPACE_SIZE);
152        uppergrid->Add(new wxTextCtrl(panel, ID_THREESTEXT),1,wxALL,SPACE_SIZE);
153        uppergrid->Add(new wxButton(panel,ID_FOURS,wxT("Fours")),0,wxALL,SPACE_SIZE);
154        uppergrid->Add(new wxTextCtrl(panel, ID_FOURSTEXT),1,wxALL,SPACE_SIZE);
155        uppergrid->Add(new wxButton(panel,ID_FIVES,wxT("Fives")),0,wxALL,SPACE_SIZE);
156        uppergrid->Add(new wxTextCtrl(panel, ID_FIVESTEXT),1,wxALL,SPACE_SIZE);
157        uppergrid->Add(new wxButton(panel,ID_SIXES,wxT("Sixes")),0,wxALL,SPACE_SIZE);
158        uppergrid->Add(new wxTextCtrl(panel, ID_SIXESTEXT),1,wxALL,SPACE_SIZE);
159        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total score:")),0,wxALL,SPACE_SIZE);
160        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERSECTIONTOTAL),1,wxALL,SPACE_SIZE);
161        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Bonus:")),0,wxALL,SPACE_SIZE);
162        uppergrid->Add(new wxTextCtrl(panel, ID_BONUS),1,wxALL,SPACE_SIZE);
163        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of upper section:")),0,wxALL,SPACE_SIZE);
164        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERTOTAL),1,wxALL,SPACE_SIZE);
165
166
167        lowergrid->Add(new wxButton(panel,ID_THREEOFAKIND,wxT("3 of a kind")),0,wxALL,SPACE_SIZE);
168        lowergrid->Add(new wxTextCtrl(panel, ID_THREEOFAKINDTEXT),1,wxALL,SPACE_SIZE);
169        lowergrid->Add(new wxButton(panel,ID_FOUROFAKIND,wxT("4 of a kind")),0,wxALL,SPACE_SIZE);
170        lowergrid->Add(new wxTextCtrl(panel, ID_FOUROFAKINDTEXT),1,wxALL,SPACE_SIZE);
171        lowergrid->Add(new wxButton(panel,ID_FULLHOUSE,wxT("Full House")),0,wxALL,SPACE_SIZE);
172        lowergrid->Add(new wxTextCtrl(panel, ID_FULLHOUSETEXT),1,wxALL,SPACE_SIZE);
173        lowergrid->Add(new wxButton(panel,ID_SMALLSEQUENCE,wxT("Sequence of 4")),0,wxALL,SPACE_SIZE);
174        lowergrid->Add(new wxTextCtrl(panel, ID_SMALLSEQUENCETEXT),1,wxALL,SPACE_SIZE);
175        lowergrid->Add(new wxButton(panel,ID_LARGESEQUENCE,wxT("Sequence of 5")),0,wxALL,SPACE_SIZE);
176        lowergrid->Add(new wxTextCtrl(panel, ID_LARGESEQUENCETEXT),1,wxALL,SPACE_SIZE);
177        lowergrid->Add(new wxButton(panel,ID_YAHTZEE,wxT("Yahtzee")),0,wxALL,SPACE_SIZE);
178        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEETEXT),1,wxALL,SPACE_SIZE);
179        lowergrid->Add(new wxButton(panel,ID_CHANCE,wxT("Chance")),0,wxALL,SPACE_SIZE);
180        lowergrid->Add(new wxTextCtrl(panel, ID_CHANCETEXT),1,wxALL,SPACE_SIZE);
181        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Yahtzee Bonus")),0,wxALL,SPACE_SIZE);
182        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEEBONUSTEXT),1,wxALL,SPACE_SIZE);
183        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of lower section:")),0,wxALL,SPACE_SIZE);
184        lowergrid->Add(new wxTextCtrl(panel, ID_LOWERTOTAL),1,wxALL,SPACE_SIZE);
185        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Grand Total:")),0,wxALL,SPACE_SIZE);
186        lowergrid->Add(new wxTextCtrl(panel, ID_GRANDTOTAL),1,wxALL,SPACE_SIZE);
187
188        uppersection->Add(uppergrid);
189        lowersection->Add(lowergrid);
190        sectionsSizer->Add(uppersection,0,wxALL,5);
191        sectionsSizer->Add(lowersection,0,wxALL,5);
192
193        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE1,*bitmap_dices[0]),0,wxALL,3);
194        diceSizer->Add(new wxCheckBox(panel, ID_DICE1KEEP, wxT("Keep")),0,wxBOTTOM,10);
195        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE2,*bitmap_dices[1]),0,wxALL,3);
196        diceSizer->Add(new wxCheckBox(panel, ID_DICE2KEEP, wxT("Keep")),0,wxBOTTOM,10);
197        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE3,*bitmap_dices[2]),0,wxALL,3);
198        diceSizer->Add(new wxCheckBox(panel, ID_DICE3KEEP, wxT("Keep")),0,wxBOTTOM,10);
199        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE4,*bitmap_dices[3]),0,wxALL,3);
200        diceSizer->Add(new wxCheckBox(panel, ID_DICE4KEEP, wxT("Keep")),0,wxBOTTOM,10);
201        diceSizer->Add(new wxDynamicBitmap(panel,ID_DICE5,*bitmap_dices[4]),0,wxALL,3);
202        diceSizer->Add(new wxCheckBox(panel, ID_DICE5KEEP, wxT("Keep")),0,wxBOTTOM,10);
203        diceSizer->Add(new wxButton(panel, ID_ROLL, wxT("Roll")),0,wxALL,3);
204
205       
206        topSizer->Add(sectionsSizer);
207        topSizer->Add(diceSizer);       
208
209        panel->SetSizer(topSizer);
210       
211        topSizer->Fit(this);
212        topSizer->SetSizeHints(this);
213       
214        //make the text boxes uneditable to the user   
215        wxTextCtrl *textctrl;
216        for(int i = ID_ACESTEXT;i<=ID_GRANDTOTAL;i++) {
217                textctrl = (wxTextCtrl*) FindWindow(i);
218                textctrl->SetEditable(false);
219        }
220       
221        //disable the undo button
222        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
223
224
225       
226
227        /***************************************/
228        /********* Declare Event Table *********/
229        /***************************************/
230       
231        /***Connect Menu items***/
232        Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnQuit));
233        Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAbout));
234        Connect(ID_CHECK_FOR_UPDATES, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnCheckForUpdates));
235        Connect(ID_NEWGAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnNewGame));
236        Connect(ID_UNDO, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnUndo));
237        Connect(ID_SHOWHIGHSCORE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnShowHighscore));
238        Connect(ID_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSettings));
239        Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnRollButton));
240
241        Connect(ID_ACES,ID_SIXES, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnUpperButtons));
242        Connect(ID_THREEOFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On3ofakindButton));
243        Connect(ID_FOUROFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On4ofakindButton));
244        Connect(ID_FULLHOUSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnFullHouseButton));
245        Connect(ID_SMALLSEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnSmallSequenceButton));
246        Connect(ID_LARGESEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnLargeSequenceButton));
247        Connect(ID_YAHTZEE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnYahtzeeButton));
248        Connect(ID_CHANCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnChanceButton));
249       
250        /*** End of Event Table ***/
251
252        ResetRolls();
253        ClearDiceHash();
254        m_yahtzee = false;
255        m_yahtzeebonus = false;
256        m_numofplaysleft = 13;
257
258 }
259
260/*********EVENT PROCCESSING FUNCTIONS********/
261
262
263void MainFrame::OnAbout(wxCommandEvent& event)
264{
265        wxString msg;
266        wxString sqliteversion = wxString(sqlite3_version,wxConvUTF8);
267        msg.Printf(wxT("Open Yahtzee %s\nCopyright (C) 2006 by Guy Rutenberg\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);
268        msg += wxT("SQLite ") + sqliteversion;
269               
270        wxMessageBox(msg, wxT("About Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
271}
272
273void MainFrame::OnCheckForUpdates (wxCommandEvent& event){
274       
275        wxString path;
276        wxPathList path_list;
277        wxString link = wxT("http://openyahtzee.sourceforge.net/update.php?version=");
278       
279        link += wxT(OY_VERSION);
280
281        if (!wxLaunchDefaultBrowser(link)){
282                 //if builtin function doesn't work search for couple of browsers manually. see
283                 //http://linux-consulting.buanzo.com.ar/2006/05/wxwidgets-code-to-launch-browser.html
284
285                // variable declarations
286                wxArrayString browsers;
287                wxPathList path_list;
288                bool BrowserWasFound = false;
289                unsigned int i = 0;
290                wxString path;
291
292                // Add directories to wxPathList's search path from PATH environment variable
293                path_list.AddEnvList(wxT("PATH"));
294               
295                // Add browsers filenames. First item = most priority
296                browsers.Add(wxT("firefox"));
297                browsers.Add(wxT("firefox-bin"));
298                browsers.Add(wxT("mozilla"));
299                browsers.Add(wxT("mozilla-bin"));
300                browsers.Add(wxT("opera"));
301                browsers.Add(wxT("konqueror"));
302                browsers.Add(wxT("epiphany"));
303               
304                for (i = 0; i < browsers.GetCount(); i++) {
305                        path = path_list.FindAbsoluteValidPath(browsers[i]);
306                        if (path.IsEmpty()) {
307                                continue;
308                        } else {
309                                BrowserWasFound = true;
310                                break;
311                        }
312                }
313               
314                browsers.Clear();
315               
316                if (BrowserWasFound) {
317                        path += wxT(" ");
318                        path += link;
319                        ::wxExecute(path);
320                } else {
321                        wxMessageBox(wxT("No browser has been found."),wxT("OpenYahtzee"));
322                }
323        }
324
325
326}
327
328void MainFrame::OnQuit(wxCommandEvent& event)
329{
330        // Destroy the frame
331        Close();
332}
333
334void MainFrame::OnNewGame(wxCommandEvent& event)
335{
336
337        ResetRolls();
338        ClearDiceHash();
339        m_yahtzee = false;
340        m_numofplaysleft = 13;
341       
342        for (int i = ID_ACESTEXT; i<= ID_GRANDTOTAL; i++)
343                ((wxTextCtrl*) FindWindow(i))->Clear();
344        for (int i = ID_ACES; i<= ID_CHANCE; i++)
345                ((wxButton*) FindWindow(i))->Enable(true);
346}
347
348///This function handles the undo events
349void MainFrame::OnUndo(wxCommandEvent& event)
350{
351        m_rolls = m_rollsundo;
352
353        //after the user scored the button was enabled, check if it should be disabled
354        if (m_rolls <= 0) //we don't have remaining rolls
355                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
356
357        //restore the 'keep' checkboxes
358        for (int i=0; i<5; i++)
359                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
360       
361        //reset the users last choice
362        FindWindow(m_lastmove)->Enable(true);
363
364        //clear the score;
365        ((wxTextCtrl*)FindWindow(ID_ACESTEXT + (m_lastmove - ID_ACES)))->SetValue(wxT(""));
366
367        //undo also the yahtzee bonus if needed
368        if (m_yahtzeebonus) {
369                long temp;
370                wxString tempstr;
371       
372                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
373                tempstr.ToLong(&temp,10);
374                temp -= 100; //this line reduces the points given for the yahtzee bonus
375                tempstr.Printf(wxT("%i"),temp);
376                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
377        }
378
379        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
380        //cancel the counting for the choice that was canceled
381        m_numofplaysleft++;
382}
383///This function enables the undo button and stores the last move
384inline void MainFrame::EnableUndo(int id)
385{
386                (GetMenuBar()->FindItem(ID_UNDO))->Enable(true);
387                m_lastmove = id;
388}
389
390void MainFrame::OnShowHighscore(wxCommandEvent& event)
391{
392        HighScoreDialog *dialog = new HighScoreDialog(this,wxID_ANY,m_highscoredb);
393        dialog->ShowModal();
394}
395
396void MainFrame::OnSettings( wxCommandEvent& event)
397{
398        SettingsDialog *dialog = new SettingsDialog(this,wxID_ANY);
399        SettingsDialogData data;
400        std::ostringstream sstr;
401       
402        data.highscoresize = m_highscoredb->GetSize();
403
404        data.animate = (m_settingsdb->GetKey("animate")=="Yes")?true:false;
405       
406        dialog->SetData(data);
407        if(dialog->ShowModal()==wxID_OK) { //user saved Changes
408                data = dialog->GetData();
409
410                if(data.reset)
411                        m_highscoredb->SetSize(0);
412               
413                sstr<<data.highscoresize<<std::flush;
414                m_settingsdb->SetKey("highscoresize",sstr.str());
415                               
416                m_highscoredb->SetSize(data.highscoresize);
417               
418                if (data.animate){
419                        m_settingsdb->SetKey("animate","Yes");
420                       
421                } else {
422                        m_settingsdb->SetKey("animate","No");
423                }
424
425        }
426}
427
428void MainFrame::OnRollButton (wxCommandEvent& event)
429{
430        //roll the dice...
431        if (m_animate) {
432                int dice_throws[5] = {0,0,0,0,0};
433                for (int i=0; i<5; i++) { //set the number of rolls for each dice
434                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
435                                dice_throws[i] = (rand()%15)+3; //ensures the number is at least one.
436                        }
437                }
438                while (dice_throws[0] || dice_throws[1] || dice_throws[2] || dice_throws[3] || dice_throws[4]) {
439                        for (int i=0 ; i<5; i++){
440                                if(dice_throws[i]){
441                                        dice_throws[i]--;
442                                        dice[i] = rand()%6;
443                                        ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
444                                }
445                        }
446                        ::wxMilliSleep(200);
447                }
448        } else {
449                for (int i=0; i<5; i++) {
450                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
451                                dice[i] = rand()%6;
452                                ((wxStaticBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
453                        }
454                }
455        }
456       
457        //Clear old dice-hash and create a new one
458        ClearDiceHash();
459        for (int i=0; i<5; i++)
460                dicehash[dice[i]] += 1;
461
462        //if out of rolls disable the roll butoon
463        m_rolls -= 1;
464        #ifndef DEBUG
465        if (m_rolls <= 0)
466                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
467        #endif
468       
469        //enable the keep checkboxes
470        for (int i=0; i<5; i++)
471                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
472       
473        //we rolled the dices so undoing isn't allowed
474        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
475        m_yahtzeebonus = false; //if we scored yahtzee bonus before we don't care anymore.
476
477}
478
479void MainFrame::OnUpperButtons (wxCommandEvent& event)
480{
481        wxString out;
482        int temp;
483        if(m_rolls < 3){
484                YahtzeeBonus();
485                temp = dicehash[(event.GetId() - ID_ACES)] * (event.GetId() - ID_ACES + 1);
486       
487                out.Printf(wxT("%i"),temp);
488                ((wxTextCtrl*) FindWindow(event.GetId() - ID_ACES + ID_ACESTEXT))->SetValue(out);
489               
490                PostScore(event.GetId());
491        }
492        else
493                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
494}
495
496void MainFrame::On3ofakindButton(wxCommandEvent& event)
497{
498        if(m_rolls>=3) {
499                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
500                return;
501        }
502        YahtzeeBonus();
503        bool three = false;
504        wxString out;
505        int temp = 0;   
506
507        //check for the conditions of scoring
508        for (int i=0; i<6; i++)
509                if (dicehash[i] >= 3)
510                        three = true;
511        if (three){
512                for(int i = 0; i<5; i++)
513                        temp += dice[i]+1;
514       
515                out.Printf(wxT("%i"),temp);
516                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(out);
517        } else
518                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(wxT("0"));
519       
520        PostScore(event.GetId());
521}
522
523void MainFrame::On4ofakindButton(wxCommandEvent& event)
524{
525        if(m_rolls>=3) {
526                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
527                return;
528        }
529        YahtzeeBonus();
530        bool four = false;
531        wxString out;
532        int temp = 0;   
533
534        //check for the conditions of scoring
535        for (int i=0; i<6; i++)
536                if (dicehash[i] >= 4)
537                        four = true;
538        if (four){
539                for(int i = 0; i<5; i++)
540                        temp += dice[i]+1;
541       
542                out.Printf(wxT("%i"),temp);
543                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(out);
544        } else
545                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(wxT("0"));
546       
547        PostScore(event.GetId());
548}
549
550void MainFrame::OnFullHouseButton(wxCommandEvent& event)
551{
552        if(m_rolls>=3) {
553                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
554                return;
555        }
556        YahtzeeBonus();
557        bool two = false;
558        bool three = false;
559
560        //check for the conditions of scoring
561        for (int i=0; i<6; i++)
562                if (dicehash[i] == 2)
563                        two = true;
564        for (int i=0; i<6; i++)
565                if (dicehash[i] == 3)
566                        three = true;
567        if (two && three)
568                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("25"));
569        else
570                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("0"));
571       
572        PostScore(event.GetId());
573}
574
575void MainFrame::OnSmallSequenceButton(wxCommandEvent& event)
576{
577        if(m_rolls>=3) {
578                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
579                return;
580        }
581
582        YahtzeeBonus();
583        bool sequence = false;
584        //check for the conditions of scoring
585        if ( (dicehash[0]>=1 && dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1) ||
586                (dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1) ||
587                (dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1 && dicehash[5]>=1))
588                        sequence = true;
589        if (sequence)
590                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("30"));
591        else
592                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("0"));
593       
594        PostScore(event.GetId());
595}
596
597void MainFrame::OnLargeSequenceButton(wxCommandEvent& event)
598{
599        if(m_rolls>=3) {
600                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
601                return;
602        }
603
604        YahtzeeBonus();
605        bool sequence = false;
606        //check for the conditions of scoring
607        if ( (dicehash[0]==1 && dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1) ||
608                (dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1 && dicehash[5]==1))
609                        sequence = true;
610        if (sequence)
611                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("40"));
612        else
613                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("0"));
614       
615        PostScore(event.GetId());
616}
617
618void MainFrame::OnYahtzeeButton(wxCommandEvent& event)
619{
620        if(m_rolls>=3) {
621                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
622                return;
623        }
624        //give the score
625        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])){
626                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("50"));
627                m_yahtzee=true;
628        } else
629                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("0"));
630
631        PostScore(event.GetId());
632}
633
634void MainFrame::OnChanceButton (wxCommandEvent& event)
635{
636        wxString out;
637        int temp = 0;
638        if(m_rolls < 3){
639                YahtzeeBonus();
640                for(int i = 0; i<5; i++)
641                        temp += dice[i]+1;
642       
643                out.Printf(wxT("%i"),temp);
644                ((wxTextCtrl*) FindWindow(ID_CHANCETEXT))->SetValue(out);
645               
646                PostScore(event.GetId());
647        }
648        else
649                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
650
651}
652
653//********************************************
654//******        General Functions       ******
655//********************************************
656
657void MainFrame::ClearDiceHash()
658{
659        for (int i=0; i<6; i++)
660                dicehash[i] = 0;
661}
662
663void MainFrame::ResetRolls()
664{
665        m_rollsundo = m_rolls;
666        m_rolls = 3;
667        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(true);
668        for (int i=0; i<5; i++){
669                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> SetValue(false);
670                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(false);
671        }
672}
673
674void MainFrame::YahtzeeBonus()
675{
676        long temp;
677        wxString tempstr;
678       
679        //if the player didn't have any yathzees yet he can't have the bonus;
680        if (!m_yahtzee)
681                return;
682        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])) {
683                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
684                tempstr.ToLong(&temp,10);
685                temp += 100;
686                tempstr.Printf(wxT("%i"),temp);
687                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
688                m_yahtzeebonus = true;
689        }       
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        //and disable the button
782        FindWindow(id)->Enable(false);
783        m_numofplaysleft--;
784        EnableUndo(id);
785        EndofGame();
786}
Note: See TracBrowser for help on using the repository browser.