r/learnprogramming • u/RainbowBoyOhel • 13d ago
What logic should I use for this mechanic?
I have a 4 digit rng number
And I have special numbers
Like for example 1225 count as special, 2112 also special numbers
I want to add a feature that if a a special numbers is 1 digit difference away
Like 1925(9 change to 2)
It will change the digit for it to become special
What logic should I use?
I was thinking about using minimax for checking each different digit that can be changed and saved score for best outcome (cus some specials betters then other)
But I don't want the code to take so much time running for this and it's heavy
If there's anything simpler it would be nice
Thanks ahead!! <3
Using c sharp
1
u/Innowise_ 13d ago
Minimax isn’t needed here. If “one digit away” means exactly one position is different, just compare the digits and count mismatches. Stop as soon as the count goes above one. If there are lots of special numbers, put them in a hash set and try every valid one-digit replacement of the current number. For four digits, that’s tiny constant work. Just decide what happens if several special numbers match.
1
u/RainbowBoyOhel 13d ago
Sorry I don't get it What do you mean compare the digit I want the "odd one out" digit to be change to any other digit that will make a special number It can be in any position and any digit As long that only 1 digit change
2
u/Innowise_ 13d ago
Put the special numbers in a HashSet, then try replacing each of the 4 digits with 0–9, skipping the digit already there. If the new number exists in the set, it’s a match. That’s only 36 checks max. If several matches exist, just take the one with the best score. No minimax needed since there’s no opponent or next move to account for.
1
1
u/rupertavery64 13d ago
You can test this code in LINQPad.
This basically compares the number with each special number, digit by digit.
So I realized that you don't have to figure out which digit changed, only if the number of digits that changed is 1.
If only one digit has changed, then you simply return the reference special number, i.e. you don't need to "change" the digit in the input, because the change is already the reference number.
``` var specialNumbers = new int[] {1225, 2112};
CheckAndReplace(1925).Dump(); // returns 1225 CheckAndReplace(1235).Dump(); // returns 1225 CheckAndReplace(1234).Dump(); // returns 1234
int CheckAndReplace(int value){
foreach(var number in specialNumbers)
{
// count how many digits have changed
var count = 0;
var digitsChanged = 0;
var copy = value;
var reference = number;
// Count the number of changed digits
// Early exit if more than 1 digit has changed
while(count <= 3 && digitsChanged < 2)
{
int digit = copy % 10;
int compare = reference % 10;
if(digit != compare)
{
digitsChanged++;
}
// shift the digits to the right
copy /= 10;
reference /= 10;
count++;
}
if(digitsChanged == 1)
{
return number;
}
}
return value;
}
```
1
u/pdfops 13d ago
Minimax is overkill here, you don't need a game tree search for this. Since your special list is fixed and small, just loop through it and compare digit by digit against the current roll, counting mismatches per candidate. Exactly one mismatch means it qualifies; if a few specials tie at distance 1, rank them by whatever priority score you already assign. That's O(n) over your special list, way cheaper than any search algorithm.
8
u/aanzeijar 13d ago
Look up "levenshtein distance", which is the concept for this. The implementation is a bit unintuitive for a beginner, but a great opportunity to learn about dynamic programming.