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

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

fixed icon issue under windows

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