C
C#ā€¢2y ago
fayaz3839

ā” N puzzle game Im getting a random color everytime I slide the puzzle

Hey! Im creating a n puzzle game and im almost done however im stuck at something. Everytime I move my blankspace to sort the numbers I get a random color and when I move it again I get a new color which I dont want. I want it to randomize the colors once then it should be the same colors all the time. Here's my code: https://pastebin.com/rdzRtejU
Pastebin
using System;using System.Collections.Generic;using System.Diagnost...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
10 Replies
fayaz3839
fayaz3839ā€¢2y ago
bare in mind, completely new to programming šŸ˜„
Pobiega
Pobiegaā€¢2y ago
Why are there so many blank lines randomly throughout the code? Near impossible to read it. new Random().Next(256) please don't do this, especially not multiple times if you are using a modern .NET version, use Random.Shared.Next(256) instead, and if using an older version, make a single random instance and reuse that
fayaz3839
fayaz3839ā€¢2y ago
There reason behind the black lines was to make it more understandable for myself to read the code. Will Random.Shared.Next(256) this one solve my problem ?
Pobiega
Pobiegaā€¢2y ago
Probably not.
LPeter1997
LPeter1997ā€¢2y ago
@Fayaz Are you doing that number slidy puzzle? And you are trying to randomize tile colors I suppose?
fayaz3839
fayaz3839ā€¢2y ago
yes exactly
LPeter1997
LPeter1997ā€¢2y ago
Ok, there are two very different ways There's the really easy way of using some associative container and generating a random color at the start, and keeping it around. For example, a Dictionary<int, Color> or whatever can work fine for that The other way is a bit more... advanced, but does not require an additional container. You have a list of all the colors, then generate a number m that is a relative prime to colors.Length, and a k offset (names don't matter, it's just textbook convention). Then the tile with the number i on it would get the colors[(m * i + k) % colors.Length] color The former is what I guess most would do The latter is just a fun thing if you know hashing šŸ˜„
fayaz3839
fayaz3839ā€¢2y ago
Thank you, for the answer. I will try the first solution you provided. If I did not manage to solve it, i'll ask for more help. I have some knowledge about hashing but not in c# only in c. šŸ™‚ šŸ˜„
LPeter1997
LPeter1997ā€¢2y ago
Alr, good luck with it!
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.