A random security colour code.

Hi everyone.

So, I have been playing around for a while now and with help from this forum and its crew, I have come along in leaps and bounds and hopefully will be able to make a released IF very soon.

Now comes the ask. I have created a snippet of code here that converts random numbers to colour codes and it works, but I am wondering if I could have done it in a more efficient way. Please forgive the // notes.

Thanks in advance,
-=Darchy=-

 //
// Create the list from Black to Silver.
//
c_list = Newlist()
list add (c_list, "Black")
list add (c_list, "Brown")
list add (c_list, "Red")
list add (c_list, "Orange")
list add (c_list, "Yellow")
list add (c_list, "Green")
list add (c_list, "Blue")
list add (c_list, "Violet")
list add (c_list, "Grey")
list add (c_list, "White")
list add (c_list, "Gold")
list add (c_list, "Silver")
//
// Generate 4 random numbers to be assigned to colour list - Remember to use zero!!
//
col_gen_1 = GetRandomInt(0,11)
col_gen_2 = GetRandomInt(0,11)
col_gen_3 = GetRandomInt(0,11)
col_gen_4 = GetRandomInt(0,11)
msg (col_gen_1)
msg (col_gen_2)
msg (col_gen_3)
msg (col_gen_4)
//
// Below Tests that the correct colours are chosen - it works,
//
// msg (code_list)
// msg (ListItem(c_list,col_gen_1))
// msg (ListItem(c_list,col_gen_2))
// msg (ListItem(c_list,col_gen_3))
// msg (ListItem(c_list,col_gen_4))
//
// Convert the random numbers into to Colour Codes
//
the_code_1 = (ListItem(c_list,col_gen_1))
the_code_2 = (ListItem(c_list,col_gen_2))
the_code_3 = (ListItem(c_list,col_gen_3))
the_code_4 = (ListItem(c_list,col_gen_4))
//
// Show the list of colours are correct then create a 4 colour string (including spaces)
//
msg (the_code_1)
msg (the_code_2)
msg (the_code_3)
msg (the_code_4)
main_code = the_code_1 + " " + the_code_2 +" " +  the_code_3 +" " +  the_code_4
//
// Show the final outcome
//
msg (main_code)

Not sure if you're using the intermediate variables for something else, but if not you could do something like:

c_list = Split("Black;Brown;Red;Orange;Yellow;Green;Blue;Violet;Grey;White;Gold;Silver", ";")
the_code = NewStringList()
for (i, 1, 4) {
  list add (the_code, PickOneString(c_list))
}

// For demonstration, the individual colours
//
msg (the_code[0])
msg (the_code[1])
msg (the_code[2])
msg (the_code[3])

// And the final code
//
main_code = Join (the_code, " ")
msg (main_code)

Thank you mrangel! - The intermediate variables I used are just my way of singurlarly checking individal parts then expanding upon them. ( I don't know if this is good practice or not)

Your example does exactly what I was trying to do AND is a lot shorter. I am assuming that if I change the "for (i, 1, 4)..." to "for (i, 1, 5 etc)" - I can create a 5 or more colour code?

I appreciate the help,

-=Darchy=-


Any code that helps you de-bug a program is good code... (or practice...)
Always keep the // notes... you never know if you will go back later for a "trick" you did on an old program, and need to use the trick again... Or if you want to revamp an old program to make it new, then have no idea what you did the first time and would find it easer to just stare all over...
The person that learns the least in programming is someone that makes no mistakes... (think about it...)
Shorter code is not always the best code, because it would be the hardest to de-bug...
Learn to write bigger first, then learn to optimize later...
Then when you get good, really good, you can write optimized code first...


Wise words indeed DarkLizerd. I remember when I first started to program using GFA basic - REM statements all over the place :D


Thanks fellow Basic programmer...


Is this to be used to randomize the colors of the game?


Looks like resistor colour codes to me. The first 10 are numbers and the Gold & Silver represent percentile tolerances.


"Bad Boys Rape Our Young Girls, But Violet Gives Willingly..."
I did not catch that before!


@jmnevil54 - It can be used to do a few things, like print the colour of the given random colour in the random colour chosen by adding this code to it:

str_0 = "{color:" + the_code[0] + ":" + the_code[0] + "}"
msg (str_0)

but in the initial instance I am using it for a random colour code generator.

@R2T1 and @DarkLizerd - it is indeed the Resistor Colour Code - Well spotted guys!

all the best,
-=Darchy=-


I have altered the original code I was given from @mrangel, to produce a new list that is coloured using the randomly chosen colours - please forgive me my folly, but I find this so addictive.

// Set up random colour code generator.
//
game.c_list = Split("Black;Brown;Red;Orange;Yellow;Green;Blue;Violet;Grey;White;Gold;Silver", ";")
game.the_code = NewStringList()
//
// Create secondary list to add the colours.
game.colour_list = NewStringList()
//
for (i, 1, 4) {
  list add (game.the_code, PickOneString(game.c_list))
}
//
// Now to add the colours.
list add (game.colour_list, "{color:" + game.the_code[0] + ":" + game.the_code[0] + "}")
list add (game.colour_list, "{color:" + game.the_code[1] + ":" + game.the_code[1] + "}")
list add (game.colour_list, "{color:" + game.the_code[2] + ":" + game.the_code[2] + "}")
list add (game.colour_list, "{color:" + game.the_code[3] + ":" + game.the_code[3] + "}")
game.main_code = Join (game.the_code, " ")
//
//  Make the colour string.
game.main_colour = Join (game.colour_list, " ")
//
msg (game.main_code)
//
// Print the coloured string.
msg (game.main_colour)

Alternatively, you could build the strings directly, and use the same loop twice.

c_list = Split("Black;Brown;Red;Orange;Yellow;Green;Blue;Violet;Grey;White;Gold;Silver", ";")
game.plain_code = ""
game.coloured_code = ""

for (i, 1, 4) {
  randomcolour = PickOneString(c_list)
  game.plain_code = Trim(game.plain_code) + " " + randomcolour
  game.coloured_code = Trim(game.coloured_code) + " {color:" + randomcolour + ":" + randomcolour + "}"
}

msg ("The plain code is "+game.plain_code)
msg ("And the coloured code is "+game.coloured_code)

If you're doing the same thing to each element of a list (like adding the {color:...} tag to it), it might be more efficient to put it in a loop. The same for loop can be used to add elements to both lists, or both strings; making it very slightly more efficient.

Also, 2 ways of making your string. You can either take an empty string, and add each colour in turn to the end of it; or you can use a stringlist and then stick them together using Join. Which one is easiest will depend on what you're doing with it; some things will be easier with the list, some easier with the string.
(Note that if you're doing somestring = somestring + " " + another_value in a loop, you might need to use Trim() to remove the extra space at the beginning)


Hi mrangel,

Awesome! - I will admit I tried to use the same loop, but I kept getting a non zero ... function error, but then again I didn't declare game.plain_code = "" or game.coloured_code = "" at the beginning :S

Your method also makes more sense.

Thanks,

-=Darchy=-


Log in to post a reply.

Support

Forums