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

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

* empty log message *

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