SOLVED Looking to receive loop assignments for practice.

flyinian

Member
I would like to receive assignments from you that involve loops. Mainly for loops. It would also be nice if you could look over the assignments and give me feedback.

Types of assignments I'm looking for:

1. Broken loops that I have to debug and fix.
2. Create a loop with an outcome in mind.
3. An already made loop that is noted and explained would help me become more familiar how one works.

4. Other

I'm new to loops and coding in general. I've been stuck on my combat system that requires loops and haven't had any luck getting it to work so, I'm here looking for practice.

Thank you.


* I'll probably be doing the assignments in the future. Bookmarked.


From FrostyCat
This is an easy two-step process that Let's Clone has already laid out for you:
  1. Create a script that takes a grid and two row numbers as arguments, that swaps the contents of the two specified rows.
  2. Implement the linear shuffle algorithm, reusing the row-swap script from the first step.
Didn't you say you wanted exercises in for loops? Now is the time for you to put your money where your mouth is. Each of these is a single for loop.
 
Last edited:
I don't mean to sound snooty, but a for loop is a pretty self contained thing. You set the start value, and the value it will end at, and then it counts from the start value to the end.

What are you having trouble with? I'd rather try to work through your actual problem, than think up examples :)
 

flyinian

Member
I don't mean to sound snooty, but a for loop is a pretty self contained thing. You set the start value, and the value it will end at, and then it counts from the start value to the end.

What are you having trouble with? I'd rather try to work through your actual problem, than think up examples :)

I guess i meant what goes into a for loop. I'm thinking its more of the logic side of it.

GML:
for (i = 0; i < 10; i++)
{

// What goes in here for each loop and many other things.

};
Here is the link to my combat system help request post for a better understanding what i need help on.
 
I will take a look at your post, but as to what goes in a for loop.....there is no set conditions for that. It's whatever you want, assuming it fits that type of loop. Which is a general type of counter, where you know how long it wants to be. Having said that: 'break' in a loop of that type will stop it if you want it to be conditional, but then you could do that in a 'while' loop.
 

ophelius

Member
for loops are useful for repeating things a certain amount of times, but for values that can change based on a pattern.
For instance, suppose I want to draw a 3x3 grid of a sprite, for loops make that easy. I can have this:
Code:
for(var y = 0; y < 3; y++){
    for(var x = 0; x < 3; x++){
       draw_sprite(sprite_index, image_index, x * width, y * height);
    }
}
The outer loop starts y = 0, and will continue to loop while the condition is met(y < 3), and add 1 to y every loop (y++, or y = y + 1)
Before the outer loop has a chance to loop again, the inner loop will loop while its condition is met, and add 1 to its value each loop.
So the values of x and y for each loop will be as follows:
y=0, x=0
y=0, x=1
y=0, x=2
y=1, x=0
y=1, x=1
y=1, x=2
y=2, x=0
y=2, x=1
y=2, x=2
At each loop, you are using those x and y values to properly calculate the position of the sprite's coordinates.

The manual is a great place to start learning, check out under the Reference sections, the Language Overview pages. Here you'll find everything that is fundamental, which you should know about before proceeding
 

flyinian

Member
I will take a look at your post, but as to what goes in a for loop.....there is no set conditions for that. It's whatever you want, assuming it fits that type of loop. Which is a general type of counter, where you know how long it wants to be. Having said that: 'break' in a loop of that type will stop it if you want it to be conditional, but then you could do that in a 'while' loop.
Check this out: https://forum.yoyogames.com/index.p...tals-in-gml-tutorial-series.70417/post-420870

And I suggest checking out the rest of Sam's tutorials. Excellent stuff.

Thank you.
 

flyinian

Member
for loops are useful for repeating things a certain amount of times, but for values that can change based on a pattern.
For instance, suppose I want to draw a 3x3 grid of a sprite, for loops make that easy. I can have this:
Code:
for(var y = 0; y < 3; y++){
    for(var x = 0; x < 3; x++){
       draw_sprite(sprite_index, image_index, x * width, y * height);
    }
}
The outer loop starts y = 0, and will continue to loop while the condition is met(y < 3), and add 1 to y every loop (y++, or y = y + 1)
Before the outer loop has a chance to loop again, the inner loop will loop while its condition is met, and add 1 to its value each loop.
So the values of x and y for each loop will be as follows:
y=0, x=0
y=0, x=1
y=0, x=2
y=1, x=0
y=1, x=1
y=1, x=2
y=2, x=0
y=2, x=1
y=2, x=2
At each loop, you are using those x and y values to properly calculate the position of the sprite's coordinates.

The manual is a great place to start learning, check out under the Reference sections, the Language Overview pages. Here you'll find everything that is fundamental, which you should know about before proceeding
Thank you.
 

gkri

Member
You have a deck of cards inside the 1d array deck[ ]. This loop shuffles the deck.

EDIT: I am including another loop at the begining to create a simple "deck" array with numbers 1 -52, in order to test it if you like.


GML:
//create a simple "deck"
var deck = [];

for (var i = 0; i < 52; i++)
{
    deck[i] = i+1;
}

show_debug_message("deck: " + string(deck));

// Fisher–Yates shuffle algorithm
//For more info about the algorithm : https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

var swap = 0;
var tmp = 0;

for(var j = array_length_1d(deck) - 1; j > 0; j--)
{
    //shuffle
    swap = floor(random(j));
    tmp = deck[j];
    deck[j] = deck[swap];
    deck[swap] = tmp;
}

show_debug_message("Shuffled deck: " + string(deck));
 
Last edited:
Looking at your other thread it seems to me that you could trade all of what you have regarding lists, and just use a ds grid.

Instead of switching between two lists, one for "alive", and one for "dead", you could just store 2 pieces of information. A ds grid works in rows and columns, meaning one row could have multiple columns. Such as:

The enemy "name" or "type" (however you want to identify it), and then its health. These would be column 0, and column 1.

I guess a for loop was suggested to you so you could go through whatever data structure you use, and check all of this data at once.

The for loop counter would be the row, and then for the columns you wouldn't need another counter. You know how many there are, and what they contain, so you just refer to them directly.

GML:
var grid_size = ds_grid_height(whatever)  // 'whatever' is the variable  you've set the grid index to when you created it

if grid_size != 0
{
for (var a = 0; a < grid_size; a++)
{
is_enemy = ds_grid_get(whatever, 0, a);  // ds grid index / column / row
is_health = ds_grid_get(whatever, 1, a);  // ds grid index / column / row

if is_health != "is dead" //  a string, that is stored in the "health" column, that tells it to ignore this "enemy". In this case it is not found
//  (!= makes it false, meaning it has found a value other than "is dead" i.e a numerical value which is its health)
{

if is_health == 0 // the health is zero, so it should be dead
{
ds_grid_set(whatever, 1, a, "is dead") // changes the value of "health" to "is dead", and so will not be checked due to the condition above
}
else
{
// its alive, and has health larger than zero, so what do you want to do with it here....?
}
}
else // "is dead" was found to be true
{
// its dead - what do you want to do with it?
}
}
}
Off the top of my head that would (I think) be all that you need.
 
Last edited:

Alexx

Member
• A) While loop. Set an object that randomly choose a random
position within 100 pixels of another object.
• B) For loop. Make a list with 100 numbers, in random order.
Display this data on screen in 4 columns.
• C) Set 4 random points in the room. Make an object visit
each of these locations.
• D) Make a system that takes in all the names of students in
your class. Display onscreen in alphabetical order, one at a time
every 5 seconds.
 
Top