A Dictionary<Image, int>
doesn't have a concept of having an order. The idea of using a SortedDictionary<Image, int>
wouldn't make sense as you still don't control where an item is added - that would be based on the key.
What you really need is some sort of list where you do control the position that you add items.
It's likely that List<(int Value, Image Image)>
, or even Queue<(int Value, Image Image)>
, is what you want. The type (int Value, Image Image)
is a tuple containing two elements. It's much like KeyValuePair<Image, int>
that you're currently using.
I'm going to suggest some code for you to play with, but I'm afraid it's difficult to know exactly what you are trying to do with the minimal code that you presented.
I'm also a little confused as to what you're attempting with this code:
cpuWinnings.Add(x, y);
imgcpuwinning.Image = cpuWinnings.Keys.ElementAt(0);
cpuWinnings.Clear();
You appear to want to add an element to the end of cpuWinnings
, but then you use the first item of the list, and then you clear the list. What was the point of adding an item to the end if you're going to just clear it? Or, if you're expecting .Add(x, y)
to put it to the front of the list then why not just do imgcpuwinning.Image = x;
before clearing the list?
I'm going to assume that you have the desire to add it to the end of the list.
So here's the code. To start I want to define a way to say who the current player is and to reduce the number of variables you have.
Let's create a Player
enum
:
public enum Player
{
Human, Cpu
}
Now if you're trying to select the first item from a list and then remove that item you probably want a Queue
. And we want a queue for each of the Human
and Cpu
players.
var player = new Dictionary<Player, Queue<(int Value, Image Image)>>()
{
{ Player.Human, new Queue<(int Value, Image Image)>() },
{ Player.Cpu, new Queue<(int Value, Image Image)>() },
};
It seems to me for the winnings
you just need a list.
var winnings = new Dictionary<Player, List<(int Value, Image Image)>>()
{
{ Player.Human, new List<(int Value, Image Image)>() },
{ Player.Cpu, new List<(int Value, Image Image)>() },
};
And for your PictureBox
elements you just need this:
var holder = new Dictionary<Player, PictureBox>()
{
{ Player.Human, imghumanwinning },
{ Player.Cpu, imgcpuwinning },
};
Now to select and remove the first element from the queue you do this:
(int Value, Image Image) xy = player[Player.Cpu].Dequeue(); //remove first
(int Value, Image Image) ab = player[Player.Human].Dequeue(); //remove first
Note: I've kept the xy
and ab
variable names from your code, I've just joined them together. Ideally you'd name these something a little more meaningful.
Next we want to find out who the current player is:
var currentPlayer = xy.Value >= ab.Value
? Player.Cpu
: Player.Human;
Now processing the values is easy:
player[currentPlayer].Enqueue(xy); //add to end of queue
player[currentPlayer].Enqueue(ab); //add to end of queue
winnings[currentPlayer].Add(xy);
holder[currentPlayer].Image = winnings[currentPlayer].First().Image;
winnings[currentPlayer].Clear();
There's no if
any more.
Now, I might be wrong about needing a Queue
, but it seems to fit you current code. It could be that you need to use a Queue
for winnings
too. Maybe just a List
for both would suffice. You'd need to provide more detail for me to have a better response.