<i id='o8XeQ'><tr id='o8XeQ'><dt id='o8XeQ'><q id='o8XeQ'><span id='o8XeQ'><b id='o8XeQ'><form id='o8XeQ'><ins id='o8XeQ'></ins><ul id='o8XeQ'></ul><sub id='o8XeQ'></sub></form><legend id='o8XeQ'></legend><bdo id='o8XeQ'><pre id='o8XeQ'><center id='o8XeQ'></center></pre></bdo></b><th id='o8XeQ'></th></span></q></dt></tr></i><div id='o8XeQ'><tfoot id='o8XeQ'></tfoot><dl id='o8XeQ'><fieldset id='o8XeQ'></fieldset></dl></div>

      <legend id='o8XeQ'><style id='o8XeQ'><dir id='o8XeQ'><q id='o8XeQ'></q></dir></style></legend>
    1. <small id='o8XeQ'></small><noframes id='o8XeQ'>

    2. <tfoot id='o8XeQ'></tfoot>
          <bdo id='o8XeQ'></bdo><ul id='o8XeQ'></ul>
      1. 在 javascript 中压缩一个开关盒

        时间:2023-09-08
        <i id='s4smC'><tr id='s4smC'><dt id='s4smC'><q id='s4smC'><span id='s4smC'><b id='s4smC'><form id='s4smC'><ins id='s4smC'></ins><ul id='s4smC'></ul><sub id='s4smC'></sub></form><legend id='s4smC'></legend><bdo id='s4smC'><pre id='s4smC'><center id='s4smC'></center></pre></bdo></b><th id='s4smC'></th></span></q></dt></tr></i><div id='s4smC'><tfoot id='s4smC'></tfoot><dl id='s4smC'><fieldset id='s4smC'></fieldset></dl></div>

          <small id='s4smC'></small><noframes id='s4smC'>

            • <bdo id='s4smC'></bdo><ul id='s4smC'></ul>
                    <tbody id='s4smC'></tbody>
                1. <tfoot id='s4smC'></tfoot>

                  <legend id='s4smC'><style id='s4smC'><dir id='s4smC'><q id='s4smC'></q></dir></style></legend>
                  本文介绍了在 javascript 中压缩一个开关盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我创建了一个棋盘,但我不喜欢我的 switch case 语句的外观.我想一定有办法压缩它,但我找不到.也许你们中的一些人可以帮助我.

                  I have created a chess board and I didn't like the look of my switch case statement. I think there must be a way to compact it, but I cant find one. Maybe some of you can help me.

                  补充说明,棋子是二维数组(arr2):

                  Extra explanations, the chess pieces are in a 2 dimensional array (arr2):

                  [
                      ["R", "N", "B", "Q", "K", "B", "N", "R"],
                      ["P", "P", "P", "P", "P", "P", "P", "P"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["p", "p", "p", "p", "p", "p", "p", "p"],
                      ["r", "n", "b", "q", "k", "b", "n", "r"]
                  ];
                  

                  棋盘的每个区域都有自己的 id 从00"到77",其中第一个数字是行,第二个是列.

                  And every field of the chess board has it's own id form "00" to "77" where first digit is the row and second is the column.

                  innerHTML 代码只是棋子的 unicode,如下所示:https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode#Unicode_code_points_and_HTML

                  The innerHTML code is just the unicode for the chess pieces as seen here: https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode#Unicode_code_points_and_HTML

                  for (let r = 0; r < arr2.length; r++) {
                      for (let c = 0; c < arr2.length; c++) {
                          div = document.getElementById(r + "" + c)
                          switch (arr2[r][c]){
                              //black piece
                              case 'k':
                                  div.innerHTML = "&#9818";
                                  break;
                              case 'q':
                                  div.innerHTML = "&#9819";
                                  break;
                              case 'r':
                                  div.innerHTML = "&#9820";
                                  break;
                              case 'b':
                                  div.innerHTML = "&#9821";
                                  break;
                              case 'n':
                                  div.innerHTML = "&#9822";
                                  break;                
                              case 'p':
                                  div.innerHTML = "&#9823";
                                  break;
                              //white piecec
                              case 'K':
                                  div.innerHTML = "&#9812";
                                  break;
                              case 'Q':
                                  div.innerHTML = "&#9813";
                                  break;
                              case 'R':
                                  div.innerHTML = "&#9814";
                                  break;
                              case 'B':
                                  div.innerHTML = "&#9815";
                                  break;
                              case 'N':
                                  div.innerHTML = "&#9816";
                                  break;                
                              case 'P':
                                  div.innerHTML = "&#9817";
                                  break;
                          }
                      }
                  }
                  

                  推荐答案

                  由于棋子的字符代码是连续的,你可以这样做:

                  Since the character codes for chess pieces are consecutive, you could do it like this:

                  div.innerHTML = "&#" + (9812 + "KQRBNPkqrbnp".indexOf(arr2[r][c])) + ";";
                  

                  请注意,即使浏览器允许,HTML 实体也需要终止分号.

                  Note that HTML entities need a terminating semi-colon even though browsers are forgiving.

                  如果您使用 textContent,您甚至不必将字符代码转换为 HTML 实体:

                  You don't even have to convert the character code to an HTML entity if you use textContent:

                  div.textContent = String.fromCharCode(9812 + "KQRBNPkqrbnp".indexOf(arr2[r][c]));
                  

                  您还需要考虑空方格,至少在您开始移动棋子并使用相同的代码更新显示时.这种情况在您的代码中没有处理,但您可以像在此演示中那样使用三元运算符隔离这种情况:

                  You will need to take the empty squares into account as well, at least when you start moving pieces and use the same code to update the display. That case is not dealt with in your code, but you can just isolate that case with ternary operator as in this demo:

                  function showBoard(arr2) {
                      for (let r = 0; r < arr2.length; r++) {
                          for (let c = 0; c < arr2.length; c++) {
                              const div = document.getElementById(r + "" + c)
                              div.textContent = arr2[r][c] === "0" ? ""
                                  : String.fromCharCode(9812 + "KQRBNPkqrbnp".indexOf(arr2[r][c]));
                          }
                      }
                  }
                  
                  const arr2 = [
                      ["R", "N", "B", "Q", "K", "B", "N", "R"],
                      ["P", "P", "P", "P", "P", "P", "P", "P"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["0", "0", "0", "0", "0", "0", "0", "0"],
                      ["p", "p", "p", "p", "p", "p", "p", "p"],
                      ["r", "n", "b", "q", "k", "b", "n", "r"]
                  ];
                  showBoard(arr2);

                  table { border-collapse: collapse; border-spacing: 0; }
                  
                  #chessboard { border: 1px solid; }
                  #chessboard tr td { width: 20px; height: 20px; }
                  #chessboard tr:nth-child(2n) td:nth-child(2n+1),
                  #chessboard tr:nth-child(2n+1) td:nth-child(2n) { background: silver; }

                  <table id="chessboard">
                  <tr><td id="00"></td><td id="01"></td><td id="02"></td><td id="03"></td><td id="04"></td><td id="05"></td><td id="06"></td><td id="07"></td></tr>
                  <tr><td id="10"></td><td id="11"></td><td id="12"></td><td id="13"></td><td id="14"></td><td id="15"></td><td id="16"></td><td id="17"></td></tr>
                  <tr><td id="20"></td><td id="21"></td><td id="22"></td><td id="23"></td><td id="24"></td><td id="25"></td><td id="26"></td><td id="27"></td></tr>
                  <tr><td id="30"></td><td id="31"></td><td id="32"></td><td id="33"></td><td id="34"></td><td id="35"></td><td id="36"></td><td id="37"></td></tr>
                  <tr><td id="40"></td><td id="41"></td><td id="42"></td><td id="43"></td><td id="44"></td><td id="45"></td><td id="46"></td><td id="47"></td></tr>
                  <tr><td id="50"></td><td id="51"></td><td id="52"></td><td id="53"></td><td id="54"></td><td id="55"></td><td id="56"></td><td id="57"></td></tr>
                  <tr><td id="60"></td><td id="61"></td><td id="62"></td><td id="63"></td><td id="64"></td><td id="65"></td><td id="66"></td><td id="67"></td></tr>
                  <tr><td id="70"></td><td id="71"></td><td id="72"></td><td id="73"></td><td id="74"></td><td id="75"></td><td id="76"></td><td id="77"></td></tr>
                  </table>

                  这篇关于在 javascript 中压缩一个开关盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:处理结合案件与switch 语句中的重复案例 下一篇:javascript fizzbuzz switch 语句

                  相关文章

                      <bdo id='YXf9M'></bdo><ul id='YXf9M'></ul>

                      <small id='YXf9M'></small><noframes id='YXf9M'>

                      <legend id='YXf9M'><style id='YXf9M'><dir id='YXf9M'><q id='YXf9M'></q></dir></style></legend>

                    1. <tfoot id='YXf9M'></tfoot>
                      <i id='YXf9M'><tr id='YXf9M'><dt id='YXf9M'><q id='YXf9M'><span id='YXf9M'><b id='YXf9M'><form id='YXf9M'><ins id='YXf9M'></ins><ul id='YXf9M'></ul><sub id='YXf9M'></sub></form><legend id='YXf9M'></legend><bdo id='YXf9M'><pre id='YXf9M'><center id='YXf9M'></center></pre></bdo></b><th id='YXf9M'></th></span></q></dt></tr></i><div id='YXf9M'><tfoot id='YXf9M'></tfoot><dl id='YXf9M'><fieldset id='YXf9M'></fieldset></dl></div>