// Prepare for a shuffle public void Shuffle() { if (Locked || Count == 0) return; lock (cards) { var cardIds = new int[cards.Count]; //var cardAliases = new ulong[cards.Count]; var rnd = new CryptoRandom(); var posit = new short[cards.Count]; var availPosList = new List<short>(cards.Count); for (var i = 0; i < cards.Count; i++) { //availPosList[i] = (short)i; availPosList.Add((short)i); } for (int i = cards.Count - 1; i >= 0; i--) { var availPos = rnd.Next(availPosList.Count); var pos = availPosList[availPos]; availPosList.RemoveAt(availPos); cardIds[i] = cards[pos].Id; //cardAliases[i] = cis[r].Visible ? ulong.MaxValue : Crypto.ModExp(cis[r].Key); //cis[pos] = cis[i]; posit[i] = pos; } // move own cards to new positions DoShuffle(cardIds, posit); // Inform other players Program.Client.Rpc.Shuffled(Player.LocalPlayer, this, cardIds, posit); } }
/// <summary>Part of a shuffle process.</summary> /// <param name="group">The group being shuffled.</param> /// <param name="card">An array containing the CardIdentity ids to shuffle.</param> public void Shuffle(Group group, int[] card) { // Array to hold the new aliases (sent to CreateAlias) ulong[] aliases = new ulong[card.Length]; // Intialize the group shuffle group.FilledShuffleSlots = 0; group.HasReceivedFirstShuffledMessage = false; group.MyShufflePos = new short[card.Length]; // Check if we received enough cards if (Player.Count - 1 <= 0) return; if (card.Length < group.Count / (Player.Count - 1)) Program.Trace.TraceEvent(TraceEventType.Warning, EventIds.Event, "[Shuffle] Too few cards received."); // Do the shuffling var rnd = new CryptoRandom(); for (int i = card.Length - 1; i >= 0; i--) { int r = rnd.Next(i + 1); int tc = card[r]; card[r] = card[i]; // Create a new alias, if the card is not face up CardIdentity ci = CardIdentity.Find(tc); if (group.FindByCardIdentity(ci) != null) { card[i] = tc; aliases[i] = ulong.MaxValue; ci.Visible = true; } else { ci = new CardIdentity(Program.GameEngine.GenerateCardId()); ci.MySecret = ci.Alias = true; ci.Key = ((ulong)Crypto.PositiveRandom()) << 32 | (uint)tc; card[i] = ci.Id; aliases[i] = Crypto.ModExp(ci.Key); ci.Visible = false; } // Give a random position to the card group.MyShufflePos[i] = (short)Crypto.Random(group.Count); } // Send the results Program.Client.Rpc.CreateAlias(card, aliases); Program.Client.Rpc.Shuffled(group, card, group.MyShufflePos); }
// Do the shuffle internal void DoShuffle() { // Set internal fields //PreparingShuffle = false; //FilledShuffleSlots = 0; //HasReceivedFirstShuffledMessage = false; // Create aliases //var cis = new CardIdentity[cards.Count]; //for (int i = 0; i < cards.Count; i++) //{ // cis[i] = cards[i].CreateIdentity(); //} // Shuffle var cardIds = new int[cards.Count]; //var cardAliases = new ulong[cards.Count]; var rnd = new CryptoRandom(); var posit = new short[cards.Count]; var availPosList = new List<short>(cards.Count); for (var i = 0; i < cards.Count; i++) { //availPosList[i] = (short)i; availPosList.Add((short)i); } for (int i = cards.Count - 1; i >= 0; i--) { var availPos = rnd.Next(availPosList.Count); var pos = availPosList[availPos]; availPosList.RemoveAt(availPos); cardIds[i] = cards[pos].Id; //cardAliases[i] = cis[r].Visible ? ulong.MaxValue : Crypto.ModExp(cis[r].Key); //cis[pos] = cis[i]; posit[i] = pos; } // Send the request //Program.Client.Rpc.CreateAlias(cardIds, cardAliases); //Program.Client.Rpc.Shuffle(this, cardIds); Program.Client.Rpc.Shuffled(this, cardIds, posit); }
private void ShuffleAlone() { var rnd = new CryptoRandom(); for (int i = cards.Count - 1; i >= 0; i--) { int r = rnd.Next(i + 1); Card temp = cards[r]; cards[r] = cards[i]; cards[i] = temp; } OnShuffled(); }
// Do the shuffle internal void DoShuffle() { // Set internal fields PreparingShuffle = false; FilledShuffleSlots = 0; HasReceivedFirstShuffledMessage = false; // Create aliases var cis = new CardIdentity[cards.Count]; for (int i = 0; i < cards.Count; i++) { if (cards[i].IsVisibleToAll()) { cis[i] = cards[i].Type; cis[i].Visible = true; } else { CardIdentity ci = cis[i] = new CardIdentity(Program.Game.GenerateCardId()); ci.Alias = ci.MySecret = true; ci.Key = ((ulong) Crypto.PositiveRandom()) << 32 | (uint) cards[i].Type.Id; ci.Visible = false; } } // Shuffle var cardIds = new int[cards.Count]; var cardAliases = new ulong[cards.Count]; var rnd = new CryptoRandom(); for (int i = cards.Count - 1; i >= 0; i--) { int r = rnd.Next(i + 1); cardIds[i] = cis[r].Id; cardAliases[i] = cis[r].Visible ? ulong.MaxValue : Crypto.ModExp(cis[r].Key); cis[r] = cis[i]; } // Send the request Program.Client.Rpc.CreateAlias(cardIds, cardAliases); Program.Client.Rpc.Shuffle(this, cardIds); }