c++小游戏,2048升级版

# include
# include
# include
# include
# include
# include
# include

const int NUM = 4;
int Board[NUM][NUM] = { 0 };


void assingment();     //重开局赋初值
void ShowGame();       //初始化显示界面
void AddRand();        //随机初始数和随机位置
void InitGame();       //初始化游戏
void StartGame();      //开始游戏
void MoveLeft();       //向左
void MoveRight();      //向右
void MoveUp();         //向上
void MoveDown();       //向下
int  GetNUll();        //获取空位置
void GameOver();       //判断周围位置是否相同
int  TotalSc();        //总分

bool IsRand;           //是否生成随机数
bool Isover;           //游戏是否结束
int  TotalScore;       //总分
int  TotalKey;         //按的总次数


int main(void){
    //InitGame();
    StartGame();
    //AddRand();

    if (TotalScore < 1024){
        printf("\n\n太菜了!\n");
    }else{
        printf("\n\n牛逼!\n");
    }
    getchar();
    return 0;
}

void StartGame(){
    InitGame();
    while (1){
        IsRand = 0;
        char ch;
        //接收一个按键消息
        switch (ch = _getch()) {
			case 'w':
			case 'W':
			case 72:MoveUp();TotalKey++;break; 
			case 's':
			case 'S':
			case 80:MoveDown();TotalKey++;break; 
			case 'a':
			case 'A':
			case 75:MoveLeft();TotalKey++;break; 
			case 'd':
			case 'D':
			case 77:MoveRight();TotalKey++;break; 
       }
      if (IsRand){
          AddRand();
          system("cls");  //清除屏幕
          ShowGame();
      }
      if (GetNUll() == 0){
          GameOver();
      }
    }
}
void InitGame(){
    assingment();
    IsRand = 1;   //需要生成
    Isover = 1;   //没有结束
    TotalKey = 0;
    TotalScore = 0;
    AddRand();
    AddRand();
    ShowGame();
}
void assingment(){
    for (int i = 0; i < NUM; i++){
        for (int j = 0; j < NUM; j++){
            Board[i][j] = 0;
        }
    }
}
void AddRand(){
    //srand(time(1));
    while (1){
        int Row = rand() % NUM;
        int Col = rand() % NUM;
        int val = rand() % 2;
        if (Board[Row][Col] == 0){
            switch (val){
                case 0:
                Board[Row][Col] = 2;
                break;
                case 1:
                Board[Row][Col] = 4;
                break;
           }
           break;
       }else{
           continue;
       }
   }
}
void ShowGame(){
    TotalScore = TotalSc();
    printf("********************************************************************\n\n");
    printf("-----------游戏名称:2048-----------\n\n\n");
    printf("----分数:%d------移动次数:%d------\n", TotalScore, TotalKey);
    printf("┏━━━━━┳━━━━━┳━━━━━┳━━━━━┓\n");
    for (int i = 0; i < NUM; i++){
        printf("┃");
        for (int j = 0; j < NUM; j++){
            if (Board[i][j] == 0){
                 printf("     ┃");
            }else{
                printf("%4d ┃", Board[i][j]);
            }
        }
        if (i < NUM - 1){
            printf("\n┣━━━━━╋━━━━━╋━━━━━╋━━━━━┫\n");
       }else{
           printf("\n┗━━━━━┻━━━━━┻━━━━━┻━━━━━┛\n\n");
       }
    }
    printf("----------------------操作方法--------------------------\n\n");
    printf("上(↑,W,w)--下(↓,S,s)--左(←,A,a)--右(→,D,d)--\n\n\n");
    printf("*********************************************************************\n");

}
void MoveLeft()    {
    for (int i = 0; i < NUM; i++){
        for (int j = 1, k = 0; j < NUM; j++){
            if (Board[i][j] != 0){
                //k项和j项的值相等
                if (Board[i][k] == Board[i][j]){
                    Board[i][k++] *= 2;  //  <<左移运算符速率大于*号
                    Board[i][j] = 0;
                    IsRand = 1;
                }else if (Board[i][k] == 0){
                    Board[i][k++] = Board[i][j];
                    Board[i][j] = 0;
                    IsRand = 1;
                }else{
                     Board[i][++k] = Board[i][j];
                     if (k != j)  {
                         Board[i][j] = 0;
                        IsRand = 1;
                     }
               }
          }
      }
   }
}
void MoveRight()      {
    for (int i = 0; i < NUM; i++){
        for (int j = NUM - 2, k = NUM - 1; j >= 0; j--){
            if (Board[i][j] != 0){
                //k项和j项的值相等
                if (Board[i][k] == Board[i][j]){
                    Board[i][k--] *= 2;  //  <<左移运算符速率大于*号
                    Board[i][j] = 0;
                    IsRand = 1;
                }else if (Board[i][k] == 0){
                    Board[i][k--] = Board[i][j];
                    Board[i][j] = 0;
                    IsRand = 1;
                }else{
                    Board[i][--k] = Board[i][j];
                    if (k != j)  {
                        Board[i][j] = 0;
                        IsRand = 1;
                   }
               }
           }
       }
    }
}
void MoveUp()    {
for (int i = 0; i < NUM; i++)
{
for (int j = 1, k = 0; j < NUM; j++)
{
if (Board[j][i] != 0)
{
//k项和j项的值相等
if (Board[k][i] == Board[j][i])
{
Board[k++][i] *= 2;  //  <<左移运算符速率大于*号
Board[j][i] = 0;
IsRand = 1;
}
//k项为0
else if (Board[k][i] == 0)
{
Board[k++][i] = Board[j][i];
Board[j][i] = 0;
IsRand = 1;
}
//k项不为0  j和k不相等
else
{
Board[++k][i] = Board[j][i];
if (k != j)  //k已经加1了
{
Board[j][i] = 0;
IsRand = 1;
}

}
}
}
}



}
void MoveDown()       //下移
{
for (int i = 0; i < NUM; i++)
{
for (int j = NUM - 2, k = NUM - 1; j >= 0; j--)
{
if (Board[j][i] != 0)
{
//k项和j项的值相等
if (Board[k][i] == Board[j][i])
{
Board[k--][i] *= 2;  //  <<左移运算符速率大于*号
Board[j][i] = 0;
IsRand = 1;
}
//k项为0
else if (Board[k][i] == 0)
{
Board[k--][i] = Board[j][i];
Board[j][i] = 0;
IsRand = 1;
}
//k项不为0  j和k不相等
else
{
Board[--k][i] = Board[j][i];
if (k != j)  //k已经加1了
{
Board[j][i] = 0;
IsRand = 1;
}

}
}
}
}
}
int  GetNUll()
{
int sum = 0;
for (int i = 0; i < NUM; i++)
{
for (int j = 0; j < NUM; j++)
{
if (Board[i][j] == 0)
{
sum++;
}
}
}
return sum;
}
void GameOver()
{
for (int i = 0; i < NUM; i++)
{
for (int j = 0; j < NUM - 1; j++)
{
if (Board[i][j] == Board[i][j + 1] || Board[j][i] == Board[j + 1][i])
{
Isover = 1;
return;
}
}
}
Isover = 0;
}


int  TotalSc()
{
int sum = 0;
for (int i = 0; i < NUM; i++)
{
for (int j = 0; j < NUM; j++)
{
sum += Board[i][j];
}
}
return sum;
}

c++2048,我还会继续升级,大家有想法可以提出来

你可能感兴趣的:(c++小游戏,c++)