Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Drawcards
- {
- public static void main(String[] args)
- {
- Card[] deck = buildDeck();
- shuffle(deck);
- Player[] players = new Player[3];
- players[0] = new Player();
- players[0].name = "Niklas";
- players[0].drawnCard = draw(deck);
- players[1] = new Player();
- players[1].name = "William";
- players[1].drawnCard = draw(deck);
- players[2] = new Player();
- players[2].name = "Maxlol";
- players[2].drawnCard = draw(deck);
- for (int i = 0; i < players.length; i++)
- {
- System.out.println(players[i].name + " drog kortet: " + players[i].drawnCard.suit + " " + players[i].drawnCard.value);
- }
- }
- public static Card[] buildDeck()
- {
- //Card array, represents deck
- Card[] deck = new Card[52];
- int cardCt = 0;
- for (int suit = 1; suit <= 4; suit++)
- {
- for (int value = 1; value <= 13; value++)
- {
- deck[cardCt] = new Card();
- deck[cardCt].suit = suit;
- deck[cardCt].value = value;
- cardCt++;
- }
- }
- return deck;
- }
- public static Card[] shuffle(Card[] array)
- {
- Random rand = new Random(); // Random number generator
- for (int i=0; i<array.length; i++)
- {
- int randomPosition = rand.nextInt(array.length);
- Card temp = array[i];
- array[i] = array[randomPosition];
- array[randomPosition] = temp;
- }
- return array;
- }
- public static Card draw(Card[] deck)
- {
- Card drawnCard;
- Random rand = new Random();
- int number = rand.nextInt(52);
- drawnCard = deck[number];
- return drawnCard;
- }
- static class Player
- {
- public String name;
- public Card drawnCard;
- }
- static class Card
- {
- public int suit;
- public int value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement