#binary search help
1 messages · Page 1 of 1 (latest)
https://hatebin.com/nwqgnsskup all of my code, because it's necessary for context
so for anyone who doesn't know already - it's a turn-based game between an AI and the player to try and guess where is the correct card
If the correct card happens to be in the second half of the card deck, it works wonderfully fine!
But if it's on the first half, it starts doing crazy things: the limits (the range of the search) is set correctly, but 1st- it ignores the change of turns and does the search automatically - and it doesn't search through the first half, but through the second
It's driving me crazy
It seems like you have a couple different things going on, based on your text here, compared to your title. As for your title, re Binary Search, your collection needs to be sorted. I'm not sure if it is based on your code.
The collection is sorted, it gave me a headache because i used // allCards = allCards.OrderBy(x => x.cardOrder).ToList(); but it suddenly stopped working, so i just ordered the cards in the unity editor
i know it's not the best answer to that problem... but something happened and the cards didn't want to be sorted haha
right now- the cards are sorted exactly the way i want them and the code can detect it
also on another script i have set the class Cards with these 3 variables public bool Winner; public bool isTurned; public int cardOrder;
Ok. I would double check that the cards are actually sorted during runtime, before you call whatever function it is (print it out or something).
Then it would just be a standard binary search algo:
int left = 0, right = nums.Length-1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (nums[mid] == target)
return mid;
else if (target > nums[mid])
left = mid + 1;
else
right = mid - 1;
}
nums being your collection, and returning mid which is the index, or you could make it nums[mid for the value.
If it's not going into the right function based on whatever turn, then that's something you need to figure out. Break it down into small problems, resolve one at a time.
yeah, i've been trying that for the past 3 or 4 days, with little to no answer hah
i've debugged a ton of things, asked for help to classmates... nothing happens
I guess I'm confused here; does your project work fine when you remove BS?
Can you just run it linearly?
Oh ok, so then 1) your array is not sorted 2) your algo is wrong.
It should be fairly simple to debug from there.
sorry it's in spanish, but I ran the game again and as you can see, the cards are sorted the way i want, from 0 to 9
so the problem is the algorithm :[
Yeah, I mean i honestly have no clue why you're looping forwards, and then looping backwards lol
I think you should follow this. This is the standard format.
Your code is not even near that.
I think you may be misunderstanding how Binary Search operates.
I can't even understand your code in that section, tbh.
i understand binary search and my code is pretty simple afaik, it's pretty much the same except that i'm working with classes and not with ints, and also changing turns and cards