Advertisement
Guest User

index.java

a guest
Nov 18th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. public class index{
  2.     public static void main(String[] args){
  3.         runGame(2);                                     // Runs game by calling runGame method.
  4.     }
  5.                                                         // Method that takes no input and return an array of 52 cards of type Card[] with each card of type Card.
  6.     public static Card[] createCardArray(){
  7.         Card[] deck=new Card[52];                       // Creates a new array of type Card and length 52 named "deck".
  8.         int count =0;                                   //counter used for iterating through the "deck" array and assigning each index to a card.
  9.        
  10.         for(int i=0; i<4;i++){                          // for loop to iterate the suits whose values are {0, 1, 2, 3}
  11.             for(int j=1;j<=13;j++ ){                    // for loop to iterate the values of each card from 1-13.
  12.                 deck[count]= new Card(i,j);             //assigns each element in deck to a new card by calling the constructor with i=suit and j=value.
  13.                 count++;                                //increment count to include every element of array "deck"
  14.             }
  15.         }
  16.        
  17.         return deck;                                    //returns the deck created.
  18.     }
  19.    
  20.                                                        
  21.     public static Card[] shuffle(Card[] cardArray){     //Method that takes an array of type Card as input and returns an array
  22.            
  23.         for(int i=0; i<cardArray.length*52;i++){
  24.             int random1=(int)Math.floor(Math.random()*52);
  25.             int random2 = (int)Math.floor(Math.random()*52);
  26.             Card temp2=cardArray[random1];
  27.             cardArray[random1]=cardArray[random2];
  28.             cardArray[random2]=temp2;
  29.         }
  30.        
  31.         return cardArray;
  32.     }
  33.    
  34.    
  35. /*  public static void printCards(Card[] deck){
  36.         for(int i =0; i<deck.length; i++){
  37.             System.out.println(deck[i].showCardSuit() + deck[i].showCardValue());
  38.         }
  39.         System.out.println();
  40.     }
  41. */
  42.    
  43.    
  44.     public static int[] createPoints(int n){
  45.         int[] points = new int[n+1];                    // Dealer last player
  46.         int count = 0;
  47.        
  48.         for(int i = 0; i<points.length; i++){
  49.             points[i] = count;
  50.         }
  51.         return points;
  52.     }
  53.    
  54.    
  55.    
  56.     public static void updatePoints(int[] nplayers, int[] points, int playern , Card card){
  57.         int index = -1;
  58.         for(int i = 0; i< nplayers.length; i++){
  59.             if (nplayers[i] == playern){
  60.                 index = i;
  61.             }
  62.         }
  63.         int point = card.getValue();
  64.         points[index] = point;
  65.         return;
  66.     }
  67.    
  68.    
  69.     public static int getPoints(int[] nplayers, int[] points, int playern){
  70.         int index = -1;
  71.         for(int i = 0; i< nplayers.length; i++){
  72.             if (nplayers[i] == playern){
  73.                 index = i;
  74.             }
  75.         }
  76.         return points[index];
  77.     }
  78.    
  79.     public static void printPlayersPoints(int[] nplayers, int[] points){
  80.         for (int i=0; i<nplayers.length-1;i++){
  81.             System.out.println("Player " + nplayers[i] + ": Points " + points[i]);
  82.             System.out.println();
  83.         }
  84.     }
  85.    
  86.     public static int[] createPlayers(int n){
  87.         int[] players = new int[n+1];                   // Dealer last player
  88.         int count = 1;
  89.         for(int i = 0; i<players.length; i++){
  90.             players[i] = count;
  91.             count++;
  92.         }
  93.         return players;
  94.     }
  95.    
  96.    
  97.     public static void runGame(int n){
  98.         Card[] newdeck=createCardArray();
  99.        
  100.         Card[] shuffleddeck = shuffle(newdeck);
  101.         Deck deck = new Deck(shuffleddeck);
  102.         int[] players = createPlayers(n);
  103.         int[] points = createPoints(n);
  104.        
  105.    
  106.         for(int i = 0 ; i< players.length - 1; i++){
  107.             Card a = deck.deal();
  108.             Card b = deck.deal();
  109.             updatePoints(players, points, i, a);
  110.             updatePoints(players, points, i, b);
  111.         }
  112.         printPlayersPoints(players, points);
  113.     }
  114.    
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement