• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Array Problems

Dagoba

Member
Hello!

Since it feels like I am never going to get these Arrays in my mind (and how they work) I have to ask a question about them regarding to my Lottery -kind of game.

BTW! I use GM8.0 so not all the scripts work, but most of them will do. Especially Arrays are same kind of script than in GM:Studio.

I have multiple problems with this, instead of making them into each topic, I'll fill them all in this one.

First problem: Restricted Numbers:

You will choose 6 unique numbers from list of 1-50 numbers (that are lottery balls) by clicking them and thus selecting them.

Once you have them all selected, you will press "Confirm" button and the computer will pick random (and unique) 6 winning numbers. This is done with the following script:

Code:
for (i = 0; i < 6; i+=1) {
    winningball = irandom_range(1,50);
    while (winningball != global.restrictedNumber[i]) {
        global.winningBall[i] = winningball;
        global.restrictedNumber[i] = winningball;
    }
}
I have set these global.restrictedNumber[] arrays like this:
global.restrictedNumber[0] = 0;
global.restrictedNumber[1] = 0;

etc...
But still, it can select a winning number that is already selected. What is wrong with my script?

GIF what I mean: http://imgur.com/a/rvf3d

EDIT: I know what causes the problem, but don't know how to fix:
while (winningball != global.restrictedNumber) <- This means, that the number for example is now 4 (i = 4), and because number 4 ball is yet 0, it will pick any number because 0 is not even in the random_range it will pick from, what I want to do, is that it checks all previous (i) numbers before picking. Not sure how to do this!


Second problem: Reselecting causes problem.

This one is annoying, whenever I deselect a number, it will deselect LAST number that was selected; for example I select number 5, then select number 8 and then select number 12. Now I want to deselect 12 because I meant to click 13, the number will be deselected and returned to 0 (global.selectedNumbers[global.selections] = 0) and then I want to choose 6 instead of 5, but it will reset the number 13 because it was "last selected number". I know this script is incorrect, but is there a way to check that which number you are going to deselect and reset the array of that given number (hard to explain, but I add a .gif here)

Code for it:
Code:
if selected == false {
    if (global.selections < global.max_selections) {
        canSelect = false;
        alarm[0] = cooldown; //alarm[0] makes canSelect = true;
        selected = true;
        image_index = 1;
        global.selections+=1
        global.selectedNumbers[global.selections] += number;
    }
}
else {
    canSelect = false;
    alarm[0] = cooldown;
    selected = false;
    image_index = 0;
    global.selectedNumbers[global.selections] = 0;
    global.selections-=1
}
GIF what I mean: http://imgur.com/a/Fx2De

For now, I won't ask for help for any else problem because I am trying to solve them by myself, these ones are just hard because I am stupid enough to not learn Arrays after 8 "lazyish" programming years. Now I need to focus 100% to coding because I am going to need to find work while coding Java or any similar language.

Thanks in advance!
 
Last edited:

FrostyCat

Redemption Seeker
I really don't think much of what you've learned at this point if you still aren't comfortable with nesting loops.
Code:
var i, j, ok;
for (i = 0; i < 6; i+=1) {
  do {
    ok = true;
    winningball = irandom_range(1,50);
    for (j = 0; j < i && ok; j+=1) {
      if (winningball == global.winningBall[j]) {
        ok = false;
      }
    }
  } until (ok)
  global.winningBall[i] = winningball;
}
Same with the second problem.
Code:
var i, j;
if (!selected) {
  if (global.selections < global.max_selections) {
    canSelect = false;
    alarm[0] = cooldown;
    selected = true;
    image_index = 1;
    global.selectedNumbers[global.selections] = number;
    global.selections += 1;
  }
}
else {
  canSelect = false;
  alarm[0] = cooldown;
  selected = false;
  image_index = 0;
  for (i = 0; i < global.selections; i += 1) {
    if (global.selectedNumbers[i] == number) {
      global.selectedNumbers[i] = 0;
      break;
    }
  }
  for (j = i+1; j < global.selections; j += 1) {
    global.selectedNumbers[j-1] = global.selectedNumbers[j];
    global.selectedNumbers[j] = 0;
  }
  global.selections -= 1;
}
All of these are patterns common to both GML and Java, which is why your grasp of basics is suspect at best.

Trust me, if you can't even solve these problems without needing to be bailed out, you have no hope of finding work in programming. The coder on the panel will blow you out of the ring in one or two hits. Google and forums can't save you from inside an interview room.
 

Dagoba

Member
All of these are patterns common to both GML and Java, which is why your grasp of basics is suspect at best.

Trust me, if you can't even solve these problems without needing to be bailed out, you have no hope of finding work in programming. The coder on the panel will blow you out of the ring in one or two hits. Google and forums can't save you from inside an interview room.
Wow hold on, that is quite rude in my opinion. Every single person has their weaknesses and strengths, my hugest problem has always been arrays; calling them and using them. In my school, once we stepped to arrays, teacher just said "copy and paste" these and literally explained nothing about them, that guy retired this year from my school because he was too old and his teaching was a bit sloppy.
I have read many times about arrays, they are quite simple but storing them and calling them (mostly when they are multi-dimensional arrays) are hard for me.

I even started with another language in 2015 December that was so different from Java or GML and after 2 months, I had about 4,000 lines of code. And yes, you can have 4000 lines of code which is bad quality, but I got help several times and learnt from that and the script was running smoothly without many bugs.

Anyways, thanks for help. I will look up to your code and try to learn from that, so I don't need to sit in my house forever and be poor forever.
 
Top