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

Last change on this file since 13 was 13, checked in by guyru, 8 years ago

updated about info

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