수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

테트리스 질문입니다. 제 코딩의 문제점을 지적해주세요.

뽀대미녀

2023.04.01

테트리스 게임 프로젝트 코딩 하던 중
답지와 차이가 나는 부분이 궁금해서 질문 드립니다.

우선 답지에는
static int CurPosX;
static int CurPosY;
static int BlockIdx;
static int RotateIdx;

void InitBlockPos(int x, int y)
{
if(x0 || y0)
return ;
CurPosX=x;
CurPosY=y;
SetCurrentCursorPos(CurPosX, CurPosY);
}
void ChooseBlock(void)
{
srand((unsigned int)time(NULL));
BlockIdx=(rand()%NUM_OF_BLOCK)*4;
}
int GetCurrentBlockIdx(void)
{
return BlockIdx+RotateIdx;
}

void BlockDown(void)
{
DeleteBlock(blockModel[GetCurrentBlockIdx()]);
CurPosY+=1;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[GetCurrentBlockIdx()]);
}
void DeleteBlock(char blockModel[][4])
{
int y,x;
point curPos=GetCurrentCursorPos();
for(y=0; y4; y++)
{
for(x=0; x4; x++)
{
SetCurrentCursorPos(curPos.x+(2*x), curPos.y+y);
if(blockModel[y][x]==1)
printf( );
}
}
SetCurrentCursorPos(curPos.x, curPos.y);
}
void ShowBlock(char blockModel[][4])
{
int y,x;
point curPos=GetCurrentCursorPos();
for(y=0; y4; y++)
{
for(x=0; x4; x++)
{
SetCurrentCursorPos(curPos.x+(2*x),curPos.y+y);

if(blockModel[y][x]==1)
printf(■);
}
}
SetCurrentCursorPos(curPos.x, curPos.y);
}
void BlockLeftRotate(void)
{
int nextRotSte;
DeleteBlock(blockModel[GetCurrentBlockIdx()]);

nextRotSte=RotateIdx+1;
nextRotSte%=4;
RotateIdx=nextRotSte;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[GetCurrentBlockIdx()]);
}
void BlockLeftMove(void)
{
DeleteBlock(blockModel[GetCurrentBlockIdx()]);
CurPosX-=2;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[GetCurrentBlockIdx()]);
}
void BlockRightMove(void)
{
DeleteBlock(blockModel[GetCurrentBlockIdx()]);
CurPosX+=2;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[GetCurrentBlockIdx()]);
}

답지에는 빨간 부분과 같이 인덱스를 함수 리턴값으로 받아서 작동하게 하더라구요..
답지대로 하니깐 문제없이 수행은 잘 되구요........

저는

static int CurPosX;
static int CurPosY;
static int BlockIdx;
static int RotateIdx;

void InitBlockPos(int x, int y)
{
if(x0 || y0)
return ;
CurPosX=x;
CurPosY=y;
SetCurrentCursorPos(CurPosX, CurPosY);
}
void ChooseBlock(void)
{
srand((unsigned int)time(NULL));
BlockIdx=(rand()%NUM_OF_BLOCK)*4;
}
int GetCurrentBlockIdx(void)
{
return BlockIdx+RotateIdx;
} 저는 이 함수는 쓰지 않았고 대신 전역 변수를 사용해서 코딩을 해보았습니다.

void BlockDown(void)
{
DeleteBlock(blockModel[BlockIdx]);
CurPosY+=1;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[BlockIdx]);
}
void DeleteBlock(char blockModel[][4])
{
int y,x;
point curPos=GetCurrentCursorPos();
for(y=0; y4; y++)
{
for(x=0; x4; x++)
{
SetCurrentCursorPos(curPos.x+(2*x), curPos.y+y);
if(blockModel[y][x]==1)
printf( );
}
}
SetCurrentCursorPos(curPos.x, curPos.y);
}
void ShowBlock(char blockModel[][4])
{
int y,x;
point curPos=GetCurrentCursorPos();
for(y=0; y4; y++)
{
for(x=0; x4; x++)
{
SetCurrentCursorPos(curPos.x+(2*x),curPos.y+y);

if(blockModel[y][x]==1)
printf(■);
}
}
SetCurrentCursorPos(curPos.x, curPos.y);
}
void BlockLeftRotate(void)
{
int nextRotSte;
DeleteBlock(blockModel[BlockIdx+RotateIdx]);

nextRotSte=RotateIdx+1;
nextRotSte%=4;
RotateIdx=nextRotSte;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[BlockIdx+RotateIdx]);
}
void BlockLeftMove(void)
{
DeleteBlock(blockModel[BlockIdx+RotateIdx]);
CurPosX-=2;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[BlockIdx+RotateIdx]);
}
void BlockRightMove(void)
{
DeleteBlock(blockModel[BlockIdx+RotateIdx]);
CurPosX+=2;
SetCurrentCursorPos(CurPosX, CurPosY);
ShowBlock(blockModel[BlockIdx+RotateIdx]);
}

저는 이렇게 해서 해봤는데 결과가
블록 방향을 바꾸면 처음에 바꼇다가 원래대로 되돌아 오는 문제가 발생하더라구요..
어떤 문제가 있어서 그렇게 되는지 잘 모르겠어서 질문드립니다.
고수님들의 가르침 부탁드립니다.

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

  • 은별

    감사합니다~~~^^^^^

  • 에다

    BlockDown 이 틀리네요. 아아 숨은그림찾기 힘들다. ㅎㅎㅎ

번호 제 목 글쓴이 날짜
2701754 strcmp,strcpy를 좀더 이해를 하기위해서 간단히 만들었는데... 말달리자 2025-07-17
2701724 배열초기화도중 이니셜라이저 가 너무 많다고 나오네요! (2) 라임나무 2025-07-16
2701697 6.0에서는 잘되던 프로그램이 2008에서는 잘안된답니다. 한번 아시는분 댓글부탁드립니다. (1) 빵순 2025-07-16
2701644 문자가 알파벳인지 검사하기(isalpha) 마음 2025-07-16
2701590 재가 C프로그래밍 아무것도 몰르는데요. (4) 대나무 2025-07-15
2701565 로그인 프로그램 개굴츼 2025-07-15
2701511 이거 오류 안나게 수정좀 부탁드릴께요 ㅠㅠ 돠주세요 ㅠㅠ 어리버리 2025-07-14
2701453 MFC문제점 해결방안좀알려주세요~~ 나샘 2025-07-14
2701429 자료형에 관한 질문 (5) 펴라 2025-07-14
2701377 훌로트형 변수를, 서식문자 %d로 읽기 vs 인트형 포인터로 참조하기 LetMeGo 2025-07-13
2701291 콘솔에서 종료시킬때 메시지를 안띄우려면 어떻게 해야하나요? (1) 세실 2025-07-12
2701262 씨언어 좀 봐주세요 아담 2025-07-12
2701211 토큰추출 겨루 2025-07-12
2701159 연산자문제 알려주세요 도1도캣 2025-07-11
2701130 중적분문제입니다. 적분구간에 변수가 들어갈순 없나요??ㅡㅜ 풀큰 2025-07-11
2701098 난수에 질문드립니다. 큰뫼 2025-07-11
2701070 또다른 시험문제 질문올립니다 채련 2025-07-10
2701042 뭐가 잘못된건지 잘 모르겠습니다.;; 지은 2025-07-10
2700986 뭐가 잘못된건지좀 봐주세요. 우주 2025-07-10
2700932 도와주세요 ㅠㅠ 모르겟어요 ㅠ 유희 2025-07-09
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com