【问题标题】:Creating mutually exclusive values in lists在列表中创建互斥值
【发布时间】:2017-08-07 04:04:32
【问题描述】:

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

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

顺便说一句,副本在某一方面没有帮助:它不包括数组,这正是我正在处理的。话虽如此,我无法查看问题并找出可以更改的地方。

【问题讨论】:

标签: java arrays list mutual-exclusion


【解决方案1】:

从卡的Deck 开始,然后将卡从Deck 中删除并将它们添加到PlayerCollection 中,将它们分配给每个Player,该Collection 模拟Player的手。

这样玩家只能从牌堆中取出牌,而你可以用每张合适的牌来初始化牌堆。

【讨论】:

  • 谢谢,我正在尝试。
  • 我只需要几个指针,1.使用数组什么的,2.如何删除和添加,3.每个玩家得到一个String[] p1 = {}或者什么
  • @BlazeDaBlur 我会使用集合而不是数组。最好是一套,因为手牌的顺序并不重要,只有在它们出现时才重要。
猜你喜欢
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2020-10-02
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
相关资源
最近更新 更多