Skip to content

Commit c01224c

Browse files
committed
Added Qt (C++) source code
1 parent 9a640d2 commit c01224c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+5536
-0
lines changed

Qt/LICENCE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2014 Ivailo Georgiev
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Qt/console/Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
CFLAGS=-c -Wall
2+
LDFLAGS=
3+
SOURCES= card.cpp iboard.cpp inotification.cpp abstract_player.cpp settings.cpp \
4+
game.cpp console_board.cpp console_notification.cpp console_player.cpp \
5+
computer_player.cpp santase.cpp
6+
OBJECTS=$(SOURCES:.cpp=.o)
7+
EXECUTABLE=csantase
8+
9+
all: $(SOURCES) $(EXECUTABLE)
10+
11+
$(EXECUTABLE): $(OBJECTS)
12+
$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
13+
14+
.cpp.o:
15+
$(CXX) $(CFLAGS) $< -o $@
16+
17+
clean:
18+
rm -rf *.o $(EXECUTABLE)

Qt/console/abstract_player.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include "abstract_player.h"
2+
#include "game.h"
3+
4+
AbstractPlayer::AbstractPlayer(Game *game, string name)
5+
{
6+
// cout << "Creating AbstractPlayer" << endl;
7+
this->game = game;
8+
this->name = name;
9+
isChoosing = false;
10+
clearCards();
11+
}
12+
13+
AbstractPlayer::~AbstractPlayer()
14+
{
15+
clearCards();
16+
// cout << "Destroying AbstractPlayer" << endl;
17+
}
18+
19+
void AbstractPlayer::clearCards()
20+
{
21+
cards.clear();
22+
takenCards.clear();
23+
pairs.clear();
24+
}
25+
26+
string AbstractPlayer::getName()
27+
{
28+
return name;
29+
}
30+
31+
void AbstractPlayer::setName(string name)
32+
{
33+
this->name = name;
34+
}
35+
36+
vector<Card *> & AbstractPlayer::getCards()
37+
{
38+
return cards;
39+
}
40+
41+
vector<Card *> & AbstractPlayer::getTakenCards()
42+
{
43+
return takenCards;
44+
}
45+
46+
void AbstractPlayer::setTakenCards(vector<Card *> & takenCards)
47+
{
48+
this->takenCards = takenCards;
49+
}
50+
51+
void AbstractPlayer::addCard(Card *card)
52+
{
53+
cards.push_back(card);
54+
}
55+
56+
void AbstractPlayer::removeCard(Card *card)
57+
{
58+
removeItem(cards, card);
59+
}
60+
61+
void AbstractPlayer::addTakenCard(Card *card)
62+
{
63+
takenCards.push_back(card);
64+
}
65+
66+
void AbstractPlayer::addPair(Suits pairSuit)
67+
{
68+
pairs.push_back(pairSuit);
69+
}
70+
71+
int AbstractPlayer::getScore()
72+
{
73+
int score = 0;
74+
for (int i=0; i<takenCards.size(); i++)
75+
{
76+
Card *card = takenCards.at(i);
77+
if (card)
78+
{
79+
score += card->getCard();
80+
}
81+
}
82+
Suits trump = game->getTrump();
83+
for (int i=0; i<pairs.size(); i++)
84+
{
85+
Suits suit = pairs.at(i);
86+
if (suit == trump)
87+
{
88+
score += SCORE_TRUMP_PAIR;
89+
}
90+
else
91+
{
92+
score += SCORE_PAIR;
93+
}
94+
}
95+
return score;
96+
}
97+
98+
bool AbstractPlayer::getIsChoosing()
99+
{
100+
return isChoosing;
101+
}
102+
103+
void AbstractPlayer::play()
104+
{
105+
// using current cards
106+
// call game.nextMove(PlayCard)
107+
sort(cards.begin(), cards.end(), cmpCard);
108+
stringstream ss;
109+
ss.str("");
110+
ss << *this << " is in turn to play.";
111+
if (game)
112+
{
113+
game->getNotification()->info(ss.str());
114+
}
115+
if (game && game->getBoard())
116+
{
117+
game->getBoard()->print();
118+
}
119+
}
120+
121+
ostream & operator<<(ostream & os, AbstractPlayer & player)
122+
{
123+
os << player.getName();
124+
return os;
125+
}

Qt/console/abstract_player.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef _ABSTRACT_PLAYER_H
2+
#define _ABSTRACT_PLAYER_H
3+
4+
#include "common.h"
5+
#include "card.h"
6+
7+
class Game;
8+
class AbstractPlayer
9+
{
10+
public:
11+
AbstractPlayer(Game *game, string name);
12+
virtual ~AbstractPlayer();
13+
void clearCards();
14+
string getName();
15+
void setName(string name);
16+
vector <Card *> & getCards();
17+
vector <Card *> & getTakenCards();
18+
void setTakenCards(vector<Card *> & takenCards);
19+
void addCard(Card *card);
20+
void removeCard(Card *card);
21+
void addTakenCard(Card *takenCard);
22+
void addPair(Suits pairSuit);
23+
int getScore();
24+
bool getIsChoosing();
25+
virtual void play() = 0;
26+
protected:
27+
Game *game;
28+
string name;
29+
vector<Card *> cards;
30+
vector<Card *> takenCards;
31+
vector<Suits> pairs;
32+
bool isChoosing;
33+
};
34+
ostream & operator<<(ostream & os, AbstractPlayer & player);
35+
36+
#endif

Qt/console/card.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "card.h"
2+
3+
Card::Card(Cards c, Suits s)
4+
{
5+
// cout << "Constructing Card" << endl;
6+
this->c = c;
7+
this->s = s;
8+
}
9+
10+
Card::~Card()
11+
{
12+
// cout << "Destroying Card" << endl;
13+
}
14+
15+
Cards Card::getCard()
16+
{
17+
return c;
18+
}
19+
20+
Suits Card::getSuit()
21+
{
22+
return s;
23+
}
24+
25+
ostream & operator<<(ostream & os, Card & card)
26+
{
27+
Cards c = card.getCard();
28+
Suits s = card.getSuit();
29+
30+
switch (c)
31+
{
32+
case NINE:
33+
os << "9";
34+
break;
35+
case JACK:
36+
os << "J";
37+
break;
38+
case QUEEN:
39+
os << "Q";
40+
break;
41+
case KING:
42+
os << "K";
43+
break;
44+
case TEN:
45+
os << "10";
46+
break;
47+
case ACE:
48+
os << "A";
49+
break;
50+
default:
51+
os << "(unknown)";
52+
}
53+
54+
switch (s)
55+
{
56+
case CLUB:
57+
os << "\u2663(sp)"; //"\u2667";
58+
break;
59+
case DIAMOND:
60+
os << "\u2666(ka)"; //"\u2662";
61+
break;
62+
case HEART:
63+
os << "\u2665(ku)"; //"\u2661";
64+
break;
65+
case SPADE:
66+
os << "\u2660(pi)"; //"\u2664";
67+
break;
68+
default:
69+
os << "(unknown)";
70+
}
71+
72+
return os;
73+
}
74+
75+
bool cmpCard(Card *a, Card *b)
76+
{
77+
Cards ca = a->getCard();
78+
Suits sa = a->getSuit();
79+
Cards cb = b->getCard();
80+
Suits sb = b->getSuit();
81+
82+
if (sa == sb)
83+
{
84+
return ca < cb;
85+
}
86+
else
87+
{
88+
return sa < sb;
89+
}
90+
}
91+
92+
bool cmpNoSuit(Card *a, Card *b)
93+
{
94+
Cards ca = a->getCard();
95+
Cards cb = b->getCard();
96+
return ca < cb;
97+
}
98+

Qt/console/card.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _CARD_H
2+
#define _CARD_H
3+
4+
#include "common.h"
5+
6+
class Card
7+
{
8+
public:
9+
Card(Cards c, Suits s);
10+
virtual ~Card();
11+
Cards getCard();
12+
Suits getSuit();
13+
14+
private:
15+
Cards c;
16+
Suits s;
17+
};
18+
ostream & operator<<(ostream & os, Card & card);
19+
20+
bool cmpCard(Card *a, Card *b);
21+
bool cmpNoSuit(Card *a, Card *b);
22+
23+
#endif

Qt/console/common.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef _COMMON_H
2+
#define _COMMON_H
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <string>
7+
#include <algorithm>
8+
#include <sstream>
9+
10+
#include <cstdlib>
11+
#include <ctime>
12+
13+
using namespace std;
14+
15+
#define CARDS_IN_HAND 6
16+
17+
#define SCORE_TRUMP_PAIR 40
18+
#define SCORE_PAIR 20
19+
#define HALF_SCORE 33
20+
#define FINAL_SCORE 66
21+
22+
enum Suits { CLUB, DIAMOND, HEART, SPADE }; // spatia, karo, kupa, pika
23+
enum Cards { NINE = 0, JACK = 2, QUEEN = 3, KING = 4, TEN = 10, ACE = 11 }; // devet, vale, dama, pop, deset, aso
24+
25+
// utility
26+
template <typename T> void removeItem(vector<T *> &vect, T *obj)
27+
{
28+
typename vector<T *>::iterator newEnd = remove(vect.begin(), vect.end(), obj);
29+
vect.erase(newEnd, vect.end());
30+
}
31+
32+
template <typename T> void removePos(vector<T> & vect, int pos)
33+
{
34+
vect.erase(vect.begin() + pos);
35+
}
36+
37+
template <typename T> void removeAll(vector<T> & v1, vector<T> & v2)
38+
{
39+
vector<T> diff;
40+
set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), inserter(diff, diff.begin()) );
41+
v1 = diff;
42+
}
43+
44+
template <typename T> bool vecContains(vector<T> & vect, T obj)
45+
{
46+
typename vector<T>::iterator iter = find(vect.begin(), vect.end(), obj);
47+
return (iter != vect.end());
48+
}
49+
50+
#endif
51+

0 commit comments

Comments
 (0)