-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented Up,Down,Left,Right behaviour of 2048 Grid.
- Loading branch information
0 parents
commit 6262968
Showing
3 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
class Grid{ | ||
int rows; | ||
int columns; | ||
|
||
long* grid; | ||
|
||
long score; | ||
|
||
|
||
|
||
|
||
}; |
This file contains 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
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); | ||
|
||
|
||
|
||
|
||
} |
This file contains 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
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); | ||
} | ||
|
||
|
||
|