2048 小游戲 主要是針對邏輯思維的一個訓練. 主要學習方面:1.隨機數產生的概率.2.行與列在進行移動的時候幾種情況.3.MessageBox的使用 include include include include using namespace std; int board[4][4] = {0 ...
2048 小游戲 主要是針對邏輯思維的一個訓練.
主要學習方面:1.隨機數產生的概率.2.行與列在進行移動的時候幾種情況.3.MessageBox的使用
#include <iostream>
#include <time.h>
#include <conio.h>
#include <windows.h>
using namespace std;
int board[4][4] = {0}; //二維數組
int if_need_rand; //是否生成隨機數
int if_game_over; //游戲結束
void showGame() { //列印邊框
system("cls"); //刷新屏幕
printf("┏━━━┳━━━┳━━━┳━━━┓\n");
for (int i = 0; i < 4; i++) {
printf("┃");
for (int j = 0; j < 4;j++) {
if (board[i][j]==0) {
printf(" ┃");
}
else {
printf("%3d┃",board[i][j]);
}
}
if (i<3) {
printf("\n┣━━━╋━━━╋━━━╋━━━┫\n");
}
else {
printf("\n┗━━━┻━━━┻━━━┻━━━┛\n");
}
}
}
//設計隨機數
void addRand() {
//隨機數種子
srand((unsigned)time(NULL));
int i, j;//行列
while (1) {
i = rand() % 4;
j = rand() % 4;
if (board[i][j]==0) {
//三目運算符,幾率時2分之一
board[i][j] = (rand() % 3 ? 2 : 4);
break;
}
else {
continue;
}
}
}
//初始化
void initGame() {
if_need_rand = 1;
if_game_over = 0;
int i, j;
i = rand() % 4;
j = rand() % 4;
board[i][j] = 2;
//將數組內值,賦值為0;重新初始化
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; j++) {
board[i][j] = 0;
}
}
addRand();
showGame();
}
//方向分為上下左右
//左移
void moveleft() {
for (int i = 0; i < 4;i++) {
for (int j = 1, k = 0; j < 4;++j) {
//先找到K項後面第一個不為0的項
if (board[i][j]!=0) {
//分為三個情況.相等,k項是0,不等
//相等
if (board[i][k]==board[i][j]) {
board[i][k++] <<= 1; //k++先賦值給k而後k++到下一個位置,進行判斷
board[i][j] = 0;
if_need_rand = 1;
}else
//k項是0
if (board[i][k]==0) {
board[i][k] = board[i][j];
board[i][j] = 0;
if_need_rand = 1;
}
//k項與j項不等,有兩種情況,j項就在k項後邊,
//所以不能將j項賦值為0
else {
board[i][++k] = board[i][j];
if (j!=k) {
board[i][j] = 0;
if_need_rand = 1;
}
}
}
}
}
}
//右移
void moveRinght() {
for (int i = 0; i < 4; i++) {
for (int j = 2, k = 3; j >= 0; j--) {
//先找到K項後面第一個不為0的項
if (board[i][j] != 0) {
//分為三個情況.相等,k項是0,不等
//相等
if (board[i][k] == board[i][j]) {
board[i][k--] <<= 1;
board[i][j] = 0;
if_need_rand = 1;
}
else
//k項是0
if (board[i][k] == 0) {
board[i][k] = board[i][j];
board[i][j] = 0;
if_need_rand = 1;
}
//k項與j項不等,有兩種情況,j項就在k項後邊,
//所以不能將j項賦值為0
else {
board[i][--k] = board[i][j];
if (j != k) {
board[i][j] = 0;
if_need_rand = 1;
}
}
}
}
}
}
//上移
void moveup() {
for (int i = 0; i < 4; i++) {
for (int j = 1, k = 0; j < 4; ++j) {
//先找到K項後面第一個不為0的項
if (board[j][i] != 0) {
//分為三個情況.相等,k項是0,不等
//相等
if (board[k][i] == board[j][i]) {
board[k++][i] <<= 1;
board[j][i] = 0;
if_need_rand = 1;
}
else
//k項是0
if (board[k][i] == 0) {
board[k][i] = board[j][i];
board[j][i] = 0;
if_need_rand = 1;
}
//k項與j項不等,有兩種情況,j項就在k項後邊,
//所以不能將j項賦值為0
else {
board[++k][i] = board[j][i];
if (j != k) {
board[j][i] = 0;
if_need_rand = 1;
}
}
}
}
}
}
//下移
void moveDown() {
for (int i = 0; i < 4; i++) {
for (int j = 2, k = 3; j >=0; --j) {
//先找到K項後面第一個不為0的項
if (board[j][i] != 0) {
//分為三個情況.相等,k項是0,不等
//相等
if (board[k][i] == board[j][i]) {
board[k--][i] <<= 1;
board[j][i] = 0;
if_need_rand = 1;
}
else
//k項是0
if (board[k][i] == 0) {
board[k][i] = board[j][i];
board[j][i] = 0;
if_need_rand = 1;
}
//k項與j項不等,有兩種情況,j項就在k項後邊,
//所以不能將j項賦值為0
else {
board[--k][i] = board[j][i];
if (j != k) {
board[j][i] = 0;
if_need_rand = 1;
}
}
}
}
}
}
//查看有沒有空著的格子
int getNull() {
int n = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (board[i][j] == 0) {
n++;
}
}
}
return n;
}
//比較相鄰的兩個數,是否相等
void gameOver() {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == board[i][j + 1] || board[j][i] == board[j + 1][i]) {
if_game_over = 0;
return;
}
}
}
if_game_over = 1;
}
//執行函數
void startGame() {
on:
initGame();
while (1) {
if_need_rand = 0;//首先將隨機值設為0.
switch (_getch())
{
case 'a':
case 'A':
case 75:
moveleft();
break;
case 'd':
case 'D':
case 77:
moveRinght();
break;
case 'w':
case 'W':
case 72:
moveup();
break;
case 's':
case 'S':
case 80:
moveDown();
default:
break;
}
if (if_need_rand) {
addRand();
showGame();
}
//游戲判斷
if (getNull()==0) {
gameOver();
if (if_game_over) {
if (MessageBox(NULL,L"是否重來一局!",L"游戲結束",MB_YESNO)==IDYES) {
goto on;
}
else {
return;
}
}
}
}
}
int main() {
startGame();
return 0;
}