2016-07-08: Week 2: Day 8
Agenda
- Weekly goal: by end of today, each group should demonstrate a fully functioning game simulation which extends PinballLogic example.
Integer Functions
Mappings from integers to integers can be implemented many, many ways. Here are just a few simple examples. First, pure program logic:
int func(int score) { switch(score) { case 0: return 0; case 1: return 1; // what happens for other values? default: return -1; } }
Following is a table-driven example. Note that use of const to hint that the table itself won't be changed. (Aside: to really save memory space, see the use of PROGMEM with access functions to place tables in physical read-only memory.)
const int table[2] = {0, 1}; int func(int score) { if (score >= 0 && score < 2) { return table[score]; } else { return -1; } }
Of if the mapping can be written as a closed-form expression, then plain arithmetic can often be the most compact code:
int func(int score) { if (score >= 0 && score < 2) { return (score & 1); // bitwise-and operation } else { return -1; } }