본문 바로가기

Native/C

couple2.cpp

[code]
#include <Turboc.h>

// 아스키 코드값을 변수에 기입
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ESC 27

enum Status { HIDDEN, FLIP, TEMPFLIP }; // 상태를 3개로 정의

struct tag_Cell
{
    int Num;
    Status St;        // *.c로 작업하면 컴파일이 되지 않는다. cpp로 저장하자... enum은.. 말이다.
};

tag_Cell arCell[4][4];

int nx,ny;
int count;

// 함수 원형 선언
void InitGame();
void DrawScreen(BOOL bHint);
void GetTempFlip(int *tx,int *ty);
int GetRemain();

void main()
{
    int ch;
    int tx,ty;

    randomize();
    InitGame();

    for (;;)
    {
        gotoxy(nx*5+2,ny*3+2); // 0(0*5+2, 0*3+2) 1(1*5+2,1*3+2)
        ch=getch();

        if (ch == 0xE0)
        {
            ch=getch();
            
            switch (ch)
            {
            case LEFT:
                if (nx > 0) nx--;
                break;
            case RIGHT:
                if (nx < 3) nx++;
                break;
            case UP:
                if (ny > 0) ny--;
                break;
            case DOWN:
                if (ny < 3) ny++;
                break;
            }
        }
        else
        {
            switch (ch)
            {
            case ESC:
                exit(0);
                break;
            case ' ':
                GetTempFlip(&tx,&ty);

                if (arCell[nx][ny].St==HIDDEN && (nx != tx || ny != ty))
                {
                    if (tx == -1)
                    {
                        arCell[nx][ny].St=TEMPFLIP;
                    }
                    else
                    {
                        count++;

                        if (arCell[tx][ty].Num == arCell[nx][ny].Num)
                        {
                            putch('\a');
                            arCell[tx][ty].St=FLIP;
                            arCell[nx][ny].St=FLIP;

                            if (GetRemain() == 0)
                            {
                                DrawScreen(FALSE);
                                gotoxy(26,22);
                                puts("축하합니다. 다시 시작합니다.");
                                delay(2000);
                                InitGame();
                            }
                        }
                        else
                        {
                            arCell[nx][ny].St=TEMPFLIP;
                            DrawScreen(FALSE);
                            delay(1000);
                            arCell[tx][ty].St=HIDDEN;
                            arCell[nx][ny].St=HIDDEN;
                        }
                    }
                    DrawScreen(FALSE);
                }
                break;
            }
        }
    }
}

void InitGame()
{
    int i,j;
    int x,y;

    nx=ny=0;    // 좌표 초기화
    count=0;    // 카운트

    memset(arCell,0,sizeof(arCell)); // 0으로 초기화

    for (i=1;i<=8;i++)
    {
        for (j=0;j<2;j++)
        {
            do
            {
                x=random(4);
                y=random(4);
            }
            while (arCell[x][y].Num != 0);
            arCell[x][y].Num=i;        // 내용 입력
        }
    }

    DrawScreen(TRUE);
    delay(2000);
    clrscr();
    DrawScreen(FALSE);
}

void DrawScreen(BOOL bHint)
{
    int x,y;
    for (x=0;x<4;x++)
    {
        for (y=0;y<4;y++)
        {
            gotoxy(x*5+2,y*3+2);
            if (bHint == TRUE || arCell[x][y].St==FLIP)
            {
                gotoxy(wherex()-1,wherey());
                printf("[%d]",arCell[x][y].Num);
            }
            else if (arCell[x][y].St==TEMPFLIP)
            {
                printf("%d",arCell[x][y].Num);
            }
            else
            {
                printf("?");
            }
        }
    }

    gotoxy(30,2);puts("짝 맞추기 게임 Ver 1.1");
    gotoxy(30,4);puts("커서키:이동. 공백:뒤집기. Esc:종료");
    gotoxy(30,6);printf("총 시도 회수 : %d",count);
    gotoxy(30,8);printf("아직 못 찾은 것 : %d ",GetRemain());
}

void GetTempFlip(int *tx,int *ty)
{
    int i,j;

    for (i=0;i<4;i++)
    {
        for (j=0;j<4;j++)
        {
            if (arCell[i][j].St == TEMPFLIP)
            {
                *tx=i;
                *ty=j;
                return;
            }
        }
    }
    *tx=-1;
}

int GetRemain()
{
    int i,j;
    int remain=16;

    for (i=0;i<4;i++)
    {
        for (j=0;j<4;j++)
        {
            if (arCell[i][j].St==FLIP)
            {
                remain--;
            }
        }
    }
    return remain;
}
[/code]

'Native > C' 카테고리의 다른 글

공용체 Union  (0) 2013.10.02
비트 구조체  (0) 2013.10.02
문제Quiz  (0) 2013.10.02
구조체  (0) 2013.10.02
문자열 함수들  (0) 2013.10.02