I am having a problem in this question. I want a system inside a game wherein the player draws 2 cards randomly, and the enemy draws 2 cards randomly. Then, what the program does is to print out to the console the cards the player draw and the enemy's. The cards should not conflict and must not be the same. Then lastly, the program prints out the card that was not drawn by both the player and the enemy. Here's how I did it but it was lengthy and full of errors:

import java.util.Random;
public class Draw {

    public static Random random = new Random();
    public static String cards[] = {"Hall", "Kitchen", "Billiard", "Study", "Pool"};
    public static int playercounter;
    public static int enemycounter;
    public static String playercardA = null;
    public static String playercardB = null;
    public static String enemycardA = null;
    public static String enemycardB = null;
    public String lastcard = null;

    public static void playercardAdraw() {
        playercounter = random.nextInt(5);
        playercardA = cards[playercounter];

    public static void playercardBdraw() {
        playercardB= cards[playercounter];
            if (playercardB==playercardA || playercardB == enemycardA || playercardB == enemycardB) {

    public static void enemycardAdraw () {
        enemycounter = random.nextInt(5);
            if (enemycardA == playercardA || enemycardA == playercardB) {


    public static void enemycardBdraw () {
        enemycounter = random.nextInt(5);
            if (enemycardB == playercardA || enemycardB == playercardB || enemycardB == enemycardA) {

    public static void main (String args []) {

        System.out.println("Starting to draw...");
        System.out.println("Player's Turn: ");
        System.out.println("Player's first card: " + playercardA);
        System.out.println("Player's second card: " + playercardB);
        System.out.println("Enemy's Turn: ");
        System.out.println("Enemy's first card: " + enemycardA);
        System.out.println("Enemy's Second card: " + enemycardB);

  • 1
    \$\begingroup\$ Hi Jen, and welcome to GDSE! I've edited the "Java" tag out of your title - your question is already tagged with a Java tag! \$\endgroup\$ Commented Oct 21, 2012 at 7:06
  • \$\begingroup\$ To prevent drawing one card more than once, either shuffle the deck array and iterate through it, or set deckSize = deckArray.length;, get a random card cardIndex = Math.floor(Math.random()*deckSize)' and swap it with the last card in deck and deckSize--;. This is a duplicate: gamedev.stackexchange.com/questions/26551/… \$\endgroup\$ Commented Oct 21, 2012 at 7:48
  • \$\begingroup\$ i'll try to change my code and see what I can do with this. thank you so much for helping :) \$\endgroup\$
    – Jen
    Commented Oct 21, 2012 at 13:36

1 Answer 1


make a deck class like:

import java.util.*;
class Deck {
    Deck() {
    String draw() {
        String s=c.get(0);
        return s;
    final List<String> c=new ArrayList<String>(Arrays.asList(cards));
    static String cards[]={"Hall","Kitchen","Billiard","Study","Pool"};
public class Gd40356 {
    static void run() {
        Deck deck=new Deck();
        System.out.print("drawing: ");
        for(int i=0;i<deck.cards.length;i++)
            System.out.print(deck.draw()+' ');
    public static void main(String[] args) {
  • \$\begingroup\$ it does draw all the cards :) how can i eliminate the cards I have drawn? \$\endgroup\$
    – Jen
    Commented Oct 21, 2012 at 6:28
  • \$\begingroup\$ they are eliminated from the deck as they are drawn by this line: c.remove(s); in the method draw(), so you can not draw a duplicate. \$\endgroup\$
    – Ray Tayek
    Commented Oct 21, 2012 at 6:30
  • \$\begingroup\$ Don't forget to mark your question's answer. \$\endgroup\$ Commented Oct 21, 2012 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .