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

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

made the gui much shorter so it would fit small screens

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