【问题标题】:java processing shuffeling 2d array without classesjava processing shuffeling 2d array without classes
【发布时间】:2022-12-01 14:13:43
【问题描述】:

school gave me an assingment. i have to make the memory cards game in processing (java). i am not allowed to use classes. i already created the game and it works, but now i have to add death cards. i am using a 2d array to load the cards. i have put the death cards in the third iteration of the array.

now the problem is, when i am trying to create a shuffle function, it will only shuffle the cards in its own iteration. so all the death cards will appear at the end.

anybody got any ideas?

void shuffle() {
  int tijdelijk = 0;
  int random = 0;

  for (int i=0; i<3; i++) {
    if (i == 0 || i == 1) {
      for (int j=0; j<aantalSetjes; j++) {
        random = int(random(0, aantalSetjes));
        tijdelijk = gekozenKaart[i][j];
        gekozenKaart[i][j] = gekozenKaart[i][random];
        gekozenKaart[i][random] = tijdelijk;
      }
    } else if (i == 2) {
      for (int j=0; j<getAantalDoodsKaarten(); j++) {
        random = int(random(0, getAantalDoodsKaarten()));
        tijdelijk = gekozenKaart[i][j];
        gekozenKaart[i][j] = gekozenKaart[i][random];
        gekozenKaart[i][random] = tijdelijk;
      }
    }
  }
}

【问题讨论】:

  • You should make sure your question contains all relevant code for readers to understand and reproduce the error/wrong behavior you are asking about. There is a lot in the short snipped that you didn't include and that we as readers have no idea what it even represents. If you don't give us all relevant information about the variables and methods you use in your methods your code is just a big black box to us and we literally cannot tell you why it behaves the way it does.
  • But that's literally what your code above does - it shuffles row 0 cards among themselves only, then it shuffles row 1 cards among themselves only and then it shuffles row 2 cards among themselves only. I suppose somewhere outside you then append the rows together, placing all row 0 cards first, then all row 1 cards next and so on.
  • please add the rest of the code as well to your example, so that we can see where the rows are then put together.

标签: java processing shuffle


【解决方案1】:

If it's the classic memory card game, you most probably don't want the data to be organized in the 2d array to begin with. Just combine all of them into one bigger 1d array first and then shuffle the 1d array. Then they will be all mixed through as you wanted.

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 1970-01-01
    • 2021-07-06
    • 2019-03-12
    • 2016-08-04
    • 1970-01-01
    • 2011-08-12
    • 2021-05-05
    • 2021-11-01
    相关资源
    最近更新 更多