0

I'm creating a simple card game where cards are created using random number. I want to use the random number as a score int. Is there a way to add the random number used to generate specif card? For example random number for level:5. Totalscore = totalscore + RandomNumber.

Card Class

public class clsCard {
    //Main variables of my program
    private int value;
    private int level, object;
    private final int MaxFaceValue= 10;
    private static String[] objects =  {"Joker","hearts","spades","diamond","clubs"};
    private static String[] levels = {"Joker","Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};

    public clsCard()  {
        this.level= (int)(Math.random()*levels.length);
        this.object= (int)(Math.random()*objects.length);
    }

    public @Override String toString() { 
        if(levels[level].equals("Joker") || objects[object].equals("Joker"))
            return "Joker";
        else
            return levels[level] + " of " + objects[object];
    }

    public int getLevel() {
        return level;
    }

    public int getObject() {
        return object;
    }
}

Main program

clsCard cardFace = new clsCard();           
System.out.println(cardFace);
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 5
    `javascript` != `java` – Turing85 Jan 16 '18 at 12:00
  • 1
    "*How to get a random number from compiler*" - The compiler gives you bytecode, provided you pass it some valid java code. Nothing else. – Turing85 Jan 16 '18 at 12:01
  • 1
    If I understand what you're asking - you could try using a Map and mapping an Integer from 1-12 with a card. Then use your randomly generated number from 1-12 to select your card. Here are the maps doc to have a look at. https://docs.oracle.com/javase/7/docs/api/java/util/Map.html – Sam Jan 16 '18 at 12:02
  • @bartlomiej-kaczmarek this is the tip we generally give our students mate: 1- understand the problem before start coding 2- break down the problem into subproblem 3- solve each subproblem separately. 4- plug each subproblem together to create a working system. – nafas Jan 16 '18 at 12:08

0 Answers0