Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • Zezuma09: how do i get this game started ?
      October 22, 2017, 03:24:27 AM
    • Striker Fox2: Much more on General Board
      October 21, 2017, 04:30:25 AM
    • Striker Fox2: New price guide link [link]
      October 21, 2017, 04:30:18 AM
    • Striker Fox2: New discord link [link]
      October 21, 2017, 04:30:03 AM
    • Saltyspade10: If you are new and reading this, stick around becaus it's a great server and community! Hope to see you in-game :)
      October 21, 2017, 02:44:16 AM
    • damian6: hello i wanne buy this server can u add me
      October 20, 2017, 03:54:38 PM
    • Wilkooo: lol jks
      October 20, 2017, 02:39:53 PM
    • Wilkooo: ilyall
      October 20, 2017, 02:39:48 PM
    • tm4dz: why cant i see any download links??
      October 20, 2017, 10:46:55 AM
    • stCky: u dont need to its fudgeing sick
      October 19, 2017, 08:13:13 PM
    • odin stormborn: how do i change my username
      October 19, 2017, 12:16:51 PM
    • qazplm102938: everyone play manicps its best server out there loads to do its badboyyyy - manicps manicps shout out kiteshield
      October 19, 2017, 10:28:21 AM
    • qazplm102938: everyone play manicps its best server out there loads to do its badboyyyy - manicps manicps
      October 19, 2017, 10:11:08 AM
    • Travas: wtf
      October 19, 2017, 07:37:52 AM
    • stCky: Adaro, you are a threat to the communities image, yes
      October 18, 2017, 02:53:43 PM
    • Krackkid: Hello everyone it is I, Krackkid.
      October 18, 2017, 07:21:56 AM
    • soma_: Does anyone have an updated link to a blank Source / Client? 317/508
      October 18, 2017, 04:57:44 AM
    • michaeltrailer: olm t pst
      October 18, 2017, 04:52:48 AM
    • Adaro: Strange that staff think I'm a threat when I'm not threatening anyone
      October 18, 2017, 03:07:16 AM
    • Adaro: My Skype is: Mod Adaro
      October 18, 2017, 02:47:02 AM

    Author Topic: Deck: Making, Shuffling, Drawing - JavaScript as well  (Read 1003 times)

    0 Members and 1 Guest are viewing this topic.

    Offlinewildskiller

    • Member
    • ****
    • Posts: 1,434
    • Thanks: +0/-0
      • View Profile
    Deck: Making, Shuffling, Drawing - JavaScript as well
    « on: June 14, 2014, 01:12:03 PM »
    I just write some stuff up on my free time at ideone.com. You could use this for a card game or something of the sorts. I know it's not the best way, but it's viable.

    Code: Java
    1. importjava.util.*;
    2. importjava.lang.*;
    3. importjava.io.*;
    4.  
    5. class Ideone {
    6.        
    7.         publicstaticvoid main(String[] args)throws java.lang.Exception{
    8.                 load();
    9.         }
    10.        
    11.         publicstaticvoid load(){
    12.                 List<Card> cards =new ArrayList<Card>(52);
    13.                 String[] non_numerical ={"Ace", "Jack", "King", "Queen"};
    14.                 for(Card.Shape shape : Card.Shape.values()){
    15.                         for(int i =1; i <14; i++){
    16.                                 String value ="";
    17.                                 if(i ==1|| i >10)
    18.                                         value = non_numerical[i ==1?0: i -10];
    19.                                 else{ value =""+ i;}
    20.                                 String name = value +" of "+ cap(shape.name());
    21.                                 cards.add(new Card(name, i, shape));
    22.                         }
    23.                 }
    24.                 Collections.shuffle(cards);
    25.                 for(int i =0; i <10; i++){
    26.                         if(i ==0)
    27.                                 System.out.print("You pulled out a ");
    28.                         if(i ==5)
    29.                                 System.out.print("The computer pulled out a ");
    30.                                
    31.                         if(i <5){
    32.                                 if(i <4){System.out.print(cards.get(i).getName()+", ");}
    33.                                 else{System.out.println(cards.get(i).getName());}
    34.                         }else{
    35.                                 if(i <9){System.out.print(cards.get(i).getName()+", ");}
    36.                                 else{System.out.println(cards.get(i).getName());}
    37.                         }
    38.                 }
    39.         }
    40.        
    41.         publicstaticString cap(String s){
    42.                 return s.substring(0, 1).toUpperCase()+ s.substring(1).toLowerCase();
    43.         }
    44.        
    45.         publicstaticclass Card {
    46.                
    47.                 publicenumShape{ DIAMONDS, CLUBS, HEARTS, SPADES }
    48.                
    49.                 privateString name;
    50.                 privatefinalint value;
    51.                 privatefinalShape shape;
    52.                
    53.                 public Card(String name, int value, Shape shape){
    54.                         this.name= name;
    55.                         this.value= value;
    56.                         this.shape= shape;
    57.                 }
    58.                
    59.                 public Card(int value, Shape shape){
    60.                         this.value= value;
    61.                         this.shape= shape;
    62.                 }
    63.                
    64.                 publicString getName(){
    65.                         return name;
    66.                 }
    67.                
    68.                 publicvoid setName(String name){
    69.                         this.name= name;
    70.                 }
    71.                
    72.                 publicint getValue(){
    73.                         return value;
    74.                 }
    75.                
    76.                 publicShape getShape(){
    77.                         return shape;
    78.                 }
    79.         }
    80.        
    81. }
    82.  

    Forgot to add the output:
    Code: [Select]
    You pulled out a 3 of Spades, King of Clubs, 7 of Clubs, Jack of Diamonds, 3 of Diamonds
    The computer pulled out a 2 of Hearts, 8 of Spades, 6 of Hearts, 7 of Spades, 10 of Clubs

    Code: Javascript
    1. var non_numerical =["Ace","Jack","Queen","King"];
    2. var categories =["Diamonds","Clubs","Spades","Hearts"];
    3. var cardList =[];
    4.  
    5. var Card =function(name, value, category){
    6.     this.name= name;
    7.     this.value= value;
    8.     this.category= category;
    9. };
    10.  
    11. var cap =function(text){
    12.     return text.substring(0,1).toUpperCase()+ text.substring(1).toLowerCase();
    13. };
    14.  
    15. var load =function(){
    16.     var value =1;
    17.     var category =0;
    18.     for(var i =0; i <52; i++){
    19.         var nameValue ="";
    20.         if(value ===14){
    21.             value =1;
    22.             category++;
    23.         }
    24.         if(value ===1|| value >10){
    25.             nameValue = non_numerical[value ===1?0: value -10];
    26.         }else{
    27.             nameValue = value;
    28.         }
    29.         var name = nameValue +" of "+ categories[category];
    30.         cardList[i]=new Card(name, value, categories[category]);
    31.         value++;
    32.     }
    33. };
    34.  
    35. var shuffle =function(){
    36.     for(var i = cardList.length-1; i >0; i--){
    37.         var j =Math.floor(Math.random()*(i +1));
    38.         var temp = cardList[i];
    39.         cardList[i]= cardList[j];
    40.         cardList[j]= temp;
    41.     }
    42. };
    43.  
    « Last Edit: June 19, 2014, 10:18:08 PM by wildskiller »


    OfflineRSCShadow

    • Member
    • ****
    • Posts: 2,024
    • Thanks: +0/-0
      • View Profile
    Re: Deck: Making, Shuffling, Drawing - JavaScript as well
    « Reply #1 on: June 19, 2014, 09:23:48 PM »
    In your JS solution, you're using:
    Code: Javascript
    1.     cardList.sort(function(){
    2.         return0.5-Math.random();
    3.     });
    to shuffle the array. This solution doesn't provide appropriate random distribution. Instead, I recommend using the Fisher-Yates algorithm. In JS you can find it at https://www.npmjs.com/package/knuth-shuffle or

    Feel free to write it yourself if you're learning, but generally in JS it's recommended to use and write small modules that accomplish one thing and are highly re-usable. Even things like your capitalize function are best done in modules (https://www.npmjs.com/package/capitalize).
    « Last Edit: June 19, 2014, 09:26:19 PM by RSCShadow »


    Offlinewildskiller

    • Member
    • ****
    • Posts: 1,434
    • Thanks: +0/-0
      • View Profile
    Re: Deck: Making, Shuffling, Drawing - JavaScript as well
    « Reply #2 on: June 19, 2014, 10:07:56 PM »
    In your JS solution, you're using:
    Code: Javascript
    1.     cardList.sort(function(){
    2.         return0.5-Math.random();
    3.     });
    to shuffle the array. This solution doesn't provide appropriate random distribution. Instead, I recommend using the Fisher-Yates algorithm. In JS you can find it at https://www.npmjs.com/package/knuth-shuffle or

    Feel free to write it yourself if you're learning, but generally in JS it's recommended to use and write small modules that accomplish one thing and are highly re-usable. Even things like your capitalize function are best done in modules (https://www.npmjs.com/package/capitalize).
    I had used the Fisher-Yates algorithm at first, but I love to use as less lines as I can. I will replace my current function with the algorithm though.

    But of course efficiency is always better
    « Last Edit: June 19, 2014, 10:19:03 PM by wildskiller »

    OfflineRSCShadow

    • Member
    • ****
    • Posts: 2,024
    • Thanks: +0/-0
      • View Profile
    Re: Deck: Making, Shuffling, Drawing - JavaScript as well
    « Reply #3 on: June 19, 2014, 11:16:20 PM »
    In your JS solution, you're using:
    Code: Javascript
    1.     cardList.sort(function(){
    2.         return0.5-Math.random();
    3.     });
    to shuffle the array. This solution doesn't provide appropriate random distribution. Instead, I recommend using the Fisher-Yates algorithm. In JS you can find it at https://www.npmjs.com/package/knuth-shuffle or

    Feel free to write it yourself if you're learning, but generally in JS it's recommended to use and write small modules that accomplish one thing and are highly re-usable. Even things like your capitalize function are best done in modules (https://www.npmjs.com/package/capitalize).
    I had used the Fisher-Yates algorithm at first, but I love to use as less lines as I can. I will replace my current function with the algorithm though.

    But of course efficiency is always better
    Well the sort solution might be negligibly more efficient, but it isn't doing what you're expecting in this case.

    Offlinewildskiller

    • Member
    • ****
    • Posts: 1,434
    • Thanks: +0/-0
      • View Profile
    Re: Deck: Making, Shuffling, Drawing - JavaScript as well
    « Reply #4 on: June 20, 2014, 12:03:34 AM »
    Well the sort solution might be negligibly more efficient, but it isn't doing what you're expecting in this case.
    I meant the Fisher-Yates algorithm was more efficient for this subject.

    Offlinematzie

    • Member
    • ****
    • Posts: 1,006
    • Thanks: +0/-0
      • View Profile
    Re: Deck: Making, Shuffling, Drawing - JavaScript as well
    « Reply #5 on: July 22, 2014, 11:31:21 AM »
    Here is the shuffle for my deck lol. Not so efficient, but realistic.

    Code: Java
    1. /**
    2.      * This method shuffles cards by:
    3.      1. Splitting deck into two halves.(With variable)
    4.      2. Randomly picks which hand lays first
    5.      3. Lays cards down, similar to splitting deck between two hands, and folding them together(bending the corners).
    6.      */
    7.     publicvoid shuffleSplit(){
    8.         int halfdeck = cards.size()/2;
    9.         //max of 5 cards off in either direction from middle, not that hard in reality
    10.         int center =(int)(Math.random()*11)+ halfdeck -5;
    11.  
    12.         //List that will store the new stack
    13.         LinkedList<Card> cardsShuffled =newLinkedList();
    14.         //0 or 1, hand1 or hand2, true or false
    15.         boolean hand =(int)(Math.random()*2)==0;
    16.         while(!(cards.isEmpty())){
    17.             if(hand){
    18.                 cardsShuffled.add(cards.pop());
    19.                 center--;
    20.             }else{
    21.                 cardsShuffled.add(cards.remove(center));
    22.             }
    23.             //50% sudo-chance it will change, only if a stack is not empty
    24.             if(!(center >= cards.size()|| center ==0)){
    25.                 if((int)(Math.random()*2)==0){
    26.                     hand =!hand;
    27.                 }
    28.             }else{//if a hand(side) is empty it says what to do :)
    29.                 if(center ==0){
    30.                     hand =false;
    31.                 }elseif(center >= cards.size()){
    32.                     hand =true;
    33.                 }else{
    34.                     System.out.println("problem");//lol ;)
    35.                 }
    36.             }
    37.         }
    38.         cards = cardsShuffled;
    39.  
    40.     }
    41.  
    42.     publicvoid shuffledeckrandom(){
    43.         Card tmp;
    44.         int shuffles =(int)(Math.random()*950)+50;
    45.         for(int i =0; i < shuffles; i++){
    46.             int carda =0;
    47.             int cardb =0;
    48.             while(carda == cardb){
    49.                 carda =(int)(Math.random()* cards.size());
    50.                 cardb =(int)(Math.random()* cards.size());
    51.             }
    52.             tmp = cards.get(carda);
    53.             cards.set(carda, cards.get(cardb));
    54.             cards.set(cardb, tmp);
    55.         }
    56.     }
    smart phones are dumb
    people are smart phones.

    Offlinewildskiller

    • Member
    • ****
    • Posts: 1,434
    • Thanks: +0/-0
      • View Profile
    Re: Deck: Making, Shuffling, Drawing - JavaScript as well
    « Reply #6 on: July 23, 2014, 11:27:14 AM »
    Here is the shuffle for my deck lol. Not so efficient, but realistic.

    Code: Java
    1. /**
    2.      * This method shuffles cards by:
    3.      1. Splitting deck into two halves.(With variable)
    4.      2. Randomly picks which hand lays first
    5.      3. Lays cards down, similar to splitting deck between two hands, and folding them together(bending the corners).
    6.      */
    7.     publicvoid shuffleSplit(){
    8.         int halfdeck = cards.size()/2;
    9.         //max of 5 cards off in either direction from middle, not that hard in reality
    10.         int center =(int)(Math.random()*11)+ halfdeck -5;
    11.  
    12.         //List that will store the new stack
    13.         LinkedList<Card> cardsShuffled =newLinkedList();
    14.         //0 or 1, hand1 or hand2, true or false
    15.         boolean hand =(int)(Math.random()*2)==0;
    16.         while(!(cards.isEmpty())){
    17.             if(hand){
    18.                 cardsShuffled.add(cards.pop());
    19.                 center--;
    20.             }else{
    21.                 cardsShuffled.add(cards.remove(center));
    22.             }
    23.             //50% sudo-chance it will change, only if a stack is not empty
    24.             if(!(center >= cards.size()|| center ==0)){
    25.                 if((int)(Math.random()*2)==0){
    26.                     hand =!hand;
    27.                 }
    28.             }else{//if a hand(side) is empty it says what to do :)
    29.                 if(center ==0){
    30.                     hand =false;
    31.                 }elseif(center >= cards.size()){
    32.                     hand =true;
    33.                 }else{
    34.                     System.out.println("problem");//lol ;)
    35.                 }
    36.             }
    37.         }
    38.         cards = cardsShuffled;
    39.  
    40.     }
    41.  
    42.     publicvoid shuffledeckrandom(){
    43.         Card tmp;
    44.         int shuffles =(int)(Math.random()*950)+50;
    45.         for(int i =0; i < shuffles; i++){
    46.             int carda =0;
    47.             int cardb =0;
    48.             while(carda == cardb){
    49.                 carda =(int)(Math.random()* cards.size());
    50.                 cardb =(int)(Math.random()* cards.size());
    51.             }
    52.             tmp = cards.get(carda);
    53.             cards.set(carda, cards.get(cardb));
    54.             cards.set(cardb, tmp);
    55.         }
    56.     }
    Wow, how could I not think of this.... The only other ways I thought for shuffling was taking a small, but random, amount of cards from the bottom and putting them on top for another random amount of times. Also, taking random amounts from a random spot and placing it in a random place in the deck.

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal