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

Last change on this file since 38 was 38, checked in by guyru, 6 years ago

check for updates support

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