0

## 打算用C++ 寫一個類似2048的遊戲 求點意見

slideTiles(); 這個function是用wsad上下左右動 自己寫了很久也有很多的 bugs 沒頭緒怎麼寫 能給點建議嗎?

``````#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

//Tiles slide as far as possible in the chosen direction until they are stopped by either another tile or the edge of the grid
//If two tiles of the same number collide while moving, they will merge into a tile with the total value of the two tiles that collided.
//The resulting tile cannot merge with another tile again in the same move.

/**
* Randomly generate a number 2 or 4
* Randomly pick an available tile, put the number to the tile
*/

void putTile(int &n1, int &n2, int &n3, int &n4, int &n5, int &n6, int &n7, int &n8, int &n9)
{
// TODO: randomly put 2 or 4 to one available tile

int put [9] = {n1 , n2 , n3 ,n4 , n5 ,n6 , n7 ,n8 ,n9};

if(n1 == 0 && n2 == 0 && n3 == 0 && n4 == 0 && n5 == 0 && n6 == 0 && n7 == 0 && n8 == 0 && n9 == 0 ){
int iRand = 1 + rand() % 9;
put [iRand] = 2;
n1=put[0];
n2=put[1];
n3=put[2];
n4=put[3];
n5=put[4];
n6=put[5];
n7=put[6];
n8=put[7];
n9=put[8];

}
else{
int randn  = 1 + rand() % 2 ;
int twoorfour;
if (randn/2 == 0){
twoorfour = 2;
}
else{twoorfour = 4;}
int iRand = 1 + rand() % 9;
while(put [iRand] != 0){
iRand = 1 + rand() % 9;
}
put [iRand] = twoorfour;
n1=put[0];
n2=put[1];
n3=put[2];
n4=put[3];
n5=put[4];
n6=put[5];
n7=put[6];
n8=put[7];
n9=put[8];

}

}

/**
* Slide the nine tiles according to the given direction
* */
void slideTiles(char direction, int& n1, int& n2, int& n3, int& n4, int& n5, int& n6, int& n7, int& n8, int& n9)
{
// TODO: slide the nine tiles based on direction

int row1[3] = {n1,n2,n3};
int row2[3] = {n4,n5,n6};
int row3[3] = {n7,n8,n9};

if( direction == 'w'){

}

n1 = row1[0];
n2 = row1[1];
n3 = row1[2];
n4 = row2[0];
n5 = row2[1];
n6 = row2[2];
n7 = row3[0];
n8 = row3[1];
n9 = row3[2];

}

/**
* You can use the following two helper functions
*/
int max3(int a, int b, int c) {return a > b && a > c ? a : (b > c ? b : c);}
int min3(int a, int b, int c) {return a < b && a < c ? a : (b < c ? b : c);}

/**
* check if the maximum value of the nine input elements reaches 32
*/
bool checkWin(int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9)
{

if(n1== 32 || n2== 32 || n3== 32|| n4== 32|| n5== 32|| n6== 32|| n7== 32|| n8== 32 || n9 == 32)
{return true;}
else {
return false;
}
}

/**
* check if there are no available tiles
* player loses the game if all grids are filled
*/
bool checkLose(int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9)
{
// TODO: check if player loses

if(n1!= 0 && n2 != 0&& n3!= 0&& n4!= 0&& n5!= 0&& n6!= 0&& n7!= 0&& n8 != 0&& n9 != 0)
{return true;}
else{
return false;

}

}

// simplified 2048 game: 3x3 game board to reach 32
int a11 = 32, a12 = 0, a13 = 0, a21 = 0, a22 = 0, a23 = 0, a31 = 0, a32 = 0, a33 = 0;

int main()
{
// 3x3 empty game board
int a11 = 0, a12 = 0, a13 = 0, a21 = 0, a22 = 0, a23 = 0, a31 = 0, a32 = 0, a33 = 0;

cout << "Welcome to 2048 Game!" << endl
<< "input w|a|s|d to slide the tiles." << endl
<< "Game start! Get 32 to win!" << endl << endl;

srand((unsigned)time(NULL));
while (true)
{
//new tile will randomly appear in an empty spot on the board with a value of either 2 or 4
putTile(a11, a12, a13, a21, a22, a23, a31, a32, a33);

//print game board
cout << a11 << '\t' << a12 << '\t' << a13 << '\n'
<< a21 << '\t' << a22 << '\t' << a23 << '\n'
<< a31 << '\t' << a32 << '\t' << a33 << '\n'
<< endl;

//grab player's choice
cout << "Input direction:" << endl;
char direction;
cin >> direction;

// slide tiles
slideTiles(direction, a11, a12, a13, a21, a22, a23, a31, a32, a33);

// check win
if (checkWin(a11, a12, a13, a21, a22, a23, a31, a32, a33))
{
cout << "Congratulations! You Win!\n" << endl;
break;
}
// check lose
if (checkLose(a11, a12, a13, a21, a22, a23, a31, a32, a33))
{
cout << "Sorry! You Lose!\n" << endl;
break;
}
} // end of game loop

// final game board
cout << a11 << '\t' << a12 << '\t' << a13 << '\n'
<< a21 << '\t' << a22 << '\t' << a23 << '\n'
<< a31 << '\t' << a32 << '\t' << a33 << '\n'
<< endl;
return 0;
}

``````

### 2 個回答

3

iT邦研究生 5 級 ‧ 2020-03-15 09:47:20

2

iT邦大神 1 級 ‧ 2020-03-15 10:08:21