Created
August 2, 2017 15:43
-
-
Save devanshdalal/dd3fe2775260ddbb59177455797cf12f to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include <stdlib.h> | |
#include <iomanip> | |
using namespace std; | |
int rand(int i){ | |
return rand()%i; | |
} | |
class Board{ | |
public: | |
Board(const int size){ | |
dim=1<<size; | |
grid.resize(dim,vector<int>(dim,-1)); | |
int p1 = rand(1); | |
grid[p1/dim][p1%dim]=2; | |
int p2 = rand(5); | |
grid[p2/dim][p2%dim]=2; | |
} | |
void push(const char dir){ | |
switch(dir){ | |
case 'j': // left | |
for(auto i=0u;i<dim;i++){ | |
grid[i]=push_helper(grid[i]); | |
} | |
break; | |
case 'l': // right | |
for(auto i=0u;i<dim;i++){ | |
vector<int> v; | |
for(auto j=dim-1;j>=0;j--){ | |
v.push_back(grid[i][j]); | |
} | |
grid[i]=push_helper(v); | |
std::reverse(grid[i].begin(),grid[i].end()); | |
} | |
break; | |
case 'k': // down | |
for(auto i=0u;i<dim;i++){ | |
vector<int> v; | |
for(auto j=dim-1;j>=0;j--){ | |
v.push_back(grid[j][i]); | |
} | |
auto res=push_helper(v); | |
for(auto j=dim-1;j>=0;j--){ | |
grid[j][i]=res[dim-1-j]; | |
} | |
} | |
break; | |
case 'i': // up | |
for(auto i=0u;i<dim;i++){ | |
vector<int> v; | |
for(auto j=0u;j<dim;j++){ | |
v.push_back(grid[j][i]); | |
} | |
auto res=push_helper(v); | |
for(auto j=0;j<dim;j++){ | |
grid[j][i]=res[j]; | |
} | |
} | |
break; | |
default: | |
break; | |
} | |
fill_new(); | |
} | |
void print(){ | |
for(auto i=0u;i<dim;i++){ | |
for(auto j=0u;j<dim;j++){ | |
cout << setw(3) << grid[i][j] << " "; | |
} | |
cout << "\n"; | |
} | |
} | |
bool is_lost(){ | |
for(auto move:{'j','l','i','k'}) | |
{ | |
Board b = *this; | |
b.push(move); | |
for(auto i=0u;i<dim;i++){ | |
for(auto j=0u;j<dim;j++){ | |
if(b.at(i,j)!=grid[i][j]){ | |
return false; | |
} | |
} | |
} | |
} | |
return true; | |
} | |
int at(int i,int j){ | |
return grid[i][j]; | |
} | |
private: | |
int dim; | |
vector< vector<int> > grid; | |
vector<int> push_helper(vector<int>& v){ | |
vector<int> res; | |
bool mergable = true; | |
for(auto i=0u;i<v.size();i++){ | |
if(v[i]!=-1){ | |
if(mergable and !res.empty() and res.back()==v[i]){ | |
res[res.size()-1]*=2; | |
mergable=false; | |
}else{ | |
res.push_back(v[i]); | |
mergable=true; | |
} | |
} | |
} | |
res.resize(dim,-1); | |
return res; | |
} | |
vector<pair<int,int>> empty_locs(){ | |
vector<pair<int,int>> v; | |
for(auto i=0u;i<dim;i++){ | |
for(auto j=0u;j<dim;j++){ | |
if(grid[i][j]==-1){ | |
v.push_back(make_pair(i,j)); | |
} | |
} | |
} | |
return v; | |
} | |
void fill_new(){ | |
auto r=empty_locs(); | |
if(r.empty()){ | |
return; | |
} | |
int val = 1<<(1+rand(2)); | |
int p = rand(r.size()); | |
// cout << val << " " << r[p].first << " " << r[p].second << "\n"; | |
grid[ r[p].first][r[p].second]=val; | |
} | |
}; | |
int main() { | |
int size=2; | |
cin >> size ; | |
Board b(size); // 4 | |
b.print(); | |
while(true){ | |
char type; | |
cin >> type; | |
b.push(type); | |
b.print(); | |
cout << "---------------------------------------\n"; | |
if(b.is_lost()){ | |
std::cout << "Game Over\n"; | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment