For Friday Finish reading chapter 4 Homework: –Lisp handout 4.
Programming Games Formulas. Date. Representation in [computer] Storage. Credit Card. Homework:...
-
Upload
peter-nash -
Category
Documents
-
view
219 -
download
3
Transcript of Programming Games Formulas. Date. Representation in [computer] Storage. Credit Card. Homework:...
Programming Games
Formulas. Date. Representation in [computer] Storage. Credit Card.
Homework: Finish slide show.Upload application
Overview
• Representation of information. How is everything/anything represented 'in' the computer– Storage
• Formulas
– Simplified credit card calculation
Storage
• Everything (data and programs) is stored in the circuitry of 'the computer'.
• The circuitry includes transistors that are switches: on or off states, 0 or 1. Each switch is called a bit.
• So….numbers are stored using the binary (base 2) system
• Symbols (characters, letters, etc.) are stored using agreed upon systems of encoding– ASCII: 8 bits per character
– UNICODE: 16 bits per character
Why?
• Why not use circuits that can more easily represent numbers using the decimal (base 10) system?
• Answer: Easier to make on/off switches than something with 10 states. Easier to build circuitry for calculations for the base 2 addition and base 2 times tables than the ones you remember…
Recall base 10
• Recall 1s column, 10s column, 100s column
• Recall borrowing (re-grouping) and carrying
• Do problem(s)
Base 2
• Same principle
• 1s column, 2s column, 4s column, ????
• Do problem(s)
Joke
Explain joke on shirt
Base 16• Hexadecimal: used for quick way to
describe bits, mostly commonly for color coding
• Symbols used are 0, 1, 2, …, 9, A, B, C, D, E, F
• You may have seen color coding: RGB (red, green blue) FF0000 is red 00FF00 is green ??
Numbers with fraction partAka numbers with a decimal point• How to represent?• ANSWER: floating point numbers
aka scientific notation– 3.4521 * 102 is the same as 345.21 * 100
– Terminology: 3.4521 (or 345.21) is the mantissa or significand and the 2 (or 0) is the exponent.
• Computer format: use 2 or 16 in place of 10• Example using 32 bits:
– 1 bit for the sign (0 for +, 1 for -)– 8 bits for the exponent – 23 bits for the mantissa (width, i.e., 23, is the
precision)
Characters
• ASCII codingThe character A is represented by 01000001
The character a is represented by 01100001
The character 1 is represented by 00110001
The character 2 is represented by 00110010
….
• Unicode is a 16 bit format big enough (hopefully) for all the world's languages
String of characters
…such as a name or a label or a piece of text
• Fixed length: allocate 1 byte (8 bits) for each character– UNICODE 2 bytes
• Variable length: store string in two parts– One part holds length as a number and
pointer (address) of the whole string– String itself
Boolean
• Named after George Boole• True / False• Theoretically: 1 bit, 0 for false, 1 for true
but• The addressability requirement means it
could be 1 byte or even bigger• String of Booleans can be combined.
– A byte can represent the answer to 8 true/false questions.
Other information
• Programming generally requires encoding of information.
• The encoding CAN BE arbitrary, but does need to be defined– Later: My rock paper scissors game assigns 0 to rock,
1 to paper and 2 to scissors. The player NEVER sees this.
• Many systems of encodings start with 0– Arrays (sets of values) are indexed starting with 0.
Aside
• Representation somewhat analogous to issue of units?– Some number represents measurement of length or
distance or displacement or size. What unit?
• Numbers by themselves rarely convey information– Compared to what?– Out of what?– Definition, Denominator, Distribution, Difference
Instructions
• Computer storage holds– Instructions (the code)
• Instructions may be translation (compilation) of higher level instructions
• Machine code: Load Register, Store Register, Add, Multiply, Check value and jump, etc.
– Information
• Can't look at the bits and say what it is– 01001010 the letter J or the number 74 or ...
Reflect and Continue• Storage of standard things (e.g., numbers)
solved but representation of information for specific problems is / will be new challenge
• Now on to representation of formulas and logic.– Closed form – mathematical formula – easy to
represent in code– Algorithm, logic, may require use of conditionals,
more…
• Models: build computer model = representation of something, some phenomenon. NOT simple (closed form) formula, but involving programs…
Formulas
• Programming languages have features for expressing mathematical formulasdistance = velocity x time
Code, assume distance, velocity, time all variables
distance = velocity * time;
multiplication
Function expressing formula
function distance (velocity, time { return velocity * time;}Give me velocity and time and I'll [the
function] will return the distance.
The function header indicates the [incoming] parameters (aka arguments). NOTE: in ActionScript and some other languages, the function header also indicates datatypes of the parameters and the returned value.
Temperature conversion
Tempfahrenheit = Tempcentigrade*(9/5)+32;
Check by putting in points
for boiling:
212 Fahrenheit and 100 Centigrade
For freezing
32 Fahrenheit and 0 Centigrade
What is formula for… the other direction?
Caution• Recall: Programming systems store whole
numbers (0, 1, 2, -10, etc.) differently than numbers with fractions (0.5, 10.23, -2.3333333, etc.)
• Need to make sure that none of the intermediate steps are truncated to whole numbers!– One approach: write 9.0 and 5.0 and 32.0– Note: problem occurs with the division, not
multiplication or addition
Precedence
• Many programming courses start off with rules of precedencea*b+cIs evaluated as (a*b)+c. The multiplication is
done first
• Recommendation: put in parentheses!
• MAYBE: avoid long statements—use multiple lines
Conditionals
• Suppose a formula (for ticket price or score or …) involves a conditional test:– One Tuesdays, drinks are half priceLogic: if it is Tuesday, dcost = .5*dcost;
• Implementation: use if statement– Alternative: conditional operator. Show later.
• But how do we know if it is Tuesday?• Implementation: use Date
– Remember from first HTML example!
Date code
today = new Date();
dayofweek = today.getDay();
//encoding is 0 for Sunday, 1 for Mon.,
// 2 for Tuesday
if (dayofweek==2) {
dcost = .5 * dcost;
}
Conditional operator
• Operator with 3 operands condition ? value_if_true : value_if_false
…
dcost = (dayofweek==2) ? (.5*dcost) : dcost;
Comfortable_with_conditional ? Use_It : if_statement
Credit card: Simplified
• Make purchases• Receive monthly bill• If you pay up (do NOT have a balance), no credit
charges• If you do not pay anything, fee + interest• If you pay the minimum, interest• Next month, balance is remaining balance, fee,
interest, new purchases• Demonstrate
Credit card
• Multiple purchases
• Do not 'get the float' if you don't pay up
• Compounding may be DAILY
• Advice: pay up—never have a balance– Read the small print
Problems• Slide show
– There is one <img> element. Its src is changed, producing the slide show.
• The code is in the <script> element. This includes 3 function definitions: startss, stopss, change. The change function changes the sn variable (the pointer into the slides array) and changes the src of the img element, displaying a new picture.
• The <body> element has the buttons, made using <a> elements and the <img> element. You can put in other stuff into the <body>
File structure on server
• You may have an index.html file for each folder, including the WEB folder
• If you have an html file in Folder A, and have a folder named pictures in folder A containing a file named bird.gif, the html file can refer to "pictures/bird.gif"
Scaling up
• … making an application bigger / big enough after testing a smaller version
• The slide show can be scaled up to hold more slides by adding names to the array
• Note: the code refers to slides.length
• Scaling up can be more difficult…
Virtual Dog
• Discrete event simulation: Model of a phenomenon
• My model of a dog's behavior• Represent (keep track of) dog's state
– Weight, divided into ranges for purposes of the display
– Time since last feeding• Player actions
– Feed– Pet
• Stochastic (probalistic) effect
Your virtual something
• Start out with small number of states (pictures) and
• 2 actions by the player
• You may build on this application for your final project.
• This scaling up may be / will be more complex since you will be adding more conditions and buttons
Homework
• Review (will be on the midterm!)– Binary, hexadecimal, ASCII– Credit card application
• Finish slide show• Upload application(s)
– Make index.html file
• Look ahead: virtual something– Plan first (states, pictures, player moves)