ランダムで、数列に一か所に文字を挿入します。
var offset = Math.floor(Math.random() * chars.length);
chars.splice(offset, 1, seikai);
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>間違い探しゲーム</title> <style> span { cusor:pointer; } </style> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>間違い探しゲーム</h1> <p><input type="button" value="開始!" onclick="gameStart();"></p> <div id="score"></div> <div id="cells"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> var level = 0; var games = [ ['問', '間'], ['大', '太'], ['白', '臼'] ]; var MAX_LEVEL = games.length - 1; var DIM_FIRST = 5; var DIM_DELTA = 3; var dim = DIM_FIRST; var t1; var t2; function gameStart(){ var dummy = games[level][0]; var seikai =games[level][1]; if (level == 0){ $('#score').empty(); t1 = new Date().getTime(); } var cells = ''; for (var i = 1; i <= dim * dim; i++){ cells += '<span id="s' + i + '"></span>'; if (i % dim == 0){ cells += '<br />'; } } $('#cells').html(cells); var chars = []; for (var i = 0; i < dim * dim; i++){ chars.push(dummy); } var offset = Math.floor(Math.random() * chars.length); chars.splice(offset, 1, seikai); // console.log(chars); for (var i = 1; i <= chars.length; i++){ $('#s'+i).text(chars[i-1]); $('#s'+i).click(function(){ if ($(this).text() == seikai){ level++; dim += DIM_DELTA; if (level > MAX_LEVEL){ t2 = new Date().getTime(); $('#score').text('Your score is '+(t2 - t1)/1000 +'!!'); level = 0; dim = DIM_FIRST; return false; } gameStart(); } }); } } </script> </body> </html>