Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <time.h>
- #include <windows.h>
- using namespace std;
- int map[16] = {0};
- int step = 0;
- char k;
- void draw(){
- system( "cls" );
- cout << "steps:" << step << "\n";
- for( int i = 0 ; i < 16 ; i++ ){
- if( map[i] == 0 ){
- cout << "\t";
- }
- else{
- cout << map[i] << "\t";
- }
- if( i % 4 == 3 ){
- cout << "\n";
- }
- }
- }
- void random(){
- int x = 0;
- while( x < 1 ){
- int index = rand() % 16;
- int number = rand() % 5;
- if( number ==2 || number == 4 ){
- if(map[index] ==0){
- map[ index ] = number;
- x+=1;
- }
- }
- }
- }
- void init(){
- int x = 0;
- while( x < 3 ){
- int index = rand() % 16;
- int number = rand() % 5;
- if( number ==2 || number == 4 ){
- map[ index ] = number;
- x+=1;
- }
- }
- }
- void push_f(){
- for(int i = 3;i <=15 ; i +=4){
- for(int j = i-1 ; j>=i-3 ; j-- ){
- int k = j;
- while(1){
- if(map[k+1]==0){
- int m_f = map[k+1];
- map[k+1] = map[k];
- map[k] = m_f;
- }
- else if(map[k+1]==map[k]){
- map[k+1]= map[k+1]*2;
- map[k]=0;
- }
- k=k+1;
- if(k ==i)break;
- }
- }
- }
- }
- push_s(){
- for(int i = 0;i <=12 ; i +=4){
- for(int j = i+1 ; j<=i+3 ; j++ ){
- int k = j;
- while(1){
- if(map[k-1]==0){
- int m_s = map[k-1];
- map[k-1] = map[k];
- map[k] = m_s;
- }
- else if(map[k-1]==map[k]){
- map[k-1]= map[k-1]*2;
- map[k]=0;
- }
- k=k-1;
- if(k ==i)break;
- }
- }
- }
- }
- void push_e(){
- for(int i = 0;i <=3 ; i ++){
- for(int j = i+4 ; j<=i+12 ; j+=4 ){
- int k = j;
- while(1){
- if(map[k-4]==0){
- int m_e = map[k-4];
- map[k-4] = map[k];
- map[k] = m_e;
- }
- else if(map[k-4]==map[k]){
- map[k-4]= map[k-4]*2;
- map[k]=0;
- }
- k=k-4;
- if(k ==i)break;
- }
- }
- }
- }
- void push_d(){
- for(int i = 12 ; i <=15 ; i ++){
- for(int j = i-4 ; j>=i-12 ; j-=4 ){
- int k = j;
- while(1){
- if(map[k+4]==0){
- int m_d = map[k+4];
- map[k+4] = map[k];
- map[k] = m_d;
- }
- else if(map[k+4]==map[k]){
- map[k+4]= map[k+4]*2;
- map[k]=0;
- }
- k=k+4;
- if(k ==i)break;
- }
- }
- }
- }
- int main(){
- init();
- draw();
- while(1){
- if( kbhit() ){
- k = _getch();
- if( k == 'q' ){
- draw();
- break;
- }
- if( k == 'f' ){
- push_f();
- random();
- step++;
- }
- if( k == 's' ){
- push_s();
- random();
- step++;
- }
- if( k =='e' ){
- push_e();
- random();
- step++;
- }
- if( k == 'd' ){
- push_d();
- random();
- step++;
- }
- }
- draw();
- Sleep(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement