Advertisement

写出自己的一个 2048小游戏

阅读量:

2048函数 整体思想

1.如何产生随机数? rand 、srand函数

2.如何产生界面? 利用一些符号组合 加上 system函数 调节颜色尺寸等等

3.如何使相同数合并? 数组中 相同的话,选择方向后 将被合并的 赋为0

4.如何操作其上下左右? GetAsyncKeyState函数 可以用键盘控制

复制代码
 #include<iostream>

    
 #include<windows.h>
    
 #include<ctime>
    
 #include<stdlib.h>
    
 using namespace std;
    
  
    
 int const ROW = 4;
    
 int const COL = 4;
    
 int game[ROW][COL] = {0};
    
  
    
 int const UP = 1;
    
 int const DOWN = 2;
    
 int const LEFT = 3;
    
 int const RIGHT = 4;
    
  
    
 int const GAME_OVER = 1;
    
 int const GAME_WIN = 2;
    
 int const GAME_CONTINUE = 3;
    
  
    
  
    
 enum GameNum//枚举出可能出现的结果
    
 {
    
 	Game_2 = 2,Game_4 = 4,Game_8 = 8,Game_16 =16,
    
 	Game_32 = 32,Game_64 = 64,Game_128 = 128,Game_256 =256,
    
 	Game_512 = 512,Game_1024 = 1024,Game_2048 = 2048,
    
 };
    
  
    
 void Print()
    
 {
    
 	system("cls");
    
 	system("color 1F");
    
 	system("mode con cols=45 lines=15");//界面颜色 尺寸
    
  
    
  
    
 	cout << "--------------welcome to 2048 ------------"<<endl;//界面的打印
    
 	cout << "--------------Thueder    lcj  ------------"<<endl<<endl<<endl;
    
 	for (int i =0; i < ROW; ++i)
    
 	{
    
 	cout << "------------------------------------------"<<endl;
    
 		for (int j =0; j<COL;++j)
    
 		{
    
 			if (game[i][j] == 0)
    
 			{
    
 				cout <<"*    \t";
    
 			}
    
 			else
    
 			{
    
 				cout <<"*    "<<game [i][j] << "\t";
    
  
    
 			}
    
 		}
    
 		cout <<"*"<<endl;
    
 	}
    
  
    
 	cout << "------------------------------------------"<<endl;
    
 }
    
 bool CreateNumber ()
    
 {
    
 	int x = -1;
    
 	int y = -1;
    
 	int times = 0;
    
 	int maxTimes = ROW* COL;
    
  
    
 	int whitch = rand() %3;//取随机数
    
 	do
    
 	{
    
 		x = rand () % ROW;
    
 		y = rand () % COL;
    
 		++times;
    
 	}
    
 	while (game[x][y] != 0 && times <= maxTimes);
    
 	if (times >= maxTimes)
    
 	{
    
 	return false;
    
 	}
    
 	else
    
 	{
    
 		GameNum num;
    
 		if(whitch == 0)
    
 		{
    
 			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 = 1; row < ROW; ++row)
    
 		{
    
 			for(int crow = row;crow>=1; --crow)
    
 			{
    
 				for(int col = 0; col < COL; ++col)
    
 				{
    
 					if (game[crow-1][col] == 0)
    
 					{
    
 						game[crow-1][col] = game[crow][col];
    
 						game[crow][col] = 0;
    
 					}
    
 					else
    
 					{
    
 						if(game[crow-1][col] == game[crow][col])
    
 						{
    
 							game[crow -1][col] *=2;
    
 							game[crow][col] = 0;
    
 						}
    
  
    
 					}
    
  
    
 				}
    
 			}
    
 			
    
 		}
    
 break;
    
 			case DOWN://下
    
 				for(int row = ROW-2; row >=0;--row)
    
 				{
    
 					for(int crow = row;crow < ROW -1;++crow)
    
 					{
    
 						for(int col = 0;col < COL;++col)
    
 							if(game[crow+1][col] == 0)
    
 							{
    
 								game[crow+1][col] = game[crow][col];
    
 							}
    
 							else
    
 							{
    
 								if(game[crow+1][col] == game[crow][col])
    
 								{
    
 									game[crow+1][col] *= 2;
    
 								}
    
 							}
    
 					}
    
 				}
    
  break;
    
 			case LEFT://左
    
 				for(int col = 1;col < COL;++col)
    
 				{
    
 					for(int rcol = col; rcol >= 1;--rcol)
    
 					{
    
 						for(int row = 0;row < ROW; ++row)
    
 						{
    
 							if(game[row][rcol-1] == 0)
    
 							{
    
 								game[row][rcol-1] = game[row][rcol];
    
 								game[row][rcol] = 0;
    
 							}
    
 							else
    
 								if(game[row][rcol-1] == game[row][rcol])
    
 							{
    
 								game[row][rcol-1] *= 2;
    
 								game[row][rcol] = 0;
    
 							}
    
 						}
    
  
    
 					}
    
 				}
    
 				break;
    
 			case RIGHT://右
    
 				for(int col = COL-2; col>=0;--col)
    
 				{
    
 					for(int rcol = col; rcol <= COL-2; ++rcol)
    
 					{
    
 					for(int row =0; row < ROW; ++row)
    
 					{
    
 						if(game[row][rcol+1] == game[row][rcol])
    
 						{
    
 							game[row][rcol+1] = game[row][rcol];
    
 							game[row][rcol] = 0;
    
 						}
    
 						else
    
 						{
    
 							if (game[row][rcol+1] == game[row][rcol])
    
 							{
    
 								game[row][rcol + 1] *= 2;
    
 								game[row][rcol] = 0;
    
 							}
    
 						}
    
  
    
 					}
    
 					}
    
 				
    
 				}
    
  
    
 				break;
    
  
    
 	}
    
 	
    
 	
    
  
    
  
    
 }
    
 int Input()//界面操作
    
 {
    
 	int up_arrow = 0;
    
 	int down_arrow = 0;
    
 	int left_arrow =0;
    
 	int right_arrow =0;
    
 	int direction = 0;
    
 	while (true)
    
 	{
    
 		up_arrow = GetAsyncKeyState(VK_UP);  
    
     down_arrow = GetAsyncKeyState(VK_DOWN);  
    
     left_arrow = GetAsyncKeyState(VK_LEFT);  
    
     right_arrow = GetAsyncKeyState(VK_RIGHT);  
    
   
    
     if(up_arrow)  
    
     {  
    
         direction = UP;  
    
         break;  
    
     }  
    
     else if(down_arrow)  
    
     {  
    
         direction = DOWN;  
    
         break;  
    
     }  
    
     else if(left_arrow)  
    
     {  
    
         direction = LEFT;  
    
         break;  
    
     }  
    
     else if(right_arrow)  
    
     {  
    
         direction = RIGHT;  
    
         break;  
    
     }  
    
   
    
     Sleep(100);  
    
     }  
    
   
    
     return direction;   
    
 	
    
 }
    
 int Judge()
    
 {
    
 	for(int i= 0; i< ROW; ++i)
    
 	{
    
 		for(int j =0; j< COL; ++j)
    
 		{
    
 			if(game[i][j] == 2048)
    
 			{
    
 				return GAME_WIN;
    
 				break;
    
 			}
    
 		}
    
 	}
    
 		for (int i =0;i<ROW; ++i)
    
 		{
    
 			for(int j =0;j< COL-1;++j)
    
 			{
    
 				if(!game[i][j] || (game[i][j] == game[i][j+1]))
    
 				{
    
 					return GAME_CONTINUE;
    
 					break;
    
 				}
    
 			}
    
 		}
    
 		 
    
 		
    
 		for(int j = 0; j< COL; ++j)  
    
     {  
    
     for(int i = 0; i < ROW -1; ++i)  
    
     {  
    
         if(!game[i][j] || (game[i][j] == game[i+1][j]))  
    
         {  
    
             return GAME_CONTINUE;  
    
             break;  
    
         }  
    
     }  
    
     }  
    
  
    
 	return GAME_OVER;
    
 }
    
  
    
  
    
 int main()//主函数
    
 {
    
 	srand((unsigned int)time(0));
    
 	CreateNumber();
    
 	CreateNumber();
    
 	Print();
    
 int direction = 0;
    
 int gameState =-1;
    
 	while (true)
    
 	{
    
 		direction = Input();
    
  
    
 		gameState = Judge();
    
 		if(direction && gameState == GAME_CONTINUE)
    
 		{
    
 			Process(direction);
    
 			CreateNumber();
    
 			Print();
    
 			Sleep(100);
    
 		}
    
 		else if(gameState == GAME_WIN)
    
 			{
    
 				Print();
    
 				cout <<"bingo! you  has beat this game!"<<endl;
    
 				break;	
    
 			}
    
 	else if(gameState == GAME_OVER)
    
 	{
    
 		Print();
    
 		cout <<"game over!"<<endl;
    
 		break;
    
 	}
    
 	}
    
 	return 0;
    
 }

全部评论 (0)

还没有任何评论哟~