본문으로 바로가기

Simple BlackJack

category C&C++ 2017. 11. 12. 21:27
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include <string>
#include "stdafx.h"
#include <iostream>
#include <vector>
#ifndef CARD_H
#define CARD_H
using namespace std;
class Card {
private:
    string pattern;
    string name;
    int value;
public:
    Card(){};
    Card(string cardPattern, string cardName, int cardValue);
    void printCard();
    int getValue();
};
#endif CARD_H
 
 
 
#ifndef DECK_H
#define DECK_H
#include <iostream>
#include <vector>
#include "Card.h"
#include <random>
#include <algorithm>
#include <iterator>
class Deck{
private:
    std::vector<Card> c;
public:
    Deck();
    void initialize();
    void shuffle();
    Card getCard();
    void printDeck();
};
#endif DECK_H
 
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include "card.h"
#include "Deck.h"
 
using namespace std;
 
class Player {
private:
    float point;
    vector <Card> playerCard;
 
public:
    Player() {};
    Player(int p);
 
    float getPoint();
    void calcPoint(float point);
 
    void addCard(Card c);
    void showCard();
    void showPoint();
 
    int cardScore();
    void clear();
 
 
};
#endif PLAYER_H
 
#ifndef DEALER_H
#define DEALER_H
#include "Card.h"
#include "Deck.h"
 
using namespace std;
 
class Dealer {
private:
    float dPoint;
    vector <Card> dCard;
public:
    Dealer() {};
    Dealer(int dP);
 
    float getDealerPoint();
    void calcDealerPoint(float point);
 
    void addDealerCard(Card c);
    void showDealerCard();
    void showDealerPoint();
    int showDealerScore();
    void Dealerclear();
    void DealerfirstCard();
};
#endif DEALER_H
 
#include "card.h"
#include <iostream>
 
using namespace std;
 
Card::Card(string cardPattern, string cardName, int cardValue) {
    pattern = cardPattern;
    name = cardName;
    value = cardValue;
 
}
void Card :: printCard(){
    cout << pattern <<  name << "   ";
}
 
int Card::getValue() {
    return value;
}
 
#include <iostream>
#include "Deck.h"
#include "Card.h"
 
 
Deck::Deck() {
    initialize();
}
void Deck::initialize(){
 
    string pattern[4= { "◆""♠""♥""♣" };
    string name[13= { "Ace""2""3""4""5""6""7""8""9"
"10""J""Q""K" };
 
    for (int i = 0; i < 4; i++) {
        string tmpP = pattern[i];
        for (int j = 0; j < 13; j++) {
            if (j == 0) {
                string tmpN = name[j];
                Card tmp = Card(tmpP, tmpN, 1);
                c.push_back(tmp);
            }
            else if (j > 8) {
                string tmpN = name[j];
                Card tmp = Card(tmpP, tmpN, 10);
                c.push_back(tmp);
            }
 
            else{
                string tmpN = name[j];
                Card tmp = Card(tmpP, tmpN, j+1);
                c.push_back(tmp);
            }
        }
    }
}
void Deck::printDeck() {
    for (int i = 0; i < 52++i) {
        c.at(i).printCard();
        cout << c.at(i).getValue()<< endl;
    }
 
    cout << c.size() << endl;
}
 
void Deck:: shuffle() {
 
    std::random_device rd;
    std::mt19937 g(rd());
 
    std::shuffle(c.begin(), c.end(), g);
    
}
 
Card Deck::getCard() {
    Card card;
    if (c.size() >= 1) {
        card = c[c.size() - 1];
        c.pop_back();
    }
    return card;
 
    
}
 
#include "Player.h"
#include "Deck.h"
#include <iostream>
 
 
Player::Player(int p) {
    point = p;
}
float Player::getPoint() {
    return point;
    
}
void Player::calcPoint(float p) {
    point += p;
}
 
void Player::addCard(Card c) {
    playerCard.push_back(c);
 
}
void Player::showCard() {
    for (int i = 0; i < playerCard.size(); i++) {
        playerCard.at(i).printCard();
    }
    cout << cardScore() << endl;
 
 
}
void Player::showPoint() {
    cout << point << endl;
 
}
int Player::cardScore() {
    int score = 0
 
 
    for(int i = 0; i<playerCard.size(); i++){
        int tmp = playerCard.at(i).getValue();
        score += tmp;
        }
 
    for (int j = 0; j < playerCard.size();j++) {
        int tmp3 = 0;
 
        int tmp2 = playerCard.at(j).getValue();
 
        if (tmp2 == 1) {
            for (int k = 0; k < j; k++) {
                tmp3 = tmp3 + playerCard.at(k).getValue();
            }
            if (tmp3+9 < 21) {
                score += 9;
            }
        }
    }
 
    return score;
}
void Player::clear() {
    playerCard.clear();
}
 
 
 
#include "Dealer.h"
#include "Deck.h"
#include <iostream>
 
 
Dealer::Dealer(int dP) {
    dPoint = dP;
}
float Dealer::getDealerPoint() {
    return dPoint;
 
}
void Dealer::calcDealerPoint(float dp) {
    dPoint += dp;
}
 
void Dealer::addDealerCard(Card c) {
    dCard.push_back(c);
}
void Dealer::showDealerCard() {
    for (int i = 0; i < dCard.size(); i++) {
        dCard.at(i).printCard();
    }
    cout << showDealerScore() << endl;
 
 
}
void Dealer::showDealerPoint() {
    cout << dPoint << endl;
 
}
int Dealer::showDealerScore() {
    int score = 0;
    for (int i = 0; i<dCard.size(); i++) {
        int tmp = dCard.at(i).getValue();
        score += tmp;
    }
 
    for (int j = 0; j < dCard.size(); j++) {
        int tmp3 = 0;
        int tmp2 = dCard.at(j).getValue();
 
        if (tmp2 == 1) {
            for (int k = 0; k < j; k++) {
                tmp3 = tmp3 + dCard.at(k).getValue();
            }
            if (tmp3 + 9 < 21) {
                score += 9;
            }
        }
    }
    
 
    return score;
}
void Dealer::Dealerclear() {
    dCard.clear();
}
void Dealer::DealerfirstCard() {
    dCard.at(0).printCard();
}
 
#include <iostream>
#include "Deck.h"
#include "Card.h"
#include "Player.h"
#include "Dealer.h"
 
 
int main()
{
 
    
    Player *= new Player(500);
    Dealer *dealer = new Dealer(500000);
    cout << "***************************************Black Jack***********************
**********************\n" << endl;
    cout << "******              초기 포인트는 500이며 최대 100을 배팅할 수 있습니다. 
               ******\n" << endl;
    cout << "******  한 쪽이 파산하거나 플레이어가 게임을 중단할 때 까지 게임을 진행할
 수 있습니다.  ******\n" << endl;
    cout << "******                              게임을 시작합니다.        
                          ******\n" << endl;
    cout << "************************************************************
**********************************\n" << endl;
    int end=0;
    int input = 0;
    int inputBet = 0;
 
    
LOOP: if (end == 1goto EXIT; {
    int burst = 0;
    if (p->getPoint() == 0) {
        cout << "Player 파산. 게임을 종료합니다." << endl;
        goto EXIT;
    }
    Deck *= new Deck();
    d->shuffle();
    p->clear();
    dealer->Dealerclear();
 
        int bet = 0;
        cout << "Player 잔여포인트 : " << p->getPoint() << endl;
        cout << "얼마를 배팅하시겠습니까 ? " << endl;
        cin >> inputBet;
        if (inputBet > p->getPoint()) {
            if (p->getPoint() > 100) {
                cout << "최대 배팅포인트는 100입니다. 100을 배팅합니다." << endl;
                bet = 100;
            }
            else {
                cout << "Player의 잔여포인트보다 배팅금액이 높습니다.
 Player의 잔여포인트를 모두 배팅합니다. " << endl;
                bet = p->getPoint();
            }
            
        }
        else if (inputBet < p->getPoint() && inputBet > 100) {
            cout << "최대 배팅포인트는 100입니다. 100을 배팅합니다." << endl;
            bet = 100;
        }
        else {
            cout << inputBet << "을 배팅합니다. " << endl;
            bet = inputBet;
        }
 
        p->addCard(d->getCard());
        p->addCard(d->getCard());
        dealer->addDealerCard(d->getCard());
        cout << "딜러의 첫번째 카드 : " << endl;
        dealer->DealerfirstCard();
        dealer->addDealerCard(d->getCard());
 
        while (true) {
            cout << "\nPlayer의 카드 : ";
            p->showCard();
 
            cout << "1. Hit                    2. Stand" << endl;
            cin >> input;
            if (input != 1) {
                break;
            }
 
            p->addCard(d->getCard());
 
            if (p->cardScore() > 21) {
                cout << "Player Burst" << endl;
                burst = 1;
                break;
            }
            else if (p->cardScore() == 21) {
                cout << "Player BlackJack" << endl;
                cout << "\nPlayer Card :";
                p->showCard();
    
                cout << "\nDealer Card : ";
                dealer->showDealerCard();
 
                p->calcPoint(bet*1.5);
                cout << "\nPlayer 잔여 포인트 : ";
                p->showPoint();
                cout << "\n계속 하시겠습니까 ?        1. NO      Else. YES " << endl;
                cin >> end;
                goto LOOP;
            }
 
        }
        if (burst == 1) {
            cout << "\nPlayer Card :";
            p->showCard();
 
            cout << "\nDealer Card : ";
            dealer->showDealerCard();
 
            p->calcPoint(-bet);
            cout << "\nPlayer 잔여 포인트 : ";
            p->showPoint();
 
 
            cout << "\n계속 하시겠습니까 ?        1. NO      Else. YES " << endl;
            cin >> end;
            goto LOOP;
        }
 
        while (true) {
            if (dealer->showDealerScore() >= 17) {
                if (dealer->showDealerScore() > 21) {
                    cout << "Dealer Burst" << endl;
                    burst = 2;
                }
                break;
            }
            else dealer->addDealerCard(d->getCard());
        }
 
        if (burst == 2) {
            cout << "\nPlayer Card :";
            p->showCard();
 
            cout << "\nDealer Card : ";
            dealer->showDealerCard();
        
            p->calcPoint(bet);
            cout << "\nPlayer 잔여 포인트 : ";
            p->showPoint();
 
    
            cout << " \n계속하시겠습니까?           1. NO           Else.YES" << endl;
            cin >> end;
            goto LOOP;
        }
 
        if (p->cardScore() > dealer->showDealerScore()) {
            cout << "Player Win" << endl;
 
            cout << "\nPlayer Card :";
            p->showCard();
 
            cout << "\nDealer Card : ";
            dealer->showDealerCard();
    
            p->calcPoint(bet);
            cout << "Player 잔여 포인트 : ";
            p->showPoint();
 
            cout << "\n계속하시겠습니까?           1. NO           Else.YES" << endl;
            cin >> end;
            goto LOOP;
        }
 
        else if (p->cardScore() < dealer->showDealerScore()) {
            cout << "Dealer Win" << endl;
 
            cout << "\nPlayer Card :";
            p->showCard();
 
            cout << "\nDealer Card : ";
            dealer->showDealerCard();
 
            p->calcPoint(-bet);
            cout << "\nPlayer 잔여 포인트 : ";
            p->showPoint();
 
 
            cout << "\n계속 하시겠습니까 ?        1. NO      Else. YES " << endl;
            cin >> end;
            goto LOOP;
 
        }
        else {
            cout << "비겼습니다." << endl;
 
            cout << "\nPlayer Card :";
            p->showCard();
 
            cout << "\nDealer Card : ";
            dealer->showDealerCard();
 
            cout << "\n계속 하시겠습니까 ?        1. NO      Else. YES " << endl;
            cin >> end;
            goto LOOP;
 
        }
        
 
    }
 
EXIT:    
    return 0;
}
 
cs


'C&C++' 카테고리의 다른 글

[Datalab-handout] bits.c  (0) 2017.10.06