Solution of 555 - Bridge Hands

Problem Description
source:https://uva.onlinejudge.org/external/5/555.html

Many games, such as Bridge, involve dealing a standard deck of 52 cards to 4 players, so each receives 13 cards. Good players can then play with the hand as it is dealt, but most ordinary players will need to sort it, firstly by suit, and then by rank within suit. 
     There is no fixed ranking of the suits for this purpose, but it is useful to alternate the colours, so we will presume the following ordering: ♣ < ♢ < ♠ < ♡. (Note that from now on we will use the more conventional ‘C’, ‘D’, ‘S’, ‘H’ for CLUBS, DIAMONDS, SPADES and HEARTS). Within a suit Ace is high, so the ordering is 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < T < J < Q < K < A.

    The players are usually designated North, South, East and West, and they sit at the points of the compass they name. One player is designated the dealer and he (or she) deals one card to each player starting with the player on his (her) left hand and proceeding clockwise until he (she) deals the last card to himself (herself). 
    Write a program that will read in a presentation of a deck of cards, deal them, sort them, and then display the 4 sorted hands in the format shown below.  

Input 

Input will consist of a series of deals. Each deal will consist of the letter representing the dealer (N, E, S, W) followed by two lines representing the deck as shown below. 
    The file will be terminated by a line consisting of a single ‘#’. 

Output 

Output will consist of a series of sets of four lines, one set for each deal. Each set will consist of four lines displaying the sorted hands, in the order and format shown below. Sets must follow each other immediately, with no blank line between them. 

Sample Input 


CQDTC4D8S7HTDAH7D2S3D6C6S6D9S4SAD7H2CKH5D3CTS8C9H3C3 
DQS9SQDJH8HAS2SKD4H4S5C7SJC8DKC5C2CAHQCJSTH6HKH9D5HJ 


Sample Output 

S: C3 C5 C7 CT CJ D9 DT DJ S3 SK H2 H9 HT 
W: C2 C4 CK D4 D5 D6 DQ DA S4 S8 ST SJ H8 
N: C6 C8 C9 CA D8 S9 SA H4 H5 H6 H7 HJ HA 
E: CQ D2 D3 D7 DK S2 S5 S6 S7 SQ H3 HQ HK

Solution:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;

void North(int count);
void Est(int count);
void South(int count);
void West(int count);
void InsertionSort();
void Dispaly();

    int s[15],w[15],n[15],e[15];
    char first[120],second[70],t1[3], t2[3];
    char rank[54][3]={"C2","C3","C4","C5","C6","C7","C8","C9","CT","CJ","CQ","CK","CA","D2","D3","D4","D5","D6","D7","D8","D9","DT","DJ","DQ","DK","DA","S2","S3","S4","S5","S6","S7","S8","S9","ST","SJ","SQ","SK","SA","H2","H3","H4","H5","H6","H7","H8","H9","HT","HJ","HQ","HK","HA"};


int main()
{
    char dealer, c ;
    int i,count;
    while(scanf("%c%c",&dealer,&c)==2)
    {
        if(dealer=='#')
            break;

        gets(first);
        gets(second);
        strcat(first, second);
        if(dealer=='N')
        {
            count=1;
            Est(count);
            count=2;
            South(count);
            count=3;
            West(count);
            count=4;
            North(count);
        }
        else if(dealer=='E')
        {
            count=1;
            South(count);
            count=2;
            West(count);
            count=3;
            North(count);
            count=4;
            Est(count);
        }
        else if(dealer=='S')
        {
            count=1;
            West(count);
            count=2;
            North(count);
            count=3;
            Est(count);
            count=4;
            South(count);
        }

        else
        {
            count=1;
            North(count);
            count=2;
            Est(count);
            count=3;
            South(count);
            count=4;
            West(count);
        }
        InsertionSort();
        Dispaly();
    }
    return 0;
}

void North(int count)
{
    int i,j,start,index;
    if(count==1)
        start=0;
    else if(count==2)
        start=2;
    else if(count==3)
        start=4;
    else
        start=6;
    index=1;
      for(i=start;i<104;i+=8)
        {
            t1[0]=first[i];
            t1[1]=first[i+1];
            for(j=0;j<52;j++)
            {
                strcpy(t2,rank[j]);
                if(strcmp(t1,t2)==0)
                {
                    n[index]=j;
                    index=index+1;
                    break;
                }

            }
        }
}

void Est(int count)
{
    int i,j,start, index;
    if(count==1)
        start=0;
    else if(count==2)
        start=2;
    else if(count==3)
        start=4;
    else
        start=6;
    index=1;
      for(i=start;i<104;i+=8)
        {
            t1[0]=first[i];
            t1[1]=first[i+1];
            for(j=0;j<52;j++)
            {
                strcpy(t2,rank[j]);
                if(strcmp(t1,t2)==0)
                {
                    e[index]=j;
                    index=index+1;
                    break;
                }

            }
        }
}
void South(int count)
{
    int i,j,start,index;
    if(count==1)
        start=0;
    else if(count==2)
        start=2;
    else if(count==3)
        start=4;
    else
        start=6;
    index=1;
      for(i=start;i<104;i+=8)
        {
            t1[0]=first[i];
            t1[1]=first[i+1];
            for(j=0;j<52;j++)
            {
                strcpy(t2,rank[j]);
                if(strcmp(t1,t2)==0)
                {
                    s[index]=j;
                    index=index+1;
                    break;
                }

            }
        }
}
void West(int count)
{
    int i,j,start,index;
    if(count==1)
        start=0;
    else if(count==2)
        start=2;
    else if(count==3)
        start=4;
    else
        start=6;
    index=1;
      for(i=start;i<104;i+=8)
        {
            t1[0]=first[i];
            t1[1]=first[i+1];
            for(j=0;j<52;j++)
            {
                strcpy(t2,rank[j]);
                if(strcmp(t1,t2)==0)
                {
                    w[index]=j;
                    index=index+1;
                    break;
                }

            }
        }
}
void InsertionSort()
{
    int i,k,ptr,temp;
    for(k=2;k<=13;k++)
    {
        temp=s[k];
        ptr=k-1;
        while(temp<s[ptr])
        {
            s[ptr+1]=s[ptr];
            ptr=ptr-1;
        }
        s[ptr+1]=temp;
    }

    for(k=2;k<=13;k++)
    {
        temp=w[k];
        ptr=k-1;
        while(temp<w[ptr])
        {
            w[ptr+1]=w[ptr];
            ptr=ptr-1;
        }
        w[ptr+1]=temp;
    }
    for(k=2;k<=13;k++)
    {
        temp=n[k];
        ptr=k-1;
        while(temp<n[ptr])
        {
            n[ptr+1]=n[ptr];
            ptr=ptr-1;
        }
        n[ptr+1]=temp;
    }
    for(k=2;k<=13;k++)
    {
        temp=e[k];
        ptr=k-1;
        while(temp<e[ptr])
        {
            e[ptr+1]=e[ptr];
            ptr=ptr-1;
        }
        e[ptr+1]=temp;
    }

}
void Dispaly()
{
    int i,ri;
    printf("S:");
    for(i=1;i<=13;i++)
    {
        ri=s[i];
        printf(" %s",rank[ri]);
    }
    printf("\n");
    printf("W:");
    for(i=1;i<=13;i++)
    {
        ri=w[i];
        printf(" %s",rank[ri]);
    }
    printf("\n");
    printf("N:");
    for(i=1;i<=13;i++)
    {
        ri=n[i];
        printf(" %s",rank[ri]);
    }
    printf("\n");
    printf("E:");
    for(i=1;i<=13;i++)
    {
        ri=e[i];
        printf(" %s",rank[ri]);
    }
    printf("\n");

}

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience