0% found this document useful (0 votes)
149 views

2048 Game Code

This C++ code implements a 2048 game. It includes functions to generate and print the 4x4 game board with random starting tiles, add new random tiles after player moves, and handle the logic for combining matching tiles and moving all tiles after each player input. The main game loop calls these functions to run the game, checking after each turn if the player won by reaching a 2048 tile or lost by having no available moves.

Uploaded by

mikayil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

2048 Game Code

This C++ code implements a 2048 game. It includes functions to generate and print the 4x4 game board with random starting tiles, add new random tiles after player moves, and handle the logic for combining matching tiles and moving all tiles after each player input. The main game loop calls these functions to run the game, checking after each turn if the player won by reaching a 2048 tile or lost by having no available moves.

Uploaded by

mikayil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include "pch.

h"
#include <iostream>
#include <time.h>
#include <conio.h>

using namespace std;

int randi, randj;


bool gameover = false;
bool gamewin = false;
int score = 0;
/*enum direction { LEFT = 0, RİGHT, UP, DOWN, STOP };
direction dir;*/

void gen(int mas[4][4])


{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
mas[i][j] = 0;
}
}
}
void print(int mas[4][4])
{
cout << "SCORE: " << score << endl;
cout << "---------------" << endl;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (mas[i][j] == 0)
cout << " " << " | ";
else
cout << mas[i][j] << " | ";
}cout << endl << "---------------" << endl;
}

}
int counter = 0;
void genrand(int mas[4][4], int randi, int randj)
{

while (true)
{
randi = rand() % 4;
randj = rand() % 4;
if (mas[randi][randj] == 0)
{
mas[randi][randj] = (rand() % 2 + 1) * 2;

break;
}
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (mas[i][j] == 0)
counter++;
}
}
if (counter == 0)
gameover = true;
return;
}

/*void input(int mas[4][4], char movement)


{
switch (movement)
{
case'a':
dir = left;
case'd':
dir = right;
case's':
dir = down;
case'w':
dir = up;
}
}*/

void logic(int mas[4][4], char movement)


{
if (score >= 2048)
{
gamewin = true;
return;
}
int temp = 0;
switch (movement)
{
case 'a':
for (int m = 0; m < 4; m++)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (j != 0 && mas[i][j] == mas[i][j - 1])
{
mas[i][j] = 0;
mas[i][j - 1] *= 2;
score += mas[i][j - 1];
}
else if (j != 0 && mas[i][j] != mas[i][j - 1])
{
if (mas[i][j - 1] == 0)
{
temp = mas[i][j];
mas[i][j] = mas[i][j - 1];
mas[i][j - 1] = temp;
}
}
}
}
}
break;
case 'd':
for (int m = 0; m < 4; m++)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (j != 3 && mas[i][j] == mas[i][j + 1])
{
mas[i][j] = 0;
mas[i][j + 1] *= 2;
score += mas[i][j + 1];
}
else if (j != 3 && mas[i][j + 1] == 0)
{
temp = mas[i][j];
mas[i][j] = mas[i][j + 1];
mas[i][j + 1] = temp;
}
}
}
}
break;
case 'w':
for (int m = 0; m < 4; m++)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (i != 0 && mas[i][j] == mas[i - 1][j])
{
mas[i][j] = 0;
mas[i - 1][j] *= 2;
score += mas[i - 1][j];
}
else if (i != 0 && mas[i - 1][j] == 0)
{
temp = mas[i][j];
mas[i][j] = mas[i - 1][j];
mas[i - 1][j] = temp;
}
}
}
}
break;
case 's':
for (int m = 0; m < 4; m++)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (i != 3 && mas[i][j] == mas[i + 1][j])
{
mas[i][j] = 0;
mas[i + 1][j] *= 2;
score += mas[i + 1][j];
}
else if (i != 3 && mas[i + 1][j] == 0)
{
temp = mas[i][j];
mas[i][j] = mas[i + 1][j];
mas[i + 1][j] = temp;
}
}
}
}
break;
/*case STOP:
gameover = true;
break;*/
}
}
int main()
{
srand(time(NULL));
int mas[4][4];
gen(mas);
while (gameover != true && gamewin != true)
{
genrand(mas, randi, randj);
print(mas);
char movement = _getch();
system("cls");
/*input(mas, movement); */
logic(mas, movement);
}
if (gameover == true)
cout << endl << "YOU LOST!" << endl;
else if (gamewin == true)
cout << endl << "WINNER WINNER CHICHEKN DINNER!" << endl;
}

You might also like