It's 2048 games! Powerful

Source: Internet
Author: User

# Include <climits> # include <cstdio> # include <cstring> # include <stack> # include <string> # include <map> # include <vector> # include <cmath> # include <cstdlib>

Using namespace STD;

Const int Maxx = 10;

Int box [Maxx] [Maxx];

Void moveleft (int n, bool & RET) {for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <N; + + J) {int K; For (k = J-1; k> = 0; -- k) {If (Box [I] [k]! =-1) {break;} box [I] [k + 1] = Box [I] [J];

If (k + 1 )! = J) {box [I] [J] =-1; ret = true ;}}}}

Void mergeleft (int n, bool & RET) {for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <n-1; ++ J) {If (Box [I] [J] =-1) continue;

If (Box [I] [J] = Box [I] [J + 1]) {box [I] [J] * = 2; box [I] [J + 1] =-1; ret = true; ++ J ;}}}}

Bool left (int n) {bool ret = false; moveleft (n, RET); mergeleft (n, RET); moveleft (n, RET); return ret ;}

Void moveright (int n, bool & RET) {for (INT I = 0; I <n; ++ I) {for (Int J = n-1; j> = 0; -- j) {If (Box [I] [J] =-1) continue;

Int K; For (k = J + 1; k <n; ++ K) {If (Box [I] [k]! =-1) {break ;}}

Box [I] [k-1] = Box [I] [J]; If (k-1 )! = J) {box [I] [J] =-1; ret = true ;}}}}

Void mergeright (int n, bool & RET) {for (INT I = 0; I <n; ++ I) {for (Int J = n-1; j> 0; -- j) {If (Box [I] [J] =-1) continue;

If (Box [I] [J] = Box [I] [J-1]) {box [I] [J] * = 2; box [I] [J-1] =-1; ret = true; -- J ;}}}}

Bool right (int n) {bool ret = false; moveright (n, RET); mergeright (n, RET); moveright (n, RET); return ret ;}

Void moveup (int n, bool & RET) {for (Int J = 0; j <n; ++ J) {for (INT I = 0; I <N; ++ I) {int K; For (k = I-1; k> = 0; -- k) {If (Box [k] [J]! =-1) {break;} box [k + 1] [J] = Box [I] [J];

If (k + 1 )! = I) {box [I] [J] =-1; ret = true ;}}}}

Void mergeup (int n, bool & RET) {for (Int J = 0; j <n; ++ J) {for (INT I = 0; I <n-1; ++ I) {If (Box [I] [J] =-1) continue;

If (Box [I] [J] = Box [I + 1] [J]) {box [I] [J] * = 2; box [I + 1] [J] =-1; ret = true; ++ I ;}}}}

Bool up (int n) {bool ret = false; moveup (n, RET); mergeup (n, RET); moveup (n, RET); return ret ;}

Void movedown (int n, bool & RET) {for (Int J = 0; j <n; ++ J) {for (INT I = n-1; i> = 0; -- I) {If (Box [I] [J] =-1) continue;

Int K; For (k = I + 1; k <n; ++ K) {If (Box [k] [J]! =-1) break ;}

Box [k-1] [J] = Box [I] [J];

If (k-1 )! = I) {box [I] [J] =-1; ret = true ;}}}}

Void mergedown (int n, bool & RET) {for (Int J = 0; j <n; ++ J) {for (INT I = n-1; I> 0; -- I) {If (Box [I] [J] =-1) continue;

If (Box [I] [J] = Box [I-1] [J]) {box [I] [J] * = 2; box [I-1] [J] =-1; ret = true; -- I ;}}}}

Bool down (int n) {bool ret = false; movedown (n, RET); mergedown (n, RET); movedown (n, RET); return ret ;}

Void line (int n) {for (INT I = 0; I <n; ++ I) {printf ("--------") ;}printf ("-"); printf ("\ n ");}

Void print (int n) {for (INT I = 0; I <n; ++ I) {Line (n); For (Int J = 0; j <N; ++ J) {printf ("|"); If (Box [I] [J] =-1) {printf ("\ t"); continue ;}

Printf ("% 2D \ t", box [I] [J]);} printf ("|"); printf ("\ n ");} line (n );}

Bool isfull (int n) {bool mark = true; For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <N; ++ J) {If (Box [I] [J] =-1) {mark = false; return mark ;}} return mark ;}

Bool isover (int n) {If (! Isfull (N) {return false ;}for (INT I = 0; I <n; ++ I) {for (Int J = 0; j <N; ++ J) {If (Box [I] [J] = Box [I] [J + 1] | box [I] [J] = Box [I + 1] [J ]) {return false ;}} return true ;}

Void fillbox (int n) {int I, j, num;

If (isfull (N) {return ;}

While (true) {I = rand () % N; j = rand () % N; num = rand () % 2 = 0? 2: 4;

If (Box [I] [J] =-1) {box [I] [J] = num; break ;}}}

Int main () {// freopen ("in.txt", "r", stdin); memset (box,-1, sizeof (box ));

Int N; bool mark; scanf ("% d % * C", & N );

Fillbox (n); print (N );

While (true) {char ch; scanf ("% C % * C", & Ch); If (CH = 'A') {mark = left (n );} else if (CH = 'D') {mark = right (n);} else if (CH = 'W') {mark = up (n );} else if (CH ='s ') {mark = down (n);} else {continue;} system ("CLS"); printf ("Move: \ n "); print (n); If (! Mark) {continue;} fillbox (n); printf ("fill: \ n"); print (N );

If (isover (N) {printf ("\ n \ Ngame over! \ N "); break;} return 0 ;}

/* Method Description: After compilation and running, enter an integer n to indicate the N * n table in the game you want to play, currently, the mobile phone or computer is usually 4*4, so we can enter 4 and press enter to start the game.

After the game starts, a s d w indicates four directions: Left, bottom, right, and top.

Like the real version of 2048, two tables are output. The move table displays the results after translation in the input direction, and the fill table displays a randomly filled grid table */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.