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

카드게임 스트레이트 체크

바로이순간 2012. 5. 18. 17:10

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int checks(int cards[]) {

  int i, x, y;

  int b[13]={0,};

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

  x=0;y=0;

  //for(i=0;i<13;++i)printf("%d ",b[i]);printf("\n");getch();

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

    if(b[i]>0 && b[(i+1)%13]>0) ++x; 

    else {

      if(x>y) y=x; 

      x=0;

    }

  }

  if(x>y) y=x;

  if(y>3) 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]%13)>(cards[j+1]%13)) {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;

}