home

Intro to Computer Programming

Using C# and the Unity3D game engine, but not about them

home

Contents

1 Introduction
 1.1 Notes
 1.2 Computer Languages, Books and Teaching
  1.2.1 Computer languages
  1.2.2 Books and Teaching
 1.3 Pedagogy
 1.4 Legal-ish
2 First program and printing
 2.1 Hooking up a simple program
  2.1.1 Install/configure Unity3D
  2.1.2 Program set-up
  2.1.3 Errors in set-up
  2.1.4 A simple program
  2.1.5 Running it
 2.2 Statements, print
 2.3 Intro to computer math
 2.4 More writing program details
  2.4.1 White space
  2.4.2 Style
  2.4.3 Comments
 2.5 Errors, Syntax errors
  2.5.1 Compilers
  2.5.2 making some errors
 2.6 Adding more programs
 2.7 more math
3 Variables, computer math
  3.0.1 Literals
 3.1 Math with types
  3.1.1 mixed types
 3.2 Variables, declarations, simple assignment
 3.3 Identifiers
  3.3.1 Style
 3.4 Simple assignment, var use
  3.4.1 More assignment statement rules
  3.4.2 Initialization, unassigned vars
  3.4.3 Computer math with variables; types
  3.4.4 mixed-type assignments
 3.5 More assign and variable rules and examples:
  3.5.1 examples
 3.6 Declare/assign shortcuts
 3.7 Semi-useful variable and assign examples
 3.8 More error messages
  3.8.1 Run-time errors
 3.9 Doubles vs. floats
4 Input, more output, testing
 4.1 Input
 4.2 Using Update to make more interesting programs
 4.3 declare = shortcut and Inspector vars
 4.4 Replacing constants with variables
 4.5 Math shortcuts
 4.6 Fun 3D stuff
 4.7 more cute cube tricks
  4.7.1 Colors
  4.7.2 Scale
 4.8 Errors, funny stuff
5 Using a 3D environment
 5.1 Making a small room
  5.1.1 Intro
  5.1.2 A floor
  5.1.3 walls
 5.2 Color, Textures
 5.3 Adjusting the Camera
6 If statements
 6.1 Comparison Operators
 6.2 Some semi-useful ifs
 6.3 Compare precedence
 6.4 else
 6.5 And, Or
  6.5.1 Useful and/or’s
 6.6 Nested ifs
 6.7 Common errors
7 more if tricks
 7.1 falling through
  7.1.1 Cascading range-slicing ifs
  7.1.2 Do only one of these, none of the above
 7.2 Swapping
 7.3 Incremental calculations
  7.3.1 guessing game example
  7.3.2 Gold mine problem
 7.4 Tricks with modulo
 7.5 Mixed and/or
  7.5.1 not, DeMorgan’s law
 7.6 Switch Statements
8 Unity if examples
 8.1 bouncing
  8.1.1 Speed increase
 8.2 Counting
 8.3 Delay counter
 8.4 State variables
9 Scope and Namespaces
 9.1 Scope
  9.1.1 More global rules
  9.1.2 block scope
  9.1.3 Common errors
 9.2 Namespaces
  9.2.1 Notes/rules
10 More ifs and randomness
 10.1 Random rolls
 10.2 Random Positions
  10.2.1 Random size
  10.2.2 Color
 10.3 More things to randomize
 10.4 Coin flips, percents, random tables
11 Functions, part I
 11.1 Intro Function examples
 11.2 Function rules
 11.3 More function examples
  11.3.1 Longer resetToLeft example
 11.4 functions with more math
 11.5 Common Errors
 11.6 Style
 11.7 Chains of functions
  11.7.1 Multiple local scopes
12 Functions, part II - parameters
 12.1 Parameters
 12.2 Rules
 12.3 More rule-showing examples
 12.4 Fun errors
 12.5 Multiple Inputs
  12.5.1 More errors
13 Casting and char
 13.1 casting
  13.1.1 (int) from float
  13.1.2 (float) from int
  13.1.3 string casts, conversions
 13.2 char type
  13.2.1 char examples
  13.2.2 Casting chars
14 Functions, part III - return
 14.1 Introduction
 14.2 return values
 14.3 Using value-returning functions
 14.4 More math functions
  14.4.1 Extra return rules
  14.4.2 Optional empty return
  14.4.3 return can use math
  14.4.4 Errors
15 Constants and enums
 15.1 Constants
 15.2 enumerated types
  15.2.1 value returning ?: ifs
 15.3 More enumerated types
16 Overloading and default parms
 16.1 Introduction
 16.2 Default parameters
 16.3 overloading
 16.4 Style
17 booleans
 17.1 Bool rules
  17.1.1 bool variables in if statements
 17.2 Common bool tricks
  17.2.1 Pre-computing if tests
  17.2.2 bool return values
  17.2.3 Bools for function settings
  17.2.4 flags
  17.2.5 Incremental bool calculations
 17.3 Testing tricks
18 Function examples
 18.1 Introduction
 18.2 Change me type functions
 18.3 Dice functions
 18.4 More style; types of functions
 18.5 More Unity Movement examples
 18.6 Reading keys
 18.7 Making a real function library
19 Structs
 19.1 Cow struct example
  19.1.1 More examples
 19.2 Common errors
 19.3 rules
  19.3.1 namespace vs. struct dots
 19.4 Builtin structs: Color, Vector3
  19.4.1 Color
 19.5 Constructors, struct literals
 19.6 Nested structs
 19.7 Structs as function inputs
 19.8 Structs as return values
 19.9 Uninitialized structs
20 Struct and Unity examples
 20.1 More Vector3, Color code
 20.2 More built-in functions
  20.2.1 Distance
  20.2.2 MoveTowards
  20.2.3 More movement examples
  20.2.4 Color namespace
 20.3 Transform struct
  20.3.1 Special errors; pull-out trick
 20.4 Rigidbodies
  20.4.1 Simple physics set-up
  20.4.2 Playing with velocity
  20.4.3 Collision callback
  20.4.4 The Collision struct
  20.4.5 Misc event notes
21 Float problems
 21.1 The Problem
 21.2 Solutions
 21.3 < vs <= in float compares
22 Loops
 22.1 Special infinite loop warning
 22.2 Number-sequence loops
 22.3 While loop rules
 22.4 Do it 10 times loops
 22.5 Loops with floats
 22.6 Go until done loops
 22.7 More oddball loops
 22.8 Infinite/broken loops
 22.9 Move and count loops
 22.10 Digit tricks
 22.11 Fencepost errors
 22.12 for loops
 22.13 Count plus formula loops
 22.14 do-while loop
23 Index Loops
 23.1 Indexes
  23.1.1 Length of a string
 23.2 Variables as indexes
 23.3 String Loop examples
  23.3.1 Index inputs/outputs
 23.4 Odd string loops
24 Pointers
 24.1 First walk-through
 24.2 Terms and Theory
  24.2.1 Pointers
  24.2.2 new vs. Declare, Heap vs. Stack
  24.2.3 Reference types
 24.3 Common pointer/class use
 24.4 Functions and pointers
  24.4.1 Pointer inputs
  24.4.2 Pointer outputs
 24.5 null
 24.6 Errors
 24.7 Pointer compare (==)
 24.8 Garbage Collection
 24.9 Why struct new’s?
 24.10 Old-style pointers
25 Pointers in Unity
  25.0.1 Pointers to existing Cubes
 25.1 Instantiate
  25.1.1 Instantiate as a copy
  25.1.2 Proper Unity use of Instantiate
  25.1.3 Pointers for “caching”
 25.2 New’ing Inspector variables
26 Pointer examples
 26.1 Text change
 26.2 More time, and APIs
 26.3 Three platforms
 26.4 More prefab use
 26.5 Multiple scripts
27 Using indexed lists
 27.1 Intro
 27.2 Creating Lists
 27.3 More playing with indexes
 27.4 List loop examples
  27.4.1 list-Initializing loops
  27.4.2 Printing
  27.4.3 List Searching
 27.5 Random item
  27.5.1 A list as a sequence
  27.5.2 Variable variables
  27.5.3 Moving parts of a list
 27.6 Two list tricks
 27.7 Size 0 and 1 lists
 27.8 Functions with lists
  27.8.1 Lists as inputs
  27.8.2 Returning lists
 27.9 errors
 27.10 Assign shortcut
 27.11 List variables as pointers
28 Nested loops
 28.1 As a grid
 28.2 Pattern in pattern
 28.3 Wedge exercises
 28.4 break, continue
 28.5 List nested loops
  28.5.1 Sorting
29 Struct in struct
 29.1 Lists of structs
 29.2 Structs with Lists in them
 29.3 Lists of strings tricks
 29.4 2D lists
 29.5 Larger structures
 29.6 Giant game board example
 29.7 List of indexes
 29.8 Internal list pointers
30 Member functions
 30.1 Example and motivation
 30.2 More simple examples
 30.3 Calling your own member functions
 30.4 Built-in member functions
  30.4.1 Unity member functions
  30.4.2 String member functions
  30.4.3 List member functions
 30.5 this
 30.6 Constructors
 30.7 Scope
 30.8 Style: member vs. non-member
 30.9 Special struct in script rule
31 Access modifiers
 31.1 How private variables work
 31.2 Classes as new types
 31.3 Interface/Implementation idea
  31.3.1 Rewriting classes
  31.3.2 Accessors
  31.3.3 Special get/set
32 Scripts as Classes
 32.1 How scripts really work
 32.2 Pointers to scripts
  32.2.1 Direct script pointers
  32.2.2 GetComponent to find scripts
  32.2.3 Multi-script ball-dropping game
  32.2.4 Hand-running fake updates
33 Nested classes, static
 33.1 Nested enum’s, classes and public
 33.2 Static / namespaces
  33.2.1 static functions
  33.2.2 Fake classes for namespaces
  33.2.3 static variables
  33.2.4 File of classes
34 Reference parms
 34.1 ref rules
 34.2 More ref examples
 34.3 Output parameters
  34.3.1 out shortcut
 34.4 mixing real return and ref parms
 34.5 Pointers and References
 34.6 Raycast example
 34.7 Reference vs. reference
35 Arrays
 35.1 Basic array rules
 35.2 Arrays and functions
  35.2.1 Array work-arounds
 35.3 Max size / current size trick
 35.4 creation shortcuts
 35.5 List / array conversion
 35.6 OverlapSphere Unity example
 35.7 2D arrays
  35.7.1 Ragged [][] arrays
  35.7.2 Real 2D [,] arrays
36 Efficiency
 36.1 Most speed-ups don’t work
 36.2 Premature Optimization
 36.3 Not all code sections are equal
 36.4 Is it already fast enough?
 36.5 Will it cause more errors?
 36.6 Things that work
37 Debugging
 37.1 Test as you go
 37.2 How/what to test
 37.3 Tracking down bugs

Contents, More Programming

37 Recursion
 37.1 How calling yourself works
 37.2 Recursive thinking
  37.2.1 Tree functions
  37.2.2 Connected / Flood Fill
  37.2.3 Maze walk
 37.3 Errors
38 First class functions
 38.1 Rules and examples
  38.1.1 Declaring function pointers
  38.1.2 Using function pointers
 38.2 Uses
  38.2.1 As a regular variable
  38.2.2 Arrays of pointers
  38.2.3 User-defined keys
  38.2.4 Passing functions into functions
 38.3 Function pointers as callbacks
 38.4 Just cramming in a function
 38.5 Sort plus function input
 38.6 Built-in array function-using functions
 38.7 Older delegate syntax
39 Inheritance
 39.1 Pre-inheritance example
 39.2 Basic Inheritance
 39.3 Intro to Polymorphism
 39.4 Dynamic casting
 39.5 Inheritance and functions
 39.6 dynamic dispatch
40 More Inheritance
 40.1 Privacy
  40.1.1 abstract base classes
 40.2 Inheritance chains
 40.3 Interfaces
 40.4 Interfaces, Pt II
 40.5 Misc examples
  40.5.1 Built-in interface for comparing in a Sort
  40.5.2 Component class
  40.5.3 Scripts and inheritance
  40.5.4 Image subtree example
 40.6 General inheritance advice
41 Templates
 41.1 Template functions
  41.1.1 Implicit types
 41.2 Template classes
  41.2.1 Tuple examples
  41.2.2 More oddball examples
 41.3 Templates and container classes
 41.4 Unity template functions
42 Linked Lists
 42.1 Simple linked list
  42.1.1 Linked List loops
  42.1.2 Insert/Remove
  42.1.3 Removing
 42.2 Misc
 42.3 Special cases
 42.4 Doubly-linked lists
 42.5 An any-type linked-list, using templates
 42.6 Built in Linked List
 42.7 Array implementation of linked lists
43 Big-O notation
 43.1 Loop counting logic
 43.2 Integer array loops
  43.2.1 Fun with bad loops
 43.3 Formal math
 43.4 More tricks and exceptions
 43.5 Other data structures
 43.6 Overview
 43.7 Numbers