Java - Draw Cards and Eliminate Cards Problem

Posted by Jen on Game Development See other posts from Game Development or by Jen
Published on 2012-10-21T05:09:56Z Indexed on 2012/10/21 5:27 UTC
Read the original article Hit count: 267

Filed under:

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);
}

}

© Game Development or respective owner

Related posts about java