wm: mastermind-qt

Download patch

ref: 974cdc231acd964a14f2d5afeb9df247c6b2a624
parent: 6608831bcd7f4cba73d4ff672d9ee6f151c87099
author: mkf <mkf@d510>
date: Mon Apr 24 03:43:41 EDT 2023

use arc4random(), remove anotherround(), move new game stuff into action_newgame(), use QMessageBox correctly (thanks mh!), check for edge cases better, make guess checking more robust and better(?) strings, show mistakes 'X' in red and disallow resizing.

--- a/main.cpp
+++ b/main.cpp
@@ -1,10 +1,8 @@
 #include "mainwindow.h"
-#include "fn.h"
 
 #include <QMessageBox>
 #include <QInputDialog>
 #include <QApplication>
-#include <iostream> //debug
 
 using namespace std;
 // I dare not to take a look at what i've done
@@ -12,6 +10,8 @@
 // move aux funcations into a .h file,
 // try to refactor it
 // debug it, if you can
+// remove resize grip
+// XXXs in code
 
 int main(int argc, char *argv[])
 {
--- a/mainwindow.cpp
+++ b/mainwindow.cpp
@@ -1,12 +1,16 @@
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
+
 #include <QMessageBox>
 #include <QInputDialog>
 #include <QApplication>
 #include <QTreeWidgetItem>
-#include <iostream>>
-int len, goal, guess, lives;
+#include <QDialogButtonBox>
 
+int len = 0, goal = 0, guess = 0, lives = 0;
+bool win;
+
+
 /* generates a goal, n digits long */
 ulong gengoal(ulong n)
 {
@@ -14,7 +18,7 @@
     /* it's technically legal, but
     we get a int that's shorter than n */
     do
-        num = rand() % 10;
+        num = arc4random() % 10;
     while (num == 0);
 
     /* we have already generated one digit */
@@ -21,13 +25,13 @@
     while(n > 1)
     {
         num *= 10;
-        num += rand() % 10;
+        num += arc4random() % 10;
         n--;
     }
     return num;
 }
 
-uint length(ulong n)
+int length(ulong n)
 {
     ulong t = 0;
     while(n > 0)
@@ -41,7 +45,7 @@
 /* we could also use arrays for this,
     alas, that would a can of worms
     digit(number, index) = number[index] */
-int digit(long number, uint index)
+int digit(long number, int index)
 {
     if((index > length(number)) || (index < 0))
         return -1;
@@ -55,14 +59,8 @@
     }
     return j;
 }
-void anotherround()
-{
-    srand(time(nullptr));
-    switch(rand % 2)
-    {
 
-    }
-}
+
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)
     , ui(new Ui::MainWindow)
@@ -69,16 +67,7 @@
 {
     /* dear Qt, with all due respect, why you on earth
      * you try to make my life so misearable? */
-    bool res = 0;
-    do
-    {
-        len = QInputDialog::getInt(0, "MasterMind", "Enter Length of number:", 2, 2, 9, 1, &res);
-    }while(!res);
-    ui->setupUi(this);
-    lives = len;
-    goal = gengoal(len);
-    ui->lcdLives->display(lives);
-    ui->spinGuess->setMaximum(pow(10, len) - 1);
+    action_newgame();
 }
 
 MainWindow::~MainWindow()
@@ -94,7 +83,7 @@
 
 void MainWindow::action_about()
 {
-	QMessageBox::about(this, "about", "MasterMind-qt r1");
+    QMessageBox::about(this, "about", "MasterMind-qt r2");
 }
 
 void MainWindow::action_guide(){}
@@ -102,52 +91,134 @@
 // cheats menu
 void MainWindow::action_showgoal()
 {
-    QString g = QString::fromStdString("It's " + std::to_string(goal) + " you cheater");
-    QMessageBox::information(this, "Cheat", g, "k");
+    QMessageBox m;
+    m.setWindowTitle("Cheat");
+    m.setText(QString::fromStdString("It's " + std::to_string(goal) + " you cheater"));
+    m.setIcon(QMessageBox::Information);
+    m.addButton("k", QMessageBox::AcceptRole);
+    m.exec();
 }
 
-void MainWindow::action_hesoyam(){
-    lives = INT_MAX;
+void MainWindow::action_inflives(){
+    if(lives > 9)
+    {
+        QMessageBox m;
+        m.setWindowTitle("Cheat");
+        m.setText("Oi mate, it is already enabled!");
+        m.setIcon(QMessageBox::Information);
+        m.addButton("oh huh", QMessageBox::AcceptRole);
+        m.exec();
+        return;
+    }
+    else if(win)
+        return;
+    lives = 999;
+    ui->lcdLives->display(9);
+    /* if the game is over */
+    ui->buttonGuess->setEnabled(true);
+    ui->spinGuess->setEnabled(true);
+    ui->textChecked->setEnabled(true);
 }
 
 // file menu
-void MainWindow::action_newgame(){}
-void MainWindow::action_giveup(){}
 
+void MainWindow::action_newgame()
+{
+    bool res = 0;
+    do
+    {
+        len = QInputDialog::getInt(0, "MasterMind", "Enter Length of number:", 2, 2, 9, 1, &res);
+    } while(!res);
+    ui->setupUi(this);
+    lives = len;
+    goal = gengoal(len);
+    win = false;
+    ui->lcdLives->display(lives);
+    ui->spinGuess->setMaximum(pow(10, len) - 1);
+    ui->spinGuess->setMinimum(pow(10, len - 1));
+
+}
+void MainWindow::action_giveup(){
+    QMessageBox m;
+    m.setText("But-But... are you really totally 100% completely sure about it?");
+    m.setIcon(QMessageBox::Question);
+    m.addButton(QString::fromStdString("Yea"), QMessageBox::AcceptRole);
+    m.addButton(QString::fromStdString("Nay!"), QMessageBox::RejectRole);
+    switch(m.exec())
+    {
+        case QMessageBox::AcceptRole:
+            action_newgame();
+    }
+}
+
 // main window
 void MainWindow::action_guess()
 {
+    QString s;
     guess = ui->spinGuess->text().toInt();
     if(guess == goal)
+    {
+       win = true;
+       s = '#';
+       ui->textChecked->setText(s.repeated(len));
+       ui->buttonGuess->setEnabled(false);
+       ui->spinGuess->setEnabled(false);
 
-       return;
+       QMessageBox m;
+       m.setWindowTitle("YOU WIN!");
+       m.setText("I am, as your servent pleased "
+                 "to assure you, that; you have managed to win.");
+       m.setIcon(QMessageBox::Information);
+       m.addButton(QString::fromStdString("umm now what..."), QMessageBox::AcceptRole);
+       m.exec();
+       // XXX: high scores?
+    }
     else if(lives == 1)
     {
-        lives--;
         /* :( */
-        QMessageBox::critical(this, "MasterMind", "I'm afraid you have failed to achive what"
-                                                  " you have tried to do, and now we are all in"
-                                                  " brink of impading doom...", "k", "Oh no, "
-                                                  "how shall i endure such shame...");
+        ui->lcdLives->display(--lives);
+        ui->buttonGuess->setEnabled(false);
+        ui->spinGuess->setEnabled(false);
+
+        QMessageBox m;
+        m.setWindowTitle("INSERT COIN");
+        m.setText("I'm afraid you have failed to achive what"
+                  " you have tried to do, and now we are all in"
+                  " brink of impading doom...");
+        m.setIcon(QMessageBox::Critical);
+        m.addButton(QString::fromStdString("another chance?"), QMessageBox::AcceptRole);
+        m.addButton(QString::fromStdString("dices were packed! new game and i shall win!"), QMessageBox::RejectRole);
+        m.addButton(QString::fromStdString("God has forsaken me, *leaving in shame*"), QMessageBox::DestructiveRole);
+        switch(m.exec())
+        {
+            case QMessageBox::AcceptRole:
+                // XXX live++
+                break;
+            case QMessageBox::RejectRole:
+                action_newgame();
+                break;
+            case QMessageBox::DestructiveRole:
+                close();
+                break;
+        }
     }
     else
     {
         int i = length(goal) - 1;
-        QString s;
-        while(i+1 > 0)
+        while(i + 1 > 0)
         {
             if(digit(goal, i) == digit(guess, i))
                 s += '#';
             else
-                s += 'X';
+                s += "<font color=red>X</font>";
             i--;
         }
-        MainWindow::ui->textChecked->setText(s);
+        ui->textChecked->setHtml(s);
         ui->lcdLives->display(--lives);
         QTreeWidgetItem *n = new QTreeWidgetItem();
         n->setText(0, QString::fromStdString(std::to_string(len - lives)));
         n->setText(1, QString::fromStdString(std::to_string(guess)));
-        n->setText(2, s);
+        n->setText(2, ui->textChecked->toPlainText());
         ui->treeHistory->addTopLevelItem(n);
     }
 }
--- a/mainwindow.h
+++ b/mainwindow.h
@@ -26,7 +26,7 @@
 
     // cheats menu
     void action_showgoal();
-    void action_hesoyam();
+    void action_inflives();
 
     // file menu
     void action_newgame();
--- a/mainwindow.ui
+++ b/mainwindow.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>495</width>
-    <height>375</height>
+    <width>440</width>
+    <height>364</height>
    </rect>
   </property>
   <property name="sizePolicy">
@@ -16,8 +16,20 @@
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
+  <property name="minimumSize">
+   <size>
+    <width>440</width>
+    <height>364</height>
+   </size>
+  </property>
+  <property name="maximumSize">
+   <size>
+    <width>440</width>
+    <height>364</height>
+   </size>
+  </property>
   <property name="windowTitle">
-   <string>MainWindow</string>
+   <string>MasterMind</string>
   </property>
   <widget class="QWidget" name="centralwidget">
    <widget class="QTreeWidget" name="treeHistory">
@@ -28,8 +40,8 @@
      <rect>
       <x>10</x>
       <y>130</y>
-      <width>471</width>
-      <height>201</height>
+      <width>411</width>
+      <height>191</height>
      </rect>
     </property>
     <column>
@@ -69,7 +81,7 @@
      <rect>
       <x>190</x>
       <y>80</y>
-      <width>291</width>
+      <width>231</width>
       <height>41</height>
      </rect>
     </property>
@@ -107,9 +119,9 @@
    <widget class="QLabel" name="labelLives">
     <property name="geometry">
      <rect>
-      <x>130</x>
+      <x>140</x>
       <y>40</y>
-      <width>41</width>
+      <width>31</width>
       <height>21</height>
      </rect>
     </property>
@@ -138,7 +150,7 @@
      <rect>
       <x>190</x>
       <y>33</y>
-      <width>291</width>
+      <width>231</width>
       <height>41</height>
      </rect>
     </property>
@@ -164,7 +176,7 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>495</width>
+     <width>440</width>
      <height>20</height>
     </rect>
    </property>
@@ -220,7 +232,7 @@
   </action>
   <action name="actionInfinate_Lives">
    <property name="text">
-    <string>HESOYAM</string>
+    <string>HESOYAM (inf lives)</string>
    </property>
   </action>
   <action name="actionGuide">
@@ -321,6 +333,54 @@
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>actionInfinate_Lives</sender>
+   <signal>triggered()</signal>
+   <receiver>MainWindow</receiver>
+   <slot>action_inflives()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>-1</x>
+     <y>-1</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>247</x>
+     <y>187</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>actionNew_game</sender>
+   <signal>triggered()</signal>
+   <receiver>MainWindow</receiver>
+   <slot>action_newgame()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>-1</x>
+     <y>-1</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>247</x>
+     <y>187</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>actionGiveup</sender>
+   <signal>triggered()</signal>
+   <receiver>MainWindow</receiver>
+   <slot>action_giveup()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>-1</x>
+     <y>-1</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>247</x>
+     <y>187</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
  <slots>
   <slot>action_aboutqt()</slot>
@@ -328,5 +388,8 @@
   <slot>action_guide()</slot>
   <slot>action_showgoal()</slot>
   <slot>action_guess()</slot>
+  <slot>action_inflives()</slot>
+  <slot>action_newgame()</slot>
+  <slot>action_giveup()</slot>
  </slots>
 </ui>