Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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() {
        playercounter=random.nextInt(5);
        playercardB= cards[playercounter];
            if (playercardB==playercardA || playercardB == enemycardA || playercardB == enemycardB) {
                return;
            }
    }

    public static void enemycardAdraw () {
        enemycounter = random.nextInt(5);
        enemycardA=cards[enemycounter];
            if (enemycardA == playercardA || enemycardA == playercardB) {
                return;
            }


    }

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

    public static void main (String args []) {


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

}
share|improve this question
1  
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! –  Jonathan Hobbs Oct 21 '12 at 7:06
    
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/… –  Markus von Broady Oct 21 '12 at 7:48
    
i'll try to change my code and see what I can do with this. thank you so much for helping :) –  Jen Oct 21 '12 at 13:36

1 Answer 1

up vote 2 down vote accepted

make a deck class like:

import java.util.*;
class Deck {
    Deck() {
        Collections.shuffle(c);
    }
    String draw() {
        String s=c.get(0);
        c.remove(s);
        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()+' ');
        System.out.println();
    }
    public static void main(String[] args) {
        run();
        run();
    }
}
share|improve this answer
    
it does draw all the cards :) how can i eliminate the cards I have drawn? –  Jen Oct 21 '12 at 6:28
    
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. –  Ray Tayek Oct 21 '12 at 6:30
    
I see, thank you so much. :)) –  Jen Oct 21 '12 at 6:43
    
Don't forget to mark your question's answer. –  Seth Battin Oct 21 '12 at 7:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.