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

Last change on this file since 5 was 5, checked in by guyru, 7 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 KB
Line 
1/***************************************************************************
2 *   Copyright (C) 2006 by Guy Rutenberg   *
3 *   guy@Guy_Computer   *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 *                                                                         *
10 *   This program is distributed in the hope that it will be useful,       *
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13 *   GNU General Public License for more details.                          *
14 *                                                                         *
15 *   You should have received a copy of the GNU General Public License     *
16 *   along with this program; if not, write to the                         *
17 *   Free Software Foundation, Inc.,                                       *
18 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19 ***************************************************************************/
20
21 /***********************************************
22 *      This File contains the definitions      *
23 *      of MainFrame's functions                *
24 ***********************************************/
25
26//#define DEBUG
27
28#include <wx/wx.h>
29
30#include "MainFrame.h"
31#include "ObjectsID.h"
32#include "HighScoreDialog.h"
33#include <iostream>
34
35//include the images for the dice
36#include "one.xpm"
37#include "two.xpm"
38#include "three.xpm"
39#include "four.xpm"
40#include "five.xpm"
41#include "six.xpm"
42
43
44MainFrame::MainFrame(const wxString& title, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE)
45        : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, size, style)
46{
47        m_settingsdb = new SettingsDB(); //Get the settings database connection
48        m_highscoredb = new HighScoreTableDB();
49        m_highscoredb->SetSize( 20 );
50
51        bitmap_dices[0] = new wxBitmap(one_xpm);
52        bitmap_dices[1] = new wxBitmap(two_xpm);
53        bitmap_dices[2] = new wxBitmap(three_xpm);
54        bitmap_dices[3] = new wxBitmap(four_xpm);
55        bitmap_dices[4] = new wxBitmap(five_xpm);
56        bitmap_dices[5] = new wxBitmap(six_xpm);
57       
58        //randomize the random-number generator based on the time
59        srand( (unsigned)time( NULL ) );
60
61
62        /*****Create and initialize the menu bar******/
63
64        /*Create the menus*/
65        wxMenu *gameMenu = new wxMenu;  //create File menu
66        wxMenu *helpMenu = new wxMenu;  //create Help menu
67       
68        //insert menu items into menu Help
69        helpMenu->Append(wxID_ABOUT, wxT("&About...\tF1"),
70                        wxT("Show about dialog"));
71
72        //insert menu items into menu File
73        gameMenu->Append(ID_NEWGAME,wxT("&New Game\tF2"),wxT("Start a new game"));
74        gameMenu->Append(ID_SHOWHIGHSCORE,wxT("High &Scores"),wxT("Show highscores table"));
75        gameMenu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"),
76                        wxT("Quit this program"));
77
78       
79        // Declare the menu-bar and append the freshly created menus to the menu bar...
80        wxMenuBar *menuBar = new wxMenuBar();
81        menuBar->Append(gameMenu, wxT("&Game"));
82       
83        menuBar->Append(helpMenu, wxT("&Help"));
84       
85        // ... and attach this menu bar to the frame
86        SetMenuBar(menuBar);
87       
88        /***End menu-bar ***/
89        wxPanel* panel = new wxPanel(this, ID_PANEL,
90                wxDefaultPosition, wxDefaultSize);
91
92        wxBoxSizer *topSizer = new wxBoxSizer( wxHORIZONTAL );
93        wxBoxSizer *sectionsSizer = new wxBoxSizer( wxVERTICAL );
94        wxBoxSizer *diceSizer = new wxBoxSizer( wxVERTICAL );
95
96        wxSizer *uppersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Upper Section") ), wxVERTICAL);
97        wxSizer *lowersection = new wxStaticBoxSizer( new wxStaticBox( panel, wxID_ANY, wxT("Lower Section") ), wxVERTICAL);
98       
99        wxFlexGridSizer* uppergrid = new wxFlexGridSizer(9, 2, 0, 0);
100        wxFlexGridSizer* lowergrid = new wxFlexGridSizer(9, 2, 0, 0);
101
102       
103        uppergrid->Add(new wxButton(panel,ID_ACES,wxT("Aces")),0,wxALL,3);
104        uppergrid->Add(new wxTextCtrl(panel, ID_ACESTEXT),1,wxALL,3);
105        uppergrid->Add(new wxButton(panel,ID_TWOS,wxT("Twos")),0,wxALL,3);
106        uppergrid->Add(new wxTextCtrl(panel, ID_TWOSTEXT),1,wxALL,3);
107        uppergrid->Add(new wxButton(panel,ID_THREES,wxT("Threes")),0,wxALL,3);
108        uppergrid->Add(new wxTextCtrl(panel, ID_THREESTEXT),1,wxALL,3);
109        uppergrid->Add(new wxButton(panel,ID_FOURS,wxT("Fours")),0,wxALL,3);
110        uppergrid->Add(new wxTextCtrl(panel, ID_FOURSTEXT),1,wxALL,3);
111        uppergrid->Add(new wxButton(panel,ID_FIVES,wxT("Fives")),0,wxALL,3);
112        uppergrid->Add(new wxTextCtrl(panel, ID_FIVESTEXT),1,wxALL,3);
113        uppergrid->Add(new wxButton(panel,ID_SIXES,wxT("Sixes")),0,wxALL,3);
114        uppergrid->Add(new wxTextCtrl(panel, ID_SIXESTEXT),1,wxALL,3);
115        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total score:")),0,wxALL,3);
116        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERSECTIONTOTAL),1,wxALL,3);
117        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Bonus:")),0,wxALL,3);
118        uppergrid->Add(new wxTextCtrl(panel, ID_BONUS),1,wxALL,3);
119        uppergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of upper section:")),0,wxALL,3);
120        uppergrid->Add(new wxTextCtrl(panel, ID_UPPERTOTAL),1,wxALL,3);
121
122
123        lowergrid->Add(new wxButton(panel,ID_THREEOFAKIND,wxT("3 of a kind")),0,wxALL,3);
124        lowergrid->Add(new wxTextCtrl(panel, ID_THREEOFAKINDTEXT),1,wxALL,3);
125        lowergrid->Add(new wxButton(panel,ID_FOUROFAKIND,wxT("4 of a kind")),0,wxALL,3);
126        lowergrid->Add(new wxTextCtrl(panel, ID_FOUROFAKINDTEXT),1,wxALL,3);
127        lowergrid->Add(new wxButton(panel,ID_FULLHOUSE,wxT("Full House")),0,wxALL,3);
128        lowergrid->Add(new wxTextCtrl(panel, ID_FULLHOUSETEXT),1,wxALL,3);
129        lowergrid->Add(new wxButton(panel,ID_SMALLSEQUENCE,wxT("Sequence of 4")),0,wxALL,3);
130        lowergrid->Add(new wxTextCtrl(panel, ID_SMALLSEQUENCETEXT),1,wxALL,3);
131        lowergrid->Add(new wxButton(panel,ID_LARGESEQUENCE,wxT("Sequence of 5")),0,wxALL,3);
132        lowergrid->Add(new wxTextCtrl(panel, ID_LARGESEQUENCETEXT),1,wxALL,3);
133        lowergrid->Add(new wxButton(panel,ID_YAHTZEE,wxT("Yahtzee")),0,wxALL,3);
134        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEETEXT),1,wxALL,3);
135        lowergrid->Add(new wxButton(panel,ID_CHANCE,wxT("Chance")),0,wxALL,3);
136        lowergrid->Add(new wxTextCtrl(panel, ID_CHANCETEXT),1,wxALL,3);
137        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Yahtzee Bonus")),0,wxALL,3);
138        lowergrid->Add(new wxTextCtrl(panel, ID_YAHTZEEBONUSTEXT),1,wxALL,3);
139        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Total of lower section:")),0,wxALL,3);
140        lowergrid->Add(new wxTextCtrl(panel, ID_LOWERTOTAL),1,wxALL,3);
141        lowergrid->Add(new wxStaticText(panel, wxID_ANY, wxT("Grand Total:")),0,wxALL,3);
142        lowergrid->Add(new wxTextCtrl(panel, ID_GRANDTOTAL),1,wxALL,3);
143
144        uppersection->Add(uppergrid);
145        lowersection->Add(lowergrid);
146        sectionsSizer->Add(uppersection,0,wxALL,5);
147        sectionsSizer->Add(lowersection,0,wxALL,5);
148
149        diceSizer->Add(new wxStaticBitmap(panel,ID_DICE1,*bitmap_dices[0]),0,wxALL,3);
150        diceSizer->Add(new wxCheckBox(panel, ID_DICE1KEEP, wxT("Keep")),0,wxALL,3);
151        diceSizer->Add(new wxStaticBitmap(panel,ID_DICE2,*bitmap_dices[1]),0,wxALL,3);
152        diceSizer->Add(new wxCheckBox(panel, ID_DICE2KEEP, wxT("Keep")),0,wxALL,3);
153        diceSizer->Add(new wxStaticBitmap(panel,ID_DICE3,*bitmap_dices[2]),0,wxALL,3);
154        diceSizer->Add(new wxCheckBox(panel, ID_DICE3KEEP, wxT("Keep")),0,wxALL,3);
155        diceSizer->Add(new wxStaticBitmap(panel,ID_DICE4,*bitmap_dices[3]),0,wxALL,3);
156        diceSizer->Add(new wxCheckBox(panel, ID_DICE4KEEP, wxT("Keep")),0,wxALL,3);
157        diceSizer->Add(new wxStaticBitmap(panel,ID_DICE5,*bitmap_dices[4]),0,wxALL,3);
158        diceSizer->Add(new wxCheckBox(panel, ID_DICE5KEEP, wxT("Keep")),0,wxALL,3);
159        diceSizer->Add(new wxButton(panel, ID_ROLL, wxT("Roll")),0,wxALL,3);
160
161       
162        topSizer->Add(sectionsSizer);
163        topSizer->Add(diceSizer);       
164
165        panel->SetSizer(topSizer);
166       
167        topSizer->Fit(this);
168        topSizer->SetSizeHints(this);
169       
170        //make the text boxes uneditable to the user   
171        wxTextCtrl *textctrl;
172        for(int i = ID_ACESTEXT;i<=ID_GRANDTOTAL;i++) {
173                textctrl = (wxTextCtrl*) FindWindow(i);
174                textctrl->SetEditable(false);
175        }
176
177       
178
179        /***************************************/
180        /********* Declare Event Table *********/
181        /***************************************/
182       
183        /***Connect Menu items***/
184        Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnQuit));
185        Connect(wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnAbout));
186        Connect(ID_NEWGAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnNewGame));
187        Connect(ID_SHOWHIGHSCORE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MainFrame::OnShowHighscore));
188       
189        Connect(ID_ROLL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnRollButton));
190
191        for (int i=ID_ACES; i<=ID_SIXES; i++)
192                Connect(i, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnUpperButtons));
193        Connect(ID_THREEOFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On3ofakindButton));
194        Connect(ID_FOUROFAKIND, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::On4ofakindButton));
195        Connect(ID_FULLHOUSE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnFullHouseButton));
196        Connect(ID_SMALLSEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnSmallSequenceButton));
197        Connect(ID_LARGESEQUENCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnLargeSequenceButton));
198        Connect(ID_YAHTZEE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnYahtzeeButton));
199        Connect(ID_CHANCE, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (MainFrame::OnChanceButton));
200       
201        /*** End of Event Table ***/
202
203        ResetRolls();
204        ClearDiceHash();
205        m_yahtzee = false;
206        m_numofplaysleft = 13;
207
208
209
210 }
211
212/*********EVENT PROCCESSING FUNCTIONS********/
213
214
215void MainFrame::OnAbout(wxCommandEvent& event)
216{
217        wxString msg;
218        msg.Printf(wxT("OpenYahtzee"));
219               
220        wxMessageBox(msg, wxT("About Yahtzee"), wxOK | wxICON_INFORMATION, this);
221}
222
223void MainFrame::OnQuit(wxCommandEvent& event)
224{
225        // Destroy the frame
226        Close();
227}
228
229void MainFrame::OnNewGame(wxCommandEvent& event)
230{
231
232        ResetRolls();
233        ClearDiceHash();
234        m_yahtzee = false;
235        m_numofplaysleft = 13;
236       
237        for (int i = ID_ACESTEXT; i<= ID_GRANDTOTAL; i++)
238                ((wxTextCtrl*) FindWindow(i))->Clear();
239        for (int i = ID_ACES; i<= ID_CHANCE; i++)
240                ((wxButton*) FindWindow(i))->Enable(true);
241}
242
243void MainFrame::OnShowHighscore(wxCommandEvent& event)
244{
245        HighScoreDialog *dialog = new HighScoreDialog(this,wxID_ANY,m_highscoredb);
246        dialog->ShowModal();
247}
248
249void MainFrame::OnRollButton (wxCommandEvent& event)
250{
251        //roll the dices...
252        for (int i=0; i<5; i++) {
253                if (!((wxCheckBox*) FindWindow(i + ID_DICE1KEEP))->IsChecked()) {
254                        dice[i] = rand()%6;
255                        ((wxStaticBitmap*) FindWindow(i + ID_DICE1)) -> SetBitmap(*bitmap_dices[dice[i]]);
256                }
257        }
258       
259        //Clear old dice-hash and create a new one
260        ClearDiceHash();
261        for (int i=0; i<5; i++)
262                dicehash[dice[i]] += 1;
263
264        //if out of rolls disable the roll butoon
265        m_rolls -= 1;
266        #ifndef DEBUG
267        if (m_rolls <= 0)
268                ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
269        #endif
270       
271        //enable the keep checkboxes
272        for (int i=0; i<5; i++)
273                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(true);
274
275}
276
277void MainFrame::OnUpperButtons (wxCommandEvent& event)
278{
279        wxString out;
280        int temp;
281        if(m_rolls < 3){
282                YahtzeeBonus();
283                temp = dicehash[(event.GetId() - ID_ACES)] * (event.GetId() - ID_ACES + 1);
284       
285                out.Printf(wxT("%i"),temp);
286                ((wxTextCtrl*) FindWindow(event.GetId() - ID_ACES + ID_ACESTEXT))->SetValue(out);
287               
288                //now after the scoring reset the rolls
289                ResetRolls();
290                //and disable the button
291                FindWindow(event.GetId())->Enable(false);
292                m_numofplaysleft--;
293                EndofGame();
294        }
295        else
296                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
297}
298
299void MainFrame::On3ofakindButton(wxCommandEvent& event)
300{
301        if(m_rolls>=3) {
302                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
303                return;
304        }
305        YahtzeeBonus();
306        bool three = false;
307        wxString out;
308        int temp = 0;   
309
310        //check for the conditions of scoring
311        for (int i=0; i<6; i++)
312                if (dicehash[i] >= 3)
313                        three = true;
314        if (three){
315                for(int i = 0; i<5; i++)
316                        temp += dice[i]+1;
317       
318                out.Printf(wxT("%i"),temp);
319                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(out);
320        } else
321                ((wxTextCtrl*) FindWindow(ID_THREEOFAKINDTEXT))->SetValue(wxT("0"));
322       
323        //now after the scoring reset the rolls
324        ResetRolls();
325        //and disable the button
326        FindWindow(event.GetId())->Enable(false);
327        m_numofplaysleft--;
328        EndofGame();
329}
330
331void MainFrame::On4ofakindButton(wxCommandEvent& event)
332{
333        if(m_rolls>=3) {
334                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
335                return;
336        }
337        YahtzeeBonus();
338        bool four = false;
339        wxString out;
340        int temp = 0;   
341
342        //check for the conditions of scoring
343        for (int i=0; i<6; i++)
344                if (dicehash[i] >= 4)
345                        four = true;
346        if (four){
347                for(int i = 0; i<5; i++)
348                        temp += dice[i]+1;
349       
350                out.Printf(wxT("%i"),temp);
351                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(out);
352        } else
353                ((wxTextCtrl*) FindWindow(ID_FOUROFAKINDTEXT))->SetValue(wxT("0"));
354       
355        //now after the scoring reset the rolls
356        ResetRolls();
357        //and disable the button
358        FindWindow(event.GetId())->Enable(false);
359        m_numofplaysleft--;
360        EndofGame();
361}
362
363void MainFrame::OnFullHouseButton(wxCommandEvent& event)
364{
365        if(m_rolls>=3) {
366                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
367                return;
368        }
369        YahtzeeBonus();
370        bool two = false;
371        bool three = false;
372
373        //check for the conditions of scoring
374        for (int i=0; i<6; i++)
375                if (dicehash[i] == 2)
376                        two = true;
377        for (int i=0; i<6; i++)
378                if (dicehash[i] == 3)
379                        three = true;
380        if (two && three)
381                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("25"));
382        else
383                ((wxTextCtrl*) FindWindow(ID_FULLHOUSETEXT))->SetValue(wxT("0"));
384       
385        //now after the scoring reset the rolls
386        ResetRolls();
387        //and disable the button
388        FindWindow(event.GetId())->Enable(false);
389        m_numofplaysleft--;
390        EndofGame();
391}
392
393void MainFrame::OnSmallSequenceButton(wxCommandEvent& event)
394{
395        if(m_rolls>=3) {
396                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
397                return;
398        }
399
400        YahtzeeBonus();
401        bool sequence = false;
402        //check for the conditions of scoring
403        if ( (dicehash[0]>=1 && dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1) ||
404                (dicehash[1]>=1 && dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1) ||
405                (dicehash[2]>=1 && dicehash[3]>=1 && dicehash[4]>=1 && dicehash[5]>=1))
406                        sequence = true;
407        if (sequence)
408                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("30"));
409        else
410                ((wxTextCtrl*) FindWindow(ID_SMALLSEQUENCETEXT))->SetValue(wxT("0"));
411       
412        //now after the scoring reset the rolls
413        ResetRolls();
414        //and disable the button
415        FindWindow(event.GetId())->Enable(false);
416        m_numofplaysleft--;
417        EndofGame();
418}
419
420void MainFrame::OnLargeSequenceButton(wxCommandEvent& event)
421{
422        if(m_rolls>=3) {
423                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
424                return;
425        }
426
427        YahtzeeBonus();
428        bool sequence = false;
429        //check for the conditions of scoring
430        if ( (dicehash[0]==1 && dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1) ||
431                (dicehash[1]==1 && dicehash[2]==1 && dicehash[3]==1 && dicehash[4]==1 && dicehash[5]==1))
432                        sequence = true;
433        if (sequence)
434                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("40"));
435        else
436                ((wxTextCtrl*) FindWindow(ID_LARGESEQUENCETEXT))->SetValue(wxT("0"));
437       
438        //now after the scoring reset the rolls
439        ResetRolls();
440        //and disable the button
441        FindWindow(event.GetId())->Enable(false);
442        m_numofplaysleft--;
443        EndofGame();
444}
445
446void MainFrame::OnYahtzeeButton(wxCommandEvent& event)
447{
448        if(m_rolls>=3) {
449                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
450                return;
451        }
452        //give the score
453        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])){
454                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("50"));
455                m_yahtzee=true;
456        } else
457                ((wxTextCtrl*) FindWindow(ID_YAHTZEETEXT))->SetValue(wxT("0"));
458
459        FindWindow(event.GetId())->Enable(false);
460        m_numofplaysleft--;
461        ResetRolls();
462        EndofGame();
463}
464
465void MainFrame::OnChanceButton (wxCommandEvent& event)
466{
467        wxString out;
468        int temp = 0;
469        if(m_rolls < 3){
470                YahtzeeBonus();
471                for(int i = 0; i<5; i++)
472                        temp += dice[i]+1;
473       
474                out.Printf(wxT("%i"),temp);
475                ((wxTextCtrl*) FindWindow(ID_CHANCETEXT))->SetValue(out);
476               
477                //now after the scoring reset the rolls
478                ResetRolls();
479                //and disable the button
480                FindWindow(event.GetId())->Enable(false);
481                m_numofplaysleft--;
482                EndofGame();
483        }
484        else
485                wxMessageBox(wxT("First you need to roll, and after you roll you may score"), wxT("OpenYahtzee"), wxOK | wxICON_INFORMATION, this);
486
487}
488
489//********************************************
490//******        General Functions       ******
491//********************************************
492
493void MainFrame::ClearDiceHash()
494{
495        for (int i=0; i<6; i++)
496                dicehash[i] = 0;
497}
498
499void MainFrame::ResetRolls()
500{
501        m_rolls = 3;
502        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(true);
503        for (int i=0; i<5; i++){
504                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> SetValue(false);
505                ((wxCheckBox*) FindWindow(i + ID_DICE1KEEP)) -> Enable(false);
506        }
507}
508
509void MainFrame::YahtzeeBonus()
510{
511        long temp;
512        wxString tempstr;
513       
514        //if the player didn't have any yathzees yet he can't have the bonus;
515        if (!m_yahtzee)
516                return;
517        if ((dice[0]==dice[1]) && (dice[1]==dice[2]) && (dice[1]==dice[3]) && (dice[1]==dice[4])) {
518                tempstr = ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> GetValue();
519                tempstr.ToLong(&temp,10);
520                temp += 100;
521                tempstr.Printf(wxT("%i"),temp);
522                ((wxTextCtrl*) FindWindow(ID_YAHTZEEBONUSTEXT)) -> SetValue(tempstr);
523
524        }       
525}
526
527void MainFrame::EndofGame()
528{
529        if(m_numofplaysleft>0)
530                return;
531       
532        wxString tempstr;
533        long temp;
534        long upperscore = 0;
535        long lowerscore = 0;
536
537        for (int i = ID_ACESTEXT; i<=ID_SIXESTEXT; i++){
538                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
539                tempstr.ToLong(&temp,10);
540                upperscore +=temp;
541        }
542       
543        tempstr.Printf(wxT("%i"),upperscore);
544        ((wxTextCtrl*) FindWindow(ID_UPPERSECTIONTOTAL)) -> SetValue(tempstr);
545       
546        //check for bonus
547        if(upperscore>=63) {
548                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("35"));
549                upperscore +=35;
550        } else
551                ((wxTextCtrl*) FindWindow(ID_BONUS)) -> SetValue(wxT("0"));
552       
553        tempstr.Printf(wxT("%i"),upperscore);
554        ((wxTextCtrl*) FindWindow(ID_UPPERTOTAL)) -> SetValue(tempstr);
555       
556        //calculate total on lower section
557        for (int i = ID_THREEOFAKINDTEXT; i<=ID_YAHTZEEBONUSTEXT; i++) {
558                tempstr = ((wxTextCtrl*) FindWindow(i)) -> GetValue();
559                tempstr.ToLong(&temp,10);
560                lowerscore +=temp;
561        }
562       
563        tempstr.Printf(wxT("%i"),lowerscore);
564        ((wxTextCtrl*) FindWindow(ID_LOWERTOTAL)) -> SetValue(tempstr);
565        tempstr.Printf(wxT("%i"),upperscore + lowerscore);
566        ((wxTextCtrl*) FindWindow(ID_GRANDTOTAL)) -> SetValue(tempstr);
567
568        //disable the roll button;
569        ((wxButton*) FindWindow(ID_ROLL)) -> Enable(false);
570
571        tempstr.Printf(wxT("Your final score is %i points!"),lowerscore+upperscore);
572        wxMessageBox(tempstr, wxT("Game Ended"), wxOK | wxICON_INFORMATION, this);
573
574        //submit to high score
575        HighScoreHandler(lowerscore+upperscore);
576       
577}
578
579void MainFrame::HighScoreHandler(int score)
580{
581        int place;
582        std::string name,date;
583        wxCommandEvent newevent;
584
585
586        place = m_highscoredb->IsHighScore(score);
587       
588        if(!place)  //if the score didn't make it to the highscore table do nothing
589                return;
590        HighScoreInfo *infodialog = new HighScoreInfo(this,place);
591        infodialog->ShowModal();
592
593        name = infodialog->GetName().mb_str();
594
595        //get the date
596        wxDateTime now = wxDateTime::Now();
597        date = now.FormatISOTime().mb_str();
598        date +=" ";
599        date += now.FormatDate().mb_str();
600
601        m_highscoredb->SendHighScore(name,date,score);
602
603        //now show the high score table
604        newevent.SetId(ID_SHOWHIGHSCORE);
605        newevent.SetEventType(wxEVT_COMMAND_MENU_SELECTED);
606        ProcessEvent(newevent);
607
608}
Note: See TracBrowser for help on using the repository browser.