c·c++/c 프로그래밍

카드게임 플러쉬 체크

바로이순간 2012. 5. 24. 02:03

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int checks(int cards[]) {

  int i, x;

  int b[4]={0,};

  for(i=0;i<7;++i) b[cards[i]/13]+=1;

  x=0;

  for(i=0;i<4;++i) {

    if(b[i]>4) ++x; 

  }

  if(x>0) return 1;

    

  return 0;

}

int main() {

  int i, j, x, n=0;

  int cards[52];

  char kinds[4][20]={"Hearts","Spades","Diamonds","Clubs"}; 

  srand(time(NULL));


  while(1) {

    n=n+1;

    for(i=0;i<52;++i) cards[i]=i;

    for(i=0;i<52;++i) {

        j=rand()%52;

        x=cards[i];cards[i]=cards[j];cards[j]=x;

    }

    if(checks(cards)) {

        for(i=1;i<7;++i) {

            for(j=0;j<7-i;++j) {

                if(cards[j]>cards[j+1]) {x=cards[j];cards[j]=cards[j+1];cards[j+1]=x; }

            }

        }

        for(i=0;i<7;++i) printf("%2d %s \n",(cards[i]%13)+1, kinds[cards[i]/13]);

        break;

    }

    else {

        for(i=0;i<7;++i) printf("%2d %s ",(cards[i]%13)+1, kinds[cards[i]/13]);

        printf(".\n"); 

    }

  }

  return 0;

}