Programming Games

Document Sample
Programming Games Powered By Docstoc
					Programming Games

Formulas. Date. Representation in
 [computer] Storage. Credit Card.
   Homework: Finish slide show.
       Upload application
• Representation of information. How is
  everything/anything represented 'in' the
  – Storage

• Formulas

  – Simplified credit card calculation
• 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 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
• Recall borrowing (re-grouping) and
• Do problem(s)
                  Base 2
• Same principle
• 1s column, 2s column, 4s column, ????

• Do problem(s)
Explain joke on shirt
                Base 16
• Hexadecimal: used for quick way to
  describe bits, mostly commonly for color
• 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 part
Aka 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
• 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
         String of characters
…such as a name or a label or a piece of
• 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
• Named after George Boole
• True / False
• Theoretically: 1 bit, 0 for false, 1 for true
• 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
• 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
• Many systems of encodings start with 0
  – Arrays (sets of values) are indexed starting with 0.
• Representation somewhat analogous to issue of
  – Some number represents measurement of length or
    distance or displacement or size. What unit?
• Numbers by themselves rarely convey
  – Compared to what?
  – Out of what?
  – Definition, Denominator, Distribution, Difference
• 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,
• Models: build computer model = representation
  of something, some phenomenon. NOT simple
  (closed form) formula, but involving programs…
• Programming languages have features for
  expressing mathematical formulas
  distance = velocity x time


  Code, assume distance, velocity, time all
  distance = velocity * time;
   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?
• 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
  – One approach: write 9.0 and 5.0 and 32.0
  – Note: problem occurs with the division, not
    multiplication or addition
• Many programming courses start off with
  rules of precedence
  Is evaluated as (a*b)+c. The multiplication is
    done first
• Recommendation: put in parentheses!
• MAYBE: avoid long statements—use
  multiple lines
• 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!
              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 :
        Credit card: Simplified
• Make purchases
• Receive monthly bill
• If you pay up (do NOT have a balance), no credit
• 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
• 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
              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
• My model of a dog's behavior
• Represent (keep track of) dog's state
  – Weight, divided into ranges for purposes of the
  – 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
• 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)

Shared By: