精華區beta NTUBA92 關於我們 聯絡資訊
#include <iostream.h> #include <stdlib.h> #include <stdio.h> #include <time.h> class BJPlayer { int ace; int score; float risk; public: int needCard(float r) {return (r > risk);} //r為拿到好牌的機率 void getCard(int); int point(); void setRisk(float r) {risk = r;} void reset() {ace = score = 0;} }; int BJPlayer::point() { //若ace == 0,則傳回目前的score值; //若score+10會超過21點,則也傳回score值; if (ace == 0 || score+10 > 21) return score; else return score+10; }; void BJPlayer::getCard(int n) { if (n > 10) score += 10; else score += n; }; class deck { int c[52]; int count; public: // deck() {srand(time(NULL)); count=52;} void shuffle(); int drawcard(); float stat(int); }; void deck::shuffle() { for(int i=0; i<52; i++) c[i] = 0; count = 52; } inline int randno() {return rand() % 52;} int deck::drawcard() { int k; if (!count) return 0; for (k = randno(); c[k];) k = randno(); c[k] = 1; count--; return (k%13 + 1); } // a simple-minded deck stat function // = ratio of the number of cards with values less than or equal to n // and the number of cards left, i.e., count float deck::stat(int n) { int v, qual = 0; for (int k = 0; k < 52; k++) if (!c[k]) { v = k%13 > 10 ? 10 : k%13; qual += n >= v; } return (float) qual / count; } //在此放入兩個類別carddeck、BJPlayer的定義 void main() { BJPlayer alex,bob; deck d; int a,b; srand(time(0)); // randomize d.shuffle(); alex.reset(); bob.reset(); alex.setRisk(0.8); bob.setRisk(0.5); alex.getCard(d.drawcard()); bob.getCard(d.drawcard()); alex.getCard(d.drawcard()); bob.getCard(d.drawcard()); if (alex.point() == 21) cout << "Alex is Blackjack. \n"; if (bob.point() == 21) cout << "Bob is Blackjack. \n"; if (alex.needCard(d.stat(21 - alex.point()))) alex.getCard(d.drawcard()); if (bob.needCard(d.stat(21 - bob.point()))) bob.getCard(d.drawcard()); a = alex.point(); b = bob.point(); cout << "Alex's point is " << a << '\n' << "Bob's point is " << b << endl; if (a > 21) a = 0; if (b > 21) b = 0; if (a > b) cout << "Alex wins! \n"; else if (b > a) cout << "Bob wins! \n"; else cout << "No winner. \n"; } -- ......遭遇史上最大危機!! ....終極版情瘤桿菌蔓延......之卷~~ -- ※ 發信站: 批踢踢實業坊(ptt.twbbs.org) ◆ From: olf.m7.ntu.edu.tw