shuffle

A library that shows off shuffling decks of cards for visualization purposes.

Created from templates made available by Stagehand under a BSD-style license.

Usage

A simple usage example:

import 'dart:math';

import 'package:shuffle/shuffle.dart';

main() {
  ShuffleIteration noShuffle = (List<int> deck, Random r) {
    return deck;
  };

  ShuffleIteration pickup = (List<int> deck, Random r) {
    List<int> next = [];
    for (int i = 0; i < 52; i++) {
      next.add(deck.removeAt(r.nextInt(deck.length)));
    }

    return next;
  };

  ShuffleIteration overhand = (List<int> deck, Random r) {
    // number of cards left in hand before shuffle ends
    int threshold = 5;
    int length = 52;
    int split = (length / 2).floor() + r.nextInt(10);

    while (length > threshold) {
      List<int> list1 = deck.sublist(0, split);
      List<int> list2 = deck.sublist(split, length);

      deck.setRange(0, length, list2 + list1);
      length = split;
      split = (length / 2).floor() + r.nextInt((length / 5).ceil());
    }
    return deck;
  };

  ShuffleIteration ruffle = (List<int> deck, Random r) {
    int length = 52;
    int split = (length / 2).floor() + r.nextInt(2);

    int i1 = 0;
    int i2 = split;

    List<int> temp = [];

    while (i2 < 52 || i1 < split) {
      int stand = r.nextInt(3) + 1;
      if (i1 + stand < split) {
        temp += deck.sublist(i1, i1 + stand);
        i1 += stand;
      } else if (i1 < split) {
        temp += deck.sublist(i1, split);
        i1 = split;
      } else {
        temp += deck.sublist(i2, 52);
        i2 = 52;
      }

      stand = r.nextInt(3) + 1;

      if (i2 + stand < 52) {
        temp += deck.sublist(i2, i2 + stand);
        i2 += stand;
      } else if (i2 < 52) {
        temp += deck.sublist(i2, 52);
        i2 = 52;
      } else {
        temp += deck.sublist(i1, split);
        i1 = split;
      }
    }
    return temp;
  };

  int decks = 100;

  Map<String, List<ShuffleStrategy>> shuffles = {
    "Initial Deck": [ShuffleStrategy(1, noShuffle)],
    "Random": [
      ShuffleStrategy(1, pickup),
      ShuffleStrategy(3, pickup),
      ShuffleStrategy(6, pickup),
    ],
    "Overhand": [
      ShuffleStrategy(1, overhand),
      ShuffleStrategy(3, overhand),
      ShuffleStrategy(6, overhand),
      ShuffleStrategy(10, overhand),
    ],
    "Ruffle": [
      ShuffleStrategy(1, ruffle),
      ShuffleStrategy(3, ruffle),
      ShuffleStrategy(6, ruffle),
      ShuffleStrategy(10, ruffle),
    ],
  };

  for (MapEntry<String, List<ShuffleStrategy>> e in shuffles.entries) {
    String name = e.key;
    List<ShuffleStrategy> list = e.value;

    for (ShuffleStrategy strategy in list) {
      for(int i=0; i < decks; i++){
      print(
          "$name, ${strategy.iterations}: ${Deck.defaultDeck(i)..shuffle(strategy)}");
      }
    }
  }
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Libraries

shuffle
Support for doing something awesome. [...]