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

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

new dice design

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.1 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\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"), 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
311        ResetRolls();
312        ClearDiceHash();
313        m_yahtzee = false;
314        m_numofplaysleft = 13;
315       
316        for (int i = ID_ACESTEXT; i<= ID_GRANDTOTAL; i++)
317                ((wxTextCtrl*) FindWindow(i))->Clear();
318        for (int i = ID_ACES; i<= ID_CHANCE; i++)
319                ((wxButton*) FindWindow(i))->Enable(true);
320}
321
322///This function handles the undo events
323void MainFrame::OnUndo(wxCommandEvent& event)
324{
325        m_rolls = m_rollsundo;
326
327        //after the user scored the button was enabled, check if it should be disabled
328        if (m_rolls <= 0) //we don't have remaining rolls
329                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
330
331        //restore the 'keep' checkboxes
332        for (int i=0; i<5; i++)
333                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
334       
335        //reset the users last choice
336        FindWindow(m_lastmove)->Enable(true);
337
338        //clear the score;
339        ((wxTextCtrl*)FindWindow(ID_ACESTEXT + (m_lastmove - ID_ACES)))->SetValue(wxT(""));
340
341        //undo also the yahtzee bonus if needed
342        if (m_yahtzeebonus) {
343                long temp;
344                wxString tempstr;
345       
346                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
347                tempstr.ToLong(&temp,10);
348                temp -= 100; //this line reduces the points given for the yahtzee bonus
349                tempstr.Printf(wxT("%i"),temp);
350                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
351        }
352
353        //recalculate the subtotals
354        CalculateSubTotal();
355
356        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
357        //cancel the counting for the choice that was canceled
358        m_numofplaysleft++;
359}
360///This function enables the undo button and stores the last move
361inline void MainFrame::EnableUndo(int id)
362{
363        if (m_numofplaysleft) {
364                (GetMenuBar()->FindItem(ID_UNDO))->Enable(true);
365                m_lastmove = id;
366        }
367}
368
369void MainFrame::OnShowHighscore(wxCommandEvent& event)
370{
371        HighScoreDialog *dialog = new HighScoreDialog(this,wxID_ANY,m_highscoredb);
372        dialog->ShowModal();
373}
374
375void MainFrame::OnSettings( wxCommandEvent& event)
376{
377        SettingsDialog *dialog = new SettingsDialog(this,wxID_ANY);
378        SettingsDialogData data;
379        std::ostringstream sstr;
380       
381        data.highscoresize = m_highscoredb->GetSize();
382
383        data.animate = (m_settingsdb->GetKey("animate")=="Yes")?true:false;
384        data.subtotal = (m_settingsdb->GetKey("calculatesubtotal")=="Yes")?true:false;
385       
386        dialog->SetData(data);
387        if(dialog->ShowModal()==wxID_OK) { //user saved Changes
388                data = dialog->GetData();
389
390                if(data.reset)
391                        m_highscoredb->SetSize(0);
392               
393                sstr<<data.highscoresize<<std::flush;
394                m_settingsdb->SetKey("highscoresize",sstr.str());
395                               
396                m_highscoredb->SetSize(data.highscoresize);
397               
398                if (data.animate){
399                        m_settingsdb->SetKey("animate","Yes");
400                        m_animate = true;
401                       
402                } else {
403                        m_settingsdb->SetKey("animate","No");
404                        m_animate = false;
405                }
406                if (data.subtotal){
407                        m_settingsdb->SetKey("calculatesubtotal","Yes");
408                        m_calculatesubtotal = true;
409                       
410                } else {
411                        m_settingsdb->SetKey("calculatesubtotal","No");
412                        m_calculatesubtotal = false;
413                }
414
415        }
416}
417
418void MainFrame::OnRollButton (wxCommandEvent& event)
419{
420        //roll the dice...
421        if (m_animate) {
422                int dice_throws[5] = {0,0,0,0,0};
423                for (int i=0; i<5; i++) { //set the number of rolls for each dice
424                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
425                                dice_throws[i] = (rand()%15)+3; //ensures the number is at least one.
426                        }
427                }
428                while (dice_throws[0] || dice_throws[1] || dice_throws[2] || dice_throws[3] || dice_throws[4]) {
429                        for (int i=0 ; i<5; i++){
430                                if(dice_throws[i]){
431                                        dice_throws[i]--;
432                                        dice[i] = rand()%6;
433                                        ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
434                                }
435                        }
436                        ::wxMilliSleep(100);
437                }
438        } else {
439                for (int i=0; i<5; i++) {
440                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
441                                dice[i] = rand()%6;
442                                ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
443                        }
444                }
445        }
446       
447        //Clear old dice-hash and create a new one
448        ClearDiceHash();
449        for (int i=0; i<5; i++)
450                dicehash[dice[i]] += 1;
451
452        //if out of rolls disable the roll butoon
453        m_rolls -= 1;
454        #ifndef DEBUG
455        if (m_rolls <= 0)
456                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
457        #endif
458       
459        //enable the keep checkboxes
460        for (int i=0; i<5; i++)
461                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
462       
463        //we rolled the dices so undoing isn't allowed
464        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
465        m_yahtzeebonus = false; //if we scored yahtzee bonus before we don't care anymore.
466
467}
468
469void MainFrame::OnUpperButtons (wxCommandEvent& event)
470{
471        wxString out;
472        int temp;
473        if(m_rolls < 3){
474                YahtzeeBonus();
475                temp = dicehash[(event.GetId() - ID_ACES)] * (event.GetId() - ID_ACES + 1);
476       
477                out.Printf(wxT("%i"),temp);
478                ((wxTextCtrl*) FindWindow(event.GetId() - ID_ACES + ID_ACESTEXT))->SetValue(out);
479               
480                PostScore(event.GetId());
481        }
482        else
483                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
484}
485
486void MainFrame::On3ofakindButton(wxCommandEvent& event)
487{
488        if(m_rolls>=3) {
489                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
490                return;
491        }
492        YahtzeeBonus();
493        bool three = false;
494        wxString out;
495        int temp = 0;   
496
497        //check for the conditions of scoring
498        for (int i=0; i<6; i++)
499                if (dicehash[i] >= 3)
500                        three = true;
501        if (three){
502                for(int i = 0; i<5; i++)
503                        temp += dice[i]+1;
504       
505                out.Printf(wxT("%i"),temp);
506                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(out);
507        } else
508                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(wxT("0"));
509       
510        PostScore(event.GetId());
511}
512
513void MainFrame::On4ofakindButton(wxCommandEvent& event)
514{
515        if(m_rolls>=3) {
516                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
517                return;
518        }
519        YahtzeeBonus();
520        bool four = false;
521        wxString out;
522        int temp = 0;   
523
524        //check for the conditions of scoring
525        for (int i=0; i<6; i++)
526                if (dicehash[i] >= 4)
527                        four = true;
528        if (four){
529                for(int i = 0; i<5; i++)
530                        temp += dice[i]+1;
531       
532                out.Printf(wxT("%i"),temp);
533                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(out);
534        } else
535                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(wxT("0"));
536       
537        PostScore(event.GetId());
538}
539
540void MainFrame::OnFullHouseButton(wxCommandEvent& event)
541{
542        if(m_rolls>=3) {
543                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
544                return;
545        }
546        YahtzeeBonus();
547        bool two = false;
548        bool three = false;
549
550        //check for the conditions of scoring
551        for (int i=0; i<6; i++)
552                if (dicehash[i] == 2)
553                        two = true;
554        for (int i=0; i<6; i++)
555                if (dicehash[i] == 3)
556                        three = true;
557        if ((two && three) || YahtzeeJoker())
558                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("25"));
559        else
560                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("0"));
561       
562        PostScore(event.GetId());
563}
564
565void MainFrame::OnSmallSequenceButton(wxCommandEvent& event)
566{
567        if(m_rolls>=3) {
568                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
569                return;
570        }
571
572        YahtzeeBonus();
573        bool sequence = false;
574        //check for the conditions of scoring
575        if ( (dicehash[0]>=1 && dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1) ||
576                (dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1) ||
577                (dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1 && dicehash[5]>=1))
578                        sequence = true;
579        if (sequence || YahtzeeJoker())
580                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("30"));
581        else
582                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("0"));
583       
584        PostScore(event.GetId());
585}
586
587void MainFrame::OnLargeSequenceButton(wxCommandEvent& event)
588{
589        if(m_rolls>=3) {
590                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
591                return;
592        }
593
594        YahtzeeBonus();
595        bool sequence = false;
596        //check for the conditions of scoring
597        if ( (dicehash[0]==1 && dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1) ||
598                (dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1 && dicehash[5]==1))
599                        sequence = true;
600        if (sequence || YahtzeeJoker())
601                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("40"));
602        else
603                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("0"));
604       
605        PostScore(event.GetId());
606}
607
608void MainFrame::OnYahtzeeButton(wxCommandEvent& event)
609{
610        if(m_rolls>=3) {
611                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
612                return;
613        }
614        //give the score
615        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])){
616                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("50"));
617                m_yahtzee=true;
618        } else
619                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("0"));
620
621        PostScore(event.GetId());
622}
623
624void MainFrame::OnChanceButton (wxCommandEvent& event)
625{
626        wxString out;
627        int temp = 0;
628        if(m_rolls < 3){
629                YahtzeeBonus();
630                for(int i = 0; i<5; i++)
631                        temp += dice[i]+1;
632       
633                out.Printf(wxT("%i"),temp);
634                ((wxTextCtrl*) FindWindow(ID_CHANCETEXT))->SetValue(out);
635               
636                PostScore(event.GetId());
637        }
638        else
639                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
640
641}
642
643//********************************************
644//******        General Functions       ******
645//********************************************
646
647void MainFrame::ClearDiceHash()
648{
649        for (int i=0; i<6; i++)
650                dicehash[i] = 0;
651}
652
653void MainFrame::ResetRolls()
654{
655        m_rollsundo = m_rolls;
656        m_rolls = 3;
657        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(true);
658        for (int i=0; i<5; i++){
659                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> SetValue(false);
660                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(false);
661        }
662}
663
664void MainFrame::YahtzeeBonus()
665{
666        long temp;
667        wxString tempstr;
668       
669        //if the player didn't have any yathzees yet he can't have the bonus;
670        if (!m_yahtzee)
671                return;
672        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])) {
673                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
674                tempstr.ToLong(&temp,10);
675                temp += 100;
676                tempstr.Printf(wxT("%i"),temp);
677                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
678                m_yahtzeebonus = true;
679        }       
680}
681
682bool MainFrame::YahtzeeJoker()
683{
684        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())) {
685                return true;
686        }
687        return false;
688}
689
690void MainFrame::EndofGame()
691{
692        if(m_numofplaysleft>0)
693                return;
694       
695        wxString tempstr;
696        long temp;
697        long upperscore = 0;
698        long lowerscore = 0;
699
700        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
701                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
702                tempstr.ToLong(&temp,10);
703                upperscore +=temp;
704        }
705       
706        tempstr.Printf(wxT("%i"),upperscore);
707        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
708       
709        //check for bonus
710        if(upperscore>=63) {
711                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("35"));
712                upperscore +=35;
713        } else
714                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("0"));
715       
716        tempstr.Printf(wxT("%i"),upperscore);
717        ((wxTextCtrl*) FindWindow(ID_UPPERTOTAL)) -> SetValue(tempstr);
718       
719        //calculate total on lower section
720        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
721                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
722                tempstr.ToLong(&temp,10);
723                lowerscore +=temp;
724        }
725       
726        tempstr.Printf(wxT("%i"),lowerscore);
727        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
728        tempstr.Printf(wxT("%i"),upperscore + lowerscore);
729        ((wxTextCtrl*) FindWindow(ID_GRANDTOTAL)) -> SetValue(tempstr);
730
731        //disable the roll button;
732        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
733
734        tempstr.Printf(wxT("Your final score is %i points!"),lowerscore+upperscore);
735        wxMessageBox(tempstr, wxT("Game Ended"), wxOK | wxICON_INFORMATION, this);
736
737        //submit to high score
738        HighScoreHandler(lowerscore+upperscore);
739       
740}
741
742void MainFrame::HighScoreHandler(int score)
743{
744        int place;
745        std::string name,date;
746        wxCommandEvent newevent;
747
748
749        place = m_highscoredb->IsHighScore(score);
750       
751        if(!place)  //if the score didn't make it to the highscore table do nothing
752                return;
753        HighScoreInfo *infodialog = new HighScoreInfo(this,place);
754        infodialog->ShowModal();
755
756        name = infodialog->GetName().mb_str();
757
758        //get the date
759        wxDateTime now = wxDateTime::Now();
760        date = now.FormatISOTime().mb_str();
761        date +=" ";
762        date += now.FormatDate().mb_str();
763
764        m_highscoredb->SendHighScore(name,date,score);
765
766        //now show the high score table
767        newevent.SetId(ID_SHOWHIGHSCORE);
768        newevent.SetEventType(wxEVT_COMMAND_MENU_SELECTED);
769        ProcessEvent(newevent);
770
771}
772
773///this function handles all the post scoring stuff such as disabling the right button.
774void MainFrame::PostScore(int id)
775{
776        //now after the scoring reset the rolls
777        ResetRolls();
778
779        CalculateSubTotal();
780
781        //and disable the button
782        FindWindow(id)->Enable(false);
783        m_numofplaysleft--;
784        EnableUndo(id);
785        EndofGame();
786}
787
788void MainFrame::CalculateSubTotal()
789{
790        if (!m_calculatesubtotal)
791                return;
792        long upperscore = 0;
793        long lowerscore = 0;
794        wxString tempstr;
795        long temp;
796
797
798        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
799                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
800                tempstr.ToLong(&temp,10);
801                upperscore +=temp;
802        }
803       
804        tempstr.Printf(wxT("%i"),upperscore);
805        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
806
807        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
808                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
809                tempstr.ToLong(&temp,10);
810                lowerscore +=temp;
811        }
812       
813        tempstr.Printf(wxT("%i"),lowerscore);
814        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
815}
816
817void MainFrame::LaunchBrowser (wxString link){
818        if (!wxLaunchDefaultBrowser(link)){
819                 //if builtin function doesn't work search for couple of browsers manually. see
820                 //http://linux-consulting.buanzo.com.ar/2006/05/wxwidgets-code-to-launch-browser.html
821
822                // variable declarations
823                wxArrayString browsers;
824                wxPathList path_list;
825                bool BrowserWasFound = false;
826                unsigned int i = 0;
827                wxString path;
828
829                // Add directories to wxPathList's search path from PATH environment variable
830                path_list.AddEnvList(wxT("PATH"));
831               
832                // Add browsers filenames. First item = most priority
833                browsers.Add(wxT("firefox"));
834                browsers.Add(wxT("firefox-bin"));
835                browsers.Add(wxT("mozilla"));
836                browsers.Add(wxT("mozilla-bin"));
837                browsers.Add(wxT("opera"));
838                browsers.Add(wxT("konqueror"));
839                browsers.Add(wxT("epiphany"));
840               
841                for (i = 0; i < browsers.GetCount(); i++) {
842                        path = path_list.FindAbsoluteValidPath(browsers[i]);
843                        if (path.IsEmpty()) {
844                                continue;
845                        } else {
846                                BrowserWasFound = true;
847                                break;
848                        }
849                }
850               
851                browsers.Clear();
852               
853                if (BrowserWasFound) {
854                        path += wxT(" ");
855                        path += link;
856                        ::wxExecute(path);
857                } else {
858                        wxMessageBox(wxT("No browser has been found."),wxT("OpenYahtzee"));
859                }
860        }
861}
Note: See TracBrowser for help on using the repository browser.