Created
July 13, 2020 11:41
-
-
Save PachUp/c5a1d3597a08c965dc547a5e23254c1a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
//Run it inC++ scripting app like visual studio or codeblocks and then running it (Raz) | |
#include <random> | |
#include <Windows.h> | |
#define zero 0 | |
enum Move { Up = 'w', Down = 's', Right = 'd', Left = 'a', Quit = 'q' }; | |
Move MoveUp = Up; | |
Move MoveDown = Down; | |
Move MoveRight = Right; | |
Move MoveLeft = Left; | |
Move QuitTheGame = Quit; | |
using namespace std; | |
int randomCoulun(); | |
int randomRow(); | |
void Map(int arr[4][4], int rows, int cols); | |
void addTheNumberAfterAMove(int Random,int Random2, bool *checkIfMovedInEmpteySpaces, int *CheckIfMoved, int arr[4][4]); | |
void moveTheNumbersDownOrUp(int arr[4][4], int i, int j, bool* checkIfMovedInEmpteySpaces, int rows); | |
void moveTheNumbersLeftOrRight(int arr[4][4], int i, int j, bool* checkIfMovedInEmpteySpaces, int cols); | |
bool checkIfPlayerWon(int arr[4][4], int rows, int cols); | |
void CheckIfPlayerWonText(); | |
void RulesAndWelcomeMessege(); | |
void findTheNumberDiffrentThanZeroLeftOrRight(int arr[4][4],int rows,int cols, bool* checkIfMovedInEmpteySpaces); | |
void findTheNumberDiffrentThanZeroUpOrDown(int arr[4][4], int rows, int cols, bool* checkIfMovedInEmpteySpaces); | |
void fillEmptySpacesForUpOrDown(char opirator,int arr[4][4],int rows,int cols,int i,int j, bool* checkIfMovedInEmpteySpaces); | |
void fillEmptySpacesForRightOrLeft(char opirator, int arr[4][4], int rows, int cols, int i, int j, bool* checkIfMovedInEmpteySpaces); | |
int CalculateFinalScore(int arr[4][4],int rows,int cols); // In work | |
void CheckIfGameIsOver(int arr[4][4], int rows, int cols, char inputMove); | |
int main() | |
{ | |
int arr[4][4] = { | |
{0, 0, 0, 0} , | |
{0, 2, 0, 0} , | |
{0, 0, 2, 0} , | |
{0, 0, 0, 0} | |
}; | |
int rows = sizeof arr / sizeof arr[0]; | |
int cols = sizeof arr[0] / sizeof(int); | |
int Random = rand() % 4; | |
int Random2 = rand() % 4; | |
int CheckIfMoved = 0; | |
int CheckTimesOf248 = 0; | |
bool checkIfMovedInEmpteySpaces = false; | |
int Check = 0; | |
char InputMove; | |
RulesAndWelcomeMessege(); | |
while (true) { | |
if (checkIfPlayerWon(arr, rows, cols) && CheckTimesOf248 == 0) { | |
CheckIfPlayerWonText(); | |
CheckTimesOf248++; | |
} | |
Map(arr, rows, cols); | |
cout << "Where do you want to move?" << endl; | |
cin >> InputMove; | |
while (InputMove != MoveUp && InputMove != MoveDown && InputMove != MoveLeft && InputMove != QuitTheGame && InputMove != MoveRight) { | |
Map(arr, rows, cols); | |
cout << "Invalid input" << endl; | |
cin >> InputMove; | |
} | |
if (InputMove == QuitTheGame) { | |
cout << "Your final score is: "<<CalculateFinalScore(arr,rows,cols); | |
cout << " Bye!" << endl; | |
return 0; | |
} | |
if (InputMove == MoveUp) { | |
findTheNumberDiffrentThanZeroUpOrDown(arr, rows, cols,&checkIfMovedInEmpteySpaces); // finds and add the numbers | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] != zero) { | |
if (arr[0][j] == zero) { | |
arr[0][j] = arr[i][j]; | |
arr[i][j] = 0; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
fillEmptySpacesForUpOrDown('-', arr, rows, cols, i, j, &checkIfMovedInEmpteySpaces); | |
} | |
} | |
} | |
addTheNumberAfterAMove(Random, Random2, &checkIfMovedInEmpteySpaces, &CheckIfMoved, arr); | |
} | |
if (InputMove == MoveDown) { | |
findTheNumberDiffrentThanZeroUpOrDown( arr, rows, cols,&checkIfMovedInEmpteySpaces); // finds and add the numbers | |
for (int i = rows-1; i >= 0; i--) { | |
for (int j = cols-1; j >= 0; j--) { | |
if (arr[i][j] != zero) { | |
if (arr[3][j] == 0) { | |
arr[3][j] = arr[i][j]; | |
arr[i][j] = zero; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
fillEmptySpacesForUpOrDown('+', arr, rows, cols, i, j, &checkIfMovedInEmpteySpaces); | |
} | |
} | |
} | |
// rand function hre | |
addTheNumberAfterAMove(Random,Random2,&checkIfMovedInEmpteySpaces,&CheckIfMoved, arr); | |
} | |
if (InputMove == MoveLeft) { | |
findTheNumberDiffrentThanZeroLeftOrRight(arr, rows, cols, &checkIfMovedInEmpteySpaces); // finds and add the numbers | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] != zero) { | |
if (arr[i][0] == zero) { | |
arr[i][0] = arr[i][j]; | |
arr[i][j] = zero; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
if (arr[i][1] == zero && arr[i][0] != zero && j>1) { | |
arr[i][1] = arr[i][j]; | |
arr[i][j] = zero; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
else if (arr[i][2] == zero && arr[i][1] != zero && arr[i][0] != zero && j>2) { | |
arr[i][2] = arr[i][j]; | |
arr[i][j] = zero; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
} | |
} | |
} | |
addTheNumberAfterAMove(Random, Random2, &checkIfMovedInEmpteySpaces, &CheckIfMoved, arr); | |
} | |
if (InputMove == MoveRight) { | |
findTheNumberDiffrentThanZeroLeftOrRight(arr,rows,cols,&checkIfMovedInEmpteySpaces);// finds and add the numbers | |
for (int i = 0; i <rows; i++) { | |
for (int j = cols-1; j >= 0; j--) { | |
if (arr[i][j] != zero) { | |
if (arr[i][3] == zero) { | |
arr[i][3] = arr[i][j]; | |
arr[i][j] = zero; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
if (arr[i][2] == zero && arr[i][3] != zero && j < 2) { | |
arr[i][2] = arr[i][j]; | |
arr[i][j] = zero; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
if (arr[i][1] == zero && arr[i][2] != zero && arr[i][3] != zero && j<1) { | |
arr[i][1] = arr[i][j]; | |
arr[i][j] = zero; | |
checkIfMovedInEmpteySpaces = true; | |
} | |
} | |
} | |
} | |
addTheNumberAfterAMove(Random, Random2, &checkIfMovedInEmpteySpaces, &CheckIfMoved, arr); | |
} | |
} | |
} | |
void Map(int arr[4][4], int rows, int cols) { | |
cout << '|'; | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
cout <<arr[i][j]; | |
cout << '|'; | |
if (j == 3) { | |
cout << endl; | |
cout << "---------" << endl; | |
} | |
} | |
if (i == rows - 1) { | |
} | |
else { | |
cout << '|'; | |
} | |
} | |
} | |
void addTheNumberAfterAMove(int Random, int Random2, bool* checkIfMovedInEmpteySpaces, int* CheckIfMoved,int arr[4][4]) { | |
if (*CheckIfMoved == 0 && *checkIfMovedInEmpteySpaces == true) { | |
*CheckIfMoved = 1; | |
} | |
// I could write insted of the if statement just if checkifmovedinemptyspaces like i did | |
for (int z = 0; z < *CheckIfMoved; z++) { | |
*checkIfMovedInEmpteySpaces = false; | |
int randomForRow = randomRow(); | |
int randomForCoulun = randomCoulun(); | |
while (1) { | |
if (arr[randomForRow][randomForCoulun] == 0) { | |
arr[randomForRow][randomForCoulun] = 2; | |
break; | |
} | |
randomForRow = randomRow(); | |
randomForCoulun = randomCoulun(); | |
} | |
} | |
*CheckIfMoved = 0; | |
} | |
bool checkIfPlayerWon(int arr[4][4], int rows, int cols) { | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] == 256) { | |
system("cls"); | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
void CheckIfPlayerWonText() { | |
char continueTheGameOrNot; | |
system("cls"); | |
cout << "You won Would you like to continue? Y/N" << endl; | |
cin >> continueTheGameOrNot; | |
if (continueTheGameOrNot == 'Y' || continueTheGameOrNot == 'y') { | |
cout << "Ok! have fun!" << endl; | |
} | |
else if (continueTheGameOrNot == 'N' || continueTheGameOrNot == 'n') { | |
exit(EXIT_FAILURE); | |
} | |
else { | |
cout << "Invalid input go fuckyourself is it really that hard to type y or n(Easter Egg)" << endl; | |
CheckIfPlayerWonText(); | |
} | |
} | |
int randomRow() { | |
string call; | |
random_device rd; | |
mt19937 number(rd()); | |
uniform_int_distribution <>random(0, 4); | |
return random(number); // prints the random number | |
} | |
int randomCoulun() { | |
string call; | |
random_device rd; | |
mt19937 number(rd()); | |
uniform_int_distribution <>random(0, 4); | |
return random(number); // prints the random number | |
} | |
void moveTheNumbersDownOrUp(int arr[4][4], int i, int j, bool* checkIfMovedInEmpteySpaces,int rows) { | |
for (int z = i + 1; z < rows; z++) { | |
if (arr[i][j] == arr[z][j]) { | |
arr[i][j] = arr[i][j] + arr[z][j]; | |
arr[z][j] = 0; | |
*checkIfMovedInEmpteySpaces = true; // could be checkedifmoved++ if you want to add the random numbers as the the adding | |
} | |
} | |
} | |
void moveTheNumbersLeftOrRight(int arr[4][4], int i, int j, bool* checkIfMovedInEmpteySpaces, int cols) { | |
for (int z = j + 1; z < cols; z++) { | |
if (arr[i][j] == arr[i][z]) { | |
arr[i][j] = arr[i][j] + arr[i][z]; | |
arr[i][z] = 0; | |
*checkIfMovedInEmpteySpaces = true; | |
} | |
} | |
} | |
void RulesAndWelcomeMessege() { | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "Hello and welcome to the special 2048" << endl; | |
cout << "In this 2048 if there is the same number twice" << endl; | |
cout << "In the row and you go up/down it will be added" << endl; | |
cout << "and if there is the same number in the coul" << endl; | |
cout << "And you go right/left these numbers will add up" << endl; | |
cout << "Demenstration:" << endl; | |
cout << "*Typing s*" << endl; | |
cout << "0000" << endl; | |
cout << "2000" << endl; | |
cout << "8000" << endl; | |
cout << "2000" << endl; | |
cout << "Result: " << endl; | |
cout << "0000" << endl; | |
cout << "0000" << endl; | |
cout << "4000" << endl; | |
cout << "8000" << endl; | |
cout << "NOTE: The upper '2' is the one who's equal to the 4 if you'd gone up, it would be the oppiste" << endl; | |
cout << "The game will start in ~10 seconds maximum 40 seconds" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "------------2048------------" << endl; | |
cout << "You can also type 'q' any time you want and leave the game" << endl; | |
Sleep(400); | |
system("cls"); | |
} | |
void findTheNumberDiffrentThanZeroLeftOrRight(int arr[4][4], int rows, int cols, bool*checkIfMovedInEmpteySpaces) { | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] != zero) { | |
moveTheNumbersLeftOrRight(arr,i,j,checkIfMovedInEmpteySpaces,rows); | |
} | |
} | |
} | |
} | |
void findTheNumberDiffrentThanZeroUpOrDown(int arr[4][4], int rows, int cols, bool* checkIfMovedInEmpteySpaces) { | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] != zero) { | |
moveTheNumbersDownOrUp(arr, i, j,checkIfMovedInEmpteySpaces, rows); | |
} | |
} | |
} | |
} | |
void fillEmptySpacesForUpOrDown(char opirator, int arr[4][4], int rows, int cols,int i,int j,bool * checkIfMovedInEmpteySpaces) { | |
if (opirator == '-') { | |
if (arr[i - 3][j] == zero) { | |
arr[i - 3][j] = arr[i][j]; | |
arr[i][j] = zero; | |
*checkIfMovedInEmpteySpaces = true; | |
} | |
else if (arr[i - 2][j] == zero) { | |
arr[i - 2][j] = arr[i][j]; | |
arr[i][j] = 0; | |
*checkIfMovedInEmpteySpaces = true; | |
} | |
else if (arr[i - 1][j] == zero) { | |
arr[i - 1][j] = arr[i][j]; | |
arr[i][j] = zero; | |
*checkIfMovedInEmpteySpaces = true; | |
} | |
} | |
else if (opirator == '+') { | |
if (arr[i + 3][j] == zero) { | |
arr[i + 3][j] = arr[i][j]; | |
arr[i][j] = zero; | |
*checkIfMovedInEmpteySpaces = true; | |
} | |
else if (arr[i + 2][j] == zero) { | |
arr[i + 2][j] = arr[i][j]; | |
arr[i][j] = 0; | |
*checkIfMovedInEmpteySpaces = true; | |
} | |
else if (arr[i + 1][j] == zero) { | |
arr[i + 1][j] = arr[i][j]; | |
arr[i][j] = zero; | |
*checkIfMovedInEmpteySpaces = true; | |
} | |
} | |
} | |
void fillEmptySpacesForRightOrLeft(char opirator, int arr[4][4], int rows, int cols, int i, int j, bool* checkIfMovedInEmpteySpaces) { | |
// on work | |
} | |
int CalculateFinalScore(int arr[4][4], int rows, int cols) { | |
int CalculateFinalScore = 0; | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
CalculateFinalScore = arr[i][j] + CalculateFinalScore; | |
} | |
} | |
return CalculateFinalScore; | |
} | |
void CheckIfGameIsOver(int arr[4][4], int rows, int cols,char InputMove) { | |
bool MaybeGameOverup = false; | |
bool MaybeGameOverdown = false; | |
bool MaybeGameOverleft = false; | |
bool MaybeGameOverright = false; | |
int CheckLastPostion[4][4] = {}; | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
arr[i][j] = CheckLastPostion[i][j]; | |
} | |
} | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] == CheckLastPostion[i][j] && InputMove == MoveUp) { | |
MaybeGameOverup = true; | |
} | |
else { | |
MaybeGameOverup = false; | |
} | |
} | |
} | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] == CheckLastPostion[i][j] && InputMove == MoveRight) { | |
MaybeGameOverdown = true; | |
} | |
else { | |
MaybeGameOverdown = false; | |
} | |
} | |
} | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] == CheckLastPostion[i][j] && InputMove == MoveLeft) { | |
MaybeGameOverleft = true; | |
} | |
else { | |
MaybeGameOverleft = false; | |
} | |
} | |
} | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
if (arr[i][j] == CheckLastPostion[i][j] && InputMove == MoveDown) { | |
MaybeGameOverright = true; | |
} | |
else { | |
MaybeGameOverright = false; | |
} | |
} | |
} | |
if (MaybeGameOverup == true&& MaybeGameOverleft == true && MaybeGameOverright == true && MaybeGameOverdown == true ) { | |
cout << "Game over" << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment