Example of jsp-based word game and jsp-based word game

Source: Internet
Author: User
Tags javascript array

Example of jsp-based word game and jsp-based word game

Make a recall childhood game, the game code is relatively simple, mainly to master the principles of algorithms, but there are some points to note.

Game Interface

Go to the topic. The project is as follows:

The game has a starting interface for you to select the player's role. Then you can select the player's first hand and start the game. The selection interface provides a mask layer for the user to choose from. After the selection, the mask layer is hidden and the game starts.

Implementation

It may be boring to say so much. The following describes the specific code implementation.

Use a two-dimensional array panel to save the status of the board. 1 indicates the computer value and-1 indicates the player value.
WinArr stores all the 8-digit combinations that may win. It maintains computerWin and userWin. The initial value is equal to winArr. Each time a computer or player plays a game, these two arrays are updated separately, delete a combination of players that cannot win. When the panel is updated, computerWin and userWin are updated respectively.

The core method is play. The pseudocode for executing the play step is as follows:

Attack
Traverse the computerWin array, find the game slots that can be attacked, play chess, and check whether the game has won.
No attack. If you need to defend
Traverse userWin and find the expected chess positions, play chess, and update the panel based on the player's win-win combination;
No defense is required. If it is the first step of the computer's first hand
Play chess at the center and update the panel;
Not the first step
If the center position is not occupied, play chess in the center, update the panel; Return
In special cases, if you play chess at the edges, update the panel.
If the corner position still has a position, select a corner to play chess and update the panel.
In the last case, find the remaining vacancies, select the ones in computerWin first, play chess, and update the panel.

The implementation of the play algorithm is as follows:

 if(canAttack()) {  console.log("attack");  var attackPos = findAttackPos();  updatePanel(attackPos, computerVal);} else if(needDefend()) {  console.log("defend");  var defendPos = findDefendPos();  updatePanel(defendPos, computerVal);} else if(firstStep()) {  console.log("first");    updatePanel(firstPos, computerVal);    running = true;} else {  console.log("other");  if(panel[1][1] == 0) {        updatePanel(firstPos, computerVal);      return;  }  if(special()) {    console.log('special');    var pos = findSpecialPos();    updatePanel(pos, computerVal);    return;  }  var random = Math.floor(Math.random() * 2);  if(panel[0][0] == 0 && panel[2][2] == 0) {    var pos = (random == 0) ? 0 : 8;    updatePanel(pos, computerVal);  } else if(panel[0][2] == 0 && panel[2][0] == 0) {    var pos = (random == 0) ? 2: 6;    updatePanel(pos, computerVal);  } else {    var otherPos = findEmptyPos();    updatePanel(otherPos, computerVal);  }}

Summary

One of the challenges encountered during the coding process is the JavaScript array object. When I first called the play method to start outputting the panel, I got the value of the panel after the play operation, later, I asked a great god to find that panel is an object, because the object traversal references the same memory address, so once there is a change, it will all be changed. If you directly output each value using a subscript, you can get the initial value. You can also use the JSON method to print the array string and view the result.

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.