Commit 05f966ed authored by Russell Liu's avatar Russell Liu
Browse files

update Deal to generate every possible deal from a frecl deal number

parent 0f66f565
package main;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
......@@ -7,17 +8,30 @@ import java.util.stream.IntStream;
public class Deal {
private List<Card> cards = new ArrayList<>();
private final List<Card> cards = new ArrayList<>();
/* Source: http://www.solitairelaboratory.com/mshuffle.txt */
public Deal(int msNum) {
int[] deck = IntStream.range(0, 52).toArray();
MSVCLCG lcg = new MSVCLCG(msNum);
int deckNumLeft = 52;
for (int i = 0; i < 52; ++i) {
int j = lcg.gen() % deckNumLeft;
for (int i = 52; i > 0; ) {
int j = lcg.gen() % i;
cards.add(new Card(deck[j]));
deck[j] = deck[--deckNumLeft];
deck[j] = deck[--i];
}
}
/* 1 to 80658175170943878571660636856403766975289505440883277824000000000000 */
public Deal(BigInteger freclNum) {
freclNum = freclNum.subtract(BigInteger.valueOf(1));
BigInteger[] factorials = new BigInteger[52];
factorials[0] = BigInteger.valueOf(1);
for (int i = 1; i < 52; ++i)
factorials[i] = factorials[i - 1].multiply(BigInteger.valueOf(i));
List<Integer> deck = IntStream.range(0, 52).boxed().collect(Collectors.toList());
for (int i = 51; i >= 0; --i) {
cards.add(new Card(deck.remove(freclNum.divide(factorials[i]).intValue())));
freclNum = freclNum.remainder(factorials[i]);
}
}
......
package main;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
......@@ -7,11 +8,14 @@ public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Input MS game number: ");
System.out.print("Input deal number (prepend \"m\" for MS deals): ");
String input = scan.nextLine();
if (input.isEmpty())
break;
System.out.println(new Deal(Integer.parseInt(input)).toAsciiString());
if (input.charAt(0) == 'm')
System.out.println(new Deal(Integer.parseInt(input.substring(1))).toAsciiString());
else
System.out.println(new Deal(new BigInteger(input)).toAsciiString());
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment