[C++项目]2048控制台游戏

时间:2023-03-09 08:27:57
[C++项目]2048控制台游戏
 #include <iostream>
#include <windows.h>
#include <ctime>
using namespace std; int const ROW = ;
int const COL = ;
int game[ROW][COL] = {}; //上下左右
int const UP = ;
int const DOWN = ;
int const LEFT = ;
int const RIGHT = ; //游戏所处的状态
int const GAME_OVER = ;
int const GAME_WIN = ;
int const GAME_CONTINUE = ; enum GameNum
{
Game_2 = ,
Game_4 = ,
Game_8 = ,
Game_16 = ,
Game_32 = ,
Game_64 = ,
Game_128 = ,
Game_256 = ,
Game_512 = ,
Game_1024 = ,
Game_2048 = ,
}; //打印所得的数组
void Print()
{
system("cls");
cout << "***************** 2048 控 制 台 版 ******************" << endl;
cout << "***************** By Linxu ******************" << endl << endl;
for (int i = ; i < ROW; ++i)
{
cout << "================================"<< endl;
for (int j = ; j < COL; ++j)
{
if (game[i][j] == )
{
cout <<"| \t";
}
else
{
cout <<"| " << game[i][j] << "\t";
}
}
cout << "|" << endl;
}
cout << "================================"<< endl;
cout << "【操作说明】:"<< endl;
cout << "请使用按照↑↓←→控制移动方向" << endl << endl;
} bool CreateNumber()
{
int x = -;
int y = -;
int times = ;
int maxTimes = ROW * COL;
//三分之二的概率生成2,三分之一的概率生成4
int whitch = rand() % ;
do
{
x = rand() % ROW;
y = rand() % COL;
++times;
} while (game[x][y] != && times <= maxTimes); //说明格子已经满了
if(times >= maxTimes)
{
return false;
}
else
{
GameNum num;
if(whitch == )
{
num = Game_4;
}
else if(whitch)
{
num = Game_2;
}
game[x][y] = num;
} return true;
} void Process(int direction)
{
switch (direction)
{
case UP:
//最上面一行不动
for(int row = ; row < ROW; ++row)
{
for(int crow = row; crow >= ; --crow)
{
for(int col = ; col < COL; ++col)
{
//上一个格子为空
if(game[crow-][col] == )
{
game[crow-][col] = game[crow][col];
game[crow][col] = ;
}
else
{
//合并
if(game[crow-][col] == game[crow][col])
{
game[crow - ][col] *= ;
game[crow][col] = ;
} }
}
}
}
break;
case DOWN:
//最下面一行不动
for(int row = ROW - ; row >= ; --row)
{
for(int crow = row; crow < ROW - ; ++crow)
{
for(int col = ; col < COL; ++col)
{
//上一个格子为空
if(game[crow + ][col] == )
{
game[crow + ][col] = game[crow][col];
game[crow][col] = ;
}
else
{
//合并
if(game[crow + ][col] == game[crow][col])
{
game[crow + ][col] *= ;
game[crow][col] = ;
} }
}
}
}
break;
case LEFT:
//最左边一列不动
for(int col = ; col < COL; ++col)
{
for(int ccol = col; ccol >= ; --ccol)
{
for(int row = ; row < ROW; ++row)
{
//上一个格子为空
if(game[row][ccol-] == )
{
game[row][ccol - ] = game[row][ccol];
game[row][ccol] = ;
}
else
{
//合并
if(game[row][ccol - ] == game[row][ccol])
{
game[row][ccol - ] *= ;
game[row][ccol] = ;
} }
}
}
}
break;
case RIGHT:
//最右边一列不动
for(int col = COL - ; col >= ; --col)
{
for(int ccol = col; ccol <= COL - ; ++ccol)
{
for(int row = ; row < ROW; ++row)
{
//上一个格子为空
if(game[row][ccol + ] == )
{
game[row][ccol + ] = game[row][ccol];
game[row][ccol] = ;
}
else
{
//合并
if(game[row][ccol + ] == game[row][ccol])
{
game[row][ccol + ] *= ;
game[row][ccol] = ;
} }
}
}
}
break;
} } //处理输入输出,返回上下左右
int Input()
{
//读取上下左右四个方向键
int upArrow = ;
int downArrow = ;
int leftArrow = ;
int rightArrow = ;
int direction = ;
while (true)
{
upArrow = GetAsyncKeyState(VK_UP);
downArrow = GetAsyncKeyState(VK_DOWN);
leftArrow = GetAsyncKeyState(VK_LEFT);
rightArrow = GetAsyncKeyState(VK_RIGHT); if(upArrow)
{
direction = UP;
break;
}
else if(downArrow)
{
direction = DOWN;
break;
}
else if(leftArrow)
{
direction = LEFT;
break;
}
else if(rightArrow)
{
direction = RIGHT;
break;
} Sleep();
} return direction;
} //判断游戏状态
int Judge()
{
//赢得游戏
for(int i = ; i < ROW; ++i)
{
for(int j = ; j < COL; ++j)
{
if(game[i][j] == )
{
return GAME_WIN;
break;
}
}
} //横向检查
for(int i = ; i < ROW; ++i)
{
for(int j = ; j < COL - ; ++j)
{
if(!game[i][j] || (game[i][j] == game[i][j+]))
{
return GAME_CONTINUE;
break;
}
}
}
//纵向检查
for(int j = ; j< COL; ++j)
{
for(int i = ; i < ROW -; ++i)
{
if(!game[i][j] || (game[i][j] == game[i+][j]))
{
return GAME_CONTINUE;
break;
}
}
} //不符合上述两种状况,游戏结束
return GAME_OVER; } int main()
{
//设置一个随机数种子
srand((unsigned int)time());
CreateNumber();
CreateNumber();
Print();
int direction = ;
int gameState = -;
while(true)
{
direction = Input(); gameState = Judge();
if(direction && gameState == GAME_CONTINUE)
{
Process(direction);
CreateNumber();
Print();
Sleep();
}
else if(gameState == GAME_WIN)
{
Print();
cout << "You Win!" << endl;
break;
}
else if(gameState == GAME_OVER)
{
Print();
cout <<"You lose!" << endl;
break;
}
} return ;
}