[UVA] 162 - Beggar My Neighbour
Beggar My Neighbour
Beggar My Neighbour |
``Beggar My Neighbour'' (sometimes known as ``StripJack Naked'') is a traditional card game, designed to help teachbeginners something about cards and their values. A standard deck isshuffled and dealt face down to the two players, the first card to thenon-dealer, the second to the dealer, and so on until each player has26 cards. The dealer receives the last card. The non-dealer starts thegame by playing the top card of her deck (the second last card dealt)face up on the table. The dealer then covers it by playing her topcard face up. Play continues in this fashion until a``face'' card (Ace, King, Queen or Jack) is played. The nextplayer must then ``cover'' that card, by playing onecard for a Jack, two for a Queen, three for a King and four for anAce. If a face card is played at any stage during this sequence, playswitches and the other player must cover that card. When this sequencehas ended, the player who exposed the last face card takes the entireheap, placing it face down under her existing deck. She then startsthe next round by playing one card face up as before, and playcontinues until one player cannot play when called upon to do so,because they have no more cards.
Write a program that will simulate playing this game. Remember that astandard deck (or pack) of cards contains 52 cards. These are dividedinto 4 suits--Spades ( ), Hearts ( ),Diamonds ( ) and Clubs ( ). Within eachsuit there are 13 cards--Ace (A), 2-9, Ten (T), Jack(J), Queen (Q) and King (K).
Input
Input will consist of aseries of decks of cards. Each deck will give the cards in order asthey would be dealt (that is in the example deck below, the non-dealerwould start the game by playing the H2). Decks will occupy 4 lineswith 13 cards on each. The designation of each card will be the suit(S, H, D, C) followed by the rank (A, 2-9, T, J, Q, K). Therewill be exactly one space between cards. The file will be terminatedby a line consisting of a single #.
Output
Output will consist of a series of lines, one for each deck in theinput. Each line will consist of the number of the winning player (1is the dealer, 2 is the first to play) and the number of cards in thewinner's hand (ignoring any on the stack), right justified in a fieldof width 3.
Sample input
HA H3 H4 CA SK S5 C5 S6 C4 D5 H7 HJ HQD4 D7 SJ DT H6 S9 CT HK C8 C9 D6 CJ C6S8 D8 C2 S2 S3 C7 H5 DJ S4 DQ DK D9 D3H9 DA SA CK CQ C3 HT SQ H8 S7 ST H2 D2#
Sample output
1 44
題目描述:
這是一個老手介紹撲克牌給新手的小遊戲,發排依序為新手-老手-新手-老手 ... 正面朝下。
然後由新手開始,每一回合由先手翻開第一張牌,而後手根據那張牌出牌,
如果前一個人出了 JACK, 而後面那個人要出 1 張牌
如果前一個人出了 QUEEN, 而後面那個人要出 2 張牌
如果前一個人出了 KING, 而後面那個人要出 3 張牌
如果前一個人出了 ACE, 而後面那個人要出 4 張牌
這麼講可能不好,如果後面那個人出牌個數達到後面寫的那個數字,則必須將桌上的牌收起,正面朝下疊到手牌之下。
如果他在翻的過程中翻到 JQKA 其中一個,則換另一個人按照那個牌繼續那個規則,此時桌上的牌會越來越多。
最後會有一個人手上沒牌則獲勝。
#include <stdio.h>
#include <queue>
using namespace std;
int trans(char c) {
if(c >= '0' && c <= '9')
return c-'0';
if(c == 'A') return 1;
if(c == 'T') return 10;
if(c == 'J') return 11;
if(c == 'Q') return 12;
if(c == 'K') return 13;
}
int main() {
char s[50];
int card[52];
int i;
while(true) {
for(i = 0; i < 52; i++) {
scanf("%s", s);
if(s[0] == '#') return 0;
card[i] = trans(s[1]);
}
queue<int> A, B;
for(i = 51; i >= 0; i--) {
if(i%2) A.push(card[i]);
else B.push(card[i]);
}
int turn = 1;//0 A, 1 B
int judge = -1;
queue<int> H;//heap
while(true) {
if(turn == 0 && A.empty()) {
judge = 0;
break;
}
if(turn == 1 && B.empty()) {
judge = 1;
break;
}
int CARD;
if(turn == 0)
CARD = A.front(), A.pop();
else
CARD = B.front(), B.pop();
//printf("%d - %c %d %d\n", CARD, turn+'A', A.size(), B.size());
H.push(CARD);
turn = 1-turn;
int ended = 1;
while(CARD >= 11 || CARD == 1) {
ended = 0;
int paid;
if(CARD == 1) paid = 4;
else if(CARD == 11) paid = 1;
else if(CARD == 12) paid = 2;
else if(CARD == 13) paid = 3;
for(i = 0; i < paid; i++) {
if(turn == 0 && A.empty()) {
judge = 0;
break;
}
if(turn == 1 && B.empty()) {
judge = 1;
break;
}
if(turn == 0)
CARD = A.front(), A.pop();
else
CARD = B.front(), B.pop();
H.push(CARD);
//printf("%d - %c\n", CARD, turn+'A');
if(CARD == 1 || CARD >= 11)//change
break;
}
if(judge >= 0) break;
turn = 1-turn;
}
if(judge >= 0) break;
if(ended == 0) {
if(turn == 0) {
while(!H.empty()) {
A.push(H.front());
H.pop();
}
} else {
while(!H.empty()) {
B.push(H.front());
H.pop();
}
}
}
}
printf("%d%3d\n", 2-judge, judge ? A.size() : B.size());
}
return 0;
}