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

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

undo button (no functioning yet)

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