刚刚进入大学学习编程,从零接触c++,然而却不甘心课内枯燥的c++习题,于是自己摸索着想整点好玩的。经过几天摸索、相关内容的自学,终于完成了这款小游戏!虽然这个游戏并不难实现,但对我这个大一小白来说简直是莫大的鼓舞!虽然界面仍是小黑窗,但我会继续学习graphic库的应用,继续完善下去。
源代码贴在下面啦~
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
#define SIZE 4
#define UP 1 //注意这里方向的正负。比如向上移动是要先处理第一二行,先将第二行加到第一行上,从上往下做
#define DOWN -1
#define LEFT 1
#define RIGHT -1
long int score = 0;
bool win_or_not = false;
long int delta_score = 0;
bool game_is_start = false;
int board[SIZE][SIZE] = {0};
void CreateBoard();
void PrintBoard();
bool moveLR(int direction);
bool moveUD(int direction);
void CreateNum(bool whether_only_two);
bool JudgeEnd();
int main()
{
srand(time(0));
CreateBoard();
PrintBoard();
bool move = false;
int key = _getch();
while (true)
{
key = _getch();
game_is_start = true;
switch (key)
{
case 72:
{
move = moveUD(UP);
break;
}
case 80:
{
move = moveUD(DOWN);
break;
}
case 75:
{
move = moveLR(LEFT);
break;
}
case 77:
{
move = moveLR(RIGHT);
break;
}
}
if (move)
{
CreateNum(false);
PrintBoard();
if (JudgeEnd())
{
break;
}
}
key = _getch();
}
if (win_or_not)
{
cout << "Game Over! Congratulations! You Win!" << endl;
cout << "Your final scores is " << score << endl;
}
else
{
cout << "Game Over! You Lose!" << endl;
cout << "Your final scores is " << score << endl;
}
cout << "Press any key to quit!" << endl;
getchar();
return 0;
}
void CreateBoard()
{
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
board[i][j] = 0;
CreateNum(true);
CreateNum(true);
}
void PrintBoard()
{
system("cls");
system("color 0B");
cout << '\t' << "Here is 2048 GAME!!";
cout<<endl<<"Press up down left or right to get a 2048 block!"<<endl;
cout << '\t'<<"Your current score is: " << score << endl;
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
{
cout << board[i][j];
if (j == SIZE - 1)
cout << endl;
else
cout << '\t';
}
cout << endl;
if (game_is_start)
{
cout << "This round's score: " << "+" << delta_score << "!" << endl;
delta_score = 0;
}
}
void CreateNum(bool whether_only_two)
{
int count=0,index[SIZE * SIZE] = { 0 };
for (int i = 0; i < SIZE * SIZE; i++)
if (board[0][i] == 0) index[count++] = i; //令index数组里面放数值为0的坐标,方便等等操作
if (count != 0)
{
int random = rand() % count, chance = rand() % 10;
if (whether_only_two) board[0][index[random]] = 2;
else
{
if (chance <= 7) // 80%的概率是2
board[0][index[random]] = 2;
else
board[0][index[random]] = 4;
}
}
}
bool JudgeEnd()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (board[i][j] >= 2048)
{
win_or_not = true; //记录获胜
return true;
}
if (board[i][j] == 0 || (j<SIZE-1 && board[i][j] ==board[i][j+1] ) || (i<SIZE-1 && board[i][j] ==board[i+1][j]) )
return false;
}
}
return true;
}
bool moveUD(int direction)
{
bool move = false;
for (int line = 0; line < SIZE; line++)
{
int current = 0,i=1;
if (direction == DOWN)
{
current = SIZE - 1;
i = current - 1;
}
while (i < SIZE && i >= 0)
{
if (board[i][line])
{
if (board[i][line] == board[current][line])
{
board[current][line] *= 2;
board[i][line] = 0;
score += board[current][line];
delta_score += board[current][line];
current+=direction;
move = true;
}
else if (board[current][line] == 0)
{
board[current][line] = board[i][line];
board[i][line] = 0;
move = true;
}
else if (i != current + direction)//0 2 0 4 移动时应将2移到4旁边,此时current与i不再紧挨着,需要加此判断!
{
board[current + direction][line] = board[i][line];
board[i][line] = 0;
current += direction;
move = true;
}
else
current+=direction;
}
i+=direction;
}
}
return move;
}
bool moveLR(int direction)
{
bool move = false;
for (int row = 0; row < SIZE; row++)
{
int current = 0, i = 1;
if (direction == RIGHT)
{
current = SIZE - 1;
i = current - 1;
}
while (i >= 0 && i < SIZE)
{
if (board[row][i])
{
if (board[row][i] == board[row][current])
{
board[row][current] *= 2;
board[row][i] = 0;
score += board[row][current];
delta_score += board[row][current];
move = true;
current += direction;
}
else if (board[row][current] == 0)
{
board[row][current] = board[row][i];
board[row][i] = 0;
move = true;
}
else if (i != current + direction)//向右, 0 2 0 4这种情况,应该将2向右移,此时current=3,i=1,若无此句将无法移动
{
board[row][current+direction] = board[row][i];
board[row][i] = 0;
current += direction;
move = true;
}
else
current += direction;
}
i += direction;
}
}
return move;
}
核心部分的实现是moveLR和moveUP两个函数:
moveLR是对左右移动的处理,moveUP是上下移动。
CreateNum这个函数里有随机生成方块的规则,如上面我是设定了0.8的概率生成2。当然如果你想降低难度,就适当修改生成的概率吧(坏笑
本文就到这里啦,谨记录本人第一个较有成就感的小程序!