JavaScript and HTML5 use canvas to build a guessing game to implement the algorithm _javascript skills

Source: Internet
Author: User
Let me guess the cards in your heart, first randomly generated 27 cards, you can not repeat the list of three cards, and then remember one, and then click on the list of cards, many times you can guess the cards you want.
If it is 9, just guess 2 times, if 27 is guessed 3 times.

Implementation method (27 pieces):
If you click on the third column, that is to say that the card must be in these 9, the third column of 9 cards on average to 3 per column, assuming the number 123,456,789
Click again, if you click on the second column, then guess the cards in 456, and then to three columns, 4,5,6
Click once again to know which card is.

Implementation algorithm:
I'm using one-dimensional array implementations, for the first time I guess the third column replaces the third column of data and 0,1,2,3,4,5,6,7,8,
Then the number of guesses in the first 9, the second guess the second column of the column in the three and 0,1,2 replaced, then three in the front.
Output is in three columns, but there is a problem, so that the cards behind it will not be messed up, others will know why you guessed
So in the output of the time will be ordered output, can only be ordered up and down the output, can not be left and right disorderly order
var random = new Array (5);//Custom two-dimensional sequence table
Random[0] = [5, 2, 8, 7, 1, 3, 4, 6, 0];
RANDOM[1] = [2, 5, 8, 0, 4, 6, 3, 7, 1];
RANDOM[2] = [6, 7, 2, 8, 0, 1, 5, 3, 4];
RANDOM[3] = [2, 1, 6, 3, 5, 4, 7, 0, 8];
RANDOM[4] = [0, 1, 2, 3, 4, 5, 6, 7, 8];
More please check: www.shengshiyouxi.com

Copy Code code as follows:

<! DOCTYPE html>
< HTML xmlns= "http://www.w3.org/1999/xhtml" >
< head>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<script type= "Text/javascript" >
var data = new Array (27);
var canvas;
var context;
var time = 0;
var random = new Array (5);//Custom two-dimensional sequence table
Random[0] = [5, 2, 8, 7, 1, 3, 4, 6, 0];
RANDOM[1] = [2, 5, 8, 0, 4, 6, 3, 7, 1];
RANDOM[2] = [6, 7, 2, 8, 0, 1, 5, 3, 4];
RANDOM[3] = [2, 1, 6, 3, 5, 4, 7, 0, 8];
RANDOM[4] = [0, 1, 2, 3, 4, 5, 6, 7, 8];
function Start () {
var guess = new Array ();
var count = 0;
while (Count < 27) {//Generate random 27 cards
var temp = parseint (Math.random () * 54) + 1;
for (var i = 0; i < count + 1; i++) {
if (temp = = guess) {//If repeat, do not
temp = 100;
Break
}
}
if (temp!= 100) {
Guess[count] = temp;
Data[count] = new Image ();
DATA[COUNT].SRC = "images/" + temp + ". gif";
count++;
}
}
}
function Draw () {
Canvas = document.getElementById ("Canvas");
Context = Canvas.getcontext ("2d");
var Temp1 = parseint (Math.random () * 5);
var temp2 = parseint (Math.random () * 5);
var Temp3 = parseint (Math.random () * 5);
for (var i = 0; i < 9; i++) {//unordered sequencing method
Context.drawimage (DATA[RANDOM[TEMP1] * 3 + 0], M, I * 30 + 40);
Context.drawimage (data[random[temp2]* 3 + 1], +, I * 30 + 40);
Context.drawimage (data[random[temp3]* 3 + 2], +, I * 30 + 40);
}
}
Function Play (index) {
if (Time >= 3)
Alert ("Please click again");
for (var i = 0; i < (3-time) * 3; i++) {
var temp = data;
Data= Data[i * 3 + index-1];
Data[i * 3 + index-1] = temp;
}
time++;
if (Time >= 3) {
Context.drawimage (Data[0], 400, 50);
Return
}
Draw ();
}
Start ();
</script>
<style type= "Text/css" >
Input {
margin-right:60px;
}
</style>
< body>
<p>
Let me guess the cards in your heart <br/>
First select a card from the list, and remember it, and then select the column that it is in, click three times I will guess the cards you have in mind. <br/>
Give it a try, I know you! <br/>
If a card cannot be displayed or is not fully displayed, refresh. If your browser does not support HTML5, please change your browser, such as Chrome, Firefox, etc.
</p>
<canvas width= "height=" id= "Canvas" > your browser does not support HTML5, please change your browser, such as Chrome, Firefox </canvas>
<br/>
<input type= "button" value= "first column"/>
<input type= "button" value= "second column"/>
<input type= "button" value= "third column"/>
<input type= "button" value= "one More Time"/>
</body>

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.