-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDisplay.cpp
More file actions
executable file
·282 lines (253 loc) · 6.7 KB
/
Display.cpp
File metadata and controls
executable file
·282 lines (253 loc) · 6.7 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/***************************************************
File: Display.h
Function: show majong card
Author: Po-Han, Chen
Version: 1.0.0
Last Modified: Sun Apr 22 23:20:38 CST 2012
Copyright: Copyright (C) 2012 Po-Han, Chen
****************************************************/
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
#include "Game.h"
#include "Player.h"
#include "Display.h"
#include "macro.h"
#include "ErrorMessage.h"
#define GRID_WIDTH 3
#define GRID_HEIGHT 5
#define GRID_HORIZONTAL 12
#define CARD_TYPE_NUM 12
#define SHOW_CARD_TOP() for ( unsigned i_ = 0; i_ < GRID_WIDTH; i_++ ) cout << "--"
const char *hashNumToChinese [10] = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
const char *hashNumToFlower [8] = { "梅", "蘭", "竹", "菊", "春", "夏", "秋", "冬" };
void
Display::operator() ( const Game &game, const List<Player>& players ) const
{
for ( unsigned i = 0; i < game.howManyPlayers(); i++ )
showPlayer( players[i] );
showSeaCards ( game.getSeaCards() );
showWallCards( game.getWallCards() );
showPrompt( game.getUsableCard() );
}
void
Display::showPlayer ( const Player &player ) const
{
#ifndef NDEBUG
START_EXCEPTION
#endif
cout << "==> Player " << player.id_ << " hand revealed card :\n";
for ( unsigned i = 0; i < GRID_HEIGHT; i++ ) {
for ( List<Majong::Card>::iterator iter = player.handRevealed_.begin();
iter != player.handRevealed_.end(); iter++ )
showCardDiagram( *iter, i );
cout << "\n";
}
cout << "hand hidden card :\n";
for ( unsigned i = 0; i < GRID_HEIGHT; i++ ) {
for ( List<Majong::Card>::iterator iter = player.handRevealed_.begin();
iter != player.handRevealed_.end(); iter++ )
showCardDiagram( *iter, i );
cout << "\n";
}
#ifndef NDEBUG
END_EXCEPTION( "Display::showPlayer ( const Player & )" );
#endif
}
void
Display::showSeaCards ( const List<Majong::Card> &seaCards ) const
{
#ifndef NDEBUG
START_EXCEPTION
#endif
cout << "==> Sea card :\n";
for ( unsigned i = 0; i < seaCards.size(); i += GRID_HORIZONTAL )
for ( unsigned j = 0; j < GRID_HEIGHT; j++ ) {
for ( unsigned k = i; k < i + GRID_HORIZONTAL && k < seaCards.size(); k++ )
showCardDiagram( seaCards[k], j );
cout << "\n";
}
#ifndef NDEBUG
END_EXCEPTION( "Display::showSeaCards ( const List<Majong::Card> & )" );
#endif
}
void
Display::showWallCards ( const List<Majong::Card> &wallCards ) const
{
#ifndef NDEBUG
START_EXCEPTION
#endif
cout << "==> Wall card :\n";
for ( unsigned i = 0; i < wallCards.size(); i += GRID_HORIZONTAL )
for ( unsigned j = 0; j < GRID_HEIGHT; j++ ) {
for ( unsigned k = i; k < i + GRID_HORIZONTAL && k < wallCards.size(); k++ )
showCardDiagram( wallCards[k], j );
cout << "\n";
}
#ifndef NDEBUG
END_EXCEPTION( "Display::showWallCards ( const List<Majong::Card> & )" );
#endif
}
void
Display::showPrompt ( const Majong::Card &usableCard ) const
{
#ifndef NDEBUG
START_EXCEPTION
#endif
cout << "==> Current usable card is :\n";
for ( unsigned i = 0; i < GRID_HEIGHT; i++ )
showCardDiagram( usableCard, i );
cout << ">>> Do what action ( Eat/Bon/Gun/Win/Draw/Pass ) ?\n"
">>> ";
#ifndef NDEBUG
END_EXCEPTION( "Display::showPrompt ( const Majong::Card & )" );
#endif
}
void
Display::showCardDiagram ( const Majong::Card &card, const unsigned &row ) const
{
#ifndef NDEBUG
START_EXCEPTION
#endif
if ( row == 0 || row == GRID_HEIGHT - 1 ) {
SHOW_CARD_TOP();
return;
}
switch ( card.type )
{
case TYPE_MILLION :
if ( row == 1)
showCardWord( ::hashNumToChinese[card.num] );
else if ( row == 2 )
showCardWord( " " );
else if ( row == 3 )
showCardWord( "萬" );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_CIRCLE :
if ( row == 1)
showCardWord( ::hashNumToChinese[card.num] );
else if ( row == 2 )
showCardWord( " " );
else if ( row == 3 )
showCardWord( "筒" );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_STRING :
if ( row == 1)
showCardWord( ::hashNumToChinese[card.num] );
else if ( row == 2 )
showCardWord( " " );
else if ( row == 3 )
showCardWord( "條" );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_N :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( "北" );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_S :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( "南" );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_E :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( "東" );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_W :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( "西" );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_M :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( "中" );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_RICH :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( "發" );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_WHITE :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( "白" );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
case TYPE_FLOWER :
if ( row == 1)
showCardWord( " " );
else if ( row == 2 )
showCardWord( ::hashNumToFlower[card.num] );
else if ( row == 3 )
showCardWord( " " );
else
throw "Invalid row num in Display::showCardDiagram() !\n";
break;
default :
throw "Invalid card type in Display::showCardDiagram() !\n";
break;
}
#ifndef NDEBUG
END_EXCEPTION( "Display::showCardDiagram ( const Majong::Card &, const unsigned & )" );
#endif
}
void
Display::showCardWord ( const char *word ) const
{
#ifndef NDEBUG
START_EXCEPTION
#endif
cout << "| ";
for ( unsigned i = 0; i < GRID_WIDTH / 3 - 1; i++ )
cout << " ";
cout<< word;
for ( unsigned i = 0; i < GRID_WIDTH / 3 - 1; i++ )
cout << " ";
cout << " |";
#ifndef NDEBUG
END_EXCEPTION( "Display::showCardWord ( const char * )" );
#endif
}