Pick a Card Any Card

10/25


WARNING!

Snap got a makeover this past weekend and had a lot of its code changed. Browsers don't always pick up these changes (ask me if you're curious why) but to fix the issue EVERYONE should hold shift and press the refresh button while in their Snap! Editor to make sure you get the most recent Snap! code. Thank you!

Brain Warm Up

Computer's only understand 1s and 0s. Every way we interact with a machine is some translation of our higher level abstraction into the 1s and 0s your electronics can then understand.

For this exercise you take on the role of Robot Typewriter Repair Person. Provided below is a script that includes a binary (1 and 0) typewriter, just the sort of thing a robot would use to finish writing that novel it's been thinking about.

http://snap.berkeley.edu/snapsource/snap.html#present:Username=MrBurgess&ProjectName=TypeWriter

The problem is the current typewriter is not correctly putting new numbers on the page. It is stuck. Every time a button is pressed it is overwriting the previously provided value. See if you can change the code so that instead of replacing the page value with a new value every time a button is pressed, it instead adds that value to the existing page. Take note of the new Snap! variable "message" (See the gif below to see a typewriter working this MAGIC)

If the user presses the 0 and then 1 and then 0 button in that order the resulting value for page should be "010".

If you are having trouble understanding the code, break it into parts. What is each button sprite doing? How does that interact with the Writer Sprite? How is the page variable being set? Dont understand message? Click the checkbox beside it so that you can see how it changes based on your interaction with the program.

Hint!: you should only be adding one block

52 Card Pick Up

For this exercise we will be building a variable that contains a representation of all 52 unique cards that exist in a standard playing card deck. (That's it, that is the task))

Consider your algorithms from Monday, one thing that some groups noticed was that you do not need to list out all 52 cards. There exists similarities between cards. Take the time to think of what all cards share. Use abstraction.

They all have a front and back, is that useful? They all have a color, how useful is that? A number? And so on. If you were to treat each card individually and wanted to build out a deck how many times would you need to invoke the "add" block to your deck list variable?

Once for each card. 52 invocations.

When you were thinking earlier, the two primary things about a card that stand out for the purpose of playing games are the suit and the rank.

Create a script in Snap! that produces a deck of 52 cards. One card for each possible combination of rank and suit (13 ranks x 4 suits)

Try to avoid 52 add blocks, think of ways you can more efficiently build out the deck you will be using for your games.


HINT!:

In Snap! create two lists, one that contains the suit values [Hearts, Diamonds, Spades, Clubs] and one that includes the ranks values [2,3,4,5,6,7,8,9,10,J,Q,K,A].

For this exercise I want you to go way back to Lesson 2.2. The "Another Brick in the Wall" exercise where you built a program that printed out bricks that filled the page. This lab used "Nested Loops". The fancy way of saying a loop in a loop.

See if you can use these two lists and our new knowledge of traversing to create a custom block that builds a deck variable for use in your card game projects. The end deck should have 52 cards. For each combination of Suit+Rank (13x4).

Loop through the ranks once for each suit and join the values.

Once you have completed your deck building script find your partner from Monday and try taking your algorithm and converting it to Snap! code.

An additional sprite that may prove useful is the following:

http://snap.berkeley.edu/snapsource/snap.html#present:Username=MrBurgess&ProjectName=Card%20Sprite

This sprite contains a costume for each possible card value. Try to implement your code without a graphical interface first. Think of the actual game logic, however if you want to add visuals this code is a good place to start.