我是Java语言和编码方面的新手,我想不出一种简化此代码的方法。

var allCards = ["A1", "A2", "B1","B2", "C1", "C2", "D1", "D2", "E1", "E2", "F1", "F2", "G1", "G2", "H1", "H2"];
var allCardBacks = ["back1","back2","back3","back4","back5","back6","back7","back8","back9","back10","back11","back12","back13","back14","back15","back16"];


必须有一种方法以某种方式将其缩短,但是我真的不知道该怎么做。
还有:

function placeCards() {
  setPosition(allCards[0],15, 30);
  setPosition(allCards[1],90, 30);
  setPosition(allCards[2],165, 30);
  setPosition(allCards[3],240, 30);
  setPosition(allCards[4],15, 105);
  setPosition(allCards[5],90, 105);
  setPosition(allCards[6],165, 105);
  setPosition(allCards[7],240, 105);
  setPosition(allCards[8],15, 180);
  setPosition(allCards[9],90, 180);
  setPosition(allCards[10],165, 180);
  setPosition(allCards[11],240, 180);
  setPosition(allCards[12],15, 255);
  setPosition(allCards[13],90, 255);
  setPosition(allCards[14],165, 255);
  setPosition(allCards[15],240, 255);
}


请帮我。我正在尝试制作一个记忆游戏,但这太重复了。

最佳答案

嵌套循环

function placeCards() {
  var cnt = 0;
  var y;
  var x;
  for (y = 30; y <= 255; y += 75) {
    for (x = 15; x <= 240; x += 75) {
        setPosition(allCards[cnt], x, y);
        cnt++;
    }
  }
}

09-25 17:25