테트리스 질문입니다. 제 코딩의 문제점을 지적해주세요.
콩순
테트리스 게임 프로젝트 코딩 하던 중
답지와 차이가 나는 부분이 궁금해서 질문 드립니다.
우선 답지에는
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]);
}
저는 이렇게 해서 해봤는데 결과가
블록 방향을 바꾸면 처음에 바꼇다가 원래대로 되돌아 오는 문제가 발생하더라구요..
어떤 문제가 있어서 그렇게 되는지 잘 모르겠어서 질문드립니다.
고수님들의 가르침 부탁드립니다.
-
역곡중
감사합니다~~~^^^^^
-
은새
BlockDown 이 틀리네요. 아아 숨은그림찾기 힘들다. ㅎㅎㅎ
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2701098 | 난수에 질문드립니다. | 큰뫼 | 2025-07-11 |
2701070 | 또다른 시험문제 질문올립니다 | 채련 | 2025-07-10 |
2701042 | 뭐가 잘못된건지 잘 모르겠습니다.;; | 지은 | 2025-07-10 |
2700986 | 뭐가 잘못된건지좀 봐주세요. | 우주 | 2025-07-10 |
2700932 | 도와주세요 ㅠㅠ 모르겟어요 ㅠ | 유희 | 2025-07-09 |
2700900 | 반복문 도움요청..!!합니다. (1) | 두힘 | 2025-07-09 |
2700875 | (유효성검사)프로그램 짜는데 질문이 잇습니다. | 휑하니 | 2025-07-09 |
2700852 | 링크드 리스트 구현시 malloc 관련 에러 입니다. | 삐용삐용 | 2025-07-08 |
2700828 | 7/4 와 7/4.0 의 차이 | 발랄한그1녀 | 2025-07-08 |
2700771 | 아스키값 질문입니다. (+추가 임베디드 다른것도!) (3) | 찰스 | 2025-07-08 |
2700746 | 코드 오류 질문드립니다 | 차분 | 2025-07-07 |
2700721 | 배열 프로그래밍 입니다. (1) | 크나 | 2025-07-07 |
2700695 | 간단한 메모장 구현을 할려고 하는데요 (9) | 늘솜 | 2025-07-07 |
2700668 | c언어 질문입니다. 도와주세요~ (3) | 가자 | 2025-07-07 |
2700639 | 한글입력받아서 ㄱㄴㄷ순서대로출력하는법좀 | 두빛나래 | 2025-07-06 |
2700610 | 정말 기초적인 더하기,여백 문제 help | 무슬 | 2025-07-06 |
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |