# Desert Dog

Member Since 26 Aug 2008
Offline Last Active Private

### Basic Array Functions

07 January 2013 - 08:19 AM

----------------- The Intro ------------------

I've noticed a few questions in the Q&A forum lately on sorting an array. I only know the bubblesort 'off hand', so I give this to them, but this is hardly the best.

Searching on the site, there are some nice sorting functions, but it seems that all of them use functions like variable_local_array_get() or such.. these functions no longer exist in GM-S.

Actually, there is no way to pass an array to a script in GM. So these functions simply work to manipulate a 'myArray'.. it's up to you to replace it with your own array name.

So I've implemented some of the standard sorting algorithm's into GML.. and I'm posting it here, so that I can just send people here.

Here is what I've got.

-------------- The Code --------------

shuffle_array( size );
Spoiler

array_in_order( size )
Spoiler

simple_bubblesort - - Note.. this is not optimal, and is only included as it's simple, and easy to understand
Spoiler

bubblesort( size ) -- optimised ( should be )
Spoiler

bogosort -- Just a bit of fun. I saw this on wikipedia, and had to write it. (don't actually use it!)
Spoiler

insertion_sort( size )
Spoiler

quicksort( size ) -- quicksort is usually a recursive algorithm. However, GM-S does have a problem with recursion.. it can only go down to a depth of 32. Also, as a general rule recursion should be avoided in GM anyways. So here is an iterative implementation that I found, and implemented in GML..
Spoiler

mergesort() - a very fast sorting algorithm, which rivals quicksort..
Spoiler

heapsort() - again, a very quick sorting algorithm. Usually recursive, this script is the bottom-up implementation, which avoid that recursive behavior.
Spoiler

--------------The Rambling --------------

I haven't done any serious speed tests..( no numbers). But I quickly tried to sort a large array ( 1000 ).. it took the bubblesort's a second or so.. the insertion_sort was marginally faster. The quicksort() was very fast! Again, no actual numbers, but I'd estimate it's at least 4 times faster than the the next fastest. Make that more like 10-20 times faster. With an array sized 3000+, there is no comparison between bubblesort, and quicksort. Phew!

What one should you use? Well, whichever one suits you. If you have a small array, just use a bubblesort.. it'll be fine. If you want to be a bit more adventurous, I believe insertion_sort is overall generally better than bubblesort.

If you have a large array, definitely go for the quicksort. If you have a VERY large array, use bogosort()

### Forum Chess (Game #2)

04 June 2012 - 11:12 PM

I've been waiting for flexaplex to start a new one. But he hasn't. D=
*steals post*

How To Play

It's actually very simple. Players take it in turns to post a generated image of their move from the chessvideos.tv chess diagram generator, like so:

You should be able to click on the image to go to that generated position then make your own move on the board (try it now by clicking the image I just posted). After you make your move click the 'Generate Diagram' button which you should see below the board. Next all you have to do is copy the generated URL into your post here to make your move, the correct URL is in the box immediately below where it says 'With link back to position:'. The start of it should look like: [url=http://www.chessvideos.tv/chess-diagram-generator.php?fe

IMPORTANT NOTE: Try and keep alert and look at the boards posted a few positions back, not just the last one posted. Watch out for people posting their move while you are making your own, if this occurs please edit your post out as fast as you can so no one else gets confused. If you see anybody that has made an invalid move then please point it out immediately, the game board will then go back and continue from the post made before the invalid move occurred. I also urge any moderators that may be looking at the topic to edit posts if they see anything wrong, leaving a note explaining what was wrong of course.

We'll make this firstsecond game a free for all, meaning anybody can come in mid-game and decide which side they want to play. In future games if this takes off we might set-up teams before starting the game or something.

Have fun

### # SUICIDE Chess (Game 1) #

02 June 2012 - 07:49 AM

SUICIDE CHESS!!

This is a fun version of chess which everyone can play, it doesn't matter if you're good or bad at chess because the aim in suicide chess is to lose all your pieces! Anyone can join in and play at any point in the game, just pick a side you want to play and make your move.

These are the rules (from wikipedia)
The rules of the game are the same as those of chess except for the following additional rules:

*Capturing is compulsory.
*When more than one capture is available, the player may exercise choice.
*The king has no special prerogative and accordingly:
-It may be captured like any other piece.
-There is no check or checkmate.
-There is no castling. castling is allowed. Because we can!
-Pawns may also promote to King.
*In the case of stalemate, there are different rules:

-It is a win for the player with the fewer number of pieces, and if both have the same number it is a draw. The type of the piece makes no difference (FICS rules).

*steal from Flexaplex*
How To Play

It's actually very simple. Players take it in turns to post a generated image of their move from the chessvideos.tv chess diagram generator, like so:

You should be able to click on the image to go to that generated position then make your own move on the board (try it now by clicking the image I just posted). After you make your move click the 'Generate Diagram' button which you should see below the board. Next all you have to do is copy the generated URL into your post here to make your move, the correct URL is in the box immediately below where it says 'With link back to position:'. The start of it should look like: [url=http://www.chessvideos.tv/chess-diagram-generator.php?fe

IMPORTANT NOTE: Try and keep alert and look at the boards posted a few positions back, not just the last one posted. Watch out for people posting their move while you are making your own, if this occurs please edit your post out as fast as you can so no one else gets confused. If you see anybody that has made an invalid move then please point it out immediately, the game board will then go back and continue from the post made before the invalid move occurred. I also urge any moderators that may be looking at the topic to edit posts if they see anything wrong, leaving a note explaining what was wrong of course.

Have fun

### [GMCJam 6]A Warriors Rest

30 April 2012 - 02:50 PM

A Warrior's Rest:
WARNING: Yes, contains blood.

http://www.host-a.net/u/Desert%20Dog/A_Warriors_Rest.zip

### Step-by-Step MineSweeper

22 April 2012 - 10:55 AM

• Title:Step-by-Step Minesweeper examples.(using Hexagons) Title
• Description: A series of editable gm files which show a minesweeper game
being built from the ground up. Uses hexagons, but the code is applicable,
and quickly changed to use other shapes.
• GM Version: :GM81: , but applicable to GM-html5, etc.
• Registered: Yes, uses ds_list function. Maybe if requested, I can post a work-around for Lite.
• File Type: .zip containing 7 .gm81
• File Size: 0.08 mb

My Rights
Regarding the editable files, and the code, use&abuse as you wish. Feel free to use
it as a base to build your own games off of.. credit is NOT required!

Intro:
Here I attempt to show you a minesweeper game being built from the ground up.
Minesweeper is a relatively simple game, but it does require solid coding skills.

This tutorial/example set is intended for the beginner, to intermediate programmers.
You must have a basic understanding of GM, and/or coding. This is a set of examples,
not a tutorial. And although I try to explain what is happening, there will be some
stuff that I miss, or assume you will know about.

Example _01

Here we have a basic field set up. I'm using hex's.

In the create event of the obj_hex, we have 2 important variables.

One is mine If this is true, then that means this particular hex
will hold a mine.. if the player clicks on it, he loses!

The other variables are an array.
With a piece of code, I get the hex to automatically check around it. If there is a hex beside it,
it stores a reference to it in an array. next[i];

Second object is the obj_controller.

First up, we set the alarm for 1. This is just to give all the hex objects a chance to initialise all
their variables.

In alarm[1] we simply call the create_mines script. This scripts simply:
creates a ds_list.
shoves all the obj_hexes into it.
shuffles the list.
grabs the first X amount of hex objects, and set the mine variable on them to true.

Note, X is an argument you pass to the script.. so you can easily control how many mines appear
in a game.

You can run the game, and you can see that 'Mine' hexes are created randomly around the
playing field. That's good, but you can't click anything yet. Also, mine sweeper is meant to
have numbers on the hex's beside the bombs!

Example _02.

Here we want to add the 'number' values for the hex's. This is actually really simple. You'll now
see one of the reasons why I want that next[i] array for!

First, add a new variable in the hex object. we'll call it number (and we'll set it to 0 )

Then, we create a new function. we'll call it add_numbers.
In this function, we simply check all the next[i] array values. If it is NOT -1 (i.e. if it's a reference
to a hex) then we check that instances mine flag variable. If it's true, we simply add number+=1;

After writing this script, we call it at the end of create_mines, like this:
```with (obj_hex)
{
}
```

Test the game (maybe add a draw_text(x,y,string(number)) to the game first) and you'll notice
that hexes next to mines now have a number.

Example _03

We've got some basics up.. now to actually make it play!
First up, I add a simple 'clicked' variable in the hex's.
clicked=false. If you click it, click =true.

In draw event, just draw a normal sprite if clicked=false, otherwise, draw our mine symbol,
or our number, etc.

However, this still is pretty basic. To begin with, you can't die. Also, remember in Windows
minesweeper, if you clicked on a square with a number of zero (no mines around it) it'd
*expand* out, and show all that area that is empty?

Well, I'll show you how to do that in 04. And you'll REALLY see why I use that next[i] array..

Example _04

First up, we edit the click event in the obj_hex. After flagging the hex as 'clicked' we
check if the mine variable is, in fact, true. If it is, then of course, we lose the game.

Otherwise, we now check if number=0. We don't really care if it = 3, or whatever, 0
is the hex which 'expands out'. like a minesweeper game does.

If it is, we call a little function called click_area_clear();

Never heard of it? That's because we're about to write it!

It's relatively simply. First, we cycle through the next[i] array.
If a next[i].clicked is false, we set it to true. We then check if number =0
if it does, we then recursively call click_area_clear(), and the process of checking
next[i] array will continue.. only with this hex.

A quick test, and we're done. Everything is working well! For the curious, this 'method' of going
through referenced objects is a type of depth-first search.

Example _05.

Here is just some polishing up.
I add a simple red flag sprite, and add a 'flagged' variable in the hex objects. If you right click them,
they will show/hide a flag.. as per traditional minesweeper.
Add some similar sprites for a mine. Also, I make a red hex sprite.. when you click on a mine,
the 'dead' won will show red. Just like Win minesweeper.

In Win minesweeper, after you step on a mine, all the mines become visible. so add a script for that.
mines_visible. It just loops through all the hex objects, and if mine=true, set clicked=true

We also need to check when we have won. To do this, I add a script called check_for_victory
Again, this just loops through all the hex objects, checking for all the hex's that are not mines.
if all of them have clicked=true, then we've done it! game over.

Finally, I add a quick global win/lose variables. Make it so you can't click any more hex's if you have
won/lost, and set those variables at the right times.

Example _06

Here I quickly show minesweeper using squares. I haven't changed any of the sprite names(it still
all says 'hex') but that doesn't matter for now... you can change those yourself.
What's important is you see how easy it is to change between the two.
Other than changing the sprites to squares, all I have done is change the setting of the array next[i]
in the create event of the obj_hex. (which is now a square..!)
It now stores references to all 8 square around it.
Give the game a run, and hey presto, everything works great!

This just shows how flexible the code base is. You could even use triangles, or someother
shape if you wanted to. You don't have to have all the objects lined up side by side, you
could build uneven shapes with them, too.

Example _07

Back to hex!
In here, we add some final little touches. We add a timer. We add something to show how
many mines are in the field. We add a simple save/load time using .ini files.
I use a .ini to ensure that it's cross-platform-friendly.. feel free to use the built in save system,
and the message box functions if your game is just going to be for the PC/mac.

Final Step+Closing .

(What I did to it: Note, source not included, as I finished it in GM-html5. Click pic to play it online!!)

Polishing the game up is up to you. We have a basic, working minesweeper game. But there is so much more
that can be done to add to the gaming experience!
Make different maps. Make nice graphics, something animated. Give the game a theme.. dark,
dirt, with green, with grubby mines, just like a real mine-field! Or mark out where the dog's gone
poop in your yard without stepping in it. *ugh*.

I've done the game in hexagons, but you can easily change it so that it uses squares, diamonds
(an isometric look) or even something weird like triangles.

Keep rocking!
~D