Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Games Formulas. Date. Representation in [computer] Storage. Credit Card. Homework: Finish slide show. Upload application.

Similar presentations


Presentation on theme: "Programming Games Formulas. Date. Representation in [computer] Storage. Credit Card. Homework: Finish slide show. Upload application."— Presentation transcript:

1 Programming Games Formulas. Date. Representation in [computer] Storage. Credit Card. Homework: Finish slide show. Upload application

2 Overview Representation of information. How is everything/anything represented 'in' the computer –Storage Formulas –Simplified credit card calculation

3 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

4 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…

5 Recall base 10 Recall 1s column, 10s column, 100s column Recall borrowing (re-grouping) and carrying Do problem(s)

6 Base 2 Same principle 1s column, 2s column, 4s column, ???? Do problem(s)

7 Joke Explain joke on shirt

8 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 ??

9 Numbers with fraction part Aka numbers with a decimal point How to represent? ANSWER: floating point numbers aka scientific notation –3.4521 * 10 2 is the same as 345.21 * 10 0 –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)

10 Characters ASCII coding The 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

11 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

12 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.

13 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.

14 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

15 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...

16 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…

17 Formulas Programming languages have features for expressing mathematical formulas distance = velocity x time Code, assume distance, velocity, time all variables distance = velocity * time; multiplication

18 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.

19 Temperature conversion Temp fahrenheit = Temp centigrade *(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?

20 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

21 Precedence Many programming courses start off with rules of precedence a*b+c Is evaluated as (a*b)+c. The multiplication is done first Recommendation: put in parentheses! MAYBE: avoid long statements—use multiple lines

22 Conditionals Suppose a formula (for ticket price or score or …) involves a conditional test: –One Tuesdays, drinks are half price Logic: 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!

23 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; }

24 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

25 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

26 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

27 Problems Slide show –There is one element. Its src is changed, producing the slide show. The code is in the 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 element has the buttons, made using elements and the element. You can put in other stuff into the

28 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"

29 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…

30 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

31 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

32 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)


Download ppt "Programming Games Formulas. Date. Representation in [computer] Storage. Credit Card. Homework: Finish slide show. Upload application."

Similar presentations


Ads by Google