Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3.  
  4.  
  5.  
  6. public class Drawcards
  7. {
  8. public static void main(String[] args)
  9. {
  10. Card[] deck = buildDeck();
  11. shuffle(deck);
  12.  
  13. Player[] players = new Player[3];
  14.  
  15. players[0] = new Player();
  16. players[0].name = "Niklas";
  17. players[0].drawnCard = draw(deck);
  18.  
  19. players[1] = new Player();
  20. players[1].name = "William";
  21. players[1].drawnCard = draw(deck);
  22.  
  23. players[2] = new Player();
  24. players[2].name = "Maxlol";
  25. players[2].drawnCard = draw(deck);
  26.  
  27. for (int i = 0; i < players.length; i++)
  28. {
  29. System.out.println(players[i].name + " drog kortet: " + players[i].drawnCard.suit + " " + players[i].drawnCard.value);
  30. }
  31. }
  32.  
  33. public static Card[] buildDeck()
  34. {
  35. //Card array, represents deck
  36. Card[] deck = new Card[52];
  37.  
  38. int cardCt = 0;
  39. for (int suit = 1; suit <= 4; suit++)
  40. {
  41. for (int value = 1; value <= 13; value++)
  42. {
  43. deck[cardCt] = new Card();
  44. deck[cardCt].suit = suit;
  45. deck[cardCt].value = value;
  46. cardCt++;
  47. }
  48. }
  49. return deck;
  50. }
  51.  
  52. public static Card[] shuffle(Card[] array)
  53. {
  54. Random rand = new Random(); // Random number generator
  55.  
  56. for (int i=0; i<array.length; i++)
  57. {
  58. int randomPosition = rand.nextInt(array.length);
  59. Card temp = array[i];
  60. array[i] = array[randomPosition];
  61. array[randomPosition] = temp;
  62. }
  63.  
  64. return array;
  65. }
  66.  
  67. public static Card draw(Card[] deck)
  68. {
  69. Card drawnCard;
  70. Random rand = new Random();
  71. int number = rand.nextInt(52);
  72.  
  73. drawnCard = deck[number];
  74. return drawnCard;
  75.  
  76. }
  77.  
  78. static class Player
  79. {
  80. public String name;
  81. public Card drawnCard;
  82. }
  83.  
  84. static class Card
  85. {
  86. public int suit;
  87. public int value;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement