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

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

started dice animation

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