#include <iostream>
#include <algorithm>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <cstring>
#include <iomanip>
using namespace std;
enum Status { Running, Over };
bool restart = true;
int MaxScore = 0;
class Game_2048 {
private:
int Map[4][4], score;
char Direction;
bool Change;
Status s;
public:
Game_2048() {
memset(Map, 0, sizeof(Map));
score = 0;
s = Running;
Change = true;
}
inline void Start(void);
inline void Print(void);
inline void NewNum(void);
inline void Move(void);
inline void MoveUp(void);
inline void MoveDown(void);
inline void MoveLeft(void);
inline void MoveRight(void);
inline void Check(void);
inline int GetScore(void) { return score; }
};
inline void Print2048(void);
inline void Game_2048::NewNum(void) {
int i, j, tmp;
if (Change) {
do {
i = ((unsigned)rand()) % 4;
j = ((unsigned)rand()) % 4;
} while (Map[i][j] != 0);
tmp = ((unsigned)rand()) % 2;
if (tmp == 0)
Map[i][j] = tmp + 2;
else
Map[i][j] = tmp + 3;
}
}
inline void Game_2048::Print(void) {
system("CLS");
cout << "Welcome to 2048 Game." << endl;
Print2048();
cout << "The Game was made by Ye Luoye on Oct. 29th." << endl;
cout << "Directions: W-Up, S-Down, A-Left, D-Right" << endl;
cout << "If you want to quit the game, please press the number 0." << endl;
cout << "Your Current Score: " << score << endl;
cout << "Max Score:" << MaxScore << endl;
cout << "" << endl;
for (int i = 0; i < 4; ++i) {
cout << "|";
for (int j = 0; j < 4; ++j) {
if (Map[i][j])
cout << " " << setw(4) << Map[i][j] << " |";
else
cout << " |";
}
cout << endl;
}
}
inline void Game_2048::Start(void) {
while (s != Over) {
//system("cls");
NewNum();
Print();
Move();
Check();
}
}
inline void Game_2048::Move(void) {
Direction = _getch();
Change = false;
switch (Direction) {
case '0' :{
cout << "Do you want to quit 2048 Game?(Y/N)" << endl;
char ch = _getch();
if (ch == 'Y' || ch == 'y')
exit(0);
break;
}
case 'W': case 'w': {
MoveUp(); break;
}
case 'S': case 's': {
MoveDown(); break;
}
case 'D': case 'd': {
MoveRight(); break;
}
case 'A': case 'a': {
MoveLeft(); break;
}
}
return;
}
inline void Game_2048::MoveUp(void) {
for (int j = 0; j < 4; ++j)
for (int i = 0; i < 4; ++i) {
int now = Map[i][j];
if (now != 0) {
for (int k = 1 + i; k < 4; ++k) {
int nxt = Map[k][j];
if (nxt != 0) {
if (nxt == now) {
Change = true;
Map[k][j] = 0;
Map[i][j] = 2 * now;
score += 2 * now;
}
k = 4;
}
}
}
}
for (int j = 0; j < 4; ++j)
for (int i = 0; i < 4; ++i) {
int now = Map[i][j];
if (now == 0) {
for (int k = 1 + i; k < 4; ++k) {
int nxt = Map[k][j];
if (nxt != 0) {
Change = true;
Map[i][j] = nxt;
Map[k][j] = 0;
k = 4;
}
}
}
}
}
inline void Game_2048::MoveDown(void) {
for (int j = 0; j < 4; ++j)
for (int i = 3; i >= 0; --i) {
int now = Map[i][j];
if (now != 0) {
for (int k = i - 1; k >= 0; --k) {
int nxt = Map[k][j];
if (nxt != 0) {
if (nxt == now) {
Change = true;
Map[k][j] = 0;
Map[i][j] = 2 * now;
score += 2 * now;
}
k = -1;
}
}
}
}
for (int j = 0; j < 4; ++j)
for (int i = 3; i >= 0; --i) {
int now = Map[i][j];
if (now == 0) {
for (int k = i - 1; k >= 0; --k) {
int nxt = Map[k][j];
if (nxt != 0) {
Change = true;
Map[i][j] = nxt;
Map[k][j] = 0;
k = -1;
}
}
}
}
}
inline void Game_2048::MoveLeft(void) {
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
int now = Map[i][j];
if (now != 0) {
for (int k = j + 1; k < 4; ++k) {
int nxt = Map[i][k];
if (nxt != 0) {
if (nxt == now) {
Map[i][k] = 0;
Map[i][j] = 2 * now;
score += 2 * now;
}
k = 4;
}
}
}
}
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j) {
int now = Map[i][j];
if (now == 0) {
for (int k = j + 1; k < 4; ++k) {
int nxt = Map[i][k];
if (nxt != 0) {
Change = true;
Map[i][k] = 0;
Map[i][j] = nxt;
k = 4;
}
}
}
}
}
inline void Game_2048::MoveRight(void) {
for (int i = 0; i < 4; ++i)
for (int j = 3; j >= 0; --j) {
int now = Map[i][j];
if (now != 0) {
for (int k = j - 1; k >= 0; --k) {
int nxt = Map[i][k];
if (nxt != 0) {
if (nxt == now) {
Change = true;
Map[i][k] = 0;
Map[i][j] = 2 * now;
score += 2 * now;
}
k = -1;
}
}
}
}
for (int i = 0; i < 4; ++i)
for (int j = 3; j >= 0; --j) {
int now = Map[i][j];
if (now == 0) {
for (int k = j - 1; k >= 0; --k) {
int nxt = Map[i][k];
if (nxt != 0) {
Change = true;
Map[i][k] = 0;
Map[i][j] = nxt;
k = -1;
}
}
}
}
}
inline void Game_2048::Check(void) {
s = Over;
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 4; ++j)
if ((i > 1 && Map[i][j] == Map[i - 1][j]) || (j > 1 && Map[i][j] == Map[i][j - 1]) || Map[i][j] == 0) {
s = Running;
return;
}
}
inline void Print2048(void) {
cout << " ... ... . ... " << endl;
cout << " . . . . .. . . " << endl;
cout << " . .. . . ... . .. " << endl;
cout << " . . . . . . . ." << endl;
cout << " . .. . . . ... " << endl;
cout << " . . . . . . .. " << endl;
cout << " . . . ...... . . " << endl;
cout << " . . . . . .. " << endl;
cout << " ..... ... . ... " << endl;
}
int main(void) {
srand((unsigned)time(NULL));
while (restart) {
Game_2048 g1;
g1.Start();
cout << "Game Over!" << endl;
int CurrentScore = g1.GetScore();
cout << "Your final score: " << CurrentScore << endl;
if (CurrentScore > MaxScore) {
cout << "You have made a new record!" << endl;
MaxScore = CurrentScore;
}
cout << "Press R to start a new game, or press any other keys to quit." << endl;
char ch = _getch();
if (ch != 'R') break;
}
return 0;
}