Command Line 2048

Source: Internet
Author: User
Command Line 2048What is the implementation of a 2048 fire game in command line? I tried it. Here I would like to thank the CSS gods for debugging. There is also the special character used in the game space, so the program may run garbled characters in Linux. you can adjust it manually.
Games:









Code:
# Include <iostream> # include <ctime> # include <cstdlib> # include <cstring> # include <conio. h> # include <cstdlib> # include <windows. h> # define coord_x 15 # define coord_y 15 # define height 20 # define width 16 using namespace STD; Class game {public: int score; int num [5] [5]; public: Game (int s = 0); bool control (char ch); void Init (); void make ();} g; Class console {public: void gotoxy (handle Hout, int X, int y ); Void enter_game (); void window (); void show (); void start_game (); void end_game (); friend class game;} C; game: Game (int s) {score = s; memset (Num, 0, sizeof (Num);} void Console: gotoxy (handle Hout, int X, int y) {coord Pos; POS. X = x; POS. y = y; setconsolecursorposition (Hout, POS);} void Console: enter_game () {handle Hout = getstdhandle (std_output_handle); handle = getstdhandle (std_output_handl E); System ("title 2048 by TC"); gotoxy (Hout, coord_x + width-1, Coord_y-12); setconsoletextattribute (handle, foreground_intensity | foreground_red | foreground_green ); cout <"Welcome to 2 0 4 8"; setconsoletextattribute (handle, foreground_intensity | foreground_green); gotoxy (Hout, coord_x + width-25, Coord_y-8 ); cout <"********" <Endl; gotoxy (Hout, coord_x + width-25, Coord_y-7); cout <"****** ** "<Endl; gotoxy (Hout, coord_x + width-25, Coord_y-6); cout <" ******** "<Endl; gotoxy (Hout, coord_x + width-25, Coord_y-5); cout <"******" <Endl; gotoxy (Hout, coord_x + width-25, coord_y-4); cout <"************" <Endl; gotoxy (Hout, coord_x + width-25, Coord_y-3 ); cout <"******" <Endl; gotoxy (Hout, coord_x + width-25, Coord_y-2 ); cout <"********" <Endl; G Otoxy (Hout, coord_x + width-25, Coord_y-1); cout <"***********" <Endl; gotoxy (Hout, coord_x + width + 1, coord_y + 3); setconsoletextattribute (handle, foreground_intensity | foreground_red); cout <"press enter to enter GAME \ n" <Endl; while (1) {char C; If (kbhit () {c = getch (); If (C = 13) {system ("CLS"); C. start_game () ;}}if (getch () = 27) {C. end_game () ;}} void Console: end_game () {handle = get Stdhandle (std_output_handle); handle Hout = getstdhandle (std_output_handle); System ("CLS"); gotoxy (Hout, coord_x + width + 5, Coord_y-10); setconsoletextattribute (handle, foreground_intensity | foreground_red); cout <"game end"; gotoxy (Hout, coord_x + width + 5, Coord_y-7); cout <"Final score:" <G. score <Endl; setconsoletextattribute (handle, foreground_intensity | foreground_red | foreground_green); exit (0);} voi D Console: window () {handle Hout = getstdhandle (std_output_handle); handle = getstdhandle (std_output_handle); System ("title 2 0 4 8 by TC "); system ("color 0C"); gotoxy (Hout, coord_x + width-2, Coord_y-13); cout <"******"; gotoxy (Hout, coord_x + width-2, Coord_y-12); cout <"2 0 4 8"; gotoxy (Hout, coord_x + width-2, Coord_y-11 ); cout <"********"; gotoxy (Hout, coord_x + 2 * width + 3, coord_y + 6); setcons Oletextattribute (handle, foreground_intensity | foreground_red | foreground_green); gotoxy (Hout, coord_x + 2 * width + 3 + 2, coord_y-8); cout <"W key: up "<" s key: Down "; gotoxy (Hout, coord_x + 2 * width + 3 + 2, coord_y-6); cout <" A key: to the left "<" d key: Right "; gotoxy (Hout, coord_x + 2 * width + 3 + 2, coord_y-4); cout <" ESC: exit ";} void Console: Show () {handle Hout = getstdhandle (std_output_handle); handle = G Etstdhandle (std_output_handle); setconsoletextattribute (handle, handle | foreground_green | foreground_red); gotoxy (Hout, coord_x + 2 * width + 3 + 2, coord_y-10 ); cout <"; gotoxy (Hout, coord_x + 2 * width + 3 + 2, coord_y-10); cout <" score: "<G. score; For (INT I = 0; I <4; I ++) {for (Int J = 0; j <4; j ++) {If (G. num [I] [J]! = 0) {cout <"; setconsoletextattribute (handle, foreground_intensity | foreground_red); gotoxy (Hout, coord_x + 6 + J * 8, coord_x-8 + I * 4 ); cout <G. num [I] [J];} else {cout <""; setconsoletextattribute (handle, foreground_intensity | foreground_green); gotoxy (Hout, coord_x + 6 + J * 8, coord_x-8 + I * 4); cout <"■" ;}} cout <Endl ;}} bool game: control (char DIR) {int num2 [4] [4]; for (I NT n = 0; n <4; n ++) for (INT m = 0; m <4; m ++) num2 [N] [m] = num [N] [m]; bool ismove = false; If (DIR = 'W' | dir = 'W ') {for (Int J = 0; j <4; j ++) {int II = 0, temp = 5; For (INT I = 0; I <4; I ++) {If (Num [I] [J] = 0) continue; else {temp = I; break ;}} if (temp = 5) continue; num [II] [J] = num [temp] [J]; for (INT I = temp + 1; I <4; I ++) {If (Num [I] [J] = 0) continue; else if (Num [I] [J]! = 0 & num [I] [J] = num [II] [J]) {num [II] [J] * = 2; G. score + = num [II] [J]; num [I] [J] = 0;} else if (Num [I] [J]! = 0 & num [I] [J]! = Num [II] [J]) num [++ II] [J] = num [I] [J];} while (II <4) num [++ II] [J] = 0; For (INT n = 0; n <4; n ++) {for (INT m = 0; m <4; M ++) {If (num2 [N] [m]! = Num [N] [m]) {ismove = true; break ;}} if (ismove) Break ;}} return ismove ;} else if (DIR = 's' | dir = 's') {for (Int J = 0; j <4; j ++) {int II = 3, temp = 5; For (INT I = 3; I> = 0; I --) {If (Num [I] [J] = 0) continue; else {temp = I; break;} If (temp = 5) continue; num [II] [J] = num [temp] [J]; for (INT I = temp-1; I> = 0; I --) {If (Num [I] [J] = 0) continue; else if (Num [I] [J]! = 0 & num [I] [J] = num [II] [J]) {num [II] [J] * = 2; G. score + = num [II] [J]; num [I] [J] = 0;} else if (Num [I] [J]! = 0 & num [I] [J]! = Num [II] [J]) num [-- II] [J] = num [I] [J];} while (II> = 0) num [-- II] [J] = 0; For (INT n = 0; n <4; n ++) {for (INT m = 0; m <4; M ++) {If (num2 [N] [m]! = Num [N] [m]) {ismove = true; break ;}} if (ismove) Break ;}} return ismove ;} else if (DIR = 'A' | dir = 'A') {for (INT I = 0; I <4; I ++) {int JJ = 0, temp = 5; For (Int J = 0; j <4; j ++) {If (Num [I] [J] = 0) continue; else {temp = J; break;} If (temp = 5) continue; num [I] [JJ] = num [I] [temp]; for (Int J = temp + 1; j <4; j ++) {If (Num [I] [J] = 0) continue; else if (Num [I] [J]! = 0 & num [I] [J] = num [I] [JJ]) {num [I] [JJ] * = 2; G. score + = num [I] [JJ]; num [I] [J] = 0;} else if (Num [I] [J]! = 0 & num [I] [J]! = Num [I] [JJ]) {num [I] [++ JJ] = num [I] [J] ;}} while (JJ <4) {num [I] [++ JJ] = 0 ;}for (INT n = 0; n <4; n ++) {for (INT m = 0; m <4; m ++) {If (num2 [N] [m]! = Num [N] [m]) {ismove = true; break ;}} if (ismove) Break ;}} return ismove ;} else if (DIR = 'D' | dir = 'D') {for (INT I = 0; I <4; I ++) {int JJ = 3, temp = 5; For (Int J = 3; j> = 0; j --) {If (Num [I] [J] = 0) continue; else {temp = J; break;} If (temp = 5) continue; num [I] [JJ] = num [I] [temp]; for (Int J = temp-1; j> = 0 & JJ> 0; j --) {If (Num [I] [J] = 0) continue; else if (Num [I] [J]! = 0 & num [I] [J] = num [I] [JJ]) {num [I] [JJ] * = 2; G. score + = num [I] [JJ]; num [I] [J] = 0;} else if (Num [I] [J]! = 0 & num [I] [J]! = Num [I] [JJ]) num [I] [-- Jj] = num [I] [J];} while (JJ> 0) num [I] [-- Jj] = 0; For (INT n = 0; n <4; n ++) {for (INT m = 0; m <4; M ++) {If (num2 [N] [m]! = Num [N] [m]) {ismove = true; break ;}} if (ismove) Break ;}} return ismove ;}return false ;} void game :: init () {srand (unsigned INT) Time (null); int x = rand () % 4; int y = rand () % 4; G. num [x] [Y] = 2; int xx = rand () % 4; int YY = rand () % 4; while (1) {If (XX! = X & YY! = Y) {G. num [XX] [YY] = 2; break;} xx = rand () % 4; YY = rand () % 4 ;}} void game: Make () {int COUNT = 0; For (INT I = 0; I <4; I ++) {for (Int J = 0; j <4; j ++) {If (Num [I] [J]) Count ++ ;}} if (COUNT = 16) C. end_game (); int xx = rand () % 4; int YY = rand () % 4; while (1) {If (Num [XX] [YY] = 0) {num [XX] [YY] = 2; break;} xx = rand () % 4; YY = rand () % 4 ;}} void Console: start_game () {G. init (); window (); show (); char dir; while (true) {bool flag = false; Dir = getch (); If (DIR = 27) end_game (); if (DIR = 'W' | dir = 'A' | dir = 'S' | dir = 's' | dir = 'D ') {flag = G. control (DIR); window (); If (FLAG) g. make (); show () ;}} int main () {C. enter_game ();}


Command Line 2048

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.