#include
#include
#include
#include
#include
#include
#ifdef _MSC_VER
#if _MSC_VER <= 1200
#error ?
#else
#if _MSC_VER >= 1600
#include
#else
typedef signed char int8_t;
typedef unsigned short uint16_t;
#endif
#ifndef __cplusplus
typedef int bool;
#define true 1
#define false 0
#endif
#endif
#else
#include
#ifndef __cplusplus
#include
#endif
#endif
static const uint16_t gs_uTetrisTable[7][4] =
{
{ 0x00F0U, 0x2222U, 0x00F0U, 0x2222U },
{ 0x0072U, 0x0262U, 0x0270U, 0x0232U },
{ 0x0223U, 0x0074U, 0x0622U, 0x0170U },
{ 0x0226U, 0x0470U, 0x0322U, 0x0071U },
{ 0x0063U, 0x0264U, 0x0063U, 0x0264U },
{ 0x006CU, 0x0462U, 0x006CU, 0x0462U },
{ 0x0660U, 0x0660U, 0x0660U, 0x0660U }
};
static const uint16_t gs_uInitialTetrisPool[28] =
{
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U,
0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xC003U, 0xFFFFU, 0xFFFFU
};
#define COL_BEGIN 2
#define COL_END 14
#define ROW_BEGIN 4
#define ROW_END 26
typedef struct TetrisManager
{
uint16_t pool[28];
int8_t x;
int8_t y;
int8_t type[3];
int8_t orientation[3];
unsigned score;
unsigned erasedCount[4];
unsigned erasedTotal;
unsigned tetrisCount[7];
unsigned tetrisTotal;
bool dead;
} TetrisManager;
typedef struct TetrisControl
{
bool pause;
bool clockwise;
int8_t direction;
int8_t color[28][16];
} TetrisControl;
HANDLE g_hConsoleOutput;
void initGame(TetrisManager *manager, TetrisControl *control);
void restartGame(TetrisManager *manager, TetrisControl *control);
void giveTetris(TetrisManager *manager);
bool checkCollision(const TetrisManager *manager);
void insertTetris(TetrisManager *manager);
void removeTetris(TetrisManager *manager);
void horzMoveTetris(TetrisManager *manager, TetrisControl *control);
void moveDownTetris(TetrisManager *manager, TetrisControl *control);
void rotateTetris(TetrisManager *manager, TetrisControl *control);
void dropDownTetris(TetrisManager *manager, TetrisControl *control);
bool checkErasing(TetrisManager *manager, TetrisControl *control);
void keydownControl(TetrisManager *manager, TetrisControl *control, int key);
void setPoolColor(const TetrisManager *manager, TetrisControl *control);
void gotoxyWithFullwidth(short x, short y);
void printPoolBorder();
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control);
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control);
void printNextTetris(const TetrisManager *manager);
void printScore(const TetrisManager *manager);
void runGame(TetrisManager *manager, TetrisControl *control);
void printPrompting();
bool ifPlayAgain();
int main()
{
TetrisManager tetrisManager;
TetrisControl tetrisControl;
initGame(&tetrisManager, &tetrisControl);
do
{
printPrompting();
printPoolBorder();
runGame(&tetrisManager, &tetrisControl);
if (ifPlayAgain())
{
SetConsoleTextAttribute(g_hConsoleOutput, 0x7);
system("cls");
restartGame(&tetrisManager, &tetrisControl);
}
else
{
break;
}
} while (1);
gotoxyWithFullwidth(0, 0);
CloseHandle(g_hConsoleOutput);
return 0;
}
void initGame(TetrisManager *manager, TetrisControl *control)
{
CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE };
g_hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(g_hConsoleOutput, &cursorInfo);
SetConsoleTitleA("俄罗斯方块——By: 林先生");
restartGame(manager, control);
}
void restartGame(TetrisManager *manager, TetrisControl *control)
{
memset(manager, 0, sizeof(TetrisManager));
memcpy(manager->pool, gs_uInitialTetrisPool, sizeof(uint16_t [28]));
srand((unsigned)time(NULL));
manager->type[1] = rand() % 7;
manager->orientation[1] = rand() & 3;
manager->type[2] = rand() % 7;
manager->orientation[2] = rand() & 3;
memset(control, 0, sizeof(TetrisControl));
giveTetris(manager);
setPoolColor(manager, control);
}
void giveTetris(TetrisManager *manager)
{
uint16_t tetris;
manager->type[0] = manager->type[1];
manager->orientation[0] = manager->orientation[1];
manager->type[1] = manager->type[2];
manager->orientation[1] = manager->orientation[2];
manager->type[2] = rand() % 7;
manager->orientation[2] = rand() & 3;
tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
if (tetris & 0xF000)
{
manager->y = 0;
}
else
{
manager->y = (tetris & 0xFF00) ? 1 : 2;
}
manager->x = 6;
if (checkCollision(manager))
{
manager->dead = true;
}
else
{
insertTetris(manager);
}
++manager->tetrisTotal;
++manager->tetrisCount[manager->type[0]];
printNextTetris(manager);
printScore(manager);
}
bool checkCollision(const TetrisManager *manager)
{
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
uint16_t dest = 0;
dest |= (((manager->pool[manager->y + 0] >> manager->x) << 0x0) & 0x000F);
dest |= (((manager->pool[manager->y + 1] >> manager->x) << 0x4) & 0x00F0);
dest |= (((manager->pool[manager->y + 2] >> manager->x) << 0x8) & 0x0F00);
dest |= (((manager->pool[manager->y + 3] >> manager->x) << 0xC) & 0xF000);
return ((dest & tetris) != 0);
}
void insertTetris(TetrisManager *manager)
{
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] |= (((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] |= (((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] |= (((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] |= (((tetris >> 0xC) & 0x000F) << manager->x);
}
void removeTetris(TetrisManager *manager)
{
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] &= ~(((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] &= ~(((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] &= ~(((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] &= ~(((tetris >> 0xC) & 0x000F) << manager->x);
}
void setPoolColor(const TetrisManager *manager, TetrisControl *control)
{
int8_t i, x, y;
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
for (i = 0; i < 16; ++i)
{
y = (i >> 2) + manager->y;
if (y > ROW_END)
{
break;
}
x = (i & 3) + manager->x;
if ((tetris >> i) & 1)
{
control->color[y][x] = (manager->type[0] | 8);
}
}
}
void rotateTetris(TetrisManager *manager, TetrisControl *control)
{
int8_t ori = manager->orientation[0];
removeTetris(manager);
manager->orientation[0] = (control->clockwise) ? ((ori + 1) & 3) : ((ori + 3) & 3);
if (checkCollision(manager))
{
manager->orientation[0] = ori;
insertTetris(manager);
}
else
{
insertTetris(manager);
setPoolColor(manager, control);
printCurrentTetris(manager, control);
}
}
void horzMoveTetris(TetrisManager *manager, TetrisControl *control)
{
int x = manager->x;
removeTetris(manager);
control->direction == 0 ? (--manager->x) : (++manager->x);
if (checkCollision(manager))
{
manager->x = x;
insertTetris(manager);
}
else
{
insertTetris(manager);
setPoolColor(manager, control);
printCurrentTetris(manager, control);
}
}
void moveDownTetris(TetrisManager *manager, TetrisControl *control)
{
int8_t y = manager->y;
removeTetris(manager);
++manager->y;
if (checkCollision(manager))
{
manager->y = y;
insertTetris(manager);
if (checkErasing(manager, control))
{
printTetrisPool(manager, control);
}
}
else
{
insertTetris(manager);
setPoolColor(manager, control);
printCurrentTetris(manager, control);
}
}
void dropDownTetris(TetrisManager *manager, TetrisControl *control)
{
removeTetris(manager);
for (; manager->y < ROW_END; ++manager->y)
{
if (checkCollision(manager))
{
break;
}
}
--manager->y;
insertTetris(manager);
setPoolColor(manager, control);
checkErasing(manager, control);
printTetrisPool(manager, control);
}
bool checkErasing(TetrisManager *manager, TetrisControl *control)
{
static const unsigned scores[5] = { 0, 10, 30, 90, 150 };
int8_t count = 0;
int8_t k = 0, y = manager->y + 3;
do
{
if (y < ROW_END && manager->pool[y] == 0xFFFFU)
{
++count;
memmove(manager->pool + 1, manager->pool, sizeof(uint16_t) * y);
memmove(control->color[1], control->color[0], sizeof(int8_t [16]) * y);
}
else
{
--y;
++k;
}
} while (y >= manager->y && k < 4);
manager->erasedTotal += count;
manager->score += scores[count];
if (count > 0)
{
++manager->erasedCount[count - 1];
}
giveTetris(manager);
setPoolColor(manager, control);
return (count > 0);
}
void keydownControl(TetrisManager *manager, TetrisControl *control, int key)
{
if (key == 13)
{
control->pause = !control->pause;
}
if (control->pause)
{
return;
}
switch (key)
{
case 'w': case 'W': case '8': case 72:
control->clockwise = true;
rotateTetris(manager, control);
break;
case 'a': case 'A': case '4': case 75:
control->direction = 0;
horzMoveTetris(manager, control);
break;
case 'd': case 'D': case '6': case 77:
control->direction = 1;
horzMoveTetris(manager, control);
break;
case 's': case 'S': case '2': case 80:
moveDownTetris(manager, control);
break;
case ' ':
dropDownTetris(manager, control);
break;
case '0':
control->clockwise = false;
rotateTetris(manager, control);
break;
default:
break;
}
}
void gotoxyWithFullwidth(short x, short y)
{
static COORD cd;
cd.X = (short)(x << 1);
cd.Y = y;
SetConsoleCursorPosition(g_hConsoleOutput, cd);
}
void printPoolBorder()
{
int8_t y;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
for (y = ROW_BEGIN; y < ROW_END; ++y)
{
gotoxyWithFullwidth(10, y - 3);
printf("%2s", "");
gotoxyWithFullwidth(23, y - 3);
printf("%2s", "");
}
gotoxyWithFullwidth(10, y - 3);
printf("%28s", "");
}
#define gotoxyInPool(x, y) gotoxyWithFullwidth(x + 9, y - 3)
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control)
{
int8_t x, y;
for (y = ROW_BEGIN; y < ROW_END; ++y)
{
gotoxyInPool(2, y);
for (x = COL_BEGIN; x < COL_END; ++x)
{
if ((manager->pool[y] >> x) & 1)
{
SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
printf("■");
}
else
{
SetConsoleTextAttribute(g_hConsoleOutput, 0);
printf("%2s", "");
}
}
}
}
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control)
{
int8_t x, y;
y = (manager->y > ROW_BEGIN) ? (manager->y - 1) : ROW_BEGIN;
for (; y < ROW_END && y < manager->y + 4; ++y)
{
x = (manager->x > COL_BEGIN) ? (manager->x - 1) : COL_BEGIN;
for (; x < COL_END && x < manager->x + 5; ++x)
{
gotoxyInPool(x, y);
if ((manager->pool[y] >> x) & 1)
{
SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
printf("■");
}
else
{
SetConsoleTextAttribute(g_hConsoleOutput, 0);
printf("%2s", "");
}
}
}
}
void printNextTetris(const TetrisManager *manager)
{
int8_t i;
uint16_t tetris;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF);
gotoxyWithFullwidth(26, 1);
printf("┏━━━━━━━━━┳━━━━━━━━━┓");
gotoxyWithFullwidth(26, 2);
printf("┃%9s┃%9s┃", "", "");
gotoxyWithFullwidth(26, 3);
printf("┃%9s┃%9s┃", "", "");
gotoxyWithFullwidth(26, 4);
printf("┃%9s┃%9s┃", "", "");
gotoxyWithFullwidth(26, 5);
printf("┃%9s┃%9s┃", "", "");
gotoxyWithFullwidth(26, 6);
printf("┗━━━━━━━━━┻━━━━━━━━━┛");
tetris = gs_uTetrisTable[manager->type[1]][manager->orientation[1]];
SetConsoleTextAttribute(g_hConsoleOutput, manager->type[1] | 8);
for (i = 0; i < 16; ++i)
{
gotoxyWithFullwidth((i & 3) + 27, (i >> 2) + 2);
((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
}
tetris = gs_uTetrisTable[manager->type[2]][manager->orientation[2]];
SetConsoleTextAttribute(g_hConsoleOutput, 8);
for (i = 0; i < 16; ++i)
{
gotoxyWithFullwidth((i & 3) + 32, (i >> 2) + 2);
((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
}
}
void printScore(const TetrisManager *manager)
{
static const char *tetrisName = "ITLJZSO";
int8_t i;
SetConsoleTextAttribute(g_hConsoleOutput, 0xE);
gotoxyWithFullwidth(2, 2);
printf("■得分:%u", manager->score);
gotoxyWithFullwidth(1, 6);
printf("■消行总数:%u", manager->erasedTotal);
for (i = 0; i < 4; ++i)
{
gotoxyWithFullwidth(2, 8 + i);
printf("□消%d:%u", i + 1, manager->erasedCount[i]);
}
gotoxyWithFullwidth(1, 15);
printf("■方块总数:%u", manager->tetrisTotal);
for (i = 0; i < 7; ++i)
{
gotoxyWithFullwidth(2, 17 + i);
printf("□%c形:%u", tetrisName[i], manager->tetrisCount[i]);
}
}
void printPrompting()
{
SetConsoleTextAttribute(g_hConsoleOutput, 0xB);
gotoxyWithFullwidth(26, 10);
printf("■控制:");
gotoxyWithFullwidth(27, 12);
printf("□向左移动:← A 4");
gotoxyWithFullwidth(27, 13);
printf("□向右移动:→ D 6");
gotoxyWithFullwidth(27, 14);
printf("□向下移动:↓ S 2");
gotoxyWithFullwidth(27, 15);
printf("□顺时针转:↑ W 8");
gotoxyWithFullwidth(27, 16);
printf("□逆时针转:0");
gotoxyWithFullwidth(27, 17);
printf("□直接落地:空格");
gotoxyWithFullwidth(27, 18);
printf("□暂停游戏:回车");
gotoxyWithFullwidth(25, 23);
printf("■By: 林先生送给老婆的游戏");
}
void runGame(TetrisManager *manager, TetrisControl *control)
{
clock_t clockLast, clockNow;
clockLast = clock();
printTetrisPool(manager, control);
while (!manager->dead)
{
while (_kbhit())
{
keydownControl(manager, control, _getch());
}
if (!control->pause)
{
clockNow = clock();
if (clockNow - clockLast > 0.45F * CLOCKS_PER_SEC)
{
clockLast = clockNow;
keydownControl(manager, control, 80);
}
}
}
}
bool ifPlayAgain()
{
int ch;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
gotoxyWithFullwidth(15, 10);
printf("游戏结束");
gotoxyWithFullwidth(13, 11);
printf("按Y重玩,按N退出");
do
{
ch = _getch();
if ( ch == 'Y'|| ch == 'y')
{
return true;
}
else if (ch == 'N' || ch == 'n')
{
return false;
}
} while (1);
}