简体   繁体   English

在列表中创建互斥值

[英]Creating mutually exclusive values in lists

I was wondering if I could change this in any way so that I could have the output: 我想知道是否可以以任何方式更改它,以便获得输出:

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.

NEVER have the same card in two different players. 在两个不同的玩家中永远不会拥有相同的卡。 My code is here: 我的代码在这里:

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())) + ".");
  }
}

BTW the duplicate didn't help in one way: It didn't include arrays, which is what I'm dealing with. 顺便说一句 ,重复项并不能以一种方式提供帮助:它不包含数组,这正是我要处理的。 With that stated, I couldn't look at the question and figure out where I can change. 说了这么多,我看不到这个问题,也找不到可以改变的地方。

Start off with a Deck of cards, and then assign the cards to each Player by removing them from the Deck and adding them to the Player 's Collection which models the Player 's Hand. 与刚开始时Deck的卡,然后分配卡每个Player从删除它们Deck ,并将它们添加到PlayerCollection该款机型Player的手。

That way the player can only get the cards from the deck, and you can initialize the deck with one of each appropriate card. 这样,玩家只能从卡组中获取卡,并且您可以使用每张相应卡中的一张来初始化卡组。

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

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