我正在构建一个Java应用程序,用于在“Out”faro shuffle中跟踪所选择的扑克牌。我很长一段时间都是一名魔术师,这似乎是一件有趣的事情,虽然我看过每次洗牌后都会向你展示一个法鲁的订单的应用程序/网站,但他们没有专门跟踪选择的卡片。
我对每次洗牌都有一种蛮力方法。我只完成了两次shuffle,但其余的shuffle基本上都是复制/粘贴,这与DRY相反,但是每次shuffle都有一个特定的顺序,所以我不确定如何确保每次顺序保持不变迭代。也许将每个迭代存储在自己的列表中?
哦,当法克拉完美编织时,一个法鲁,但顶部和底部卡片保持不变。所以完美的26/26分裂然后是完美的编织。
如果有人有任何更好的想法,请告诉我。代码还不是很漂亮,我有重构要做。
package Tracker;
import java.util.ArrayList;
import java.util.List;
public class NewDeckOrder {
private ArrayList<String> bicycleDeckOrder;
public NewDeckOrder() {
String[] suitesFirstHalf = {"Hearts", "Clubs"};
String[] valuesFirstHalf = {"Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King"};
String[] suitesSecondHalf = {"Diamonds", "Spades"};
String[] valuesSecondHalf = {"King", "Queen", "Jack", "10", "9",
"8", "7", "6", "5", "4", "3", "2", "Ace"};
bicycleDeckOrder = new ArrayList<>();
for (String deckFirstSuites : suitesFirstHalf) {
for (String deckFirstValues : valuesFirstHalf) {
bicycleDeckOrder.add(deckFirstValues + " of " +
deckFirstSuites);
}
}
for (String deckSecondSuites : suitesSecondHalf) {
for (String deckSecondValues : valuesSecondHalf) {
bicycleDeckOrder.add(deckSecondValues + " of " +
deckSecondSuites);
}
}
}
public ArrayList<String> getList() {
return bicycleDeckOrder;
}
@Override
public String toString() {
return "New Deck Order " + bicycleDeckOrder;
}
}
package Tracker;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class OutFaro {
private ArrayList<String> oneShuffle;
private ArrayList<String> twoShuffles;
private ArrayList<String> threeShuffles;
public ArrayList<String> getOneShuffle() {
return oneShuffle;
}
public void setOneShuffle(ArrayList<String> oneShuffle) {
this.oneShuffle = oneShuffle;
}
public ArrayList<String> firstShuffle(ArrayList<String> newDeckOrder)
{
ArrayList<String> first26 = new ArrayList<>();
ArrayList<String> last26 = new ArrayList<>();
for(int i = 0; i < newDeckOrder.size() - 26; i++) {
first26.add(newDeckOrder.get(i));
}
for(int j = 26; j < newDeckOrder.size(); j++) {
last26.add(newDeckOrder.get(j));
}
// One Shuffle
oneShuffle = new ArrayList<>();
for (int i = 0; i < first26.size(); i++) {
for (int j = 0; j < last26.size(); j++) {
oneShuffle.add(first26.get(i));
oneShuffle.add(last26.get(j));
i++;
}
}
return oneShuffle;
}
public ArrayList<String> getTwoShuffles() {
return twoShuffles;
}
public void setTwoShuffles(ArrayList<String> twoShuffles) {
this.twoShuffles = twoShuffles;
}
public ArrayList<String> secondShuffle(ArrayList<String>
firstShuffleResults) {
ArrayList<String> first26SecondShuffle = new ArrayList<>();
ArrayList<String> last26SecondShuffle = new ArrayList<>();
for(int i = 0; i < firstShuffleResults.size() - 26; i++) {
first26SecondShuffle.add(firstShuffleResults.get(i));
}
for(int j = 26; j < firstShuffleResults.size(); j++) {
last26SecondShuffle.add(firstShuffleResults.get(j));
}
// Second Shuffle
twoShuffles = new ArrayList<>();
for (int i = 0; i < first26SecondShuffle.size() - 1; i++) {
for (int j = 0; j < last26SecondShuffle.size() - 1; j++) {
twoShuffles.add(first26SecondShuffle.get(i));
twoShuffles.add(last26SecondShuffle.get(j));
i++;
}
}
return twoShuffles;
}
}
import Tracker.NewDeckOrder;
import Tracker.OutFaro;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
NewDeckOrder deckOrder;
// Bicycle new deck order
System.out.println("Current deck order:");
NewDeckOrder newDeckOrder = new NewDeckOrder();
System.out.println(newDeckOrder.toString());
// One out faro
System.out.println("\nPlease enter the card you wish to track: ");
Scanner input = new Scanner(System.in);
String cardToTrack = input.nextLine();
deckOrder = new NewDeckOrder();
int cardsPosition = 0;
ArrayList<String> temp = deckOrder.getList();
if (temp.contains(cardToTrack)) {
cardsPosition += temp.indexOf(cardToTrack) + 1;
}
System.out.println("\n" + cardToTrack + " starting position is " + cardsPosition + ".");
System.out.println("\n" + "Performing one out faro.");
OutFaro outFaro = new OutFaro();
String[] firstHalfSuites = new String[]{"Hearts", "Clubs"};
String[] firstHalfValues = new String[]{"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] secondHalfSuites = new String[]{"Diamonds", "Spades"};
String[] secondHalfValues = new String[]{"King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2", "Ace"};
outFaro.setOneShuffle(outFaro.firstShuffle(newDeckOrder.getList()));
// System.out.println("\n" + "First shuffle results: " + outFaro.getOneShuffle());
outFaro.setTwoShuffles(outFaro.secondShuffle(outFaro.getOneShuffle()));
int cardAfterOneShuffle = 0;
if (outFaro.getOneShuffle().contains(cardToTrack)) {
cardAfterOneShuffle += outFaro.firstShuffle(newDeckOrder.getList()).indexOf(cardToTrack) + 1;
}
System.out.println("\n" + "After one shuffle, your card is at number " + cardAfterOneShuffle);
int cardAfterTwoShuffles = 0;
// System.out.println("\n" + "Second shuffle results: " + outFaro.getTwoShuffles());
if (outFaro.getTwoShuffles().contains(cardToTrack)) {
cardAfterTwoShuffles += outFaro.getTwoShuffles().indexOf(cardToTrack) + 1;
}
System.out.println("\n" + "After two shuffles, your card is at number " + cardAfterTwoShuffles);
}
}
答案 0 :(得分:1)
好吧,好像每次我在stackoverflow上发帖时,我最终都会自己找到解决方案。也许它只是输入并通过它?无论如何,我发现了一种复杂得多的复杂方法。我实际上为自己感到骄傲,因为我自己解决了这个问题,所以我猜我的Java /编程知识处于一个不错的位置。
我认为代码不是100%完美,但远比我以前的“代码呕吐”解决方案好得多。感谢Meepo指导我。
具有混洗逻辑的OutFaro类:
package Tracker;
import java.util.ArrayList;
public class OutFaro {
private ArrayList<String> shuffleResults;
private int shuffleCounter;
public ArrayList<String> getOneShuffle() {
return shuffleResults;
}
public ArrayList<String> outFaro(ArrayList<String> newDeckOrder, int numberOfFaros) {
shuffleResults = new ArrayList<>();
ArrayList<String> temp = new ArrayList<>();
while (shuffleCounter != numberOfFaros) {
for (int i = 0; i < newDeckOrder.size() - 26; i++) {
for (int j = 26; j < newDeckOrder.size(); j++) {
temp.add(newDeckOrder.get(i));
temp.add(newDeckOrder.get(j));
i++;
}
}
shuffleCounter++;
if (shuffleCounter == numberOfFaros) {
shuffleResults = temp;
} else {
outFaro(temp, numberOfFaros);
}
}
return shuffleResults;
}
@Override
public String toString() {
return "OutFaro{" +
"oneShuffle=" + shuffleResults +
'}';
}
}
构建新套牌的New Deck Order类:
package Tracker;
import java.util.ArrayList;
public class NewDeckOrder {
private ArrayList<String> bicycleDeckOrder;
public ArrayList<String> americanNewDeckOrder() {
String[] suitesFirstHalf = {"Hearts", "Clubs"};
String[] valuesFirstHalf = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] suitesSecondHalf = {"Diamonds", "Spades"};
String[] valuesSecondHalf = {"King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2", "Ace"};
bicycleDeckOrder = new ArrayList<>();
for (String deckFirstSuites : suitesFirstHalf) {
for (String deckFirstValues : valuesFirstHalf) {
bicycleDeckOrder.add(deckFirstValues + " of " + deckFirstSuites);
}
}
for (String deckSecondSuites : suitesSecondHalf) {
for (String deckSecondValues : valuesSecondHalf) {
bicycleDeckOrder.add(deckSecondValues + " of " + deckSecondSuites);
}
}
return bicycleDeckOrder;
}
public ArrayList<String> getBicycleDeckOrder() {
return bicycleDeckOrder;
}
public void setBicycleDeckOrder(ArrayList<String> bicycleDeckOrder) {
this.bicycleDeckOrder = bicycleDeckOrder;
}
@Override
public String toString() {
return "New Deck Order " + bicycleDeckOrder;
}
}
客户代码:
import Tracker.NewDeckOrder;
import Tracker.OutFaro;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int cardAfterOneShuffle = 0;
Scanner input = new Scanner(System.in);
// Bicycle new deck order
System.out.println("Current deck order:");
NewDeckOrder newDeckOrder = new NewDeckOrder();
newDeckOrder.setBicycleDeckOrder(newDeckOrder.americanNewDeckOrder());
System.out.println(newDeckOrder.getBicycleDeckOrder().toString());
// User input
System.out.println("\nPlease enter the card you wish to track: ");
String cardToTrack = input.nextLine();
System.out.println("\nHow many out Faro's would you like to shuffle?");
int numberOfFaros = input.nextInt();
int cardsPosition = 0;
if (newDeckOrder.americanNewDeckOrder().contains(cardToTrack)) {
cardsPosition += newDeckOrder.americanNewDeckOrder().indexOf(cardToTrack) + 1;
}
System.out.println("\n" + cardToTrack + " starting position is " + cardsPosition + ".");
OutFaro outFaro = new OutFaro();
outFaro.outFaro(newDeckOrder.getBicycleDeckOrder(), numberOfFaros);
System.out.println("\n" + "After " + numberOfFaros + " shuffles the deck order is: " + outFaro.getOneShuffle());
if (outFaro.getOneShuffle().contains(cardToTrack)) {
cardAfterOneShuffle += outFaro.getOneShuffle().indexOf(cardToTrack) + 1;
}
System.out.println("\n" + "After " + numberOfFaros + " shuffles your card is at number " + cardAfterOneShuffle);
}
}