^^ 김시환님 답변에 반하는 거 같아 죄송하네요 꾸벅
그냥 실행해보고 재미삼아 ^^ 올려봅니다.
아마 레포트이겠지요. 그리고 답변은 안다는게 관행인걸 알지만
정말 재미삼아 입니다.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#pragma argsused
#define box_length 15
#define box_height 15
void intro_game(void);
void game_control(void);
void gotoxy(int x, int y);
int left_right_move(void);
void move_down(int x);
void draw_rectangle(int c, int r);
int max_block(void);
void draw_box(int x);
int block_stack[box_length*2+2]={0}; // 해당위치의 블록을 누적
/*
int main(void)
{
intro_game();
game_control();
gotoxy(1, box_height+3);
printf("game이 종료되었습니다. \n");
return 0;
}
*/
void intro_game(void)
{
system("cls");
printf("블록쌓기 \n\n");
printf("블록이 좌우로 움직일때 스페이스키를 누르면\n");
printf("블록이 떨어져 바닥에 쌓입니다.\n\n");
printf("아무키나 누르면 게임을 시작합니다. \n");
getch();
}
void game_control(void)
{
int x, count=0;
char checkrow = 0;
system("cls");
draw_rectangle(box_length, box_height );
gotoxy(box_length*2+5,3);
printf("블록의 개수: %2d", box_height);
gotoxy(1, box_height+3);
printf("스페이스키를 누르면 블록이 떨어지고\n");
printf("바닥에 쌓입니다. \n");
// while(count<box_height)
while(1)
{
gotoxy(box_length*2+5,4);
printf("시도한 횟수: %2d", count+1);
gotoxy(box_length*2+5,5);
printf("쌓인 블록수: %2d", max_block());
if(max_block() > 14)
{
gotoxy(box_length*2+5,6);
printf("failed game");
break;
}
x=left_right_move();
move_down(x);
checkrow = 1;
for(x = 3; x <= box_height*2+1; x+=2)
{
if (block_stack[x] == 0)
checkrow = 0;
}
if(checkrow)
{
system("cls");
draw_rectangle(box_length, box_height);
gotoxy(box_length*2+5,3);
printf("블록의 개수: %2d", box_height);
gotoxy(1, box_height+3);
printf("스페이스키를 누르면 블록이 떨어지고\n");
printf("바닥에 쌓입니다. \n");
for(x = 3; x <= box_height*2+1; x+=2)
{
// block_stack[x]--;
if(block_stack[x] > 0)
{
block_stack[x]--;
draw_box(x);
}
}
}
count++;
getch();
}
}
int max_block(void)
{
int i, max=0;
for(i=1;i<box_height*2+1;i++)
{
if (max<=block_stack[i])
max=block_stack[i];
}
return max;
}
int left_right_move(void)
{
int x=3, y=2, temp=2;
do
{
x+=temp;
if (x>(box_length*2)) //x방향 최대값 설정
temp=-2;
if (x<3)
{
x=3;
temp=2;
}
gotoxy(x, y);
printf("□");
Sleep(100); //블록이 좌우로 움직이는 속도를 조절
gotoxy(x, y);
printf(" ");
}while(!kbhit());
block_stack[x]+=1;
return x;
}
void draw_box(int x)
{
int i;
for(i = 0; i < block_stack[x]; i++)
{
gotoxy(x, box_height+2-block_stack[x]+i);
printf("□");
}
}
void move_down(int x)
{
int y;
for(y=2;y<box_height+2-block_stack[x];y+=1)
{
gotoxy(x, y);
printf("□");
Sleep(20);
gotoxy(x, y);
printf(" ");
Sleep(10);
}
gotoxy(x,box_height+2-block_stack[x]);
printf("□");
}
void gotoxy(int x, int y)
{
COORD Pos = {x - 1, y - 1};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}
void draw_rectangle(int r, int c)
{
int i, j;
unsigned char a=0xa6;
unsigned char b[7];
for(i=1;i<7;i++)
b[i]=0xa0+i;
printf("%c%c",a, b[3]);
for(i=0;i<r;i++)
printf("%c%c", a, b[1]);
printf("%c%c", a, b[4]);
printf("\n");
for(i=0;i<c;i++)
{
printf("%c%c", a, b[2]);
for(j=0;j<r;j++)
printf(" ");
printf("%c%c",a, b[2]);
printf("\n");
}
printf("%c%c", a, b[6]);
for(i=0;i<r;i++)
printf("%c%c", a, b[1]);
printf("%c%c", a, b[5]);
printf("\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
intro_game();
game_control();
gotoxy(1, box_height+3);
printf("game이 종료되었습니다. \n");
getch();
return 0;
}
//---------------------------------------------------------------------------
프로그램 동작합니다만 소스를 제가 임의로 개판으로 고쳐서요.
개인적인 의견입니다만 제룽뎅이님 올리신 소스에서 허접스럽게 고쳐봤습니다. ^^
소스 공부는 아래 김시환님 글이 너무 정리 잘되있어서 한번 해보시길 그냥 한번 권해 봅니다.
그럼 이만...
김시환 님이 쓰신 글 :
: 글 내용이 보아하니 학교 레포트 인듯 보이네요.. 이곳 게시판에서는 레포트는 답변을 안주는 것이 관행으로 되어있어서 답변이 안달릴 것입니다.
:
: 저는 답을 드리기 보다는 혼자 해 볼 수 있도록 몇가지 힌트만 드리지요...
:
: 일단 블럭이 무제한 으로 나오게 하려면 무한루프를 돌려야 하는데 여기는 한정된 루프만 돌고 있습니다.
:
: 1. while(count<box_height) 이 부분 코드를 보시면 box_height 가 15로 define 되어 있지요..
: 아마 블럭이 전체 높이(15블럭)만큼 쌓이면 게임을 마치게 하려고 한듯 한데 count 가 증가만 되지 감소하는 부분이 전혀 없습니다. 그러니 당연히 15번만 루프가 돌면 게임이 끝나게 되는 것입니다.
: while 루프 안에 블럭이 한줄 쌓이면 사라지게 하고 그 때 count 값을 블럭이 사라진 개수만큼 빼주는 것이 필요합니다.
:
: 2. move_down(x); 함수의 위치가 문제가 될듯 싶습니다.
: game_control 함수 내부에 while 루프 안에 move_down(x); 함수가 들어있는데 game_control 함수가 한개의 블럭에 해당하는 함수라면 이해가 가지만 전체 구성으로 볼때 전체 게임의 컨트롤처럼 보이므로 left_right_move 함수 안에 블럭이 완전이 아래로 떨어질때까지 루프를 돌리고 그 루프 안에 move_down 함수가 들어가 있는것이 맞겠지요...
: 현재 코드로서는 아마 블럭이 많아야 2~3개 떨어지면 끝날듯 싶네요..
:
: 3. 한줄이 다 맞추어 지면 없어지게? 이것역시 함수로 각 라인마다 블럭이 다 차있는지 확인하고 처리하는 함수를 만들어서 move_down 함수 내부에서 호출해 주는 식으로 하면 될듯 싶네요...
:
: 이것이 레포트가 맞고 프로그램을 제대로 하려고 한다면 본인이 스스로 노력해 보시고 자신이 노력한 결과 까지만 레포트로 제출하세요.. 그것이 나중에라도 진정한 프로그래머가 되기위한 길이 아닐까 생각합니다.
:
:
:
: 제룽뎅이 님이 쓰신 글 :
: :
#include <stdio.h>
: : #include <stdlib.h>
: : #include <conio.h>
: : #include <windows.h>
: :
: : #define box_length 15
: : #define box_height 15
: :
: : void intro_game(void);
: : void game_control(void);
: : void gotoxy(int x, int y);
: : int left_right_move(void);
: : void move_down(int x);
: : void draw_rectangle(int c, int r);
: : int max_block(void);
: :
: : int block_stack[box_length*2+1]={0}; // 해당위치의 블록을 누적
: :
: : int main(void)
: : {
: : intro_game();
: : game_control();
: : gotoxy(1, box_height+3);
: : printf("game이 종료되었습니다. \n");
: : return 0;
: : }
: :
: : void intro_game(void)
: : {
: : system("cls");
: : printf("블록쌓기 \n\n");
: : printf("블록이 좌우로 움직일때 스페이스키를 누르면\n");
: : printf("블록이 떨어져 바닥에 쌓입니다.\n\n");
: : printf("아무키나 누르면 게임을 시작합니다. \n");
: : getch();
: : }
: :
: : void game_control(void)
: : {
: : int x, count=0;
: : system("cls");
: : draw_rectangle(box_length, box_height);
: : gotoxy(box_length*2+5,3);
: : printf("블록의 개수: %2d", box_height);
: : gotoxy(1, box_height+3);
: : printf("스페이스키를 누르면 블록이 떨어지고\n");
: : printf("바닥에 쌓입니다. \n");
: : while(count<box_height)
: : {
: : gotoxy(box_length*2+5,4);
: : printf("시도한 횟수: %2d", count+1);
: : gotoxy(box_length*2+5,5);
: : printf("쌓인 블록수: %2d", max_block());
: : x=left_right_move();
: : move_down(x);
: : count++;
: : getch();
: : }
: : }
: :
: : int max_block(void)
: : {
: : int i, max=0;
: : for(i=1;i<box_height*2+1;i++)
: : {
: : if (max<=block_stack[i])
: : max=block_stack[i];
: : }
: : return max;
: : }
: :
: :
: : int left_right_move(void)
: : {
: : int x=3, y=2, temp=2;
: : do
: : {
: : x+=temp;
: : if (x>(box_length*2)) //x방향 최대값 설정
: : temp=-2;
: : if (x<3)
: : {
: : x=3;
: : temp=2;
: : }
: :
: : gotoxy(x, y);
: : printf("□");
: : Sleep(50); //블록이 좌우로 움직이는 속도를 조절
: : gotoxy(x, y);
: : printf(" ");
: :
: : }while(!kbhit());
: : block_stack[x]+=1;
: : return x;
: : }
: :
: : void move_down(int x)
: : {
: : int y;
: : for(y=2;y<box_height+2-block_stack[x];y+=1)
: : {
: : gotoxy(x, y);
: : printf("□");
: : Sleep(20);
: : gotoxy(x, y);
: : printf(" ");
: : Sleep(10);
: : }
: : gotoxy(x,box_height+2-block_stack[x]);
: : printf("□");
: : }
: :
: : void gotoxy(int x, int y)
: : {
: : COORD Pos = {x - 1, y - 1};
: : SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
: : }
: :
: : void draw_rectangle(int r, int c)
: : {
: :
: : int i, j;
: : unsigned char a=0xa6;
: : unsigned char b[7];
: :
: : for(i=1;i<7;i++)
: : b[i]=0xa0+i;
: :
: : printf("%c%c",a, b[3]);
: : for(i=0;i<r;i++)
: : printf("%c%c", a, b[1]);
: : printf("%c%c", a, b[4]);
: : printf("\n");
: :
: : for(i=0;i<c;i++)
: : {
: : printf("%c%c", a, b[2]);
: : for(j=0;j<r;j++)
: : printf(" ");
: : printf("%c%c",a, b[2]);
: : printf("\n");
: : }
: : printf("%c%c", a, b[6]);
: : for(i=0;i<r;i++)
: : printf("%c%c", a, b[1]);
: : printf("%c%c", a, b[5]);
: : printf("\n");
: : }
: :
: : -----------------------------------------------------------
: : 태트리스를처럼 완벽히 원하진않습니다
: : 하지만..........
: : 아님 한줄이 다 맞춰지면 없어지는것처럼
: : 하려고하는데 소스를뭘넣어야할지모르겠습니다
: : 도와주세요!!!!!!!!!!!!!!!!!!!!
: : 그리고 15개만 블럭이내려오는데
: : 무한으로내려와서
: : 계속할수있게끔은 어캐해야하는겁니까?
: : 무한...................댓글부탁드립니다
: : 1. 테트리스처럼 한줄이완성되면 없어지는거처럼하는 경우
: : 소스는 무엇입니까?
: : 2. 블럭이무제한나오게해주시게해주세요
: : 소스는 무엇이죠?
: :
: : 부탁드립니다.........ㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜㅜ