ā 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
bare in mind, completely new to programming š
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 thatThere 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 ?
Probably not.
@Fayaz Are you doing that number slidy puzzle?
And you are trying to randomize tile colors I suppose?
yes exactly
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 š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. š
š
Alr, good luck with it!
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.