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