/*
Subject: #170 - Clock Patience
e-mail: morcavon@gmail.com
Homepage: http://www.morcavon.com
Building: 27/07/2005 ~ 27/07/2005
Last Update: 27/07/2005
*/
/*
<PURPOSE>
Read in a number of shuffled decks, and play the game.
-
<PREDICATE>
The cards are dealt out(face down) in a circle, representing a clock.
(an extra pile in the center of the clock)
The game ends when the pile indicated by the current card has no face down cards in it.
-
<INPUT>
decks
...
#
-
<OUTPUT>
[# of cards exposed during the game],[last card exposed]
*/
#include <stdio.h>
#include <string.h>
#define ONLINE_JUDGE
int expose(char pile[][4], char idx[13], char* currentPile);
char rankToChar(char card);
int main()
{
/* 각 카드더미에는 4개의 카드가 포함 */
char pile[13][4];
char suit[52]; /* 각 카드의 모양은 따로 저장 */
char buf[3];
char idx[13]; /* 각 카드더미의 top인덱스 */
int i, j, cnt;
char currentCard, currentPile;
while (scanf("%s", buf) != 0) {
if (buf[0] == '#')
break;
/* deal cards */
i = 12;
j = 3;
cnt = 0;
do {
/* 카드의 suit값은 필요없음=_= */
pile[i--][j] = buf[0] == 'A' ? 0 :
buf[0] == 'T' ? 9 :
buf[0] == 'J' ? 10 :
buf[0] == 'Q' ? 11 :
buf[0] == 'K' ? 12 : buf[0] - '1';
suit[cnt++] = buf[1];
if (i < 0) {
i = 12;
j--;
}
if (j < 0)
break;
scanf("%s", buf);
} while(1);
/* idx 초기화 */
memset(idx, 3, sizeof(char)*13);
/* start game */
currentPile = 12;
currentCard = pile[12][3];
cnt = 1;
while (expose(pile, idx, ¤tPile) >= 0) {
cnt++;
}
printf("%s%d,%c%c\n", cnt < 10?"0":"", cnt,
rankToChar(pile[currentPile][idx[currentPile]+1]),
suit[(13 - currentPile) + (3 - (idx[currentPile] + 1))*13 - 1]);
i = 0;
}
return 0;
}
int expose(char pile[][4], char idx[13], char* currentPile)
{
/* currentPile의 맨위 카드와 값이 같은 카드더미의 밑에
* 그 카드를 넣으며 카드를 꺼낸 더미의 idx를 감소,
* currentPile를 변경하고, 새로 정해진 currentCard값을 리턴.
* 더 이상 카드가 남아 있지 않을경우 -1리턴.
*/
char nextCard = pile[*currentPile][idx[*currentPile]];
idx[*currentPile]--; /* 현재더미에서 카드를 빼냄 */
if (idx[nextCard] < 0)
return -1;
return (*currentPile = nextCard);
}
char rankToChar(char card)
{
switch (card) {
case 0: return 'A';
case 9: return 'T';
case 10: return 'J';
case 11: return 'Q';
case 12: return 'K';
default: return '0' + card + 1;
}
}