简体   繁体   English

如何制作基于数组的Simon Js游戏?

[英]How to make a Simon Js game based on arrays?

Im writing a js simple simon game and im clueless on how to do it. 我正在编写一个js简单的simon游戏,对如何做到这一点一无所知。

I know that : 我知道 :

I need to create two arrays, and a level(score) variable 我需要创建两个数组和一个level(score)变量

  • A randomly generated number from 1 to 4 (inclusive) needs to be added to the first array, When one of four buttons is pressed, the value of it is added to the second array, if the second array is not the 需要向第一个数组中添加一个从1到4(含)的随机生成的数字。如果按下四个按钮之一,则将其值添加到第二个数组中(如果第二个数组不是)。
    same size or bigger than the first array. 大小等于或大于第一个数组。 Each time a value is added to the second array, check that the value is equal to the value in 每次将值添加到第二个数组中时,请检查该值是否等于中的值
    the same position in the first array, if not, clear both arrays, and set levelvar to 1, and alert "gameover" This means if you get one 如果没有,则清除第一个数组中的相同位置,清除两个数组,并将levelvar设置为1,并警告“ gameover”
    wrong, you cannot continue. 错误,您无法继续。 If the length of the second array 如果第二个数组的长度
    matches the level variable, add a random number to array one, clear 匹配level变量,向数组1添加一个随机数,清除
    array two, increment levelvar. 数组2,增量为levelvar。

But, I am clueless in aspect to the code. 但是,我对代码一无所知。

My Jsfiddle : http://jsfiddle.net/jbWcG/2/ 我的Jsfiddle: http : //jsfiddle.net/jbWcG/2/

JS: JS:

var x = []
var y = []
var levelvar = 1
document.getElementById("test").onclick= function() {
document.getElementById("test").innerHTML=x
};
document.getElementById("button1").onclick= function() {
x.push("Red")
};
document.getElementById("button2").onclick= function() {
x.push("Green")
};
document.getElementById("button3").onclick= function() {
x.push("Yellow")
};
document.getElementById("button4").onclick= function() {
x.push("Blue")
};

HTML: HTML:

<button id="button1">Red</button><br />
<button id="button2">Green</button><br />
<button id="button3">Yellow</button><br />
<button id="button4">Blue</button><br />
<p id="test">Click To see What you have clicked</p>

How would I make a two arrays see if a certain value is the same? 我如何使两个数组查看某个值是否相同?

Lets say, that the generated array is : [1,2,3,4,1,2,3] and i am at position 5 and i press 2, how would i check that the two numbers match? 可以说,生成的数组是:[1,2,3,4,1,2,3],我在位置5并按2,如何检查两个数字是否匹配? Thanks in advance 提前致谢

The easiest way to check one at a time that position i of your array is x is 一次检查一个数组的位置ix的最简单方法是

if (gen_arr[i] == x) {
  // matches
} else {
  // doesn't match
}

So if you conceptualize the flow of your game, you're going to want to, at each button press: 因此,如果您将游戏流程概念化,则需要按一下每个按钮:

  1. somehow keep track of which index they are on (maybe have a counter that increments with each button press) 以某种方式跟踪它们在哪个索引上(也许有一个计数器,每次按下按钮都会递增)
  2. checks if gen_arr[i] == x (and displays game over if it doesn't). 检查gen_arr[i] == x (如果没有,则显示游戏结束)。

Alternatively, instead of keeping track of which index, you can call gen_array.shift() to get the first item in gen_array AND delete it from the array, in a flow kind of like this: 另外,除了跟踪哪个索引之外,还可以调用gen_array.shift()来获取gen_array的第一项gen_array其从数组中删除,流程如下:

var gen_array = [1,2,3,4,1];

function press_button(button_pressed) {
  var supposed_to_be = gen_array.shift();

  // at this point, on the first call,
  //   supposed_to_be = 1, and gen_array = [2,3,4,1]

  if (supposed_to_be != button_pressed) {

    // game over!

  } else {
    // you survive for now!

    if (gen_array.length() == 0) {
      // gen_array is empty, they made it through the entire array
      // game is won!
    }
  }
}

While that represents the general "what to check" at every step, using this verbatim is not recommended as it quickly leads to an unstructured game. 尽管这代表了每个步骤的一般“检查内容”,但不建议使用这种逐字记录,因为它会很快导致游戏结构混乱。

I recommend looking into things called "game state" diagrams, which are basically flow charts which have every "state" of the game -- which in your case, includes at least 我建议您研究一下称为“游戏状态”图的内容,它们基本上是流程图,包含游戏的每个“状态”-在您的情况下,至少包括

  1. "displaying" the pattern “显示”模式
  2. waiting for button press 等待按钮按下
  3. checking if button press is correct 检查按钮是否正确
  4. game over 游戏结束
  5. game won 比赛获胜

And from each state, draw arrows on "how" to transition from one state to the next. 然后从每个状态绘制箭头,说明“如何”从一种状态过渡到另一种状态。 You can do a google search to see examples. 您可以进行Google搜索以查看示例。

Once you have a good game state diagram/flow chart, it's easier to break down your program into specific chunks and organize it better ... and you can usually then see exactly what you need to code and what is missing/what is not missing. 一旦有了良好的游戏状态图/流程图,就可以将程序分解为特定的块并进行更好的组织...更容易了,通常您可以准确地看到需要编写什么代码以及缺少/不缺少的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM