您的位置:首页 > 其它

控制台版2048 version_1.0总结

2016-03-23 23:17 253 查看
  这两天大概花了3~4个钟头写了一个简单的控制台版的2048。之前简单的思考过这个游戏,感觉实现逻辑比较简单,适合练手,所以简单构思一下就开始写了。结果真是惨不忍睹。因为总有些细节没考虑到,构思过于粗糙,所以最后写出来的东西有一些小bug,所以姑且命名为version_1.0吧。

  总结两点教训:

  1.一定要有完整的构思,尤其是较核心较复杂的部分,一定要完全构思好在实现,否则将会是无休止的时间浪费。

  2.写代码不要从前到后实现,先把最核心的实现了,其他代码都要围绕最核心的代码写。

贴个源代码(欢迎指点批评)

//2016-3-22
//

#include <iostream>
#include <windows.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;
int Blocks[4][4] = {0};

void SetCursor(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}
//
void PrintColor(int x, int y, int num)
{
SetCursor(x, y+1);
printf(" %-4d\n", num);
DWORD dwSize = 0;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
WORD wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN;
switch (num)
{
case 0: wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_INTENSITY; break;
case 2: wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY; break;
case 4: wAttribute=FOREGROUND_BLUE|BACKGROUND_BLUE|BACKGROUND_INTENSITY; break;
case 8: wAttribute=FOREGROUND_BLUE|BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY; break;
case 16: wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED|BACKGROUND_INTENSITY; break;
case 32: wAttribute=FOREGROUND_BLUE|BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_INTENSITY; break;
case 64: wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY; break;
case 128: wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN; break;
case 256: wAttribute=FOREGROUND_BLUE|BACKGROUND_RED; break;
case 512: wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY; break;
case 1024: wAttribute=FOREGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED|BACKGROUND_BLUE; break;
case 2048: wAttribute=FOREGROUND_BLUE|BACKGROUND_RED; break;
case 4096: wAttribute=FOREGROUND_BLUE|BACKGROUND_RED|BACKGROUND_INTENSITY; break;
default: break;
}
FillConsoleOutputAttribute(hOut, wAttribute, 6, pos, &dwSize);
pos.Y++;
FillConsoleOutputAttribute(hOut, wAttribute, 6, pos, &dwSize);
pos.Y++;
FillConsoleOutputAttribute(hOut, wAttribute, 6, pos, &dwSize);
pos.Y++;
}
void HideCursor()
{
//
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
//
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void PrintFigure(void)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
PrintColor(8+6*j, 3+3*i, Blocks[i][j]);
}
}
}
int Update_UP(void)
{
int IsMove = 0;
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 4; i++)
{
if (Blocks[i][j] != 0)
{
for (int m = i+1; m < 4; m++)
{
if (Blocks[m][j] == Blocks[i][j])
{
Blocks[i][j] *= 2;
Blocks[m][j] = 0;
IsMove = 1;
break;
}
else if (Blocks[m][j] != 0)
break;
}
}
else
{
for (int m = i; m < 4; m++)
{
if (Blocks[m][j] != 0)
{
Blocks[i][j] = Blocks[m][j];
Blocks[m][j] = 0;
i--;
IsMove = 1;
break;
}
}
}
}
}
return IsMove;
}
int Update_DOWN(void)
{
int IsMove = 0;
for (int j = 0; j < 4; j++)
{
for (int i = 3; i >= 0; i--)
{
if (Blocks[i][j] != 0)
{
for (int m = i-1; m >= 0; m--)
{
if (Blocks[m][j] == Blocks[i][j])
{
Blocks[i][j] *= 2;
Blocks[m][j] = 0;
IsMove = 1;
break;
}
else if (Blocks[m][j != 0])
break;
}
}
else if (Blocks[i][j] == 0)
{
for (int m = i; m >= 0; m--)
{
if (Blocks[m][j] != 0)
{
Blocks[i][j] = Blocks[m][j];
Blocks[m][j] = 0;
i++;
IsMove = 1;
break;
}
}
}
}
}
return IsMove;
}
int Update_LEFT(void)
{
int IsMove = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (Blocks[i][j] != 0)
{
for (int m = j+1; m < 4; m++)
{
if (Blocks[i][m] == Blocks[i][j])
{
Blocks[i][j] *= 2;
Blocks[i][m] = 0;
IsMove = 1;
break;
}
else if (Blocks[i][m] != 0)
break;
}
}
else
{
for (int m = j+1; m < 4; m++)
{
if (Blocks[i][m] != 0)
{
Blocks[i][j] = Blocks[i][m];
Blocks[i][m] = 0;
IsMove = 1;
j--;
break;
}
}
}
}
}
return IsMove;
}

int Update_RIGHT(void)
{
int IsMove = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 3; j >= 0; j--)
{
if (Blocks[i][j] != 0)
{
for (int m = j-1; m >= 0; m--)
{
if (Blocks[i][j] == Blocks[i][m])
{
Blocks[i][j] *= 2;
Blocks[i][m] = 0;
IsMove = 1;
break;
}
else if (Blocks[i][j] != 0)
break;
}
}
else
{
for (int m = j; m >= 0; m--)
{
if (Blocks[i][m] != 0)
{
Blocks[i][j] = Blocks[i][m];
Blocks[i][m] = 0;
IsMove = 1;
j++;
break;
}
}
}
}
}
return IsMove;
}
void CreateNew(void)
{
int i = 0, j = 0;
int flag = 1;
while (1 == flag)
{
i = (int)rand()%4;
j = (int)rand()%4;
if (0 == Blocks[i][j])
{
if (rand()%2 == 1)
{
Blocks[i][j] = 2;
flag = 0;
}
else
{
Blocks[i][j] = 4;
flag = 0;
}
}
}
}
void StartGame(void)
{
int IsMove = 0;
system("cls");
SetCursor(10, 0);
printf("2048 author:zhaoyu");
Blocks[1][1] = 2;
Blocks[3][3] = 2;
PrintFigure();
int flag = 0;
char direction = 'x';
while (-1 != flag)
{
direction = getch();
if (direction == 'w')
{
IsMove = Update_UP();

}
// constant before avoid logic error:
//equal is written as assign to
if ('s' == direction)
{
IsMove = Update_DOWN();
}
if ('a' == direction)
{
IsMove = Update_LEFT();
}
if ('d' == direction)
{
IsMove = Update_RIGHT();
}

if (1 == IsMove)
{
CreateNew();
PrintFigure();
IsMove = 0;
}
direction = 'x';
if (GetAsyncKeyState(VK_ESCAPE))
exit(0);
}

}
void GUI(void)
{
system("cls");
//
system("mode con cols=40 lines=20");

HideCursor();
SetCursor(10, 0);
printf("2048 author:zhaoyu");
SetCursor(6, 6);
cout << "Press any key to start....";
getchar();
StartGame();

}

int main(int argc, char const *argv[])
{
srand((unsigned)time(NULL));
GUI();
StartGame();
return 0;
}


虽然有bug,还是顽强的玩到了2048

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  控制台 游戏