-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
140 lines (120 loc) · 3.77 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/// File: game.cpp
/// This class is responsible for runing the game, including dealing, and bidding.
///
#include "game.h"
#include "deck.h"
#include "card.h"
#include "hand.h"
#include <iostream>
#include <iomanip>
#include <fstream>
const int NUMBER_OF_HANDS = 4;
const int MAX_NUMBER_OF_CARDS_PER_HAND = 13;
using namespace std;
/// No args constructor, initialise the dynamic array of Hand, sets dealer to NORTH.
Game::Game() {
dealer = NORTH;
players = new Hand*[NUMBER_OF_HANDS];
players[NORTH] = new Hand;
players[EAST] = new Hand;
players[SOUTH] = new Hand;
players[WEST] = new Hand;
}
/// If input is NOT to be read from a file, shuffle the deck. Call the reset method on the deck. Also clears each Hand.
void Game::setup (bool fromFile) {
if (fromFile == false) {
deck.shuffle();
}
deck.reset();
for (int hand = NORTH; hand < NUMBER_OF_HANDS; hand++) {
players[hand]->clear();
}
}
/// Deals the cards from the deck clockwise to each player in turn starting with the player on the dealer’s left.
void Game::deal() {
Card* dealtCard;
int currentPosition = dealer + 1;
for (int card = NORTH; card < MAX_NUMBER_OF_CARDS_PER_HAND; card++) {
for (int cardsDealt = 0; cardsDealt < NUMBER_OF_HANDS; cardsDealt++) {
dealtCard = deck.dealNextCard();
if (currentPosition > WEST) {
currentPosition = NORTH;
}
players[currentPosition]->addCard(dealtCard);
currentPosition++;
}
}
}
/// Starting with the dealer, calls makeBid() to see what bid is made. If a bid other than Pass is received from the current Hand, the auction can stop.
void Game::auction() {
int currentBidder = dealer;
bool bidDecided = false;
string currentBid;
for (int hand = NORTH; hand < NUMBER_OF_HANDS; hand++) {
if (bidDecided == false) {
if (currentBidder > WEST) {
currentBidder = NORTH;
}
currentBid = players[currentBidder]->makeBid();
if (currentBid != "PASS") {
bidder = currentBidder;
bidDecided = true;
}
currentBidder++;
}
}
bid = currentBid;
}
/// Increments the dealer Position to the next player.
void Game::nextDealer() {
dealer++;
if (dealer > WEST) {
dealer = NORTH;
}
}
/// Puts a string representation of the game onto the output stream.
ostream& operator<<(ostream& out, Game& game) {
int currentPosition = game.dealer;
for (int hand = NORTH; hand < NUMBER_OF_HANDS; hand++) {
if (currentPosition > WEST) {
currentPosition = NORTH;
}
string handName = game.getHandName(currentPosition);
out << handName << endl << *game.players[currentPosition] << endl;
currentPosition++;
}
if (game.bid == "PASS") {
out << "All hands PASS";
}
else {
out << "Opening bid is " << game.bid << " made by " << game.getHandName(game.bidder) <<".";
}
return out;
}
/// Passes the input stream onto the deck so that the cards can be read from a text file.
istream& operator>>(istream& in, Game& game) {
in >> game.deck;
return in;
}
/// Destructor responsible for discarding the array.
Game::~Game() {
delete[] players;
}
/// Returns the corresponding hand/player name based on their number (North, East, etc).
string Game::getHandName(int hand) {
switch(hand) {
case NORTH:
return "NORTH";
break;
case EAST:
return "EAST";
break;
case SOUTH:
return "SOUTH";
break;
case WEST:
return "WEST";
break;
}
return "error, name of hand not found";
}