C + + Implementation Console version 2048

Source: Internet
Author: User
Tags rand

Objective

Before doing a JavaScript version of the 2048 game, recently in C + +, last night suddenly whim, want to use C + + to achieve, because the core algorithm has been very understanding, so two hours to roll out a simple version of C + +.

Brief introduction

Two-dimensional array traversal, C + + basic data type, string class, control structure, function.

This method does not involve pointers and object-oriented thinking, so it can be the first small project for newcomers to C + +.

Implementation ideas1. Background

The game's background is a 4*4 two-dimensional array, which is accomplished by changing the value of the two-dimensional array in each motion and the position of the number.

2. Random Numbers and Locations

At the beginning of the game, a random number of two digits is required, and each move requires a new number (2/4) to be randomly located in the space.

    Random one position    int randx = rand ()%4;    int randY = rand ()%4;    int times = 0;    while (Times <)    {        if (0 = = Board[randx][randy]) break            ;        Randx = rand ()%4;        RandY = rand ()%4;        times++;    }    if (= = times)    {for        (int. i=0; i<4; i++) for            (int j=0; j<4; j + +)                if (0 = = Board[i][j])                { C21/>randx = i;                    RandY = j;                }    }

First randomly get a space (that is, two-dimensional array equal to 0), in the algorithm has been optimized, first by the system arbitrarily select 50 times, if not found in the space, then manually find a position, to a certain extent, speed up the location of random.

    Random a number    int randomnumber = rand ()%100*0.01 < 0.5? 2:4;

Arbitrary value between the 0~1, if less than 0.5 is randomly obtained 2, the inverse is randomly obtained 4, guaranteed to appear 2 and 4 the same probability.

3. Motion algorithm

The motion in four directions is roughly the same, except that there is a slight difference at the critical point, which is explained by moving to the left.

Determine if there are obstructions on the horizontal path bool NoBlock1 (int row, int col1, int col2, int board[][4]) {for    (int i=col1+1; i<col2; i++)        if ( 0! = Board[row][i])            return false;    return true;}

Each object is judged by whether there are obstructions in row row, from col1 to col2.

Determines whether to move the bool Canmoveleft (int board[][4]) {for    (int i=0; i<4; i++) to the left (int        j=1; j<4; j + +)            if (0! = Boa RD[I][J])                if (0 = = Board[i][j-1] | | board[i][j-1] = = Board[i][j])                   return true;    return false;}

When you press ←, determine whether the entire canvas can move to the left.

Left shift function bool MoveLeft () {    if (!canmoveleft (board))        return false;    MoveLeft    //Whether the location is empty    //The location number is equal    //moving path in whether there is an obstacle for    (int i=0; i<4; i++) for        (int j=0; j<4; J + +)            if (0! = Board[i][j])                for (int k=0; k<j; k++)                    if (0 = = Board[i][k] && noBlock1 (i, K, J, board )                    {                        //move                        board[i][k] = board[i][j];                        BOARD[I][J] = 0;                        Continue;                    }                    else if (board[i][k] = = Board[i][j] && noBlock1 (i, K, J, board))                    {                        //move and add                        board[i][k] *= 2;                        Board[i][j] = 0;                        Continue;                    }    Initial ();}

Based on the above two judgments, the left-shift core algorithm is constructed.

Full Code

Https://files.cnblogs.com/files/henuzyx/2048.zip

C + + version is intended to review the basic game algorithm, familiar with C + + syntax, and not in the details, such as no game to add the end of the decision.

However, my JavaScript version is fully functional, including the end of the game display, animation effects, record the current number of steps, record the current score, save the highest score, undo back to the previous step.

I hope we can exchange discussions.

JavaScript version of GitHub Links:

Https://github.com/henuzyx/2048-by-JavaScript

C + + Implementation Console version 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.