Вы находитесь на странице: 1из 4

#include

#include
#include
#include
#include

<iostream>
<vector>
<algorithm>
<random>
<chrono>

using namespace std;


class game
{
//Variables
int size_board;
vector<vector<int>> board;
default_random_engine dre;
//Functions
//Random Number Generation
int rand();
//Random Number Input
void putrand();
public:
//Constructors
game(int);
//Member Functions
//User Input Update
void update(char c);
//Input/Output
friend ostream & operator<<(ostream&,const game &);
friend istream & operator>>(istream&,game &);
//wWin check
bool win();
};
game::game(int s=4)
{
size_board=s;
dre.seed(chrono::system_clock::to_time_t(chrono::system_clock::now()));
board.assign(size_board,vector<int> (size_board,0));
this->putrand();
cout<<*this;
}
int game::rand()
{
int count_t(0);
for(const auto &x:board)
count_t+=count(x.begin(),x.end(),0);
uniform_int_distribution<int> uid(1,count_t);
return uid(dre);
}
void game::putrand()
{
auto rnd=this->rand();
int count_t(0);

for(auto &x:board)
{
for(auto pt=x.begin();pt!=x.end();++pt)
{
pt=find(pt,x.end(),0);
if(pt==x.end())
break;
++count_t;
if(count_t==rnd)
{
*pt=2;
return;
}
}
}
}
void game::update(char c)
{
vector<int> vect(size_board);
auto t=[](vector<int> & vec)->void
{
auto f=[](int & x,int & y)->bool
{
if(x==y)
{
x*=2;
return true;
}
return false;
};
auto it=stable_partition(vec.begin(),vec.end(),[](int x){return
x!=0;});
auto pt=unique(vec.begin(),it,f);
fill(pt,vec.end(),0);
};
switch(c)
{
case 'a':for(auto &x:board)
{
vect.assign(x.begin(),x.end());
t(vect);
x.assign(vect.begin(),vect.end());
}
break;
case 'd':for(auto &x:board)
{
vect.assign(x.rbegin(),x.rend());
t(vect);
x.assign(vect.rbegin(),vect.rend());
}
break;
case 'w':for(int i=0;i!=size_board;++i)
{
for(int j=0;j!=size_board;++j)
{
vect[j]=(board[j][i]);
}
t(vect);
for(int j=0;j!=size_board;++j)

board[j][i]=vect[j];
}
break;
case 's':for(int i=0;i!=size_board;++i)
{
for(int j=0;j!=size_board;++j)
{
vect[size_board-j-1]=(board[j][i]);
}
t(vect);
for(int j=0;j!=size_board;++j)
board[j][i]=vect[size_board-j-1];
}
break;
default:cout<<"Wrong Character";
return;
}
this->putrand();
}
istream &operator>>(istream & is,game & game_o)
{
char c;
is>>c;
game_o.update(c);
return is;
}
ostream &operator<<(ostream & os,const game & game_o)
{
os<<endl;
for(const auto & x:game_o.board)
{
for(const auto & y:x)
os<<y<<"\t";
os<<endl;
}
return os;
}
bool game::win()
{
for(auto &x:board)
{
auto pt=find(x.begin(),x.end(),0);
if(pt!=x.end())
return false;
}
cout<<"You won\n";
return true;
}
int main()
{
int k;
cout<<"Please enter board size"<<endl;
cin>>k;
game obj(k);
while(!obj.win())

{
cout<<"Enter any character w a s d"<<endl;
cin>>obj;
cout<<obj;
}
return 0;
}