C ++ implements console version 2048 and console 2048

Source: Internet
Author: User

C ++ implements console version 2048 and console 2048
Preface

I used to play a JavaScript version 2048 game. Recently I was studying C ++. I had a sudden whim last night and wanted to implement it using C ++, because the core algorithm is quite understandable, therefore, a simple C ++ version is released in two hours.

 

 

Introduction

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

This method does not involve pointers and object-oriented ideas. Therefore, it can be the first small project for beginners of C ++.

 

 

Implementation Concept 1. Background

The background of the game is a 4*4 two-dimensional array. The game is completed by changing the value of the two-dimensional array and the position of the number in each movement.

2. Random numbers and locations

At the beginning of the game, two numbers must be randomly generated, and a new number (2/4) must be randomly generated in the blank space for each move ).

 

// Random position int randX = rand () % 4; int randY = rand () % 4; int times = 0; while (times <50) {if (0 = board [randX] [randY]) break; randX = rand () % 4; randY = rand () % 4; times ++ ;} if (50 = times) {for (int I = 0; I <4; I ++) for (int j = 0; j <4; j ++) if (0 = board [I] [j]) {randX = I; randY = j ;}}

First, a blank space is randomly obtained (that is, the two-dimensional array is equal to 0). The algorithm is optimized. The system selects 50 blank spaces at will first. If the blank space is not found, a position is manually located, the random location speed can be accelerated to a certain extent.

 

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

 

From 0 ~ Any value between 1. If the value is less than 0.5, 2 is obtained randomly. Otherwise, 4 is obtained randomly, so that the probability of occurrence of 2 and 4 is the same.

 

3. Motion Algorithms

The movements in the four directions are roughly the same, but there are slight differences in the critical points. The following is an explanation of moving left.

 

 

// Determine whether there are obstacles in 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 ;}

Determine whether there are obstacles in the row range from col1 to col2.

 

 

// Determine whether bool canMoveLeft (int board [] [4]) can be moved to the left {for (int I = 0; I <4; I ++) for (int j = 1; j <4; j ++) if (0! = Board [I] [j]) if (0 = board [I] [J-1] | board [I] [J-1] = board [I] [j]) return true; return false ;}

 

When the canvas is pressed, determine whether the entire canvas can be moved to the left.

 

// Left shift function bool moveLeft () {if (! CanMoveLeft (board) return false; // moveLeft // whether the location is empty // whether the number of the location is equal // whether there are obstacles in the moving path 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 ();}

 

Build a left-shift core algorithm based on the above two judgments.

 

 

 

Complete code

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

 

 

 

The C ++ version is intended to review basic game algorithms and be familiar with the C ++ syntax. It is not considered in details, for example, it does not include the determination of the end of the game.

However, my JavaScript version has complete functions, including the game end display, animation effect, the number of current steps, the current score, the highest score, and the previous step.

I hope you can exchange ideas.

Github link for the JavaScript version:

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

 

Related Article

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.