这是效果图
用c++写的,其实就是用了c++的输入输出,剩下全是C的内容
代码中有详解,很简单,欢迎借鉴和交流
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include <conio.h> //为了读取方向键
#include <iomanip> //设置控制台填充字符等
using namespace std;
int score = 0;
int map[4][4]; //棋盘
void showMap() //显示棋盘
{
cout << setw(46) << "2048 by DoubleCake" << endl;
cout << setw(50) << " |-----------------------|" << endl;
for (int i = 0; i <= 3; i++)
{
cout << setw(24) << "";
for (int j = 0; j <= 3; j++)
{
if (map[i][j] == 0)
cout << setw(2) << "|" << setw(4) << " ";
else
cout << setw(2) << "|" << setw(4) << map[i][j];
if (j == 3)
{
cout << setw(2) << "|" << endl;
cout << setw(50) << " |-----------------------|" << endl;
}
}
}
}
void randNum() //从随机位置产生数
{
int m = rand() % 4;
int n = rand() % 4;
while (map[m][n]){
m = rand() % 4;
n = rand() % 4;
}
map[m][n] = 2;
}
void startGame() //开始游戏
{
memset(map, 0, sizeof(map));
randNum();
showMap();
}
int moveUp() //上移
{
int i, j;
int res = 0;//用来记录是否发生移动
for (j = 0; j < 4; j++){
//先补一次空位
for (i = 1; i < 4; i++){
if (map[i][j] && !map[i - 1][j]){
map[i - 1][j] = map[i][j];
map[i][j] = 0;
//把空位补到底
if (i > 1)
i -= 2;
res = 1;
}
}
//从根部开始,相同的合并
for (i = 1; i < 4; i++){
if (map[i][j] && map[i][j] == map[i - 1][j]){
map[i - 1][j] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
//再补一次空位
for (i = 1; i < 4; i++){
if (map[i][j] && !map[i - 1][j]){
map[i - 1][j] = map[i][j];
map[i][j] = 0;
//把空位补到底
if (i > 1)
i -= 2;
}
}
}
return res;
}
int moveDown() //下移
{
int i, j;
int res = 0;
for (j = 0; j < 4; j++){
for (i = 2; i >= 0; i--){
if (map[i][j] && !map[i + 1][j]){
map[i + 1][j] = map[i][j];
map[i][j] = 0;
if (i < 2)
i += 2;
res = 1;
}
}
for (i = 2; i >= 0; i--){
if (map[i][j] && map[i][j] == map[i + 1][j]){
map[i + 1][j] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
for (i = 2; i >= 0; i--){
if (map[i][j] && !map[i + 1][j]){
map[i + 1][j] = map[i][j];
map[i][j] = 0;
if (i < 2)
i += 2;
}
}
}
return res;
}
int moveLeft()
{
int i, j;
int res = 0;
for (i = 0; i < 4; i++){
for (j = 1; j < 4; j++){
if (map[i][j] && !map[i][j - 1]){
map[i][j - 1] = map[i][j];
map[i][j] = 0;
if (j > 1)
j -= 2;
res = 1;
}
}
for (j = 1; j < 4; j++){
if (map[i][j] && map[i][j] == map[i][j - 1]){
map[i][j - 1] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
for (j = 1; j < 4; j++){
if (map[i][j] && !map[i][j - 1]){
map[i][j - 1] = map[i][j];
map[i][j] = 0;
if (j > 1)
j -= 2;
}
}
}
return res;
}
int moveRight()
{
int i, j;
int res = 0;
for (i = 0; i < 4; i++){
for (j = 2; j >= 0; j--){
if (map[i][j] && !map[i][j + 1]){
map[i][j + 1] = map[i][j];
map[i][j] = 0;
if (j < 2)
j += 2;
res = 1;
}
}
for (j = 2; j >= 0; j--){
if (map[i][j] && map[i][j] == map[i][j + 1]){
map[i][j + 1] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
for (j = 2; j >= 0; j--){
if (map[i][j] && !map[i][j + 1]){
map[i][j + 1] = map[i][j];
map[i][j] = 0;
if (j < 2)
j += 2;
}
}
}
return res;
}
int maxNum() //棋盘最大数
{
int max = map[0][0];
for (int i = 0; i <= 3; i++)
for (int j = 0; j <= 3; j++)
if (map[i][j]>max)
max = map[i][j];
return max;
}
int Win() //判断是否胜利
{
int flag = 0;
if (maxNum() == 2048)
{
cout << setw(45) << "You Win!" << endl;
flag = 1;
}
return flag;
}
int GameOver() //判断是否游戏结束
{
int flag = 1;
int i, j;
//如果有空位就可以继续
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (!map[i][j])
flag = 0;
//如果没有空位但是有相邻相同的数也可以继续
if (flag == 1){
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
if (i != 0 && map[i][j] == map[i - 1][j])
flag = 0;
if (i != 3 && map[i][j] == map[i + 1][j])
flag = 0;
if (j != 0 && map[i][j] == map[i][j - 1])
flag = 0;
if (j != 3 && map[i][j] == map[i][j + 1])
flag = 0;
}
}
}
if (flag == 1)
cout << setw(43) << "Game Over!" << endl;
return flag;
}
void keydown() //读取方向
{
int ch = _getch();
srand((unsigned)time(NULL));
switch (ch)
{
case 72: // top
if (moveUp()){
randNum();
system("cls");
showMap();
}
break;
case 75: // left
if (moveLeft()){
randNum();
system("cls");
showMap();
}
break;
case 77: // right
if (moveRight()){
randNum();
system("cls");
showMap();
}
break;
case 80: // down
if (moveDown()){
randNum();
system("cls");
showMap();
}
break;
default:
break;
}
}
int main() //主函数
{
system("color f9");
int makesure = 1; //游戏结束后是否继续游戏
while (makesure)
{
system("cls");
startGame();
while (Win() + GameOver() == 0)
{
keydown();
}
cout << setw(43) << "你的最后成绩为:" << score << endl;
cout << setw(60) << "若要重新开始游戏请输入1,若要结束请输入0。" << endl;
cin >> makesure;
while (makesure != 1 && makesure != 0)
{
cout << "输入不正确,请重新输入!" << endl;
cin >> makesure;
}
}
cout << "再见!" << endl;
system("pause");
return 0;
}