Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C by dima ( 14 years ago )
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MH 30
#define MW 20
#define MB 4
#define MR 4
#define CSHIFT 1000
#define C_Empty -1
#define C_Line 99
struct coo
{
int x, y;
};
struct map
{
struct coo block[MB];
};
struct figdata
{
int blocks, rots;
struct map rot[MR];
};
int room[MH][MW];
int heigh, width;
void show();
void init();
void drawblock(int x);
void drawfig(int bit);
void clearroom();
void down();
void rotate();
void newfig();
void checklines();
void drawnext();
void quick_throw();
void move(int dx);
int canstay(int fig, int rot, int x, int y);
struct figdata data(int fig);
int lines_collected, score;
struct { int x, y, fig, rot;} cur;
struct { int fig, rot;} next;
int main()
{
int key = CSHIFT;
unsigned ctime = time(NULL);
init();
show();
do
{
if (kbhit())
{
key = getch();
if(key==224 | key==0) key = (int) getch() + (int) CSHIFT;
switch(key)
{
case 72+CSHIFT : rotate(); break;
case 75+CSHIFT : move(-1); break;
case 77+CSHIFT : move(+1); break;
case 80+CSHIFT : down(); break;
case ' ' : quick_throw();
// default : printf("%dn",key);
}
}
if (time(NULL) != ctime)
{
ctime = time(NULL);
down();
}
} while(key!=27);
return 0;
}
void init()
{
srand( (unsigned int) time(NULL));
lines_collected = score = 0;
heigh = 20;
width = 10;
clearroom();
next.fig = rand()%7;
next.rot = rand()�ta(next.fig).rots;
newfig();
}
void show()
{
int i, j;
drawfig(cur.fig);
system("cls");
printf("Lines : %dnScore : %dn",lines_collected,score);
printf("/");
for(i=0;i<width*2;++i) printf("-");
printf("\n");
for(i=1;i<heigh;++i)
{
printf("|");
for(j=0;j<width;++j) drawblock(room[i][j]);
printf("|n");
}
printf("\");
for(i=0;i<width*2;++i) printf("-");
printf("/n");
printf("Next :n");
drawnext();
drawfig(C_Empty);
}
void drawblock(int x)
{
switch(x)
{
case C_Empty : printf(" %c",250); break;
case C_Line : printf("--"); break;
default : printf("[]");
}
}
void drawnext()
{
int i, j, nmap[MB][MB];
for(i=0;i<MB;++i)
for(j=0;j<MB;++j)
nmap[i][j] = C_Empty;
for(i=0;i<data(next.fig).blocks;++i)
nmap[data(next.fig).rot[next.rot].block[i].y]
[data(next.fig).rot[next.rot].block[i].x] = next.fig;
for(i=0;i<MB;++i)
{
for(j=0;j<MB;++j)
drawblock(nmap[i][j]);
printf("n");
}
}
void down()
{
if (canstay(cur.fig,cur.rot,cur.x,cur.y+1)) cur.y++;
else
{
drawfig(cur.fig);
newfig();
checklines();
}
show();
}
void move(int dx)
{
if (canstay(cur.fig,cur.rot,cur.x+dx,cur.y)) cur.x+=dx;
show();
}
void drawfig(int bit)
{
int i;
for(i=0;i<data(cur.fig).blocks;++i)
room[cur.y+data(cur.fig).rot[cur.rot].block[i].y]
[cur.x+data(cur.fig).rot[cur.rot].block[i].x] = bit;
}
int canstay(int fig, int rot, int x, int y)
{
int i;
for(i=0;i<data(fig).blocks;++i)
{
int xx = x + data(fig).rot[rot].block[i].x;
int yy = y + data(fig).rot[rot].block[i].y;
if ( !((xx>=0 && xx<width && yy>=0 && yy<heigh) && room[yy][xx]==C_Empty)) return 0;
}
return 1;
}
void newfig()
{
cur.fig = next.fig;
cur.rot = next.rot;
next.fig = rand()%7;
next.rot = rand()�ta(next.fig).rots;
cur.x=(width-1)/2; cur.y=0;
if (!canstay(cur.fig,cur.rot,cur.x,cur.y))
{
printf("GAME OVER!!! Press any key for new game...n");
getch();
init();
}
show();
}
void clearroom()
{
int i, j;
for(i=0;i<MH;++i)
for(j=0;j<MW;++j)
room[i][j] = C_Empty;
}
void checklines()
{
int i, j, k = 0;
for(i=0;i<heigh;++i)
{
int j, l = 1;
for(j=0;j<width;++j) l &= room[i][j] != C_Empty;
if(l) for(j=0;j<width;++j) room[i][j] = C_Line;
k += l;
}
lines_collected+=k;
score+=50*(1+k)*k;
show();
for(i=0;i<heigh;++i)
{
if (room[i][0] == C_Line)
{
int ii;
for(ii=i;ii>0;--ii)
for(j=0;j<width;++j)
room[ii][j]=room[ii-1][j];
for(j=0;j<width;++j) room[0][j]=C_Empty;
show();
}
}
}
void rotate()
{
if ( canstay(cur.fig,(cur.rot+1)�ta(cur.fig).rots,cur.x,cur.y))
{
cur.rot = (cur.rot+1)�ta(cur.fig).rots;
show();
}
}
void quick_throw()
{
while (canstay(cur.fig,cur.rot,cur.x,cur.y+1)) cur.y++;
down();
}
struct figdata data(int fig)
{
struct figdata i_fig = { 4, 2, { 1, 0, 1, 1, 1, 2, 1, 3 ,
0, 1, 1, 1, 2, 1, 3, 1 } };
struct figdata j_fig = { 4, 4, { 1, 0, 1, 1, 1, 2, 0, 2 ,
0, 0, 0, 1, 1, 1, 2, 1 ,
1, 0, 2, 0, 1, 1, 1, 2 ,
0, 1, 1, 1, 2, 1, 2, 2 } };
struct figdata l_fig = { 4, 4, { 1, 0, 1, 1, 1, 2, 2, 2 ,
0, 2, 0, 1, 1, 1, 2, 1 ,
0, 0, 1, 0, 1, 1, 1, 2 ,
0, 1, 1, 1, 2, 1, 2, 0 } };
struct figdata o_fig = { 4, 1, { 0, 0, 0, 1, 1, 0, 1, 1 } };
struct figdata s_fig = { 4, 2, { 1, 1, 2, 1, 1, 2, 0, 2 ,
1, 0, 1, 1, 2, 1, 2, 2 } };
struct figdata t_fig = { 4, 4, { 0, 1, 1, 1, 2, 1, 1, 2 ,
1, 0, 1, 1, 1, 2, 0, 1 ,
1, 0, 0, 1, 1, 1, 2, 1 ,
1, 0, 1, 1, 1, 2, 2, 1 } };
struct figdata z_fig = { 4, 2, { 0, 1, 1, 1, 1, 2, 2, 2 ,
1, 0, 1, 1, 0, 1, 0, 2 } };
switch(fig)
{
case 0 : return i_fig; // I
case 1 : return j_fig; // J
case 2 : return l_fig; // L
case 3 : return o_fig; // O
case 4 : return s_fig; // S
case 5 : return t_fig; // T
case 6 : return z_fig; // Z
}
}
Revise this Paste
Parent: 51616
Children: 51618