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

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

implemented the Yahtzee Joker scoring rules.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.2 KB
RevLine 
[15]1// $Header$
[5]2/***************************************************************************
3 *   Copyright (C) 2006 by Guy Rutenberg   *
[17]4 *   guyrutenberg@gmail.com   *
[5]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
[55]27#define DEBUG
[5]28
29#include <wx/wx.h>
30
31#include "MainFrame.h"
[47]32#include "wxDynamicBitmap.h"
[5]33#include "ObjectsID.h"
34#include "HighScoreDialog.h"
[9]35#include "SettingsDialog.h"
[5]36#include <iostream>
[10]37#include <sstream>
[9]38#include <cstdlib>
[13]39#include <wx/version.h>
[5]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
[31]49//include the icon file
[38]50#include "Icon.h"
[31]51
[28]52//default values
[14]53#define SPACE_SIZE 1
[42]54#define DEF_HIGHSCORESIZE 20
[46]55#define OY_VERSION "1.7.0"
[5]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{
[31]60        //give the frame an icon
[38]61        SetIcon(wxIcon(ICON));
[33]62
[38]63
[28]64        std::ostringstream sstr;
65
[5]66        m_settingsdb = new SettingsDB(); //Get the settings database connection
67        m_highscoredb = new HighScoreTableDB();
68
[46]69        //DATABASE initialization
70        if (m_settingsdb->GetKey("highscoresize") == "") { //check if we need to create a new high score table
[28]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        }
[46]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        }
[28]88
[46]89        // END Database initialization
90
[5]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       
[20]108       
[5]109        //insert menu items into menu Help
[38]110        helpMenu->Append(ID_CHECK_FOR_UPDATES, wxT("&Check for Updates"),
111                        wxT("Check for new version of the game via the web"));
[5]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"));
[20]117        //create the undo button and make it disabled
[21]118        gameMenu->Append(ID_UNDO,wxT("&Undo"),wxT("Undo the last move"));
[9]119        gameMenu->Append(ID_SHOWHIGHSCORE,wxT("High &Scores"),wxT("Show high-scores table"));
120        gameMenu->Append(ID_SETTINGS,wxT("Settings"),wxT("Show settings dialog"));
[5]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       
[14]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);
[5]165
166
[14]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);
[49]182        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEEBONUSTEXT),1,wxALL,SPACE_SIZE);
[14]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);
[5]187
188        uppersection->Add(uppergrid);
189        lowersection->Add(lowergrid);
190        sectionsSizer->Add(uppersection,0,wxALL,5);
191        sectionsSizer->Add(lowersection,0,wxALL,5);
192
[47]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);
[5]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        }
[21]220       
221        //disable the undo button
222        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
[5]223
[21]224
[5]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));
[38]234        Connect(ID_CHECK_FOR_UPDATES, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnCheckForUpdates));
[5]235        Connect(ID_NEWGAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnNewGame));
[20]236        Connect(ID_UNDO, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnUndo));
[5]237        Connect(ID_SHOWHIGHSCORE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnShowHighscore));
[9]238        Connect(ID_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnSettings));
[5]239        Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnRollButton));
240
[51]241        Connect(ID_ACES,ID_SIXES, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnUpperButtons));
[5]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;
[49]255        m_yahtzeebonus = false;
[5]256        m_numofplaysleft = 13;
257
258 }
259
260/*********EVENT PROCCESSING FUNCTIONS********/
261
262
263void MainFrame::OnAbout(wxCommandEvent& event)
264{
265        wxString msg;
[13]266        wxString sqliteversion = wxString(sqlite3_version,wxConvUTF8);
[42]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);
[13]268        msg += wxT("SQLite ") + sqliteversion;
[5]269               
[42]270        wxMessageBox(msg, wxT("About Open Yahtzee"), wxOK | wxICON_INFORMATION, this);
[5]271}
272
[38]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
[5]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
[20]348///This function handles the undo events
349void MainFrame::OnUndo(wxCommandEvent& event)
350{
351        m_rolls = m_rollsundo;
352
[21]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);
[20]356
[21]357        //restore the 'keep' checkboxes
358        for (int i=0; i<5; i++)
359                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
360       
[20]361        //reset the users last choice
362        FindWindow(m_lastmove)->Enable(true);
[21]363
[20]364        //clear the score;
365        ((wxTextCtrl*)FindWindow(ID_ACESTEXT + (m_lastmove - ID_ACES)))->SetValue(wxT(""));
[21]366
[49]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
[21]379        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
380        //cancel the counting for the choice that was canceled
381        m_numofplaysleft++;
[20]382}
383///This function enables the undo button and stores the last move
384inline void MainFrame::EnableUndo(int id)
385{
[53]386        if (m_numofplaysleft) {
[21]387                (GetMenuBar()->FindItem(ID_UNDO))->Enable(true);
[20]388                m_lastmove = id;
[53]389        }
[20]390}
391
[5]392void MainFrame::OnShowHighscore(wxCommandEvent& event)
393{
394        HighScoreDialog *dialog = new HighScoreDialog(this,wxID_ANY,m_highscoredb);
395        dialog->ShowModal();
396}
397
[9]398void MainFrame::OnSettings( wxCommandEvent& event)
399{
400        SettingsDialog *dialog = new SettingsDialog(this,wxID_ANY);
401        SettingsDialogData data;
[10]402        std::ostringstream sstr;
[9]403       
[28]404        data.highscoresize = m_highscoredb->GetSize();
[46]405
406        data.animate = (m_settingsdb->GetKey("animate")=="Yes")?true:false;
[9]407       
[10]408        dialog->SetData(data);
[9]409        if(dialog->ShowModal()==wxID_OK) { //user saved Changes
[10]410                data = dialog->GetData();
411
412                if(data.reset)
413                        m_highscoredb->SetSize(0);
414               
415                sstr<<data.highscoresize<<std::flush;
416                m_settingsdb->SetKey("highscoresize",sstr.str());
417                               
418                m_highscoredb->SetSize(data.highscoresize);
[46]419               
420                if (data.animate){
421                        m_settingsdb->SetKey("animate","Yes");
422                       
423                } else {
424                        m_settingsdb->SetKey("animate","No");
425                }
426
[9]427        }
428}
429
[5]430void MainFrame::OnRollButton (wxCommandEvent& event)
431{
[47]432        //roll the dice...
433        if (m_animate) {
434                int dice_throws[5] = {0,0,0,0,0};
435                for (int i=0; i<5; i++) { //set the number of rolls for each dice
436                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
[50]437                                dice_throws[i] = (rand()%15)+3; //ensures the number is at least one.
[47]438                        }
[5]439                }
[47]440                while (dice_throws[0] || dice_throws[1] || dice_throws[2] || dice_throws[3] || dice_throws[4]) {
441                        for (int i=0 ; i<5; i++){
442                                if(dice_throws[i]){
443                                        dice_throws[i]--;
444                                        dice[i] = rand()%6;
445                                        ((wxDynamicBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
446                                }
447                        }
[50]448                        ::wxMilliSleep(200);
[47]449                }
450        } else {
451                for (int i=0; i<5; i++) {
452                        if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
453                                dice[i] = rand()%6;
454                                ((wxStaticBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
455                        }
456                }
[5]457        }
458       
459        //Clear old dice-hash and create a new one
460        ClearDiceHash();
461        for (int i=0; i<5; i++)
462                dicehash[dice[i]] += 1;
463
464        //if out of rolls disable the roll butoon
465        m_rolls -= 1;
466        #ifndef DEBUG
467        if (m_rolls <= 0)
468                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
469        #endif
470       
471        //enable the keep checkboxes
472        for (int i=0; i<5; i++)
473                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
[21]474       
475        //we rolled the dices so undoing isn't allowed
476        (GetMenuBar()->FindItem(ID_UNDO))->Enable(false);
[49]477        m_yahtzeebonus = false; //if we scored yahtzee bonus before we don't care anymore.
[5]478
479}
480
481void MainFrame::OnUpperButtons (wxCommandEvent& event)
482{
483        wxString out;
484        int temp;
485        if(m_rolls < 3){
486                YahtzeeBonus();
487                temp = dicehash[(event.GetId() - ID_ACES)] * (event.GetId() - ID_ACES + 1);
488       
489                out.Printf(wxT("%i"),temp);
490                ((wxTextCtrl*) FindWindow(event.GetId() - ID_ACES + ID_ACESTEXT))->SetValue(out);
491               
[37]492                PostScore(event.GetId());
[5]493        }
494        else
495                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
496}
497
498void MainFrame::On3ofakindButton(wxCommandEvent& event)
499{
500        if(m_rolls>=3) {
501                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
502                return;
503        }
504        YahtzeeBonus();
505        bool three = false;
506        wxString out;
507        int temp = 0;   
508
509        //check for the conditions of scoring
510        for (int i=0; i<6; i++)
511                if (dicehash[i] >= 3)
512                        three = true;
513        if (three){
514                for(int i = 0; i<5; i++)
515                        temp += dice[i]+1;
516       
517                out.Printf(wxT("%i"),temp);
518                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(out);
519        } else
520                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(wxT("0"));
521       
[37]522        PostScore(event.GetId());
[5]523}
524
525void MainFrame::On4ofakindButton(wxCommandEvent& event)
526{
527        if(m_rolls>=3) {
528                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
529                return;
530        }
531        YahtzeeBonus();
532        bool four = false;
533        wxString out;
534        int temp = 0;   
535
536        //check for the conditions of scoring
537        for (int i=0; i<6; i++)
538                if (dicehash[i] >= 4)
539                        four = true;
540        if (four){
541                for(int i = 0; i<5; i++)
542                        temp += dice[i]+1;
543       
544                out.Printf(wxT("%i"),temp);
545                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(out);
546        } else
547                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(wxT("0"));
548       
[37]549        PostScore(event.GetId());
[5]550}
551
552void MainFrame::OnFullHouseButton(wxCommandEvent& event)
553{
554        if(m_rolls>=3) {
555                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
556                return;
557        }
558        YahtzeeBonus();
559        bool two = false;
560        bool three = false;
561
562        //check for the conditions of scoring
563        for (int i=0; i<6; i++)
564                if (dicehash[i] == 2)
565                        two = true;
566        for (int i=0; i<6; i++)
567                if (dicehash[i] == 3)
568                        three = true;
[55]569        if ((two && three) || YahtzeeJoker())
[5]570                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("25"));
571        else
572                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("0"));
573       
[37]574        PostScore(event.GetId());
[5]575}
576
577void MainFrame::OnSmallSequenceButton(wxCommandEvent& event)
578{
579        if(m_rolls>=3) {
580                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
581                return;
582        }
583
584        YahtzeeBonus();
585        bool sequence = false;
586        //check for the conditions of scoring
587        if ( (dicehash[0]>=1 && dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1) ||
588                (dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1) ||
589                (dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1 && dicehash[5]>=1))
590                        sequence = true;
[55]591        if (sequence || YahtzeeJoker())
[5]592                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("30"));
593        else
594                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("0"));
595       
[37]596        PostScore(event.GetId());
[5]597}
598
599void MainFrame::OnLargeSequenceButton(wxCommandEvent& event)
600{
601        if(m_rolls>=3) {
602                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
603                return;
604        }
605
606        YahtzeeBonus();
607        bool sequence = false;
608        //check for the conditions of scoring
609        if ( (dicehash[0]==1 && dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1) ||
610                (dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1 && dicehash[5]==1))
611                        sequence = true;
[55]612        if (sequence || YahtzeeJoker())
[5]613                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("40"));
614        else
615                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("0"));
616       
[37]617        PostScore(event.GetId());
[5]618}
619
620void MainFrame::OnYahtzeeButton(wxCommandEvent& event)
621{
622        if(m_rolls>=3) {
623                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
624                return;
625        }
626        //give the score
627        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])){
628                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("50"));
629                m_yahtzee=true;
630        } else
631                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("0"));
632
[37]633        PostScore(event.GetId());
[5]634}
635
636void MainFrame::OnChanceButton (wxCommandEvent& event)
637{
638        wxString out;
639        int temp = 0;
640        if(m_rolls < 3){
641                YahtzeeBonus();
642                for(int i = 0; i<5; i++)
643                        temp += dice[i]+1;
644       
645                out.Printf(wxT("%i"),temp);
646                ((wxTextCtrl*) FindWindow(ID_CHANCETEXT))->SetValue(out);
647               
[37]648                PostScore(event.GetId());
[5]649        }
650        else
651                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
652
653}
654
655//********************************************
656//******        General Functions       ******
657//********************************************
658
659void MainFrame::ClearDiceHash()
660{
661        for (int i=0; i<6; i++)
662                dicehash[i] = 0;
663}
664
665void MainFrame::ResetRolls()
666{
[20]667        m_rollsundo = m_rolls;
[5]668        m_rolls = 3;
669        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(true);
670        for (int i=0; i<5; i++){
671                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> SetValue(false);
672                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(false);
673        }
674}
675
676void MainFrame::YahtzeeBonus()
677{
678        long temp;
679        wxString tempstr;
680       
681        //if the player didn't have any yathzees yet he can't have the bonus;
682        if (!m_yahtzee)
683                return;
684        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])) {
685                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
686                tempstr.ToLong(&temp,10);
687                temp += 100;
688                tempstr.Printf(wxT("%i"),temp);
689                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
[49]690                m_yahtzeebonus = true;
[5]691        }       
692}
693
[55]694bool MainFrame::YahtzeeJoker()
695{
696        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())) {
697                return true;
698        }
699        return false;
700}
701
[5]702void MainFrame::EndofGame()
703{
704        if(m_numofplaysleft>0)
705                return;
706       
707        wxString tempstr;
708        long temp;
709        long upperscore = 0;
710        long lowerscore = 0;
711
712        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
713                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
714                tempstr.ToLong(&temp,10);
715                upperscore +=temp;
716        }
717       
718        tempstr.Printf(wxT("%i"),upperscore);
719        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
720       
721        //check for bonus
722        if(upperscore>=63) {
723                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("35"));
724                upperscore +=35;
725        } else
726                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("0"));
727       
728        tempstr.Printf(wxT("%i"),upperscore);
729        ((wxTextCtrl*) FindWindow(ID_UPPERTOTAL)) -> SetValue(tempstr);
730       
731        //calculate total on lower section
732        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
733                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
734                tempstr.ToLong(&temp,10);
735                lowerscore +=temp;
736        }
737       
738        tempstr.Printf(wxT("%i"),lowerscore);
739        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
740        tempstr.Printf(wxT("%i"),upperscore + lowerscore);
741        ((wxTextCtrl*) FindWindow(ID_GRANDTOTAL)) -> SetValue(tempstr);
742
743        //disable the roll button;
744        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
745
746        tempstr.Printf(wxT("Your final score is %i points!"),lowerscore+upperscore);
747        wxMessageBox(tempstr, wxT("Game Ended"), wxOK | wxICON_INFORMATION, this);
748
749        //submit to high score
750        HighScoreHandler(lowerscore+upperscore);
751       
752}
753
754void MainFrame::HighScoreHandler(int score)
755{
756        int place;
757        std::string name,date;
758        wxCommandEvent newevent;
759
760
761        place = m_highscoredb->IsHighScore(score);
762       
763        if(!place)  //if the score didn't make it to the highscore table do nothing
764                return;
765        HighScoreInfo *infodialog = new HighScoreInfo(this,place);
766        infodialog->ShowModal();
767
768        name = infodialog->GetName().mb_str();
769
770        //get the date
771        wxDateTime now = wxDateTime::Now();
772        date = now.FormatISOTime().mb_str();
773        date +=" ";
774        date += now.FormatDate().mb_str();
775
776        m_highscoredb->SendHighScore(name,date,score);
777
778        //now show the high score table
779        newevent.SetId(ID_SHOWHIGHSCORE);
780        newevent.SetEventType(wxEVT_COMMAND_MENU_SELECTED);
781        ProcessEvent(newevent);
782
783}
[37]784
785///this function handles all the post scoring stuff such as disabling the right button.
786void MainFrame::PostScore(int id)
787{
788        //now after the scoring reset the rolls
789        ResetRolls();
790
791        //and disable the button
792        FindWindow(id)->Enable(false);
793        m_numofplaysleft--;
794        EnableUndo(id);
795        EndofGame();
796}
Note: See TracBrowser for help on using the repository browser.