Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
implemented Up,Down,Left,Right behaviour of 2048 Grid.
  • Loading branch information
AnasImloul authored Jun 2, 2022
0 parents commit 6262968
Show file tree
Hide file tree
Showing 3 changed files with 334 additions and 0 deletions.
23 changes: 23 additions & 0 deletions grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@











class Grid{
int rows;
int columns;

long* grid;

long score;




};
51 changes: 51 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<iostream>
#include "move.cpp"
#include <vector>
#include<chrono>

int64_t get_time(){
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}


int digit(long number){
int digits = 1;
while (number >= 10){
digits++;
number = number / 10;
}
return digits;
}



void show(long* grid, int rows, int columns){
for (int i = 0; i<rows*columns; i++){
std::cout << grid[i] << (((i==0 && columns != 1) || (i+1)%columns) ? " " : "\n");
}
}

int main(){

std::vector<Move> arr;

long grid[16] = {2,0,2,0,2,0,2,0,2,0,2,0,2,4,8,8};

show(grid, 4,4);

int64_t start = get_time();

for (int i = 0; i<10'000'000; i++){
Move move = left(grid,4,4);
}

std::cout << get_time() - start << std::endl;

//arr.push_back(Move(0,1013,false,grid,0));

//show(move.grid, 4,4);




}
260 changes: 260 additions & 0 deletions move.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
#include<stdio.h>

struct Move{

int merged;
long score;
bool blocked;
long *grid;
int index;

Move(int merged, long score, bool blocked, long *grid, int index){
this->merged = merged;
this->score = score;
this->blocked = blocked;
this->grid = grid;
this->index = index;
}

int Compare (const Move other) {
return
((merged > other.merged) || (merged == other.merged)&&(score > other.merged)) ? 1:

(merged == other.merged)&&(score == other.merged) ? 0:

-1;
}


bool operator == (const Move other) const {
return (merged == other.merged)&&(score == other.score);
}

bool operator < (const Move other) const {
return ((other.merged > merged) || (other.merged == merged)&&(other.score > score));
}

bool operator > (const Move other) const {
return ((merged > other.merged) || (merged == other.merged)&&(score > other.score));
}
};


Move up(long* grid, const int &rows, const int &columns){


int merged = 0;

long score = 0;

bool blocked = true;

for (int col = 0; col < columns; col++){
for (int row = 0; row < rows; row++){
if (grid[columns * row + col] == 0){
blocked = false;
continue;
}

int k = row;
bool merge = true;

while (k > 0){

long current = (grid[col + columns * k]);
long previous = (grid[col + columns * (k - 1)]);

if (previous == 0){

grid[col + columns * k] = previous;
grid[col + columns * (k-1)] = current;

blocked = false;
}

if (merge and previous == current){
grid[col + columns * (k-1)] += current;

score += (current << 1);

grid[col + columns * k] = 0;

merged += 1;

blocked = false;

merge = false;
}

k -= 1;
}
}
}
return Move(merged,score,blocked,grid,0);
}



Move down(long* grid, const int &rows, const int &columns){


int merged = 0;

long score = 0;

bool blocked = true;

for (int col = 0; col < columns; col++){
for (int row = rows - 1; row >= 0; row--){
if (grid[columns * row + col] == 0){
blocked = false;
continue;
}

int k = row;
bool merge = true;

while (k < rows - 1){

long current = (grid[col + columns * k]);
long previous = (grid[col + columns * (k + 1)]);

if (previous == 0){

grid[col + columns * k] = previous;
grid[col + columns * (k+1)] = current;

blocked = false;
}

if (merge and previous == current){
grid[col + columns * (k+1)] += current;

score += (current << 1);

grid[col + columns * k] = 0;

merged += 1;

blocked = false;

merge = false;
}

k += 1;
}
}
}
return Move(merged,score,blocked,grid,0);
}


Move left(long* grid, const int &rows, const int &columns){


int merged = 0;

long score = 0;

bool blocked = true;

for (int row = 0; row < rows; row++){
for (int col = 0; col < columns; col++){
if (grid[columns * row + col] == 0){
blocked = false;
continue;
}


int k = col;
bool merge = true;

while (k > 0){

long current = grid[columns * row + k];
long previous = grid[columns * row + (k-1)];

if (previous == 0){

grid[columns * row + k] = previous;
grid[columns * row + (k-1)] = current;

blocked = false;
}

if (merge and previous == current){
grid[columns * row + (k-1)] += current;

score += (current << 1);

grid[columns * row + k] = 0;

merged += 1;

blocked = false;

merge = false;
}

k -= 1;
}
}
}
return Move(merged,score,blocked,grid,0);
}


Move right(long* grid, const int &rows, const int &columns){


int merged = 0;

long score = 0;

bool blocked = true;

for (int row = 0; row < rows; row++){
for (int col = columns - 1; col >= 0; col--){
if (grid[columns * row + col] == 0){
blocked = false;
continue;
}

int k = col;
bool merge = true;

while (k < columns - 1){

long current = grid[columns * row + k];
long previous = grid[columns * row + (k+1)];
if (previous == 0){

grid[columns * row + k] = previous;
grid[columns * row + (k+1)] = current;

blocked = false;
}

if (merge and previous == current){
grid[columns * row + (k+1)] += current;

score += (current << 1);

grid[columns * row + k] = 0;

merged += 1;

blocked = false;

merge = false;
}

k += 1;
}
}
}
return Move(merged,score,blocked,grid,0);
}



0 comments on commit 6262968

Please sign in to comment.