Home  >  Article  >  Web Front-end  >  JS猜数字游戏如何判断一个数字是否被猜过?

JS猜数字游戏如何判断一个数字是否被猜过?

WBOY
WBOYOriginal
2016-06-07 08:43:222389browse

写一个竞猜游戏,用户必须猜一个秘密的数字,在每次猜完后程序会告诉用户他猜的数是太大了还是太小了,直到猜测正确,最后打印出猜测的次数。如果用户连续猜测同一个数字则只算一次。

我就是不知道怎么实现“如果用户连续猜测同一个数字则只算一次。”

没有思路,下面附上我写的代码,就是不知道实现最后一句。

如果有大神看到的话帮忙回答下,真的是想不出来。

回复内容:

涉及到javascript中的一个很有用的点:把object当hash来用

<code class="language-text">/**
 *
 * 用object当做hash来记录猜过的数字
 * 假设把所有猜过的数字存在一个array中, 其中有重复的数字
 * [1, 2, 5, 6, 3, 5, 1, 6, 2, 6]
 *
 */

var guesses = [1, 2, 5, 6, 3, 5, 1, 6, 2, 6];
var guessesHash = {};
var guessesCount = 0; // total guess count exclude duplicated guesses

for (var i = 0; i </code>
用数组或者object把输入过的数存起来即可。
<code class="language-js"><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">history</span><span class="o">=</span><span class="p">{},</span>
        <span class="nx">secret</span><span class="o">=</span><span class="mi">4</span><span class="p">;</span> <span class="c1">// https://www.xkcd.com/221/</span>
    
    <span class="k">do</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">input</span><span class="o">=</span><span class="nb">parseInt</span><span class="p">(</span><span class="nx">prompt</span><span class="p">(</span><span class="s1">'输入数字: '</span><span class="p">));</span>
        <span class="nx">history</span><span class="p">[</span><span class="nx">input</span><span class="p">]</span><span class="o">=</span><span class="kc">true</span><span class="p">;</span>
        <span class="k">if</span><span class="p">(</span><span class="nx">input</span><span class="o">></span><span class="nx">secret</span><span class="p">)</span>
            <span class="nx">alert</span><span class="p">(</span><span class="s1">'比这个小'</span><span class="p">);</span>
        <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">input</span><span class="o"><span class="nx">secret</span><span class="p">)</span>
            <span class="nx">alert</span><span class="p">(</span><span class="s1">'比这个大'</span><span class="p">);</span>
    <span class="p">}</span> <span class="k">while</span><span class="p">(</span><span class="nx">input</span><span class="o">!==</span><span class="nx">secret</span><span class="p">);</span>
    
    <span class="kd">var</span> <span class="nx">count</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">_</span> <span class="k">in</span> <span class="nx">history</span><span class="p">)</span>
        <span class="nx">count</span><span class="o">++</span><span class="p">;</span>
    <span class="nx">alert</span><span class="p">(</span><span class="s1">'你猜了 '</span><span class="o">+</span><span class="nx">count</span><span class="o">+</span><span class="s1">' 次'</span><span class="p">);</span>
<span class="p">})();</span>
</span></code>
定义个全局变量,记录用户每次输入的值,一样的值不做重复记录,不一样的记录。最后看看这个变量的数。。。。 第一种方法:建个数组,用户输入时检查数组最后一个数是否和输入相等,不相等就push进去。最后输出数组的长度就好了。
第二种方法:不管用户输入是否有连续重复的数字,都push到一个数组里,最后把连续的数字剔除掉之后的长度就好了。这样问题就变成了:实现一个函数,用户输入:[1,2,2,2,3,3,4,4,4,5,5],要求输出为5 (即[1,2,3,4,5]的长度)
<code class="language-js"><span class="kd">var</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">];</span>

<span class="kd">function</span> <span class="nx">getLength</span><span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="nx">arr</span><span class="p">.</span><span class="nx">filter</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">v</span><span class="p">,</span><span class="nx">i</span><span class="p">,</span><span class="nx">arr</span><span class="p">){</span>
    <span class="k">return</span> <span class="nx">v</span><span class="o">!==</span><span class="nx">arr</span><span class="p">[</span><span class="nx">i</span><span class="o">-</span><span class="mi">1</span><span class="p">];</span>
  <span class="p">}).</span><span class="nx">length</span><span class="p">;</span>
<span class="p">}</span>

<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">getLength</span><span class="p">(</span><span class="nx">arr</span><span class="p">));</span> <span class="c1">// 5</span>
</code>
var n=0;
var lastAnswer=null;
//var 答案数组=[];//题主的问题里没有要求记录用户输入的答案,如果有需求的话,就加上这个。
function 用户输入之后的处理函数(int 用户输入的值){
if(lastAnswer!=用户输入的值){
n++;
lastAnswer=用户输入的值;
//答案数组.push(用户输入的值);//题主的问题里没有要求记录用户输入的答案,如果有需求的话,就加上这个。
//和正确答案比较。
}
else{
//连续猜测同一个数字;
}
} 不知道间隔猜测同样的数字算不算你说的连续输入相同的数字 ?不过原理是一样的 :
声明一个数组 ,里面放之前猜测过的数字,每次输入新数字时到数组中查找是否已存在,已经存在的,则跳过。 参考数组去重,很简单实现 把每次数字存入数组,对比数字和数组最后一个数是否相等。 hash虽说不太懂大概意思就是再弄一个数组 专门放这个数字猜没猜过? 这不是很简单么,两个变量的事,一个答案,一个是上次的值
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn