2 // mainwin.cpp: Implementation of the MainWin class
4 // JLH = James Hammons <jlhamm@acm.org>
7 // --- ---------- ------------------------------------------------------------
8 // JLH 03/01/2014 Created this file
16 #include <stdlib.h> // For rand()
17 #include <time.h> // For time()
18 #include "gamewidget.h" // Actual mouse/drawing window
19 //#include "resource.h"
20 //#include "optiondlg.h" // Options dialog class
21 //#include "statdlg.h" // Statistics dialog class
26 gameWidget = new GameWidget(this);
27 setCentralWidget(gameWidget);
28 gameWidget->setFocusPolicy(Qt::StrongFocus); // Without this, it gets no keys
29 setWindowTitle("Warehouse Man Deluxe");
30 setWindowIcon(QIcon(":/wmd-icon.png"));
32 newGame = CreateAction(tr("&New"), tr("New Game"), tr("Start a new game of Warehouse Man Deluxe"), QIcon(), QKeySequence(tr("ctrl+n")));
33 // connect(newGame, SIGNAL(triggered()), this, SLOT(OnNewGame()));
35 gamePlay = CreateAction(tr("&Play"), tr(""), tr(""), QIcon(), QKeySequence(tr("ctrl+a")));
36 // connect(gamePlay, SIGNAL(triggered()), this, SLOT(OnGamePlay()));
38 helpUndo = CreateAction(tr("&Undo"), tr(""), tr(""), QIcon(), QKeySequence(tr("ctrl+z")));
39 // connect(helpUndo, SIGNAL(triggered()), this, SLOT(OnHelpUndo()));
41 resetLevel = CreateAction(tr("&Reset Level"), tr("Reset Level"), tr("Start the current level over"), QIcon(), QKeySequence(tr("ctrl+r")));
42 connect(resetLevel, SIGNAL(triggered()), this, SLOT(ResetCurrentLevel()));
44 gameOptions = CreateAction(tr("&Options..."), tr("Options"), tr("Configure Warehouse Man Deluxe's options"), QIcon(), QKeySequence(tr("ctrl+o")));
45 // connect(gameOptions, SIGNAL(triggered()), this, SLOT(OnGameOptions()));
47 gameStats = CreateAction(tr("&Stats..."), tr("Game Stats"), tr("Show game stats"), QIcon(), QKeySequence(tr("ctrl+s")));
48 // connect(gameStats, SIGNAL(triggered()), this, SLOT(OnGameStats()));
50 appExit = CreateAction(tr("E&xit"), tr("Exit"), tr("Quit playing Warehouse Man Deluxe..."), QIcon(), QKeySequence(tr("ctrl+q")));
51 connect(appExit, SIGNAL(triggered()), this, SLOT(close()));
53 appAbout = CreateAction(tr("&About"), tr("About"), tr("Display program information..."), QIcon(), QKeySequence(tr("")));
54 connect(appAbout, SIGNAL(triggered()), this, SLOT(AboutGame()));
56 // Get signals from the gameboard to update scores...
57 // connect(gameBoard, SIGNAL(UpdateScore(int)), this, SLOT(OnUpdateScore(int)));
58 connect(gameWidget, SIGNAL(GameWasWon()), this, SLOT(WeHaveAWinner()));
60 QMenu * menu = menuBar()->addMenu(tr("&Game"));
61 menu->addAction(newGame);
63 menu->addAction(gamePlay);
64 menu->addAction(helpUndo);
65 menu->addAction(resetLevel);
67 menu->addAction(gameOptions);
68 menu->addAction(gameStats);
70 menu->addAction(appExit);
72 menu = menuBar()->addMenu(tr("&Help"));
73 menu->addAction(appAbout);
75 // statusBar()->addPermanentWidget(gameBoard->score);
76 statusBar()->showMessage(tr("Ready"));
78 QSettings settings("Underground Software", "Warehouse Man Deluxe");
79 QPoint mainWinPosition = settings.value("pos", QPoint(200, 100)).toPoint();
80 move(mainWinPosition);
82 /* nCardBack = settings.value("CardBack", BACK4).toInt();
83 m_nBackground = settings.value("Background", 0).toInt();
84 bSoundOn = settings.value("EnableSound", true).toBool();
85 m_bAnimationsOn = settings.value("EnableAnimations", true).toBool();
86 bAutoRemove = settings.value("AutoRemove", true).toBool();
87 scoringOn = settings.value("EnableScoring", false).toBool();
88 m_bCumulativeOn = settings.value("CumulativeScore", false).toBool();
89 m_bVegasStyle = settings.value("VegasScoring", false).toBool();
90 m_bDrawThree = settings.value("DrawThree", false).toBool();
91 m_nWins = settings.value("NumWins", 0).toInt();
92 m_nLosses = settings.value("NumLosses", 0).toInt();
93 m_nHiScore = settings.value("HiScore", 0).toInt();
94 m_nVHiScore = settings.value("VegasHiScore", 0).toInt();
95 m_nStreakW = settings.value("WinStreak", 0).toInt();
96 m_nStreakL = settings.value("LoseStreak", 0).toInt();
97 m_nStreakC = settings.value("CurrentStreak", 0).toInt();
98 m_bWonLastGame = settings.value("WonLastGame", false).toBool();
99 m_nScore = settings.value("LastScore", -52).toInt();*/
101 // gameBoard->CreateBackground();
102 // InitializeAudio();
104 // mouseDown = false;
105 // myDeck.SetCardWidth(CARD_WIDTH);
106 // myDeck.SetCardHeight(CARD_HEIGHT);
108 // for(int i=0; i<52; i++)
109 // myDeck.SetCardPosition(i, QPoint(0, 0));
112 // allowUserInput = true;
113 // m_bFreeCard = false;
114 // m_pFirstCard = NULL;
125 // Consolidates action creation from a multi-step process to a single-step one.
127 QAction * MainWin::CreateAction(QString name, QString tooltip, QString statustip,
128 QIcon icon, QKeySequence key, bool checkable/*= false*/)
130 QAction * action = new QAction(icon, name, this);
131 action->setToolTip(tooltip);
132 action->setStatusTip(statustip);
133 action->setShortcut(key);
134 action->setCheckable(checkable);
141 // This is essentially the same as the previous function, but this allows more
142 // than one key sequence to be added as key shortcuts.
144 QAction * MainWin::CreateAction(QString name, QString tooltip, QString statustip,
145 QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/)
147 QAction * action = new QAction(icon, name, this);
148 action->setToolTip(tooltip);
149 action->setStatusTip(statustip);
150 QList<QKeySequence> keyList;
151 keyList.append(key1);
152 keyList.append(key2);
153 action->setShortcuts(keyList);
154 action->setCheckable(checkable);
160 void MainWin::closeEvent(QCloseEvent * event)
162 /* if (allowUserInput && m_bTouchedGame)
164 gameBoard->HandleStatistics();
165 m_bWonLastGame = false;
168 QSettings settings("Underground Software", "Warehouse Man Deluxe");
169 settings.setValue("pos", pos());
172 settings.setValue("CardBack", nCardBack);
173 settings.setValue("Background", m_nBackground);
174 settings.setValue("EnableSound", bSoundOn);
175 settings.setValue("EnableAnimations", m_bAnimationsOn);
176 settings.setValue("AutoRemove", bAutoRemove);
177 settings.setValue("EnableScoring", scoringOn);
178 settings.setValue("CumulativeScore", m_bCumulativeOn);
179 settings.setValue("VegasScoring", m_bVegasStyle);
180 settings.setValue("DrawThree", m_bDrawThree);
181 settings.setValue("NumWins", m_nWins);
182 settings.setValue("NumLosses", m_nLosses);
183 settings.setValue("HiScore", m_nHiScore);
184 settings.setValue("VegasHiScore", m_nVHiScore);
185 settings.setValue("WinStreak", m_nStreakW);
186 settings.setValue("LoseStreak", m_nStreakL);
187 settings.setValue("CurrentStreak", m_nStreakC);
188 settings.setValue("WonLastGame", m_bWonLastGame);
189 settings.setValue("LastScore", m_nScore);*/
196 void MainWin::OnUpdateScore(int n)
204 s = QString(tr("Score: $%1")).arg(n);
206 s = QString(tr("Score: %1")).arg(n);
208 gameBoard->score->setText(s);
209 printf("OUS: n=%i, s=%s\n", n, s.toAscii().data());
213 void MainWin::ResetGame(void)
215 gameWidget->level = 1;
219 for(int i=0; i<7; i++)
220 for(int j=0; j<20; j++)
221 solBoard[i][j] = -1; // Empty the board...
224 myDeck.SetAllFaceDown();
226 for(int i=0; i<7; i++) // Fill the tableaux
228 for(int j=i; j<7; j++)
230 solBoard[6 - i][6 - j] = card;
233 myDeck.SetCardFaceUp(card);
239 for(int i=0; i<24; i++) // Fill the stack
244 nAce[0] = nAce[1] = nAce[2] = nAce[3] = -1;
246 hitStack = hitDiscard = false;
248 m_bTouchedGame = false;
254 if (!m_bCumulativeOn)
260 m_nExhaustedDeck = 0;
264 OnUpdateScore(m_nScore);*/
268 void MainWin::AboutGame(void)
270 QMessageBox::about(this, tr("About Warehouse Man Deluxe"), tr("Warehouse Man Deluxe Version 1.0\n\nCopyright © 2014 Underground Software\n\nWritten by James L. Hammons"));
274 void MainWin::WeHaveAWinner(void)
276 QMessageBox::information(this, "Warehouse Man Deluxe", "Congratulations!\nYou beat this level!\nNow try the next level!");
277 gameWidget->NextLevel();
281 void MainWin::ResetCurrentLevel(void)
283 gameWidget->ResetLevel();
288 void MainWin::OnNewGame(void)
290 if (allowUserInput && m_bTouchedGame) // Player didn't win...
292 gameBoard->HandleStatistics();
293 m_bWonLastGame = false;
298 allowUserInput = true;
303 PlaySound(IDW_CARDKNOCK);
304 gameBoard->Pause(300);
305 PlaySound(IDW_SHUFFLE);
311 void MainWin::OnGameOptions(void)
315 dlg.m_cardBack = nCardBack - BACK1;
316 dlg.m_nBackground = m_nBackground;
317 dlg.m_bCheck1 = bSoundOn;
318 dlg.m_bCheck2 = m_bAnimationsOn;
319 dlg.m_bCheck3 = bAutoRemove;
320 dlg.m_bCheck4 = scoringOn;
321 dlg.m_bCheck5 = m_bCumulativeOn;
322 dlg.m_nRadio1 = m_bVegasStyle;
323 dlg.m_nRadio3 = m_bDrawThree;
327 if (dlg.exec() == QDialog::Rejected)
332 nCardBack = dlg.m_cardBack + BACK1;
333 m_nBackground = dlg.m_nBackground;
334 bSoundOn = (bool)dlg.m_bCheck1;
335 m_bAnimationsOn = (bool)dlg.m_bCheck2;
336 bAutoRemove = (bool)dlg.m_bCheck3;
337 m_bCumulativeOn = (bool)dlg.m_bCheck5;
339 // Reset game if Vegas Style/Draw Three changes at all...
340 bool bChanged = (bool)((m_bVegasStyle != (bool)dlg.m_nRadio1)
341 || (m_bDrawThree != (bool)dlg.m_nRadio3) || (scoringOn != (bool)dlg.m_bCheck4));
343 scoringOn = (bool)dlg.m_bCheck4;
344 m_bVegasStyle = (bool)dlg.m_nRadio1;
345 m_bDrawThree = (bool)dlg.m_nRadio3;
347 if (bChanged && m_bTouchedGame)
348 OnNewGame(); // Probably should warn of this...
350 gameBoard->CreateBackground();
355 void MainWin::OnGameStats(void)
359 /* dlg.m_nWins = m_nWins;
360 dlg.m_nLosses = m_nLosses;
361 dlg.m_nHiScore = m_nHiScore;
362 dlg.m_nVHiScore = m_nVHiScore;
363 dlg.m_nStreakW = m_nStreakW;
364 dlg.m_nStreakL = m_nStreakL;
365 dlg.m_nStreakC = m_nStreakC;*/
369 // Since it's possible to reset the stats, we grab the fields back here...
370 /* m_nWins = dlg.m_nWins;
371 m_nLosses = dlg.m_nLosses;
372 m_nHiScore = dlg.m_nHiScore;
373 m_nVHiScore = dlg.m_nVHiScore;
374 m_nStreakW = dlg.m_nStreakW;
375 m_nStreakL = dlg.m_nStreakL;
376 m_nStreakC = dlg.m_nStreakC;*/
380 void MainWin::OnHelpUndo(void)
384 for(int i=0; i<7; i++)
385 for(int j=0; j<20; j++)
386 solBoard[i][j] = solBoardUndo[i][j];
388 for(int i=0; i<24; i++)
389 stack1[i] = stack1Undo[i], stack2[i] = stack2Undo[i];
391 for(int i=0; i<4; i++)
392 nAce[i] = nAceUndo[i];
394 stack1p = stack1pUndo, stack2p = stack2pUndo;
395 m_nScore = m_nScoreUndo;
396 m_nExhaustedDeck = m_nExhaustedDeckUndo;
397 m_nNumToSplay = m_nNumToSplayUndo;
398 m_bShowX = m_bShowXUndo;
400 OnUpdateScore(m_nScore);
404 gameBoard->update(); // wil wok?
408 void MainWin::OnGamePlay(void)
410 // Play a card from discard deck...
411 //NOTE: can reduce size by eliminating duplicate code in OnLButtonDown...
414 if (stack1p == -1) // I.e. it's empty...
417 if ((m_bDrawThree && (m_nExhaustedDeck == 3))
418 || (!m_bDrawThree && (m_nExhaustedDeck == 1)))
422 m_nExhaustedDeck = 0;
423 m_nScore -= (m_bDrawThree ? 20 : 100);
428 OnUpdateScore(m_nScore);
432 if (!m_bVegasStyle || (m_bVegasStyle && ((m_bDrawThree && (m_nExhaustedDeck < 3))
433 || (!m_bDrawThree && (m_nExhaustedDeck < 1)))))
435 for(; stack2p!=-1; stack2p--)
436 stack1[++stack1p] = stack2[stack2p];
440 PlaySound(IDW_CARDKNOCK);
448 for(int i=0; i<(m_bDrawThree ? 3 : 1); i++)
450 if (stack1p != -1) // I.e., there are cards left...
452 stack2[++stack2p] = stack1[stack1p--];
457 PlaySound(IDW_CARDFLIP);
459 gameBoard->Pause(55);
467 if (stack1p == -1) // This is done here because it's right!
471 if (m_bVegasStyle && ((m_bDrawThree && (m_nExhaustedDeck == 3))
472 || (!m_bDrawThree && (m_nExhaustedDeck == 1))))
478 gameBoard->HandleAutoRemove();