Skip to content

Commit 9c3cc86

Browse files
committed
initial stages in building CardGameGUI
1 parent ab65482 commit 9c3cc86

File tree

11 files changed

+439
-0
lines changed

11 files changed

+439
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
######################################################################
2+
# Automatically generated by qmake (3.0) Thu 16. May 23:33:20 2013
3+
######################################################################
4+
5+
QT += widgets gui core
6+
TEMPLATE = app
7+
CONFIG += console
8+
TARGET = playingcard
9+
INCLUDEPATH += .
10+
QMAKE_CXXFLAGS += -std=c++11
11+
12+
# Input
13+
SOURCES += carddeck.cpp \
14+
cardhand.cpp \
15+
card.cpp \
16+
cardgame-client.cpp \
17+
blackjack.cpp
18+
19+
HEADERS += \
20+
carddeck.h \
21+
cardhand.h \
22+
card.h \
23+
blackjack.h
24+
25+
RESOURCES += ../../../libs/cards2/cards2.qrc
26+
27+
FORMS += \
28+
blackjack.ui
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "blackjack.h"
2+
#include "ui_blackjack.h"
3+
4+
BlackJack::BlackJack(QWidget *parent) :
5+
QMainWindow(parent),
6+
ui(new Ui::BlackJack)
7+
{
8+
ui->setupUi(this);
9+
}
10+
11+
BlackJack::~BlackJack()
12+
{
13+
delete ui;
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef BLACKJACK_H
2+
#define BLACKJACK_H
3+
4+
#include <QMainWindow>
5+
6+
namespace Ui {
7+
class BlackJack;
8+
}
9+
10+
class BlackJack : public QMainWindow
11+
{
12+
Q_OBJECT
13+
14+
public:
15+
explicit BlackJack(QWidget *parent = 0);
16+
~BlackJack();
17+
18+
private:
19+
Ui::BlackJack *ui;
20+
};
21+
22+
#endif // BLACKJACK_H
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>BlackJack</class>
4+
<widget class="QMainWindow" name="BlackJack">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>464</width>
10+
<height>310</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralwidget">
17+
<layout class="QHBoxLayout" name="horizontalLayout_3" stretch="3,1">
18+
<item>
19+
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="1,4">
20+
<item>
21+
<layout class="QHBoxLayout" name="horizontalLayout_2">
22+
<property name="leftMargin">
23+
<number>20</number>
24+
</property>
25+
<property name="topMargin">
26+
<number>0</number>
27+
</property>
28+
<property name="rightMargin">
29+
<number>20</number>
30+
</property>
31+
<item>
32+
<widget class="QLabel" name="label_3">
33+
<property name="text">
34+
<string>Cards Left:</string>
35+
</property>
36+
</widget>
37+
</item>
38+
<item>
39+
<widget class="QLineEdit" name="lineEdit">
40+
<property name="readOnly">
41+
<bool>true</bool>
42+
</property>
43+
</widget>
44+
</item>
45+
</layout>
46+
</item>
47+
<item>
48+
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0">
49+
<item>
50+
<layout class="QVBoxLayout" name="verticalLayout_3">
51+
<item>
52+
<widget class="QLabel" name="label">
53+
<property name="text">
54+
<string>Dealer</string>
55+
</property>
56+
<property name="alignment">
57+
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
58+
</property>
59+
</widget>
60+
</item>
61+
</layout>
62+
</item>
63+
<item>
64+
<layout class="QVBoxLayout" name="verticalLayout">
65+
<item>
66+
<widget class="QLabel" name="label_2">
67+
<property name="text">
68+
<string>Player</string>
69+
</property>
70+
<property name="alignment">
71+
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
72+
</property>
73+
</widget>
74+
</item>
75+
</layout>
76+
</item>
77+
</layout>
78+
</item>
79+
</layout>
80+
</item>
81+
<item>
82+
<layout class="QVBoxLayout" name="verticalLayout_4">
83+
<item>
84+
<widget class="QPushButton" name="pushButton">
85+
<property name="text">
86+
<string>New Game</string>
87+
</property>
88+
</widget>
89+
</item>
90+
<item>
91+
<spacer name="verticalSpacer">
92+
<property name="orientation">
93+
<enum>Qt::Vertical</enum>
94+
</property>
95+
<property name="sizeType">
96+
<enum>QSizePolicy::Fixed</enum>
97+
</property>
98+
<property name="sizeHint" stdset="0">
99+
<size>
100+
<width>20</width>
101+
<height>40</height>
102+
</size>
103+
</property>
104+
</spacer>
105+
</item>
106+
<item>
107+
<widget class="QPushButton" name="pushButton_2">
108+
<property name="text">
109+
<string>Hit Me!</string>
110+
</property>
111+
</widget>
112+
</item>
113+
<item>
114+
<widget class="QPushButton" name="pushButton_3">
115+
<property name="text">
116+
<string>Stay</string>
117+
</property>
118+
</widget>
119+
</item>
120+
<item>
121+
<spacer name="verticalSpacer_2">
122+
<property name="orientation">
123+
<enum>Qt::Vertical</enum>
124+
</property>
125+
<property name="sizeHint" stdset="0">
126+
<size>
127+
<width>20</width>
128+
<height>40</height>
129+
</size>
130+
</property>
131+
</spacer>
132+
</item>
133+
</layout>
134+
</item>
135+
</layout>
136+
</widget>
137+
<widget class="QMenuBar" name="menubar">
138+
<property name="geometry">
139+
<rect>
140+
<x>0</x>
141+
<y>0</y>
142+
<width>464</width>
143+
<height>21</height>
144+
</rect>
145+
</property>
146+
<widget class="QMenu" name="menuGame">
147+
<property name="title">
148+
<string>Game</string>
149+
</property>
150+
</widget>
151+
<addaction name="menuGame"/>
152+
</widget>
153+
<widget class="QStatusBar" name="statusbar"/>
154+
</widget>
155+
<resources/>
156+
<connections/>
157+
</ui>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "card.h"
2+
#include <QStringList>
3+
4+
// conversion constructor
5+
Card::Card(int faceNbr, int suitNbr)
6+
: m_FaceNbr(faceNbr), m_SuitNbr(suitNbr){}
7+
8+
QString Card::toString() const
9+
{
10+
// return getFace() + " " + getSuit();
11+
return QString("%1 %2").arg(getFace()).arg(getSuit());
12+
}
13+
14+
// returns value of card based on rules of bridge
15+
int Card::getValue() const
16+
{
17+
switch(m_FaceNbr) {
18+
case 0: return 1; // ace default
19+
break;
20+
case 1: return 2;
21+
break;
22+
case 2: return 3;
23+
break;
24+
case 3: return 4;
25+
break;
26+
case 4: return 5;
27+
break;
28+
case 5: return 6;
29+
break;
30+
case 6: return 7;
31+
break;
32+
case 7: return 8;
33+
break;
34+
case 8: return 9;
35+
break;
36+
default : return 10;
37+
}
38+
}
39+
40+
QStringList Card::s_Faces = QStringList() << "A" << "2" << "3" << "4"
41+
<< "5" << "6" << "7" << "8"
42+
<< "9" << "T" << "J" << "Q"
43+
<< "K";
44+
45+
QStringList Card::s_Suits = QStringList() << "Clubs" << "Diamonds"
46+
<< "Hearts" << "Spades";
47+
48+
QString Card::getFace() const { return s_Faces[m_FaceNbr]; }
49+
QString Card::getSuit() const { return s_Suits[m_SuitNbr]; }
50+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef CARD_H
2+
#define CARD_H
3+
4+
#include <QString>
5+
6+
class Card
7+
{
8+
public:
9+
Card(int faceNbr, int suitNbr);
10+
QString toString() const;
11+
QString getFace() const;
12+
QString getSuit() const;
13+
int getValue() const;
14+
15+
private:
16+
int m_FaceNbr;
17+
int m_SuitNbr;
18+
static QStringList s_Faces;
19+
static QStringList s_Suits;
20+
};
21+
22+
#endif // CARD_H
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "carddeck.h"
2+
#include <QDebug>
3+
#include <cstdlib>
4+
#include <ctime>
5+
#include <memory>
6+
7+
static int const SUIT_NUM = 4;
8+
static int const FACE_NUM = 13;
9+
10+
CardDeck::CardDeck()
11+
{
12+
restoreDeck();
13+
}
14+
15+
// random takes out card and returns a hand
16+
CardHand CardDeck::deal(int handSize)
17+
{
18+
// set seed:
19+
srand(time(0));
20+
21+
std::shared_ptr<CardHand> hand(new CardHand);
22+
int randInt;
23+
24+
// if we ran out of cards:
25+
if(handSize > this->count()) {
26+
qDebug() << "\n*~ We're out of cards, restoring deck... ~*\n";
27+
restoreDeck();
28+
}
29+
30+
for(int i = 0; i < handSize; i++) {
31+
// select an index from deck:
32+
randInt = rand() % this->count();
33+
// take and remove
34+
hand->append((*this)[randInt]);
35+
this->removeAt(randInt);
36+
}
37+
return *hand;
38+
}
39+
40+
QString CardDeck::toString()
41+
{
42+
QString str;
43+
for(Card& card : *this) {
44+
str.append(card.toString() + ", ");
45+
}
46+
return str;
47+
}
48+
49+
void CardDeck::restoreDeck()
50+
{
51+
// clear deck if it's empty
52+
if(!this->isEmpty()) {
53+
this->clear();
54+
}
55+
for(int suit = 0; suit < SUIT_NUM; suit++) {
56+
for(int face = 0; face < FACE_NUM; face++) {
57+
std::shared_ptr<Card> card(new Card(face, suit));
58+
this->append(*card);
59+
}
60+
}
61+
}
62+
63+
int CardDeck::getCardsLeft() const { return this->count(); }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CARDDECK_H
2+
#define CARDDECK_H
3+
4+
#include "card.h"
5+
#include "cardhand.h"
6+
#include <QList>
7+
8+
class CardDeck : public QList <Card>
9+
{
10+
public:
11+
CardDeck();
12+
CardHand deal(int handSize);
13+
QString toString();
14+
int getCardsLeft() const;
15+
void restoreDeck();
16+
};
17+
18+
#endif // CARDDECK_H

0 commit comments

Comments
 (0)