这是一个用c++编写的2048,输入wasd控制移动,望支持!
代码如下:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int SIZE = 4;
void printBoard(int board[][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
cout << board[i][j] << "\t";
}
cout << endl;
}
}
void addRandom(int board[][SIZE]) {
int emptyCount = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
emptyCount++;
}
}
}
if (emptyCount == 0) {
return;
}
int r = rand() % emptyCount;
emptyCount = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
if (emptyCount == r) {
board[i][j] = (rand() % 2 + 1) * 2;
return;
}
emptyCount++;
}
}
}
}
void shiftLeft(int board[][SIZE]) {
for (int i = 0; i < SIZE; i++) {
int lastNonZero = -1;
for (int j = 0; j < SIZE; j++) {
if (board[i][j] != 0) {
if (lastNonZero == -1) {
lastNonZero = j;
} else if (board[i][j] == board[i][lastNonZero]) {
board[i][lastNonZero] *= 2;
board[i][j] = 0;
lastNonZero = -1;
} else {
lastNonZero = j;
}
}
}
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == 0) {
int k = j + 1;
while (k < SIZE && board[i][k] == 0) {
k++;
}
if (k < SIZE) {
board[i][j] = board[i][k];
board[i][k] = 0;
}
}
}
}
}
void shiftRight(int board[][SIZE]) {
for (int i = 0; i < SIZE; i++) {
int lastNonZero = -1;
for (int j = SIZE - 1; j >= 0; j--) {
if (board[i][j] != 0) {
if (lastNonZero == -1) {
lastNonZero = j;
} else if (board[i][j] == board[i][lastNonZero]) {
board[i][lastNonZero] *= 2;
board[i][j] = 0;
lastNonZero = -1;
} else {
lastNonZero = j;
}
}
}
for (int j = SIZE - 1; j >= 0; j--) {
if (board[i][j] == 0) {
int k = j - 1;
while (k >= 0 && board[i][k] == 0) {
k--;
}
if (k >= 0) {
board[i][j] = board[i][k];
board[i][k] = 0;
}
}
}
}
}
void shiftUp(int board[][SIZE]) {
for (int j = 0; j < SIZE; j++) {
int lastNonZero = -1;
for (int i = 0; i < SIZE; i++) {
if (board[i][j] != 0) {
if (lastNonZero == -1) {
lastNonZero = i;
} else if (board[i][j] == board[lastNonZero][j]) {
board[lastNonZero][j] *= 2;
board[i][j] = 0;
lastNonZero = -1;
} else {
lastNonZero = i;
}
}
}
for (int i = 0; i < SIZE; i++) {
if (board[i][j] == 0) {
int k = i + 1;
while (k < SIZE && board[k][j] == 0) {
k++;
}
if (k < SIZE) {
board[i][j] = board[k][j];
board[k][j] = 0;
}
}
}
}
}
void shiftDown(int board[][SIZE]) {
for (int j = 0; j < SIZE; j++) {
int lastNonZero = -1;
for (int i = SIZE - 1; i >= 0; i--) {
if (board[i][j] != 0) {
if (lastNonZero == -1) {
lastNonZero = i;
} else if (board[i][j] == board[lastNonZero][j]) {
board[lastNonZero][j] *= 2;
board[i][j] = 0;
lastNonZero = -1;
} else {
lastNonZero = i;
}
}
}
for (int i = SIZE - 1; i >= 0; i--) {
if (board[i][j] == 0) {
int k = i - 1;
while (k >= 0 && board[k][j] == 0) {
k--;
}
if (k >= 0) {
board[i][j] = board[k][j];
board[k][j] = 0;
}
}
}
}
}
int main() {
srand(time(NULL));
int board[SIZE][SIZE] = {0};
addRandom(board);
addRandom(board);
printBoard(board);
while (true) {
char move;
cin >> move;
switch (move) {
case 'a':
shiftLeft(board);
break;
case 'd':
shiftRight(board);
break;
case 'w':
shiftUp(board);
break;
case 's':
shiftDown(board);
break;
default:
cout << "Invalid move" << endl;
continue;
}
addRandom(board);
printBoard(board);
}
return 0;
}