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

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

several bug fixes to the high score table

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