简体   繁体   English

您如何洗牌非标准牌?

[英]How do you shuffle a non-standard deck of cards?

I am making a program to play a game of UNO. 我正在编写一个程序来玩UNO游戏。 In the UNO deck, some cards are repeated, and therefore I cannot just make a list of integers; 在UNO牌组中,有些牌是重复的,因此我不能仅列出整数。 I have to use objects. 我必须使用对象。 I plan on using a LinkedList for the deck, but I am aware that shuffles on a LinkedList are horridly slow. 我计划在平台上使用LinkedList,但我知道LinkedList上的随机播放速度非常慢。

My question is, should I.... 我的问题是,我应该...

  1. Avoid a LinkedList entirely and just go with an ArrayList 完全避免使用LinkedList,而只需使用ArrayList
  2. Use an ArrayList or similar, shuffle, then put the contents into the LinkedList 使用ArrayList或类似的东西,随机播放,然后将内容放入LinkedList
  3. Construct an ArrayList, then make my own shuffling routine (aka not using Random) that adds to the LinkedList as we go 构造一个ArrayList,然后制作我自己的改组例程(也就是不使用Random),将其添加到LinkedList中
  4. Shuffle the LinkedList anyway (as in, it's not really that bad) 不管怎样,都随机播放LinkedList(例如,并没有那么糟糕)

This is not for homework; 这不是为了功课; it is to assist in having fun :) 这是为了帮助玩得开心:)

You can represent cards by plain integers. 您可以用普通整数表示卡。 If an integer represents a type of card, and Uno has multiple cards of the same type, just use the integer corresponding to that card more than once. 如果整数代表一张卡的类型,并且Uno有多张相同类型的卡,则请多次使用与该卡对应的整数。

Shuffling and dealing is easy. 洗牌和交易很容易。

To start the game, set up a fixed size, dumb array of type integer (no fancy linked lists or Arraylist need apply) that can hold the entire deck (size = N). 要开始游戏,请设置一个固定大小的,整数类型的哑数组(不需要应用任何奇特的链表或Arraylist),该数组可以容纳整个套牌(大小= N)。 Fill this array with the integers representing the Uno deck including the duplicate integers representing duplicate cards. 用代表Uno牌组的整数(包括代表重复卡的重复整数)填充此数组。 Set UNDEALT to N. 将UNDEALT设置为N。

To shuffle, execute the following code some modest (100?) times: 要随机播放,请在适当的时间(100分钟)内执行以下代码:

 1)  Pick a random number from 1 to UNDEALT, R.
 2)  Exchange the the first array slot with the Rth slot.

To deal: 交易,处理:

 1) Give out the card in the UNDEALT slot.
 2) Decrement UNDEALT.

You can do all this with fancier data structures, too, but there just isn't any point. 您也可以使用更高级的数据结构来完成所有这些操作,但是没有任何意义。 Given that the total information involved is 100 data items, unless you do something outrageously dumb, it'll be faster than people. 鉴于涉及的信息总数为100个数据项,除非您做一些愚蠢的愚蠢的工作,否则它将比人们更快。 But my motto is: if simple works, stick with simple. 但是我的座右铭是:如果简单可行,那就坚持简单。

A few thoughts: 一些想法:

  • There is no reason to stick with a LinkedList for this; 为此,没有理由坚持使用LinkedList。 you can just as easily use an ArrayList to get the first and last cards. 您也可以使用ArrayList轻松获得第一张和最后一张卡片。 In fact, it appears that ArrayList's performance is better for removing single elements . 实际上,看来ArrayList的性能对于删除单个元素更好

  • You may also shuffle an array of elements which have identical integers. 您也可以将具有相同整数的元素数组改组。 As in, there is no reason that you cannot use a shuffling algorithm with an array that looks like this: 与之类似,没有理由不能对这样的数组使用混洗算法

cards[] = {1, 1, 1, 1, 2, 3, 4, 5, 6, 6} , where 1 = "Wild", 2 = "Draw Four", or what have you. cards[] = {1, 1, 1, 1, 2, 3, 4, 5, 6, 6} ,其中1 =“ Wild”,2 =“ Draw Four”或您拥有什么。

In my opinion, using an Array(List) would make it easiest to do so. 我认为,使用Array(List)将使其最容易做到。 The difference here is using the array's values for gameplay, rather than their keys to determine what the card is. 这里的区别是使用数组的作为游戏玩法,而不是使用它们的来确定卡的种类。

You can do the same thing with objects if you'd like; 如果愿意,您可以对对象执行相同的操作; you shuffle the array based on array index, but use the values in the array (objects representing cards) to know what the card actually is. 您可以根据数组索引对数组进行混洗,但是可以使用数组中的值(代表卡的对象)来了解卡的实际含义。

edit: Apparently Java will shuffle things for you! 编辑:显然Java会为您洗牌! http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List) http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)

If you don't shuffle too often, it won't be that slow. 如果您不经常洗牌,那就不会那么慢。

One way to shuffle is to randomly permute the first card with another one. 随机播放的一种方法是用另一张随机地排列第一张卡。 This isn't that slow with a LinkedList. 使用LinkedList并不是那么慢。 Copying it to/from an ArrayList, on the other hand, is going to take some time. 另一方面,将其复制到ArrayList或从ArrayList复制将需要一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM