![]() ![]() So your page will have about 52 lines (one for each card). You should get a web page that looks something like: Write an html file, testCards.html, that invokes testCards(). To test your functions, also write a function testCards() that takes no parameters and loops through all the integers from 0 to 51 and generates an html representation of each card (use document.write()). (I'll leave you to supply the "." part.) You can use this array to quickly convert the result of your % operator into a printable representation of a card's rank.) You may want to define a 13 element array called cardRank: var cardRank = Your functions should be defined in a file named "cards.js". The function rank() should return '2', '3'. The function suit() should return 'C', 'D', 'H' or 'S'. In the file cards.js, write two javascript functions, suit() and rank() that take a single integer 0-51 as a parameter and return the suit and rank of the card, respectively. In your lab document, cut and paste the resulting output of permute.html and label it "Step 1". ![]() If you are having trouble remembering how to structure an html/javascript document that defines Javascript functions and then invokes them in the body, see a related example, params.html. In the body of the html, invoke permute, then printPermutation.Define printPermutation in the header to use document.write to print the contents of numbers.I.e., in each of the 1000 iterations, generate two random numbers between 0 and 9 and use those as indices into the array to determine which elements of the array are swapped. You can generate random array indices using Math.random(). Define permute to randomly permute the contents of the array via 1000 random swaps.This should be a global JS variable, numbers ![]() Define an array holding the 10 integers in the header as well.So your element will include a element that contains the JS code to define permute. ![]() Create a mostly empty permute.html, but with an empty Javascript function, permute() in the header (we'll define the function in a sec).Write a page, permute.html, that uses Javascript to generate and display a random permutation of the integers 1-10. Continuing our example, n%13, will return a value from 0-12, with 0 representing 2's, 9 representing 'J' (jacks), and 12 representing 'A' (aces). (It is also called the modulus operator.) For example, 13%5 is 3, and 22%3 is 1. The Javascript operator '%' calculates remainders. The rank of each card n is specified by the remainder of that division. So the whole number of times 13 "goes into" n represents its suit. Then Math.floor(n/13) will be 0 for n=0-12 (the clubs), 1 for n=13-25 (the diamonds), etc. The following table shows some of the encodings:Ī bit of simple arithmetic can accomplish the decoding of the integers from 0-51 into the cards they represent. The lowest value in each suit will represents a '2' card, and the highest will represent an 'A' (ace). Each suit should be represented by 13 consecutive integers: clubs will be 0-12, diamonds 13-25, hearts 26-38, spades 39-51. You should represent a deck of cards with a 52 element array consisting of the numbers from 0 to 51. Lab: Playing Cards Lab: Working with Playing CardsĮach playing card is specified by a suit (clubs, diamonds, hearts, spades) and a rank (2,3,4,5,6, 7, 8, 9, 10, J, Q, K, A). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |