File indexing completed on 2022-09-20 13:02:50

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by Albert Astals Cid <aacid@kde.org>               *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  ***************************************************************************/
0009 
0010 #include "diceswidget.h"
0011 
0012 #include <cstdlib>
0013 
0014 #include <QPainter>
0015 #include <QMouseEvent>
0016 #include <QStyleOptionViewItem>
0017 
0018 #include <QRandomGenerator>
0019 #include <QStandardPaths>
0020 
0021 
0022 dicesWidget::dicesWidget(QWidget *parent) : QWidget(parent)
0023 {
0024     m_highlightDice[0] =
0025     m_highlightDice[1] =
0026     m_highlightDice[2] =
0027     m_highlightDice[3] =
0028     m_highlightDice[4] = 0;
0029     setMinimumSize(90, 450);
0030 
0031     m_images[0] = QPixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("images/dice-none.png")));
0032     m_images[1] = QPixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("images/dice-1.png")));
0033     m_images[2] = QPixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("images/dice-2.png")));
0034     m_images[3] = QPixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("images/dice-3.png")));
0035     m_images[4] = QPixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("images/dice-4.png")));
0036     m_images[5] = QPixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("images/dice-5.png")));
0037     m_images[6] = QPixmap(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("images/dice-6.png")));
0038 
0039     rollAll();
0040 }
0041 
0042 void dicesWidget::setEnabled(bool enabled)
0043 {
0044     m_enabled = enabled;
0045     update();
0046 }
0047 
0048 void dicesWidget::rollAll()
0049 {
0050     for (int i = 0; i < 5; i++) m_rollDice[i] = true;
0051     roll();
0052 }
0053 
0054 bool dicesWidget::roll()
0055 {
0056     bool b;
0057     b = generateDices();
0058     if (b) update();
0059     return b;
0060 }
0061 
0062 int dicesWidget::getDice(int dice) const
0063 {
0064     return m_dice[dice];
0065 }
0066 
0067 void dicesWidget::selectDice(int dice, bool select)
0068 {
0069     m_rollDice[dice] = select;
0070 }
0071 
0072 void dicesWidget::highlightDice(int dice, bool highlight)
0073 {
0074     m_highlightDice[dice] = highlight;
0075 }
0076 
0077 int dicesWidget::getOnes() const
0078 {
0079     int n = getSimilar(1);
0080     return n;
0081 }
0082 
0083 int dicesWidget::getTwos() const
0084 {
0085     int n = getSimilar(2);
0086     return n * 2;
0087 }
0088 
0089 int dicesWidget::getThrees() const
0090 {
0091     int n = getSimilar(3);
0092     return n * 3;
0093 }
0094 
0095 int dicesWidget::getFours() const
0096 {
0097     int n = getSimilar(4);
0098     return n * 4;
0099 }
0100 
0101 int dicesWidget::getFives() const
0102 {
0103     int n = getSimilar(5);
0104     return n * 5;
0105 }
0106 
0107 int dicesWidget::getSixs() const
0108 {
0109     int n = getSimilar(6);
0110     return n * 6;
0111 }
0112 
0113 int dicesWidget::getThreeOfAKind() const
0114 {
0115     return getNOfKind(3);
0116 }
0117 
0118 int dicesWidget::getFourOfAKind() const
0119 {
0120     return getNOfKind(4);
0121 }
0122 
0123 int dicesWidget::getFullHouse() const
0124 {
0125     int i;
0126     bool three, two;
0127     three = false;
0128     i = 0;
0129     while (!three && i <= 6)
0130     {
0131         if (getSimilar(i) == 3) three = true;
0132         else i++;
0133     }
0134     if (three)
0135     {
0136         two = false;
0137         i = 0;
0138         while (!two && i <= 6)
0139         {
0140             if (getSimilar(i) == 2) two = true;
0141             else i++;
0142         }
0143         if (two) return 25;
0144     }
0145     return 0;
0146 }
0147 
0148 int dicesWidget::getSStraight() const
0149 {
0150     if (getSimilar(1) && getSimilar(2) && getSimilar(3) && getSimilar(4)) return 30;
0151     if (getSimilar(2) && getSimilar(3) && getSimilar(4) && getSimilar(5)) return 30;
0152     if (getSimilar(3) && getSimilar(4) && getSimilar(5) && getSimilar(6)) return 30;
0153     return 0;
0154 }
0155 
0156 int dicesWidget::getLStraight() const
0157 {
0158     if (getSimilar(1) && getSimilar(2) && getSimilar(3) && getSimilar(4) && getSimilar(5)) return 40;
0159     if (getSimilar(2) && getSimilar(3) && getSimilar(4) && getSimilar(5) && getSimilar(6)) return 40;
0160     return 0;
0161 }
0162 
0163 int dicesWidget::getKiriki() const
0164 {
0165     if (getNOfKind(5) != 0) return 50;
0166     else return 0;
0167 }
0168 
0169 int dicesWidget::totalSum() const
0170 {
0171     return m_dice[0] + m_dice[1] + m_dice[2] + m_dice[3] + m_dice[4];
0172 }
0173 
0174 void dicesWidget::paintEvent(QPaintEvent *)
0175 {
0176     QPainter p(this);
0177     for (int i = 0; i < 5; i++)
0178     {
0179         QPixmap pixmap(m_rollDice[i] ? m_images[0] : m_images[m_dice[i]]);
0180         // TODO need suggestions
0181         // if (!m_enabled) pixmap = KPixmapEffect::toGray(pixmap, false);
0182         if (m_highlightDice[i])
0183         {
0184             QStyleOptionViewItem option;
0185             option.initFrom(this);
0186             option.rect = QRect(5, 9 + 90 * i, 80, 80);
0187             option.state |= QStyle::State_Selected;
0188             option.showDecorationSelected = true;
0189             style()->drawControl(QStyle::CE_ItemViewItem, &option, &p);
0190         }
0191         p.drawPixmap(5, 10 + (10 + 80) * i, pixmap);
0192     }
0193 }
0194 
0195 void dicesWidget::mousePressEvent(QMouseEvent *e)
0196 {
0197     if (!m_enabled) return;
0198 
0199     int x = e -> x();
0200     int y = e -> y();
0201 
0202     if (x > 5 && x < 85 && y > 10)
0203     {
0204         y -= 10;
0205         y = y / 90;
0206         m_rollDice[y] = !m_rollDice[y];
0207         update();
0208     }
0209 }
0210 
0211 bool dicesWidget::generateDices()
0212 {
0213     bool any = false;
0214     for (int i = 0; i < 5; i++)
0215     {
0216         if (m_rollDice[i])
0217         {
0218             m_dice[i] = 1 + QRandomGenerator::global()->bounded(6);
0219             m_rollDice[i] = false;
0220             any = true;
0221         }
0222     }
0223     return any;
0224 }
0225 
0226 int dicesWidget::getSimilar(int number) const
0227 {
0228     int n = 0;
0229     for (int i = 0; i < 5; i++)
0230     {
0231         if (m_dice[i] == number) n++;
0232     }
0233     return n;
0234 }
0235 
0236 int dicesWidget::getNOfKind(int number) const
0237 {
0238     int i = 1;
0239     bool have = false;
0240     while (!have && i <= 6)
0241     {
0242         if (getSimilar(i) >= number) have = true;
0243         i++;
0244     }
0245     if (have) return totalSum();
0246     else return 0;
0247 }