360 lines
6.6 KiB
C++
360 lines
6.6 KiB
C++
#include "../inc/header.h"
|
|
|
|
|
|
Case matrice[GRID][GRID] = {{Case()}};
|
|
Case oldMatrice[GRID][GRID] = {{Case()}};
|
|
|
|
|
|
void game(int *argc, char **argv)
|
|
{
|
|
initializationGlut(argc, argv);
|
|
|
|
// At the beginning, the grid will have only 2 cases
|
|
getRandomNewCase();
|
|
getRandomNewCase();
|
|
|
|
glutMainLoop();
|
|
}
|
|
|
|
|
|
void getRandomNewCase(void)
|
|
{
|
|
std::random_device rd;
|
|
std::mt19937 mt(rd());
|
|
std::uniform_int_distribution<int> random_position(0, GRID * GRID);
|
|
|
|
short i, j;
|
|
int temp;
|
|
|
|
do
|
|
{
|
|
temp = int(random_position(mt));
|
|
j = temp / GRID;
|
|
i = temp - (j * GRID);
|
|
|
|
} while(matrice[i][j].getState() == FILLED);
|
|
|
|
matrice[i][j].setState(FILLED);
|
|
|
|
// A '2' case has 90% chance of appearing
|
|
std::uniform_real_distribution<double> random_digit(0.0, 10.0);
|
|
if(random_digit(mt) <= 9)
|
|
{
|
|
matrice[i][j].setValue((unsigned int)2);
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setValue((unsigned int)4);
|
|
}
|
|
|
|
oldMatrice[i][j].setState(matrice[i][j].getState());
|
|
oldMatrice[i][j].setValue(matrice[i][j].getValue());
|
|
}
|
|
|
|
|
|
bool caseFree(void)
|
|
{
|
|
for(short i(0); i < GRID; i++)
|
|
{
|
|
for(short j(0); j < GRID; j++)
|
|
{
|
|
if(matrice[i][j].getState() == EMPTY)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool gridFinished(void)
|
|
{
|
|
if(!caseFree())
|
|
{
|
|
for(short i(1); i < GRID - 1; i++)
|
|
{
|
|
for(short j(1); j < GRID - 1; j++)
|
|
{
|
|
if(matrice[i][j].getValue() == matrice[i - 1][j].getValue() ||
|
|
matrice[i][j].getValue() == matrice[i + 1][j].getValue() ||
|
|
matrice[i][j].getValue() == matrice[i][j - 1].getValue() ||
|
|
matrice[i][j].getValue() == matrice[i][j + 1].getValue())
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(matrice[0][0].getValue() == matrice[0][1].getValue() ||
|
|
matrice[0][0].getValue() == matrice[1][0].getValue() ||
|
|
matrice[0][GRID - 1].getValue() == matrice[0][GRID - 1 - 1].getValue() ||
|
|
matrice[0][GRID - 1].getValue() == matrice[1][GRID - 1].getValue() ||
|
|
matrice[GRID - 1][0].getValue() == matrice[GRID - 1 - 1][0].getValue() ||
|
|
matrice[GRID - 1][0].getValue() == matrice[GRID - 1][1].getValue() ||
|
|
matrice[GRID - 1][ GRID - 1].getValue() == matrice[GRID - 1 - 1][GRID - 1].getValue() ||
|
|
matrice[GRID - 1][ GRID - 1].getValue() == matrice[GRID - 1][GRID - 1 - 1].getValue())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
bool matriceGot(unsigned int power)
|
|
{
|
|
for(short i(0); i < GRID; i++)
|
|
{
|
|
for(short j(0); j < GRID; j++)
|
|
{
|
|
if(matrice[i][j].getValue() == power && matrice[i][j].getState() == FILLED)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
bool matriceHavePowers(void)
|
|
{
|
|
for(short i(3); i <= (GRID * GRID) + 1; i++)
|
|
{
|
|
/* Doesn't have the powers of 2 from 3 to 17 */
|
|
if(!matriceGot((unsigned int)pow((double)2, (double)i)))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/* Neither 1 or 2 simultaneously */
|
|
if(!matriceGot((unsigned int)2) && !matriceGot((unsigned int)4))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool matriceDidChanged(void)
|
|
{
|
|
bool hasChanged(false);
|
|
|
|
for(short i(0); i < GRID; i++)
|
|
{
|
|
for(short j(0); j < GRID; j++)
|
|
{
|
|
if(oldMatrice[i][j].getValue() != matrice[i][j].getValue() || oldMatrice[i][j].getState() != matrice[i][j].getState())
|
|
{
|
|
hasChanged = true;
|
|
|
|
oldMatrice[i][j].setState(matrice[i][j].getState());
|
|
oldMatrice[i][j].setValue(matrice[i][j].getValue());
|
|
}
|
|
}
|
|
}
|
|
|
|
return hasChanged;
|
|
}
|
|
|
|
|
|
bool collision(Case case1, Case case2)
|
|
{
|
|
return case1.getValue() == case2.getValue() ? true : false;
|
|
}
|
|
|
|
|
|
void motionUp(void)
|
|
{
|
|
for(short j(0); j < GRID; j++)
|
|
{
|
|
for(short i(1); i < GRID; i++)
|
|
{
|
|
if(matrice[i][j].getState() == FILLED)
|
|
{
|
|
short k(i - 1);
|
|
for(; k > 0; k--)
|
|
{
|
|
if(matrice[k][j].getState() == FILLED)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(matrice[k][j].getState() == FILLED)
|
|
{
|
|
if(collision(matrice[k][j], matrice[i][j]))
|
|
{
|
|
matrice[k][j].setValue(2 * matrice[i][j].getValue());
|
|
matrice[i][j].setState(EMPTY);
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[k + 1][j].setValue(matrice[i][j].getValue());
|
|
matrice[k + 1][j].setState(FILLED);
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[k][j].setValue(matrice[i][j].getValue());
|
|
matrice[k][j].setState(FILLED);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void motionDown(void)
|
|
{
|
|
for(short j(0); j < GRID; j++)
|
|
{
|
|
for(short i(GRID - 1 - 1); i >= 0; i--)
|
|
{
|
|
if(matrice[i][j].getState() == FILLED)
|
|
{
|
|
short k(i + 1);
|
|
for(; k < GRID - 1; k++)
|
|
{
|
|
if(matrice[k][j].getState() == FILLED)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(matrice[k][j].getState() == FILLED)
|
|
{
|
|
if(collision(matrice[k][j], matrice[i][j]))
|
|
{
|
|
matrice[k][j].setValue(2 * matrice[i][j].getValue());
|
|
matrice[i][j].setState(EMPTY);
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[k - 1][j].setValue(matrice[i][j].getValue());
|
|
matrice[k - 1][j].setState(FILLED);
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[k][j].setValue(matrice[i][j].getValue());
|
|
matrice[k][j].setState(FILLED);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void motionLeft(void)
|
|
{
|
|
for(short i(0); i < GRID; i++)
|
|
{
|
|
for(short j(1); j < GRID; j++)
|
|
{
|
|
if(matrice[i][j].getState() == FILLED)
|
|
{
|
|
short k(j - 1);
|
|
for(; k > 0; k--)
|
|
{
|
|
if(matrice[i][k].getState() == FILLED)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(matrice[i][k].getState() == FILLED)
|
|
{
|
|
if(collision(matrice[i][k], matrice[i][j]))
|
|
{
|
|
matrice[i][k].setValue(2 * matrice[i][j].getValue());
|
|
matrice[i][j].setState(EMPTY);
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[i][k + 1].setValue(matrice[i][j].getValue());
|
|
matrice[i][k + 1].setState(FILLED);
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[i][k].setValue(matrice[i][j].getValue());
|
|
matrice[i][k].setState(FILLED);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void motionRight(void)
|
|
{
|
|
for(short i(0); i < GRID; i++)
|
|
{
|
|
for(short j(GRID - 1 - 1); j >= 0; j--)
|
|
{
|
|
if(matrice[i][j].getState() == FILLED)
|
|
{
|
|
short k(j + 1);
|
|
for(; k < GRID - 1; k++)
|
|
{
|
|
if(matrice[i][k].getState() == FILLED)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(matrice[i][k].getState() == FILLED)
|
|
{
|
|
if(collision(matrice[i][k], matrice[i][j]))
|
|
{
|
|
matrice[i][k].setValue(2 * matrice[i][j].getValue());
|
|
matrice[i][j].setState(EMPTY);
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[i][k - 1].setValue(matrice[i][j].getValue());
|
|
matrice[i][k - 1].setState(FILLED);
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
matrice[i][j].setState(EMPTY);
|
|
matrice[i][k].setValue(matrice[i][j].getValue());
|
|
matrice[i][k].setState(FILLED);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|