_JSP programming of Well word game example based on JSP

Source: Internet
Author: User
Tags javascript array

To do a memory of childhood games, the game code is relatively simple, mainly to master the principle of the algorithm, but there are some places to pay attention to.

Game interface

Get to the point. The effect of the project is shown below:

The game has a starting interface to select the player's role, and then choose which side of the play, and then start the game. Select the interface to make a mask layer, which provides users with a choice, after the selection of the mask layer to hide and start the game.

Concrete implementation

Said so much, may be more boring, the following describes the specific code implementation.

Using a two-dimensional array panel to save the state of the chessboard, 1 is the value of the computer-1 is the player's value.
Winarr saves all 8 chess-bit combinations that can be won; maintain Computerwin and Userwin, the initial value equals Winarr, and when the computer or player plays chess each time, each of these two arrays is updated to remove the not-winning combination. The Computerwin and Userwin are updated separately when the panel is updated.

The core approach is to play,play the execution of the steps pseudo code as follows:

If you can attack
Iterate over the Computerwin array, find the chess bits that can be attacked, play chess, and show whether or not to win.
You can't attack if you need to defend.
Traverse Userwin, according to the player can win the combination, find the need to defend the position of chess, chess, update panel;
No need to defend, if it's the first step in the computer
Play chess at central position, update panel;
Not the first step.
If the center position is not taken up, play chess at the center position, update the panel;
If it is a special case, play chess at the prism, update the panel;
If the corner still has a position, select a corner position to play chess, update the panel;
In the last case, find the remaining vacancies, prioritize the vacancy in Computerwin, play chess, update the panel;

The play algorithm is implemented 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 ("a");
    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);
 }
}

Summarize

In the process of coding a problem is the JavaScript array object, I first call the play method at the beginning of the output panel, the play is performed after the value of the panel, and then consulted a great God, found that because the panel is an object, Since the object traversal refers to the same memory address, it is changed all at once. If you use the subscript to output each value, you can get the initial value, or you can use the JSON method to print out the array string and see the results.

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.