2048 Console Edition

Source: Internet
Author: User

This is

Written in C + +, in fact, using the input and output of C + +, the rest is all C content

The code is detailed, very simple, welcome to learn from and exchange

#include <iostream> #include <stdlib.h> #include <string.h> #include <time.h> #include <    Conio.h>//In order to read the direction key # include <iomanip>//set console padding characters such as using namespace Std;int score = 0;int Map[4][4]; Checkerboard void Showmap ()//display checkerboard {cout << setw ($) << "2048 by Doublecake" << endl;cout << setw (+) &lt ;< "|-----------------------|" << endl;for (int i = 0; I <= 3; i++) {cout << setw () << ""; for (i NT J = 0; J <= 3; J + +) {if (map[i][j] = = 0) cout << setw (2) << "|" << SETW (4) << ""; Elsecout << SETW (2) <&lt ; "|" << SETW (4) << map[i][j];if (j = = 3) {cout << setw (2) << "|" << endl;cout << SETW (50 ) << "|-----------------------|" << Endl;}}} void Randnum ()//Generate number from random position {int m = rand ()% 4;int n = rand ()% 4;while (Map[m][n]) {m = rand ()% 4;n = rand ()% 4;} Map[m][n] = 2;} void Startgame ()//start Game {memset (map, 0, sizeof (map)); Randnum (); Showmap ();} int moveUp ()//Move Up {int I, j;int res = 0;//is used to record whether the move for (j = 0; J < 4; J + +) {////First fill in slots for (i = 1; i < 4; i++) {if (Map[i][j] &&amp ;!map[i-1][j]) {map[i-1][j] = map[i][j];map[i][j] = 0;//fill the void if (i > 1) i-= 2;res = 1;}} Starting from the root, the same merge for (i = 1; i < 4; i++) {if (Map[i][j] && map[i][j] = Map[i-1][j]) {Map[i-1][j] *= 2;score + = m AP[I][J];MAP[I][J] = 0;res = 1;}} Fill in the vacancy for (i = 1; i < 4; i++) {if (Map[i][j] &&!map[i-1][j]) {map[i-1][j] = map[i][j];map[i][j] = 0;//fill the Void Exactly if (i > 1) i-= 2;}}} return res;} int MoveDown ()//Move Down {int I, j;int res = 0;for (j = 0; J < 4; J + +) {for (i = 2; I >= 0; i--) {if (Map[i][j] &&!m Ap[i + 1][j]) {map[i + 1][j] = map[i][j];map[i][j] = 0;if (i < 2) i + = 2;res = 1;}} for (i = 2; I >= 0; i--) {if (Map[i][j] && map[i][j] = = Map[i + 1][j]) {map[i + 1][j] *= 2;score + = Map[i][j];map I [j] = 0;res = 1;}} for (i = 2; I >= 0; i--) {if (Map[i][j] &&!map[i + 1][j]) {map[i + 1][j] = map[i][j];map[i][j] = 0;if (i < 2) i + = 2;}}} return res;} int MoveLeft () {int I, j;int res = 0;for (i = 0; i < 4; i++) {for (j = 1; j < 4; J + +) {if (Map[i][j] &&!map[i] [J-1]) {Map[i][j-1] = map[i][j];map[i][j] = 0;if (J > 1) J-= 2;res = 1;}} for (j = 1; j < 4; J + +) {if (Map[i][j] && map[i][j] = = Map[i][j-1]) {map[i][j-1] *= 2;score + = map[i][j];map[ I][J] = 0;res = 1;}}  for (j = 1; j < 4; J + +) {if (Map[i][j] &&!map[i][j-1]) {map[i][j-1] = map[i][j];map[i][j] = 0;if (J > 1) J -= 2;}}} return res;} int MoveRight () {int I, j;int res = 0;for (i = 0; i < 4; i++) {for (j = 2; J >= 0; j--) {if (Map[i][j] &&!map[ I][j + 1]) {map[i][j + 1] = map[i][j];map[i][j] = 0;if (J < 2) J + = 2;res = 1;}} for (j = 2; J >= 0; j--) {if (Map[i][j] && map[i][j] = = map[i][j + 1]) {map[i][j + 1] *= 2;score + = Map[i][j];map I [j] = 0;res = 1;}} for (j = 2; J >= 0; j--) {if (Map[i][j] &&!map[i][j + 1]) {map[i][j + 1] = map[i][j];map[i][j] = 0;if (J < 2) J + = 2;}}} return res;}int maxnum ()//checkerboard Maximum number {int max = map[0][0];for (int i = 0; I <= 3; i++) for (int j = 0; J <= 3; j + +) if (map[i][j]> max) max = Map[i][j];return Max;} int win ()//determine if victory {int flag = 0;if (maxnum () = = 2048) {cout << setw ($) << "you win!" << Endl;flag = 1 ;} return flag;} int Gameover ()//Determine if the game is over {int flag = 1;int I, j;//if there is a vacancy you can continue for (i = 0; i < 4; i++) for (j = 0; J < 4; j + +) if ( !MAP[I][J]) flag = 0;//If there is no vacancy but there are adjacent identical numbers can also continue if (flag = = 1) {for (i = 0; i < 4; i++) {for (j = 0; J < 4; J + +) {if (I! = 0 && Map[i][j] = = Map[i-1][j]) flag = 0;if (i! = 3 && Map[i][j] = = Map[i + 1][j]) flag = 0;if (J! = 0 && Amp MAP[I][J] = = map[i][j-1]) flag = 0;if (J! = 3 && Map[i][j] = = map[i][j + 1]) flag = 0;}} if (flag = = 1) cout << setw << "Game over!" << endl;return flag;} void KeyDown ()//read direction {int ch = _getch (); Srand ((unsigned) time (NULL)); switch (CH) {case 72://topif (MoveUp ()) {randnum (); System ("CLS"); Showmap ();} Break;case://Leftif (MoveLeft ()) {randnum (); System ("CLS"); Showmap ();} break;case 77://Rightif (MoveRight ()) {randnum (); System ("CLS"); Showmap ();} Break;case 80://downif (MoveDown ()) {randnum (); System ("CLS"); Showmap ();} Break;default:break;} int main ()//main function {System ("color F9"); int makesure = Whether to continue the game after the 1;//game while (makesure) {System ("CLS"); Startgame (); Win () + gameover () = = 0) {KeyDown ();} cout << setw << "Your final score is:" << score << endl;cout << setw (+) << "To restart the game, enter 1 to end Please enter 0.  "<< endl;cin >> makesure;while (makesure! = 1 && makesure! = 0) {cout <<" input not correct, please re-enter! "<< Endl;cin >> makesure;}} cout << "Good-bye!" "<< Endl;system (" pause "); return 0;}


2048 Console Edition

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.