How to implement a number matching game using javascript
The editor below will share with you an example of how to implement a digital matching game using JavaScript. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.
The game effect is shown in the picture below:
Rules:
In the 4X5 grid, there are 10 random numbers that are not equal to each other, and there are two copies of each data (that is, 20 numbers, there are two Two equal ten pairs), randomly distributed in 20 grids. The game starts and a sequence of twenty numbers pops up. Each time you click the grid, the data in the current grid will be displayed and temporarily retained until the next click. If the data displayed by the next click is different from the retained data, the data retained by the previous click will disappear (still exist in the grid but not show). If the two data displayed by continuous clicks are the same, both data will be displayed and will not disappear again.
Until all data are displayed by continuously clicking on the same data, the game is over and the game time is reported. At this point you can click Start Game or Refresh to continue.
Analysis:
1: Twenty grids correspond to twenty data, two sets of ten equal random numbers are generated and placed Array, the array subscript determines the display position.
2: There are three states of each grid: data hiding, temporary data retention and permanent display. The data hidden grid is temporarily retained by the next click. The grid that temporarily retains data will be permanently displayed or data hidden by clicking the next time. It is judged based on whether the data obtained twice in a row are equal. After permanent display, the status is no longer changeable and can only be displayed permanently. At this time, it is invalid for clicks.
3: Timing starts when the first grid is clicked after clicking the start button. The timer cannot be stopped until the game is completed or you click Refresh to restart.
4: It follows that a Boolean value is needed here to record whether the game has started. The game that has already started should reject the start button, and the timer runs until the game is completed. When the game is complete, change the boolean value, the timer stops working, the game time is displayed, and the start button becomes available.
Implementation:
The table is created through script, and the elements in it are first displayed as "" empty strings by default. Displayed by corresponding click. CSS styles can be set by yourself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
NEW_STARTVariable that records whether the game can be started
times records the elapsed time
trans records the flip status of each grid, each array The grid has three states: 0: hidden - 1: displayed (can still be flipped) - 2: displayed (cannot be flipped). That is, each element of the array has only three possible values 0, 1, 2)
numArr A random sequence array of thirty numbers
1 2 3 4 |
|
Method of obtaining elements through ID:
1 2 3 |
|
The following is a function to obtain twenty random numbers, ten equal groups (see: Generating random numbers in a specified range)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Creating tables and generating random number arrays are all preparatory work.
The following is the specific logic:
Click function to start the game
1 2 3 4 |
|
Click to start the game, you need to initialize the game related Parameters, please note that if it has already started, you need to reject the processing. Use the sorting function to shuffle the array elements to achieve randomness.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Timing function
When you click on the first grid, you need to start timing. NEW_START=false means it has already started. You need to ensure that the timer is only started when the game is in progress. Calls itself once per second and displays the elapsed time in real time through innerHTML.
1 2 3 4 5 6 7 8 |
|
The click function of each grid (super important)
Refuses to click the grid before it starts (no effect). Enter the game and click on the first grid. The game starts and the status changes. NEW_START=false means that a new game has been started and cannot be created. Timing begins.
The subsequent click event needs to determine the clicked grid to process different logic:
Clicking on an element that has been permanently displayed does not process return.
Clicking on an element that has just been displayed but is not permanently displayed does not handle return.
(Note that to determine whether the same element is the same element, the index index is found directly in trans through the status value and compared)
Click on the undisplayed element to get the value and compare it with the previously displayed element Comparison:
is equal, the status value of the corresponding index in trans is changed to 2, indicating that
is permanently displayed, and the corresponding index status value of the newly clicked element in trans is changed to 1 (temporarily Reserved), the index value of the previously clicked element is 0 (needs to be hidden).
After setting the status value, you need to update the display immediately (refreshUI function). When updating the display, it operates based on the array trans that records the status value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
Set the displayed function refreshUI based on the status value
根据trans中每个元素的值,改变对应索引的格子的值。注意,如果格子的数据永久显示,需要记录已经永久显示的格子的数量,当等于所有格子数量时,表示已经全部显示。需要判定游戏结束,显示出游戏用时。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
通过数组和表格的配合,实现How to implement a number matching game using javascript,加深对表格创建和数组的运用。处理逻辑和数据显示分离,根据状态值做到不同显示的状态。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
The above is the detailed content of How to implement a number matching game using javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
