java - 在列表中创建互斥值

标签 java arrays list mutual-exclusion

我想知道是否可以以任何方式改变它,以便我可以获得输出:

P1 Hand: 10 of diamonds, 9 of clubs, 5 of spades, Queen of spades, 4 of clubs.
P2 Hand: 7 of diamonds, 10 of spades, Jack of clubs, 6 of diamonds, 5 of clubs.
P3 Hand: 8 of spades, 8 of diamonds, 4 of spades, 3 of spades, 10 of hearts.
P4 Hand: Ace of spades, 8 of clubs, 6 of spades, Jack of spades, 3 of hearts.

永远不要在两个不同的玩家身上使用同一张牌。我的代码在这里:

import java.util.Random;
import java.util.Arrays;
import java.util.ArrayList;

public class cardGame {
  public static void main(String[] args) {
    Random sad = new Random();
    ArrayList<String> all = new ArrayList<>(Arrays.asList("Ace of spades","2 of spades","3 of spades","4 of spades","5 of spades","6 of spades","7 of spades","8 of spades","9 of spades","10 of spades","Jack of spades","Queen of spades","King of spades","Ace of spades","2 of spades","3 of spades","4 of spades","5 of spades","6 of spades","7 of spades","8 of spades","9 of spades","10 of spades","Jack of spades","Queen of spades","King of spades","Ace of hearts","2 of hearts","3 of hearts","4 of hearts","5 of hearts","6 of hearts","7 of hearts","8 of hearts","9 of hearts","10 of hearts","Jack of hearts","Queen of hearts","King of hearts","Ace of diamonds","2 of diamonds","3 of diamonds","4 of diamonds","5 of diamonds","6 of diamonds","7 of diamonds","8 of diamonds","9 of diamonds","10 of diamonds","Jack of diamonds","Queen of diamonds","King of diamonds","Ace of clubs","2 of clubs","3 of clubs","4 of clubs","5 of clubs","6 of clubs","7 of clubs","8 of clubs","9 of clubs","10 of clubs","Jack of clubs","Queen of clubs","King of clubs"));
    ArrayList<String> trump = new ArrayList<>(Arrays.asList("spades","hearts","diamonds","clubs"));
    System.out.println("Boure hands and trump game.");
    System.out.println("");
    System.out.println("Trump: " + trump.remove(sad.nextInt(trump.size())) + ".");
    System.out.println("");
    System.out.println("P1 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
    System.out.println("");
    System.out.println("P2 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
    System.out.println("");
    System.out.println("P3 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
    System.out.println("");
    System.out.println("P4 Hand: " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ", " + all.remove(sad.nextInt(all.size())) + ".");
  }
}

顺便说一句重复的内容在某种程度上没有帮助:它不包含数组,而这正是我正在处理的。话虽如此,我无法查看问题并找出可以更改的地方。

最佳答案

从一副牌组开始,然后将牌从牌组中移除并添加到每个玩家中。 PlayerCollection,它模拟了Player 的手牌。

这样玩家只能从牌组中获取牌,并且您可以使用每张适当的牌中的一张来初始化牌组。

关于java - 在列表中创建互斥值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539260/

相关文章:

java - 从 java 集合中删除重复项

java - 在 Tsurgeon 中将节点插入树时如何命名节点

Python:将 2D 数组与 1 个具有不同值的公共(public)列组合

java - 无法更新从数据库获取的 JtextArea 文本同时手动插入(运行时)

php - WordPress - 带有类别列表的主题选项页面

python - 使用 python map 将元组中的类型 func 应用于字符串列表

java - 列出 Java 中的常见元素

python - 解析、拼接和构造嵌套字符串以在 Python 中列出

javascript - 如何用约束布局交换两个按钮的位置?

java - 如何修复 “Incorrect string value for the column at the row 1”错误?